From 88bf9425db8d63b6b04e49390e9853e35fc8433e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 27 May 2013 14:02:57 -0400 Subject: [PATCH] install script + patching up integration with install wizard --- app.js | 185 +++++++++++++++++------- public/templates/config.json | 4 +- public/templates/install/footer.tpl | 34 +++-- public/templates/install/header.tpl | 6 - public/templates/install/mail.tpl | 5 +- public/templates/install/privileges.tpl | 2 +- public/templates/install/social.tpl | 14 +- src/meta.js | 3 +- src/notifications.js | 3 +- src/postTools.js | 5 +- src/posts.js | 1 - src/redis.js | 4 +- src/routes/authentication.js | 25 ++-- src/routes/user.js | 3 - src/threadTools.js | 3 +- src/topics.js | 1 - src/user.js | 3 +- src/webserver.js | 5 +- src/websockets.js | 18 +-- 19 files changed, 188 insertions(+), 136 deletions(-) diff --git a/app.js b/app.js index 4005af1aa1..38bcdfd222 100644 --- a/app.js +++ b/app.js @@ -1,54 +1,131 @@ -var categories = require('./src/categories.js'), - templates = require('./public/src/templates.js'), - webserver = require('./src/webserver.js'), - websockets = require('./src/websockets.js'), - admin = { - 'categories': require('./src/admin/categories.js') - }, - fs = require('fs'); - -DEVELOPMENT = true; - -global.configuration = {}; -global.templates = {}; - -(function(config) { - config['ROOT_DIRECTORY'] = __dirname; - - templates.init([ - 'header', 'footer', 'logout', 'admin/header', 'admin/footer', 'admin/index', - 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext', - 'emails/header', 'emails/footer', 'install/header', 'install/footer', 'install/redis' - ]); - - templates.ready(function() { - webserver.init(); - }); - - //setup scripts to be moved outside of the app in future. - function setup_categories() { - console.log('Checking categories...'); - categories.getAllCategories(function(data) { - if (data.categories.length === 0) { - console.log('Setting up default categories...'); - - fs.readFile(config.ROOT_DIRECTORY + '/install/data/categories.json', function(err, default_categories) { - default_categories = JSON.parse(default_categories); - - for (var category in default_categories) { - admin.categories.create(default_categories[category]); - } - }); - - } else { - console.log('Good.'); - } - }); - } - - - setup_categories(); - - - -}(global.configuration)); \ No newline at end of file +// Read config.js to grab redis info +var fs = require('fs'), + path = require('path'), + utils = require('./public/src/utils.js'); + +console.log('Info: Checking for valid base configuration file'); +fs.readFile(path.join(__dirname, 'config.json'), function(err, data) { + if (!err) { + global.config = JSON.parse(data); + global.config.url = global.config.base_url + (global.config.use_port ? ':' + global.config.port : '') + '/'; + global.config.upload_url = global.config.url + 'uploads/'; + console.log('Info: Base Configuration OK.'); + + var meta = require('./src/meta.js'); + meta.config.get(function(config) { + for(c in config) { + if (config.hasOwnProperty(c)) { + global.config[c] = config[c]; + } + } + + var categories = require('./src/categories.js'), + templates = require('./public/src/templates.js'), + webserver = require('./src/webserver.js'), + websockets = require('./src/websockets.js'), + admin = { + 'categories': require('./src/admin/categories.js') + }; + + DEVELOPMENT = true; + + global.configuration = {}; + global.templates = {}; + + (function(config) { + config['ROOT_DIRECTORY'] = __dirname; + + templates.init([ + 'header', 'footer', 'logout', 'admin/header', 'admin/footer', 'admin/index', + 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext', + 'emails/header', 'emails/footer', 'install/header', 'install/footer', 'install/redis' + ]); + + templates.ready(function() { + webserver.init(); + }); + + //setup scripts to be moved outside of the app in future. + function setup_categories() { + console.log('Info: Checking categories...'); + categories.getAllCategories(function(data) { + if (data.categories.length === 0) { + console.log('Info: Setting up default categories...'); + + fs.readFile(config.ROOT_DIRECTORY + '/install/data/categories.json', function(err, default_categories) { + default_categories = JSON.parse(default_categories); + + for (var category in default_categories) { + admin.categories.create(default_categories[category]); + } + }); + + } else { + console.log('Info: Good.'); + } + }); + } + setup_categories(); + }(global.configuration)); + }); + } else { + // New install, ask setup questions + console.log('Info: Configuration not found, starting NodeBB setup'); + var ask = function(question, callback) { + process.stdin.resume(); + process.stdout.write(question + ': '); + + process.stdin.once('data', function(data) { + callback(data.toString().trim()); + }); + } + + process.stdout.write( + "\nWelcome to NodeBB!\nThis looks like a new installation, so you'll have to answer a " + + "few questions about your environment before we can proceed with the setup.\n\n\nWhat is...\n\n" + ); + + ask('... the publically accessible URL of this installation? (http://localhost)', function(base_url) { + ask('... the port number of your install? (4567)', function(port) { + ask('Will you be using a port number to access NodeBB? (y)', function(use_port) { + ask('... the host IP or address of your Redis instance? (127.0.0.1)', function(redis_host) { + ask('... the host port of your Redis instance? (6379)', function(redis_port) { + ask('... your NodeBB secret? (keyboard mash for a bit here)', function(secret) { + if (!base_url) base_url = 'http://localhost'; + if (!port) port = 4567; + if (!use_port) use_port = true; else use_port = (use_port === 'y' ? true : false); + if (!redis_host) redis_host = '127.0.0.1'; + if (!redis_port) redis_port = 6379; + if (!secret) secret = utils.generateUUID(); + + var fs = require('fs'), + path = require('path'), + config = { + secret: secret, + base_url: base_url, + port: port, + use_port: use_port, + redis: { + host: redis_host, + port: redis_port + } + } + + fs.writeFile(path.join(__dirname, 'config.json'), JSON.stringify(config, null, 4), function(err) { + if (err) throw err; + else { + process.stdout.write( + "\n\nConfiguration Saved OK\n\nPlease start NodeBB again and navigate to " + + base_url + (use_port ? ':' + port : '') + "/install to continue setup.\n\n" + ); + process.exit(); + } + }); + }); + }); + }); + }); + }); + }); + } +}); \ No newline at end of file diff --git a/public/templates/config.json b/public/templates/config.json index 5af89957fb..551606ba59 100644 --- a/public/templates/config.json +++ b/public/templates/config.json @@ -10,9 +10,7 @@ "admin/twitter[^]*": "admin/twitter", "admin/facebook[^]*": "admin/facebook", "admin/gplus[^]*": "admin/gplus", - "install/?$": "install/redis", - "install/basic/?": "install/basic", - "install/redis/?": "install/redis", + "install/?$": "install/mail", "install/mail/?": "install/mail", "install/social/?": "install/social", "install/privileges/?": "install/privileges", diff --git a/public/templates/install/footer.tpl b/public/templates/install/footer.tpl index 9823d43371..e76153ecb1 100644 --- a/public/templates/install/footer.tpl +++ b/public/templates/install/footer.tpl @@ -5,17 +5,26 @@ config: undefined, prepare: function() { // Bounce if config is not ready - if (nodebb_setup.config === undefined) { - ajaxify.go('install/redis'); - app.alert({ - alert_id: 'config-ready', - type: 'error', - timeout: 10000, - title: 'NodeBB Configuration Not Ready!', - message: 'NodeBB cannot proceed with setup at this time as Redis database information ' + - 'was not found. Please enter the information below.' - }); + // if (nodebb_setup.config === undefined) { + // ajaxify.go('install/redis'); + // app.alert({ + // alert_id: 'config-ready', + // type: 'error', + // timeout: 10000, + // title: 'NodeBB Configuration Not Ready!', + // message: 'NodeBB cannot proceed with setup at this time as Redis database information ' + + // 'was not found. Please enter the information below.' + // }); + + // return; + // } + // Come back in 500ms if the config isn't ready yet + if (nodebb_setup.config === undefined) { + console.log('Config not ready...'); + setTimeout(function() { + nodebb_setup.prepare(); + }, 500); return; } @@ -106,6 +115,11 @@ } }, false); + socket.emit('api:config.get'); + socket.on('api:config.get', function(data) { + nodebb_setup.config = data; + }); + socket.on('api:config.set', function(data) { if (data.status === 'ok') { app.alert({ diff --git a/public/templates/install/header.tpl b/public/templates/install/header.tpl index d598b0bb06..d9f66909cb 100644 --- a/public/templates/install/header.tpl +++ b/public/templates/install/header.tpl @@ -33,12 +33,6 @@