Files
cloudron-box/src/platform.js

172 lines
6.8 KiB
JavaScript
Raw Normal View History

2016-05-24 09:40:26 -07:00
'use strict';
exports = module.exports = {
start: start,
stop: stop,
// exported for testing
_isReady: false
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'),
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'),
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'),
settings = require('./settings.js'),
2019-04-04 20:46:01 -07:00
sftp = require('./sftp.js'),
2016-05-24 13:16:31 -07:00
shell = require('./shell.js'),
2019-08-28 15:00:55 -07:00
tasks = require('./tasks.js'),
_ = require('underscore');
2016-05-24 09:40:26 -07:00
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);
onPlatformReady();
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),
2018-11-11 10:35:26 -08:00
// mark app state before we start addons. this gives the db import logic a chance to mark an app as errored
startApps.bind(null, existingInfra),
graphs.startGraphite.bind(null, existingInfra),
2019-04-04 20:46:01 -07:00
sftp.startSftp.bind(null, existingInfra),
addons.startServices.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);
onPlatformReady();
callback();
});
2016-06-21 10:37:12 -05:00
}
function stop(callback) {
2019-08-28 15:00:55 -07:00
tasks.stopAllTasks(callback);
2016-06-21 10:37:12 -05:00
}
function onPlatformReady() {
debug('onPlatformReady: platform is ready');
exports._isReady = true;
2019-09-24 20:29:01 -07:00
apps.schedulePendingTasks(NOOP_CALLBACK);
applyPlatformConfig(NOOP_CALLBACK);
pruneInfraImages(NOOP_CALLBACK);
}
function applyPlatformConfig(callback) {
// scale back db containers, if possible. this is retried because updating memory constraints can fail
// with failed to write to memory.memsw.limit_in_bytes: write /sys/fs/cgroup/memory/docker/xx/memory.memsw.limit_in_bytes: device or resource busy
async.retry({ times: 10, interval: 5 * 60 * 1000 }, function (retryCallback) {
settings.getPlatformConfig(function (error, platformConfig) {
if (error) return retryCallback(error);
addons.updateServiceConfig(platformConfig, function (error) {
if (error) debug('Error updating services. Will rety in 5 minutes', platformConfig, error);
retryCallback(error);
});
});
}, callback);
}
2018-10-27 13:04:13 -07:00
function pruneInfraImages(callback) {
debug('pruneInfraImages: checking existing images');
// cannot blindly remove all unused images since redis image may not be used
const images = infra.baseImages.concat(Object.keys(infra.images).map(function (addon) { return infra.images[addon]; }));
async.eachSeries(images, function (image, iteratorCallback) {
let output = safe.child_process.execSync(`docker images --digests ${image.repo} --format "{{.ID}} {{.Repository}}:{{.Tag}}@{{.Digest}}"`, { encoding: 'utf8' });
if (output === null) return iteratorCallback(safe.error);
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
2019-05-29 12:14:53 -07:00
debug(`pruneInfraImages: removing unused image of ${image.repo}: tag: ${parts[1]} id: ${parts[0]}`);
2016-05-28 01:56:32 -07:00
2019-05-29 12:14:53 -07:00
let result = safe.child_process.execSync(`docker rmi ${parts[0]}`, { encoding: 'utf8' });
if (result === null) debug(`Erroring removing image ${parts[0]}: ${safe.error.mesage}`);
}
2019-05-29 12:14:53 -07:00
iteratorCallback();
}, 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) {
// TODO: only nuke containers with isCloudronManaged=true
2016-07-25 09:38:31 -07:00
debug('stopping all containers for infra upgrade');
async.series([
shell.exec.bind(null, 'stopContainers', 'docker ps -qa --filter \'network=cloudron\' | xargs --no-run-if-empty docker stop'),
shell.exec.bind(null, 'stopContainers', 'docker ps -qa --filter \'network=cloudron\' | xargs --no-run-if-empty docker rm -f')
], callback);
2016-07-25 00:39:57 -07:00
} else {
assert(typeof infra.images, 'object');
var changedAddons = [ ];
2019-04-04 20:46:01 -07:00
for (var imageName in existingInfra.images) { // do not use infra.images because we can only stop things which are existing
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);
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}
// ignore error if container not found (and fail later) so that this code works across restarts
async.series([
shell.exec.bind(null, 'stopContainers', `docker ps -qa ${filterArg} --filter 'network=cloudron' | xargs --no-run-if-empty docker stop || true`),
shell.exec.bind(null, 'stopContainers', `docker ps -qa ${filterArg} --filter 'network=cloudron' | xargs --no-run-if-empty docker rm -f || true`)
], callback);
2016-07-25 00:39:57 -07:00
}
}
2016-05-24 10:58:18 -07:00
function startApps(existingInfra, callback) {
if (existingInfra.version === 'none') { // cloudron is being restored from backup
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();
}
}