| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | var	async = require('async'), | 
					
						
							|  |  |  | 	utils = require('../public/src/utils.js'), | 
					
						
							|  |  |  | 	fs = require('fs'), | 
					
						
							|  |  |  | 	url = require('url'), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 	prompt = require('prompt'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 	reds = require('reds'), | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 	install = { | 
					
						
							|  |  |  | 		questions: [ | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'base_url', | 
					
						
							|  |  |  | 				description: 'URL of this installation', | 
					
						
							|  |  |  | 				'default': 'http://localhost', | 
					
						
							|  |  |  | 				pattern: /^http(?:s)?:\/\//, | 
					
						
							|  |  |  | 				message: 'Base URL must begin with \'http://\' or \'https://\'', | 
					
						
							| 
									
										
										
										
											2013-07-17 12:21:40 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'port', | 
					
						
							|  |  |  | 				description: 'Port number of your NodeBB', | 
					
						
							|  |  |  | 				'default': 4567 | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'use_port', | 
					
						
							|  |  |  | 				description: 'Use a port number to access NodeBB?', | 
					
						
							|  |  |  | 				'default': 'y', | 
					
						
							|  |  |  | 				pattern: /y[es]*|n[o]?/, | 
					
						
							|  |  |  | 				message: 'Please enter \'yes\' or \'no\'', | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'secret', | 
					
						
							|  |  |  | 				description: 'Please enter a NodeBB secret', | 
					
						
							|  |  |  | 				'default': utils.generateUUID() | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'redis:host', | 
					
						
							|  |  |  | 				description: 'Host IP or address of your Redis instance', | 
					
						
							|  |  |  | 				'default': '127.0.0.1' | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'redis:port', | 
					
						
							|  |  |  | 				description: 'Host port of your Redis instance', | 
					
						
							|  |  |  | 				'default': 6379 | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: 'redis:password', | 
					
						
							|  |  |  | 				description: 'Password of your Redis database' | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 		setup: function(callback) { | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 			async.series([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					// prompt prepends "prompt: " to questions, let's clear that.
 | 
					
						
							|  |  |  | 					prompt.start(); | 
					
						
							|  |  |  | 					prompt.message = ''; | 
					
						
							|  |  |  | 					prompt.delimiter = ''; | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 					prompt.get(install.questions, function(err, config) { | 
					
						
							| 
									
										
										
										
											2013-09-07 11:45:04 -04:00
										 |  |  | 						if (!config) return next(new Error('aborted')); | 
					
						
							| 
									
										
										
										
											2013-07-17 12:05:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						// Translate redis properties into redis object
 | 
					
						
							|  |  |  | 						config.redis = { | 
					
						
							|  |  |  | 							host: config['redis:host'], | 
					
						
							|  |  |  | 							port: config['redis:port'], | 
					
						
							|  |  |  | 							password: config['redis:password'] | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 						delete config['redis:host']; | 
					
						
							|  |  |  | 						delete config['redis:port']; | 
					
						
							|  |  |  | 						delete config['redis:password']; | 
					
						
							| 
									
										
										
										
											2013-07-17 12:05:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						// Add hardcoded values
 | 
					
						
							|  |  |  | 						config['bcrypt_rounds'] = 12, | 
					
						
							|  |  |  | 						config['upload_path'] = '/public/uploads'; | 
					
						
							| 
									
										
										
										
											2013-09-07 11:29:26 -04:00
										 |  |  | 						config['use_port'] = (config['use_port'].slice(0, 1) === 'y') ? true : false; | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						var urlObject = url.parse(config.base_url), | 
					
						
							|  |  |  | 							relative_path = (urlObject.pathname && urlObject.pathname.length > 1) ? urlObject.pathname : '', | 
					
						
							|  |  |  | 							host = urlObject.host, | 
					
						
							|  |  |  | 							protocol = urlObject.protocol, | 
					
						
							|  |  |  | 							server_conf = config, | 
					
						
							|  |  |  | 							client_conf = { | 
					
						
							|  |  |  | 								socket: { | 
					
						
							|  |  |  | 									address: protocol + '//' + host + (config.use_port ? ':' + config.port : '') | 
					
						
							|  |  |  | 								}, | 
					
						
							|  |  |  | 								api_url: protocol + '//' + host + (config.use_port ? ':' + config.port : '') + relative_path + '/api/', | 
					
						
							|  |  |  | 								relative_path: relative_path | 
					
						
							|  |  |  | 							}; | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						server_conf.base_url = protocol + '//' + host; | 
					
						
							|  |  |  | 						server_conf.relative_path = relative_path; | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						install.save(server_conf, client_conf, next); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 				function(next) { | 
					
						
							|  |  |  | 					// Applying default database configs
 | 
					
						
							|  |  |  | 					var	meta = require('./meta'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					meta.configs.set('postDelay', 10000); | 
					
						
							|  |  |  | 					meta.configs.set('minimumPostLength', 8); | 
					
						
							|  |  |  | 					meta.configs.set('minimumTitleLength', 3); | 
					
						
							|  |  |  | 					meta.configs.set('minimumUsernameLength', 2); | 
					
						
							|  |  |  | 					meta.configs.set('maximumUsernameLength', 16); | 
					
						
							|  |  |  | 					meta.configs.set('minimumPasswordLength', 6); | 
					
						
							|  |  |  | 					meta.configs.set('imgurClientID', ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					meta.configs.init(next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 				function(next) { | 
					
						
							|  |  |  | 					// Check if an administrator needs to be created
 | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 					var	Groups = require('./groups'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 					Groups.getGidFromName('Administrators', function(err, gid) { | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 						if (err) return next(err.message); | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						if (gid) { | 
					
						
							|  |  |  | 							Groups.get(gid, {}, function(err, groupObj) { | 
					
						
							|  |  |  | 								if (groupObj.count > 0) { | 
					
						
							|  |  |  | 									winston.info('Administrator found, skipping Admin setup'); | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								} else install.createAdmin(next); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else install.createAdmin(next); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					// Categories
 | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 					var	Categories = require('./categories'), | 
					
						
							|  |  |  | 						admin = { | 
					
						
							|  |  |  | 							categories: require('./admin/categories') | 
					
						
							|  |  |  | 						}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 					categories.getAllCategories(function(data) { | 
					
						
							|  |  |  | 						if (data.categories.length === 0) { | 
					
						
							|  |  |  | 							winston.warn('No categories found, populating instance with default categories') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							fs.readFile(path.join(__dirname, '../', 'install/data/categories.json'), function(err, default_categories) { | 
					
						
							|  |  |  | 								default_categories = JSON.parse(default_categories); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								async.eachSeries(default_categories, function(category, next) { | 
					
						
							|  |  |  | 									admin.categories.create(category, next); | 
					
						
							|  |  |  | 								}, function(err) { | 
					
						
							|  |  |  | 									if (!err) next(); | 
					
						
							|  |  |  | 									else winston.error('Could not set up categories'); | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							winston.info('Categories OK. Found ' + data.categories.length + ' categories.'); | 
					
						
							|  |  |  | 							next(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-09-07 11:45:04 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					// Default plugins
 | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 					var	Plugins = require('./plugins'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-07 11:45:04 -04:00
										 |  |  | 					winston.info('Enabling default plugins'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var	defaultEnabled = [ | 
					
						
							|  |  |  | 							'nodebb-plugin-markdown', 'nodebb-plugin-mentions' | 
					
						
							|  |  |  | 						]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					async.each(defaultEnabled, function(pluginId, next) { | 
					
						
							|  |  |  | 						Plugins.isActive(pluginId, function(err, active) { | 
					
						
							|  |  |  | 							if (!active) { | 
					
						
							| 
									
										
										
										
											2013-09-07 11:53:55 -04:00
										 |  |  | 								Plugins.toggleActive(pluginId, function() { | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2013-09-07 11:45:04 -04:00
										 |  |  | 							} else next(); | 
					
						
							|  |  |  | 						}) | 
					
						
							|  |  |  | 					}, next); | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-09-07 11:45:04 -04:00
										 |  |  | 			], function(err) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					winston.warn('NodeBB Setup Aborted.'); | 
					
						
							|  |  |  | 					process.exit(); | 
					
						
							|  |  |  | 				} else callback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		createAdmin: function(callback) { | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 			var	User = require('./user'), | 
					
						
							|  |  |  | 				Groups = require('./groups'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 			winston.warn('No administrators have been detected, running initial user setup'); | 
					
						
							|  |  |  | 			var	questions = [ | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						name: 'username', | 
					
						
							|  |  |  | 						description: 'Administrator username', | 
					
						
							|  |  |  | 						required: true, | 
					
						
							|  |  |  | 						type: 'string' | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						name: 'email', | 
					
						
							|  |  |  | 						description: 'Administrator email address', | 
					
						
							|  |  |  | 						pattern: /.+@.+/, | 
					
						
							|  |  |  | 						required: true | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						name: 'password', | 
					
						
							|  |  |  | 						description: 'Password', | 
					
						
							|  |  |  | 						required: true, | 
					
						
							|  |  |  | 						hidden: true, | 
					
						
							|  |  |  | 						type: 'string' | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-09-07 15:49:23 -04:00
										 |  |  | 				], | 
					
						
							|  |  |  | 				getAdminInfo = function(callback) { | 
					
						
							|  |  |  | 					prompt.get(questions, function(err, results) { | 
					
						
							|  |  |  | 						if (!results) return callback(new Error('aborted')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						nconf.set('bcrypt_rounds', 12); | 
					
						
							|  |  |  | 						User.create(results.username, results.password, results.email, function(err, uid) { | 
					
						
							|  |  |  | 							if (err) { | 
					
						
							|  |  |  | 								winston.warn(err.message + ' Please try again.'); | 
					
						
							|  |  |  | 								return getAdminInfo(); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							Groups.getGidFromName('Administrators', function(err, gid) { | 
					
						
							|  |  |  | 								if (gid) Groups.join(gid, uid, callback); | 
					
						
							|  |  |  | 								else { | 
					
						
							|  |  |  | 									Groups.create('Administrators', 'Forum Administrators', function(err, groupObj) { | 
					
						
							|  |  |  | 										Groups.join(groupObj.gid, uid, callback); | 
					
						
							|  |  |  | 									}); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-09-07 15:49:23 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-09-07 15:49:23 -04:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			getAdminInfo(callback); | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		save: function(server_conf, client_conf, callback) { | 
					
						
							|  |  |  | 			// Server Config
 | 
					
						
							|  |  |  | 			async.parallel([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					fs.writeFile(path.join(__dirname, '../', 'config.json'), JSON.stringify(server_conf, null, 4), function(err) { | 
					
						
							|  |  |  | 						next(err); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					fs.writeFile(path.join(__dirname, '../', 'public', 'config.json'), JSON.stringify(client_conf, null, 4), function(err) { | 
					
						
							|  |  |  | 						next(err); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			], function(err) { | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 				winston.info('Configuration Saved OK'); | 
					
						
							| 
									
										
										
										
											2013-09-10 16:26:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				nconf.file({ file: path.join(__dirname, '..', 'config.json') }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var	RDB = require('./redis'); | 
					
						
							|  |  |  | 				reds.createClient = function() { | 
					
						
							|  |  |  | 					return reds.client || (reds.client = RDB); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-16 16:12:09 -04:00
										 |  |  | 				callback(err); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = install; |