2016-05-24 09:40:26 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
initialize: initialize
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-24 10:52:55 -07:00
|
|
|
var apps = require('./apps.js'),
|
2016-05-24 10:58:18 -07:00
|
|
|
assert = require('assert'),
|
2016-05-24 10:52:55 -07:00
|
|
|
config = require('./config.js'),
|
2016-05-24 09:40:26 -07:00
|
|
|
certificates = require('./certificates.js'),
|
|
|
|
|
debug = require('debug')('box:platform'),
|
2016-05-24 10:52:55 -07:00
|
|
|
fs = require('fs'),
|
2016-05-24 13:10:18 -07:00
|
|
|
infra = require('./infra_version.js'),
|
2016-05-24 09:40:26 -07:00
|
|
|
path = require('path'),
|
|
|
|
|
paths = require('./paths.js'),
|
2016-05-24 13:10:18 -07:00
|
|
|
safe = require('safetydance'),
|
2016-05-24 09:40:26 -07:00
|
|
|
shell = require('./shell.js');
|
|
|
|
|
|
|
|
|
|
var SETUP_INFRA_CMD = path.join(__dirname, 'scripts/setup_infra.sh');
|
|
|
|
|
|
|
|
|
|
function initialize(callback) {
|
|
|
|
|
if (process.env.BOX_ENV === 'test' && !process.env.CREATE_INFRA) return callback();
|
|
|
|
|
|
|
|
|
|
debug('initializing addon infrastructure');
|
2016-05-24 10:52:55 -07:00
|
|
|
|
2016-05-24 13:10:18 -07:00
|
|
|
var existingInfra = { version: 'none' };
|
2016-05-24 10:52:55 -07:00
|
|
|
if (fs.existsSync(paths.INFRA_VERSION_FILE)) {
|
2016-05-24 13:10:18 -07:00
|
|
|
existingInfra = safe.JSON.parse(fs.readFileSync(paths.INFRA_VERSION_FILE, 'utf8'));
|
|
|
|
|
if (!existingInfra) existingInfra = { version: 'legacy' };
|
2016-05-24 10:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 13:10:18 -07:00
|
|
|
if (infra.version === existingInfra.version) {
|
|
|
|
|
debug('platform is uptodate at version %s', infra.version);
|
2016-05-24 10:52:55 -07:00
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 13:10:18 -07:00
|
|
|
debug('Updating infrastructure from %s to %s', existingInfra.version, infra.version);
|
2016-05-24 10:52:55 -07:00
|
|
|
|
|
|
|
|
stopContainersSync();
|
|
|
|
|
|
|
|
|
|
if (!existingInfra.INFRA_VERSION) removeImagesSync(); // a hack for --recreate-infra
|
|
|
|
|
|
2016-05-24 10:58:18 -07:00
|
|
|
startAddons(function (error) {
|
2016-05-24 09:40:26 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2016-05-24 10:58:18 -07:00
|
|
|
var func = existingInfra ? apps.configureInstalledApps : apps.restoreInstalledApps;
|
2016-05-24 10:52:55 -07:00
|
|
|
|
2016-05-24 10:58:18 -07:00
|
|
|
func(function (error) {
|
|
|
|
|
if (error) return callback(error);
|
2016-05-24 10:52:55 -07:00
|
|
|
|
2016-05-24 13:10:18 -07:00
|
|
|
fs.writeFileSync(paths.INFRA_VERSION_FILE, JSON.stringify(infra));
|
2016-05-24 10:52:55 -07:00
|
|
|
|
2016-05-24 10:58:18 -07:00
|
|
|
callback();
|
2016-05-24 10:52:55 -07:00
|
|
|
});
|
2016-05-24 09:40:26 -07:00
|
|
|
});
|
|
|
|
|
}
|
2016-05-24 10:52:55 -07:00
|
|
|
|
|
|
|
|
function removeImagesSync() {
|
|
|
|
|
debug('removing existing images');
|
|
|
|
|
shell.execSync('removeImagesSync', 'docker images -q | xargs --no-run-if-empty docker rmi -f');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stopContainersSync() {
|
|
|
|
|
// TODO: be nice and stop addons cleanly (example, shutdown commands)
|
|
|
|
|
debug('stopping existing containers');
|
|
|
|
|
shell.execSync('stopContainersSync', 'docker ps -qa | xargs --no-run-if-empty docker rm -f');
|
|
|
|
|
}
|
2016-05-24 10:58:18 -07:00
|
|
|
|
|
|
|
|
function startAddons(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
|
|
|
|
certificates.getAdminCertificatePath(function (error, certFilePath, keyFilePath) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
shell.sudo('seutp_infra', [ SETUP_INFRA_CMD, paths.DATA_DIR, config.fqdn(), config.adminFqdn(), certFilePath, keyFilePath ], function (error) {
|
|
|
|
|
callback(error);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|