Files
cloudron-box/src/platform.js

154 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-05-24 09:40:26 -07:00
'use strict';
exports = module.exports = {
start: start,
stop: stop,
handleCertChanged: handleCertChanged
2016-05-24 09:40:26 -07:00
};
var addons = require('./addons.js'),
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'),
config = require('./config.js'),
2016-05-24 09:40:26 -07:00
debug = require('debug')('box:platform'),
fs = require('fs'),
graphs = require('./graphs.js'),
2016-05-24 13:10:18 -07:00
infra = require('./infra_version.js'),
locker = require('./locker.js'),
mail = require('./mail.js'),
2016-05-24 09:40:26 -07:00
paths = require('./paths.js'),
reverseProxy = require('./reverseproxy.js'),
2016-05-24 13:10:18 -07:00
safe = require('safetydance'),
2016-05-24 13:16:31 -07:00
shell = require('./shell.js'),
taskmanager = require('./taskmanager.js'),
_ = require('underscore');
2016-05-24 09:40:26 -07:00
var gPlatformReadyTimer = null;
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
function start(callback) {
assert.strictEqual(typeof callback, 'function');
2016-09-03 11:46:57 -07:00
if (process.env.BOX_ENV === 'test' && !process.env.TEST_CREATE_INFRA) return callback();
2016-05-24 09:40:26 -07:00
debug('initializing addon infrastructure');
2016-05-24 13:10:18 -07:00
var existingInfra = { version: 'none' };
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: 'corrupt' };
}
// short-circuit for the restart case
if (_.isEqual(infra, existingInfra)) {
2016-05-24 13:10:18 -07:00
debug('platform is uptodate at version %s', infra.version);
emitPlatformReady();
return callback();
}
2016-05-24 13:10:18 -07:00
debug('Updating infrastructure from %s to %s', existingInfra.version, infra.version);
var error = locker.lock(locker.OP_PLATFORM_START);
if (error) return callback(error);
2016-05-28 01:56:32 -07:00
async.series([
2016-07-25 00:39:57 -07:00
stopContainers.bind(null, existingInfra),
graphs.startGraphite.bind(null, existingInfra),
addons.startAddons.bind(null, existingInfra),
2016-05-28 01:56:32 -07:00
removeOldImages,
startApps.bind(null, existingInfra),
fs.writeFile.bind(fs, paths.INFRA_VERSION_FILE, JSON.stringify(infra, null, 4))
], function (error) {
if (error) return callback(error);
2016-06-21 10:37:12 -05:00
locker.unlock(locker.OP_PLATFORM_START);
emitPlatformReady();
callback();
});
2016-06-21 10:37:12 -05:00
}
function stop(callback) {
2016-06-21 10:37:12 -05:00
clearTimeout(gPlatformReadyTimer);
gPlatformReadyTimer = null;
exports.events = null;
taskmanager.pauseTasks(callback);
2016-06-21 10:37:12 -05:00
}
function emitPlatformReady() {
// give some time for the platform to "settle". For example, mysql might still be initing the
// database dir and we cannot call service scripts until that's done.
// TODO: make this smarter to not wait for 15secs for the crash-restart case
gPlatformReadyTimer = setTimeout(function () {
debug('emitting platform ready');
gPlatformReadyTimer = null;
taskmanager.resumeTasks();
}, 15000);
}
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) {
const image = infra.images[imageName];
2018-09-15 18:41:06 -07:00
const tag = image.tag.replace(/:.*@/, '@'); // this remove the semver tag
2016-06-08 15:05:43 +02:00
debug('cleaning up images of %j', image);
// older docker images did not have sha256 and thus have it as <none>
const cmd = `docker images --digests "${image.repo}" | tail -n +2 | awk '{ print $1 ($3=="<none>" ? (":" $2) : ("@" $3)) }' | grep -v "${tag}" | xargs --no-run-if-empty docker rmi`;
shell.execSync('removeOldImagesSync', cmd);
2016-05-24 13:16:31 -07:00
}
2016-05-28 01:56:32 -07:00
callback();
2016-05-24 13:16:31 -07:00
}
2016-07-25 00:39:57 -07:00
function stopContainers(existingInfra, callback) {
// always stop addons to restart them on any infra change, regardless of minor or major update
if (existingInfra.version !== infra.version) {
2016-07-25 09:38:31 -07:00
debug('stopping all containers for infra upgrade');
shell.execSync('stopContainers', 'docker ps -qa | xargs --no-run-if-empty docker stop');
2016-07-25 00:39:57 -07:00
shell.execSync('stopContainers', 'docker ps -qa | xargs --no-run-if-empty docker rm -f');
} else {
assert(typeof infra.images, 'object');
var changedAddons = [ ];
for (var imageName in infra.images) {
2016-07-25 09:38:31 -07:00
if (infra.images[imageName].tag !== existingInfra.images[imageName].tag) changedAddons.push(imageName);
2016-07-25 00:39:57 -07:00
}
debug('stopContainer: stopping addons for incremental infra update: %j', changedAddons);
let filterArg = changedAddons.map(function (c) { return `--filter 'name=${c}`; }).join(' '); // name=c matches *c*. required for redis-{appid}
// ignore error if container not found (and fail later) so that this code works across restarts
shell.execSync('stopContainers', `docker ps -qa ${filterArg} | xargs --no-run-if-empty docker stop || true`);
shell.execSync('stopContainers', `docker ps -qa ${filterArg} | xargs --no-run-if-empty docker rm -f || true`);
2016-07-25 00:39:57 -07:00
}
2016-05-28 01:56:32 -07:00
callback();
}
2016-05-24 10:58:18 -07:00
function startApps(existingInfra, callback) {
if (existingInfra.version === 'none') {
debug('startApps: restoring installed apps');
apps.restoreInstalledApps(callback);
} else if (existingInfra.version !== infra.version) {
debug('startApps: reconfiguring installed apps');
reverseProxy.removeAppConfigs(); // should we change the cert location, nginx will not start
apps.configureInstalledApps(callback);
} else {
debug('startApps: apps are already uptodate');
callback();
}
}
function handleCertChanged(cn) {
assert.strictEqual(typeof cn, 'string');
2018-01-28 14:19:28 -08:00
if (cn === '*.' + config.adminDomain() || cn === config.adminFqdn()) {
mail.startMail(NOOP_CALLBACK);
}
}