2016-05-24 09:40:26 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2016-05-24 16:36:52 -07:00
|
|
|
initialize: initialize,
|
|
|
|
|
|
|
|
|
|
mailConfig: mailConfig
|
2016-05-24 09:40:26 -07:00
|
|
|
};
|
|
|
|
|
|
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 16:28:59 -07:00
|
|
|
ini = require('ini'),
|
2016-05-28 00:05:54 -07:00
|
|
|
mailboxes = require('./mailboxes.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 13:16:31 -07:00
|
|
|
shell = require('./shell.js'),
|
|
|
|
|
util = require('util');
|
2016-05-24 09:40:26 -07:00
|
|
|
|
|
|
|
|
var SETUP_INFRA_CMD = path.join(__dirname, 'scripts/setup_infra.sh');
|
|
|
|
|
|
2016-05-24 16:28:59 -07:00
|
|
|
var gAddonVars = null;
|
|
|
|
|
|
2016-05-24 09:40:26 -07:00
|
|
|
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();
|
|
|
|
|
|
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 13:16:31 -07:00
|
|
|
removeOldImagesSync();
|
|
|
|
|
|
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 16:28:59 -07:00
|
|
|
loadAddonVarsSync();
|
|
|
|
|
|
2016-05-28 00:05:54 -07:00
|
|
|
mailboxes.setupAliases(function (error) {
|
|
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
|
|
|
|
fs.writeFile(paths.INFRA_VERSION_FILE, JSON.stringify(infra), 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
|
|
|
|
2016-05-24 13:16:31 -07:00
|
|
|
function removeOldImagesSync() {
|
|
|
|
|
debug('removing old addon images');
|
|
|
|
|
|
|
|
|
|
for (var imageName in infra.images) {
|
|
|
|
|
var image = infra.images[imageName];
|
|
|
|
|
debug('cleaning up images of %s', image);
|
|
|
|
|
var cmd = 'docker images "%s" | tail -n +2 | awk \'{ print $1 ":" $2 }\' | grep -v "%s" | xargs --no-run-if-empty docker rmi';
|
|
|
|
|
shell.execSync('removeOldImagesSync', util.format(cmd, image.repo, image.tag));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-05-24 16:28:59 -07:00
|
|
|
|
|
|
|
|
function loadAddonVarsSync() {
|
|
|
|
|
gAddonVars = {
|
|
|
|
|
mail: ini.parse(fs.readFileSync(paths.DATA_DIR + '/addons/mail_vars.sh', 'utf8')),
|
|
|
|
|
postgresql: ini.parse(fs.readFileSync(paths.DATA_DIR + '/addons/postgresql_vars.sh', 'utf8')),
|
|
|
|
|
mysql: ini.parse(fs.readFileSync(paths.DATA_DIR + '/addons/mysql_vars.sh', 'utf8')),
|
|
|
|
|
mongodb: ini.parse(fs.readFileSync(paths.DATA_DIR + '/addons/mongodb_vars.sh', 'utf8'))
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-05-24 16:36:52 -07:00
|
|
|
|
|
|
|
|
function mailConfig() {
|
2016-05-24 17:36:54 -07:00
|
|
|
if (!gAddonVars) return { username: 'no-reply', from: 'no-reply@' + config.fqdn(), password: 'doesnotwork' }; // for tests which don't run infra
|
|
|
|
|
|
2016-05-24 16:36:52 -07:00
|
|
|
return {
|
|
|
|
|
username: gAddonVars.mail.MAIL_ROOT_USERNAME,
|
|
|
|
|
from: '"Cloudron" <' + gAddonVars.mail.MAIL_ROOT_USERNAME + '@' + config.fqdn() + '>',
|
|
|
|
|
password: gAddonVars.mail.MAIL_ROOT_PASSWORD
|
|
|
|
|
};
|
|
|
|
|
}
|