| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2015-04-21 14:32:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | var winston = require('winston'); | 
					
						
							|  |  |  | var express = require('express'); | 
					
						
							|  |  |  | var bodyParser = require('body-parser'); | 
					
						
							|  |  |  | var fs = require('fs'); | 
					
						
							|  |  |  | var path = require('path'); | 
					
						
							|  |  |  | var less = require('less'); | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | var uglify = require('uglify-js'); | 
					
						
							|  |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var app = express(); | 
					
						
							|  |  |  | var server; | 
					
						
							| 
									
										
										
										
											2015-04-21 14:52:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-02 18:41:05 -04:00
										 |  |  | winston.add(winston.transports.File, { | 
					
						
							|  |  |  | 	filename: 'logs/webinstall.log', | 
					
						
							|  |  |  | 	colorize: true, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	timestamp: function () { | 
					
						
							| 
									
										
										
										
											2015-09-02 18:41:05 -04:00
										 |  |  | 		var date = new Date(); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 		return date.getDate() + '/' + (date.getMonth() + 1) + ' ' + date.toTimeString().substr(0, 5) + ' [' + global.process.pid + ']'; | 
					
						
							| 
									
										
										
										
											2015-09-02 18:41:05 -04:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 	level: 'verbose', | 
					
						
							| 
									
										
										
										
											2015-09-02 18:41:05 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | var web = {}; | 
					
						
							|  |  |  | var scripts = [ | 
					
						
							|  |  |  | 	'public/vendor/xregexp/xregexp.js', | 
					
						
							|  |  |  | 	'public/vendor/xregexp/unicode/unicode-base.js', | 
					
						
							|  |  |  | 	'public/src/utils.js', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 	'public/src/installer/install.js', | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | ]; | 
					
						
							| 
									
										
										
										
											2015-04-21 14:32:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | web.install = function (port) { | 
					
						
							| 
									
										
										
										
											2015-04-22 15:52:12 -04:00
										 |  |  | 	port = port || 4567; | 
					
						
							| 
									
										
										
										
											2015-04-22 14:06:59 -04:00
										 |  |  | 	winston.info('Launching web installer on port', port); | 
					
						
							| 
									
										
										
										
											2015-04-21 14:32:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 19:50:58 -04:00
										 |  |  | 	app.use(express.static('public', {})); | 
					
						
							|  |  |  | 	app.engine('tpl', require('templates.js').__express); | 
					
						
							|  |  |  | 	app.set('view engine', 'tpl'); | 
					
						
							|  |  |  | 	app.set('views', path.join(__dirname, '../src/views')); | 
					
						
							|  |  |  | 	app.use(bodyParser.urlencoded({ | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		extended: true, | 
					
						
							| 
									
										
										
										
											2015-09-02 18:17:58 -04:00
										 |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2015-04-21 19:50:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	async.parallel([compileLess, compileJS], function () { | 
					
						
							| 
									
										
										
										
											2015-04-21 16:21:04 -04:00
										 |  |  | 		setupRoutes(); | 
					
						
							|  |  |  | 		launchExpress(port); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-04-21 14:32:21 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 14:52:57 -04:00
										 |  |  | function launchExpress(port) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	server = app.listen(port, function () { | 
					
						
							| 
									
										
										
										
											2015-05-24 10:15:16 -04:00
										 |  |  | 		winston.info('Web installer listening on http://%s:%s', '0.0.0.0', port); | 
					
						
							| 
									
										
										
										
											2015-04-21 14:52:57 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function setupRoutes() { | 
					
						
							| 
									
										
										
										
											2015-04-21 19:50:58 -04:00
										 |  |  | 	app.get('/', welcome); | 
					
						
							|  |  |  | 	app.post('/', install); | 
					
						
							| 
									
										
										
										
											2015-04-22 14:49:31 -04:00
										 |  |  | 	app.post('/launch', launch); | 
					
						
							| 
									
										
										
										
											2015-04-21 14:52:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 19:50:58 -04:00
										 |  |  | function welcome(req, res) { | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 	var dbs = ['redis', 'mongo']; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	var databases = dbs.map(function (el) { | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 		return { | 
					
						
							| 
									
										
										
										
											2015-04-21 19:10:47 -04:00
										 |  |  | 			name: el, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			questions: require('../src/database/' + el).questions, | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-04-21 19:10:47 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 	var defaults = require('./data/defaults'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 15:29:29 -04:00
										 |  |  | 	res.render('install/index', { | 
					
						
							|  |  |  | 		databases: databases, | 
					
						
							| 
									
										
										
										
											2015-04-24 15:11:22 -04:00
										 |  |  | 		skipDatabaseSetup: !!nconf.get('database'), | 
					
						
							| 
									
										
										
										
											2017-02-18 12:59:46 -07:00
										 |  |  | 		error: !!res.locals.error, | 
					
						
							|  |  |  | 		success: !!res.locals.success, | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 		values: req.body, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		minimumPasswordLength: defaults.minimumPasswordLength, | 
					
						
							| 
									
										
										
										
											2015-04-21 19:10:47 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-04-21 14:52:57 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 19:50:58 -04:00
										 |  |  | function install(req, res) { | 
					
						
							| 
									
										
										
										
											2015-04-22 11:36:19 -04:00
										 |  |  | 	for (var i in req.body) { | 
					
						
							| 
									
										
										
										
											2015-04-24 15:11:22 -04:00
										 |  |  | 		if (req.body.hasOwnProperty(i) && !process.env.hasOwnProperty(i)) { | 
					
						
							|  |  |  | 			process.env[i.replace(':', '__')] = req.body[i]; | 
					
						
							| 
									
										
										
										
											2015-04-22 11:36:19 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-04-22 11:22:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 14:39:37 -04:00
										 |  |  | 	var child = require('child_process').fork('app', ['--setup'], { | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		env: process.env, | 
					
						
							| 
									
										
										
										
											2015-04-22 11:22:55 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-04-22 11:36:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	child.on('close', function (data) { | 
					
						
							| 
									
										
										
										
											2015-04-22 14:39:37 -04:00
										 |  |  | 		if (data === 0) { | 
					
						
							|  |  |  | 			res.locals.success = true; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			res.locals.error = true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		welcome(req, res); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-04-21 19:50:58 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 14:49:31 -04:00
										 |  |  | function launch(req, res) { | 
					
						
							| 
									
										
										
										
											2015-04-22 15:29:29 -04:00
										 |  |  | 	res.json({}); | 
					
						
							| 
									
										
										
										
											2015-04-22 15:51:14 -04:00
										 |  |  | 	server.close(); | 
					
						
							| 
									
										
										
										
											2015-04-22 17:51:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-22 17:59:08 -04:00
										 |  |  | 	var child = require('child_process').spawn('node', ['loader.js'], { | 
					
						
							| 
									
										
										
										
											2015-04-22 17:51:35 -04:00
										 |  |  | 		detached: true, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		stdio: ['ignore', 'ignore', 'ignore'], | 
					
						
							| 
									
										
										
										
											2015-04-22 17:51:35 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-24 10:39:52 -04:00
										 |  |  | 	process.stdout.write('\nStarting NodeBB\n'); | 
					
						
							|  |  |  | 	process.stdout.write('    "./nodebb stop" to stop the NodeBB server\n'); | 
					
						
							|  |  |  | 	process.stdout.write('    "./nodebb log" to view server output\n'); | 
					
						
							| 
									
										
										
										
											2015-05-25 14:47:54 -04:00
										 |  |  | 	process.stdout.write('    "./nodebb restart" to restart NodeBB\n'); | 
					
						
							| 
									
										
										
										
											2015-09-02 18:17:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 11:45:22 -05:00
										 |  |  | 	async.parallel([ | 
					
						
							|  |  |  | 		async.apply(fs.unlink(path.join(__dirname, '../public/installer.css'))), | 
					
						
							| 
									
										
										
										
											2017-02-18 01:27:46 -07:00
										 |  |  | 		async.apply(fs.unlink(path.join(__dirname, '../public/installer.min.js'))), | 
					
						
							| 
									
										
										
										
											2016-12-22 11:45:22 -05:00
										 |  |  | 	], function (err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			winston.warn('Unable to remove installer files'); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-02 18:17:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 11:45:22 -05:00
										 |  |  | 		child.unref(); | 
					
						
							|  |  |  | 		process.exit(0); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2015-04-22 14:49:31 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 16:21:04 -04:00
										 |  |  | function compileLess(callback) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	fs.readFile(path.join(__dirname, '../public/less/install.less'), function (err, style) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return winston.error('Unable to read LESS install file: ', err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		less.render(style.toString(), function (err, css) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:52:56 -07:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2015-04-21 16:21:04 -04:00
										 |  |  | 				return winston.error('Unable to compile LESS: ', err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 11:45:22 -05:00
										 |  |  | 			fs.writeFile(path.join(__dirname, '../public/installer.css'), css.css, callback); | 
					
						
							| 
									
										
										
										
											2015-04-21 16:21:04 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-21 14:32:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-21 17:02:36 -04:00
										 |  |  | function compileJS(callback) { | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 	var scriptPath = path.join(__dirname, '..'); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	var result = uglify.minify(scripts.map(function (script) { | 
					
						
							| 
									
										
										
										
											2016-08-12 01:14:01 +03:00
										 |  |  | 		return path.join(scriptPath, script); | 
					
						
							|  |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:02:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 11:45:22 -05:00
										 |  |  | 	fs.writeFile(path.join(__dirname, '../public/installer.min.js'), result.code, callback); | 
					
						
							| 
									
										
										
										
											2015-04-21 17:02:36 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 02:30:48 -07:00
										 |  |  | module.exports = web; |