Files
cloudron-box/src/platform.js
T

114 lines
3.8 KiB
JavaScript
Raw Normal View History

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-28 01:56:32 -07:00
async = require('async'),
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'),
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');
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
2016-05-28 01:56:32 -07:00
async.series([
stopContainers,
startAddons,
removeOldImages,
existingInfra ? apps.configureInstalledApps : apps.restoreInstalledApps,
loadAddonVars,
mailboxes.setupAliases,
fs.writeFile.bind(fs, paths.INFRA_VERSION_FILE, JSON.stringify(infra))
], callback);
2016-05-24 09:40:26 -07:00
}
2016-05-24 10:52:55 -07:00
2016-05-28 01:56:32 -07:00
function removeOldImages(callback) {
2016-05-24 13:16:31 -07:00
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-28 01:56:32 -07:00
callback();
2016-05-24 13:16:31 -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');
}
2016-05-28 01:56:32 -07:00
function stopContainers(callback) {
2016-05-24 10:52:55 -07:00
// 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-28 01:56:32 -07:00
callback();
2016-05-24 10:52:55 -07:00
}
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-28 01:56:32 -07:00
function loadAddonVars(callback) {
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-28 01:56:32 -07:00
callback();
}
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
};
}