2016-05-24 09:40:26 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2017-02-07 10:30:52 -08:00
|
|
|
start: start,
|
2018-01-26 22:35:08 -08:00
|
|
|
stop: stop,
|
|
|
|
|
|
2018-10-29 12:44:01 -07:00
|
|
|
handleCertChanged: handleCertChanged,
|
|
|
|
|
|
|
|
|
|
// exported for testing
|
|
|
|
|
_isReady: false
|
2016-05-24 09:40:26 -07:00
|
|
|
};
|
|
|
|
|
|
2018-10-14 13:26:01 -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'),
|
2018-01-26 22:35:08 -08:00
|
|
|
config = require('./config.js'),
|
2016-05-24 09:40:26 -07:00
|
|
|
debug = require('debug')('box:platform'),
|
2018-10-27 11:47:24 -07:00
|
|
|
execSync = require('child_process').execSync,
|
2016-05-24 10:52:55 -07:00
|
|
|
fs = require('fs'),
|
2018-10-16 14:07:41 -07:00
|
|
|
graphs = require('./graphs.js'),
|
2016-05-24 13:10:18 -07:00
|
|
|
infra = require('./infra_version.js'),
|
2017-11-28 23:18:43 -08:00
|
|
|
locker = require('./locker.js'),
|
2018-01-20 18:56:17 -08:00
|
|
|
mail = require('./mail.js'),
|
2016-05-24 09:40:26 -07:00
|
|
|
paths = require('./paths.js'),
|
2018-01-30 12:23:27 -08:00
|
|
|
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'),
|
2017-04-24 13:50:24 -07:00
|
|
|
taskmanager = require('./taskmanager.js'),
|
2016-07-24 23:19:11 -07:00
|
|
|
_ = require('underscore');
|
2016-05-24 09:40:26 -07:00
|
|
|
|
2018-01-26 22:35:08 -08:00
|
|
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
|
|
2017-02-07 10:30:52 -08:00
|
|
|
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 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'));
|
2016-06-20 11:59:43 -05:00
|
|
|
if (!existingInfra) existingInfra = { version: 'corrupt' };
|
2016-05-24 10:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-24 23:19:11 -07:00
|
|
|
// 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);
|
2018-05-16 17:31:32 -07:00
|
|
|
|
2018-10-29 12:44:01 -07:00
|
|
|
onPlatformReady();
|
2018-05-16 17:31:32 -07:00
|
|
|
|
2018-07-25 13:06:38 -07:00
|
|
|
return callback();
|
2016-05-24 10:52:55 -07:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2017-11-28 23:18:43 -08:00
|
|
|
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),
|
2018-10-16 14:07:41 -07:00
|
|
|
graphs.startGraphite.bind(null, existingInfra),
|
|
|
|
|
addons.startAddons.bind(null, existingInfra),
|
2018-10-27 13:04:13 -07:00
|
|
|
pruneInfraImages,
|
2016-07-25 18:57:54 -07:00
|
|
|
startApps.bind(null, existingInfra),
|
2018-07-25 13:06:38 -07:00
|
|
|
fs.writeFile.bind(fs, paths.INFRA_VERSION_FILE, JSON.stringify(infra, null, 4))
|
2016-07-24 22:59:47 -07:00
|
|
|
], function (error) {
|
|
|
|
|
if (error) return callback(error);
|
2016-06-21 10:37:12 -05:00
|
|
|
|
2017-11-28 23:18:43 -08:00
|
|
|
locker.unlock(locker.OP_PLATFORM_START);
|
|
|
|
|
|
2018-10-29 12:44:01 -07:00
|
|
|
onPlatformReady();
|
2016-07-24 22:59:47 -07:00
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2016-06-21 10:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-24 13:50:24 -07:00
|
|
|
function stop(callback) {
|
|
|
|
|
taskmanager.pauseTasks(callback);
|
2016-06-21 10:37:12 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-29 12:44:01 -07:00
|
|
|
function onPlatformReady() {
|
|
|
|
|
debug('onPlatformReady: resuming task manager');
|
|
|
|
|
exports._isReady = true;
|
|
|
|
|
taskmanager.resumeTasks();
|
2017-03-15 20:31:15 -07:00
|
|
|
}
|
|
|
|
|
|
2018-10-27 13:04:13 -07:00
|
|
|
function pruneInfraImages(callback) {
|
|
|
|
|
debug('pruneInfraImages: checking existing images');
|
2018-10-27 11:47:24 -07:00
|
|
|
|
|
|
|
|
// cannot blindly remove all unused images since redis image may not be used
|
2018-10-27 13:04:13 -07:00
|
|
|
let images = infra.baseImages.concat(Object.keys(infra.images).map(function (addon) { return infra.images[addon]; }));
|
2018-10-27 11:47:24 -07:00
|
|
|
|
2018-10-27 13:04:13 -07:00
|
|
|
for (let image of images) {
|
|
|
|
|
let output = execSync(`docker images --digests ${image.repo} --format "{{.ID}} {{.Repository}}:{{.Tag}}@{{.Digest}}"`, { encoding: 'utf8' });
|
2018-10-27 11:47:24 -07:00
|
|
|
let lines = output.trim().split('\n');
|
|
|
|
|
for (let line of lines) {
|
|
|
|
|
if (!line) continue;
|
|
|
|
|
let parts = line.split(' '); // [ ID, Repo:Tag@Digest ]
|
|
|
|
|
if (image.tag === parts[1]) continue; // keep
|
2018-10-27 13:04:13 -07:00
|
|
|
debug(`pruneInfraImages: removing unused image of ${image.repo}: ${line}`);
|
|
|
|
|
shell.execSync('pruneInfraImages', `docker rmi ${parts[0]}`);
|
2018-10-27 11:47:24 -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) {
|
2017-03-30 12:48:46 +02:00
|
|
|
// 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');
|
2018-10-16 13:23:23 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2018-10-16 13:23:23 -07:00
|
|
|
debug('stopContainer: stopping addons for incremental infra update: %j', changedAddons);
|
2018-10-17 18:20:39 +02:00
|
|
|
let filterArg = changedAddons.map(function (c) { return `--filter 'name=${c}'`; }).join(' '); // name=c matches *c*. required for redis-{appid}
|
2016-09-03 11:24:12 -07:00
|
|
|
// ignore error if container not found (and fail later) so that this code works across restarts
|
2018-10-16 13:23:23 -07:00
|
|
|
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:52:55 -07:00
|
|
|
}
|
2016-05-24 10:58:18 -07:00
|
|
|
|
2016-07-25 18:57:54 -07:00
|
|
|
function startApps(existingInfra, callback) {
|
2018-10-16 11:04:34 -07:00
|
|
|
if (existingInfra.version === 'none') {
|
2016-07-25 18:57:54 -07:00
|
|
|
debug('startApps: restoring installed apps');
|
|
|
|
|
apps.restoreInstalledApps(callback);
|
2018-10-16 11:04:34 -07:00
|
|
|
} else if (existingInfra.version !== infra.version) {
|
2016-07-25 18:57:54 -07:00
|
|
|
debug('startApps: reconfiguring installed apps');
|
2018-01-30 12:23:27 -08:00
|
|
|
reverseProxy.removeAppConfigs(); // should we change the cert location, nginx will not start
|
2016-07-25 18:57:54 -07:00
|
|
|
apps.configureInstalledApps(callback);
|
2018-10-16 11:04:34 -07:00
|
|
|
} else {
|
|
|
|
|
debug('startApps: apps are already uptodate');
|
|
|
|
|
callback();
|
2016-07-25 18:57:54 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-01-26 22:35:08 -08:00
|
|
|
|
|
|
|
|
function handleCertChanged(cn) {
|
|
|
|
|
assert.strictEqual(typeof cn, 'string');
|
|
|
|
|
|
2018-10-24 20:06:43 -07:00
|
|
|
debug('handleCertChanged', cn);
|
|
|
|
|
|
2018-01-28 14:19:28 -08:00
|
|
|
if (cn === '*.' + config.adminDomain() || cn === config.adminFqdn()) {
|
2018-01-26 22:35:08 -08:00
|
|
|
mail.startMail(NOOP_CALLBACK);
|
|
|
|
|
}
|
|
|
|
|
}
|