Add web ui to create config file
This commit is contained in:
committed by
Girish Ramakrishnan
parent
3038521916
commit
0c3dda8ee0
@@ -16,6 +16,7 @@ var assert = require('assert'),
|
||||
json = require('body-parser').json,
|
||||
lastMile = require('connect-lastmile'),
|
||||
morgan = require('morgan'),
|
||||
path = require('path'),
|
||||
safe = require('safetydance');
|
||||
|
||||
exports = module.exports = {
|
||||
@@ -29,6 +30,7 @@ var CLOUDRON_CONFIG_FILE = '/home/yellowtent/configs/cloudron.conf';
|
||||
var BOX_VERSIONS_URL = 'https://s3.amazonaws.com/prod-cloudron-releases/versions.json';
|
||||
|
||||
var gHttpServer = null; // update server; used for updates
|
||||
var gSetupServer = null; // setup server; only used for initial setup
|
||||
|
||||
function provision(callback) {
|
||||
if (fs.existsSync(CLOUDRON_CONFIG_FILE)) {
|
||||
@@ -40,6 +42,9 @@ function provision(callback) {
|
||||
|
||||
function retry(error) {
|
||||
if (error) console.error(error);
|
||||
|
||||
if (!gSetupServer) startSetupServer(function () { debug('Setup Server started.'); });
|
||||
|
||||
setTimeout(provision.bind(null, callback), 5000);
|
||||
}
|
||||
|
||||
@@ -66,6 +71,8 @@ function provision(callback) {
|
||||
|
||||
if (typeof userData.data.boxVersionsUrl !== 'string') return retry('boxVersionsUrl in user data has to be a non-empty string');
|
||||
|
||||
stopSetupServer(function () { debug('Setup Server stopped.'); });
|
||||
|
||||
installer.provision(userData, callback);
|
||||
}
|
||||
|
||||
@@ -118,6 +125,61 @@ function stopUpdateServer(callback) {
|
||||
gHttpServer = null;
|
||||
}
|
||||
|
||||
function setup(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
if (!req.body.fqdn || typeof req.body.fqdn !== 'string') return next(new HttpError(400, 'No fqdn provided'));
|
||||
|
||||
debug('setup: received from box %j', req.body);
|
||||
|
||||
var data = {
|
||||
fqdn: req.body.fqdn
|
||||
};
|
||||
|
||||
if (req.body.boxVersionsUrl && typeof req.body.boxVersionsUrl === 'string') data.boxVersionsUrl = req.body.boxVersionsUrl;
|
||||
|
||||
fs.writeFile(PROVISION_CONFIG_FILE_JSON, JSON.stringify({ data: data }), function (error) {
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(201, {}));
|
||||
});
|
||||
}
|
||||
|
||||
function startSetupServer(callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
debug('Starting setup server');
|
||||
|
||||
var app = express();
|
||||
|
||||
var router = new express.Router();
|
||||
|
||||
if (process.env.NODE_ENV !== 'test') app.use(morgan('dev', { immediate: false }));
|
||||
|
||||
app.use(express.static(path.join(__dirname, '../www/')))
|
||||
.use(json({ strict: true }))
|
||||
.use(router)
|
||||
.use(lastMile());
|
||||
|
||||
router.post('/api/v1/setup', setup);
|
||||
|
||||
gSetupServer = http.createServer(app);
|
||||
gSetupServer.on('error', console.error);
|
||||
|
||||
gSetupServer.listen(2021, '127.0.0.1', callback);
|
||||
}
|
||||
|
||||
function stopSetupServer(callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
debug('Stopping setup server');
|
||||
|
||||
if (!gSetupServer) return callback(null);
|
||||
|
||||
gSetupServer.close(callback);
|
||||
gSetupServer = null;
|
||||
}
|
||||
|
||||
function start(callback) {
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user