Files
cloudron-box/src/platform.js
T

155 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-05-24 09:40:26 -07:00
'use strict';
exports = module.exports = {
2021-01-21 11:31:35 -08:00
start,
stopAllTasks,
2018-01-26 22:35:08 -08:00
// exported for testing
_isReady: false
2016-05-24 09:40:26 -07:00
};
2021-01-21 11:31:35 -08:00
const 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'),
2016-05-24 10:52:55 -07:00
fs = require('fs'),
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'),
2021-01-21 11:31:35 -08:00
services = require('./services.js'),
2018-11-12 21:43:53 -08:00
settings = require('./settings.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'),
2016-07-24 23:19:11 -07:00
_ = require('underscore');
2016-05-24 09:40:26 -07:00
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
2020-09-02 18:13:08 -07:00
onPlatformReady(false /* !infraChanged */);
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
var error = locker.lock(locker.OP_PLATFORM_START);
if (error) return callback(error);
2016-05-28 01:56:32 -07:00
async.series([
2020-11-20 14:13:16 -08:00
(next) => { if (existingInfra.version !== infra.version) removeAllContainers(next); else next(); },
2020-07-30 14:09:25 -07:00
markApps.bind(null, existingInfra), // mark app state before we start addons. this gives the db import logic a chance to mark an app as errored
2021-01-21 11:31:35 -08:00
services.startServices.bind(null, existingInfra),
2018-07-25 13:06:38 -07:00
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);
2020-09-02 18:13:08 -07:00
onPlatformReady(true /* infraChanged */);
callback();
});
2016-06-21 10:37:12 -05:00
}
2020-08-06 22:04:46 -07:00
function stopAllTasks(callback) {
2019-08-28 15:00:55 -07:00
tasks.stopAllTasks(callback);
2016-06-21 10:37:12 -05:00
}
2020-09-02 18:13:08 -07:00
function onPlatformReady(infraChanged) {
debug(`onPlatformReady: platform is ready. infra changed: ${infraChanged}`);
exports._isReady = true;
2019-09-24 20:29:01 -07:00
2020-09-02 18:13:08 -07:00
let tasks = [ apps.schedulePendingTasks ];
2021-01-21 12:18:11 -08:00
if (infraChanged) tasks.push(applyServicesConfig, pruneInfraImages);
2020-09-02 18:13:08 -07:00
async.series(async.reflectAll(tasks), function (error, results) {
results.forEach((result, idx) => {
if (result.error) debug(`Startup task at index ${idx} failed: ${result.error.message}`);
});
});
2018-11-12 21:43:53 -08:00
}
2021-01-21 12:18:11 -08:00
function applyServicesConfig(callback) {
settings.getServicesConfig(function (error, platformConfig) {
if (error) return callback(error);
2018-11-12 21:43:53 -08:00
2021-01-21 11:31:35 -08:00
services.updateServiceConfig(platformConfig, callback);
});
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
const images = infra.baseImages.concat(Object.keys(infra.images).map(function (addon) { return infra.images[addon]; }));
2018-10-27 11:47:24 -07:00
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);
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
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' });
2020-11-21 22:12:56 -08:00
if (result === null) debug(`Error 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
}
2020-11-20 14:13:16 -08:00
function removeAllContainers(callback) {
debug('removeAllContainers: removing all containers for infra upgrade');
2016-07-25 00:39:57 -07:00
async.series([
shell.exec.bind(null, 'removeAllContainers', 'docker ps -qa --filter \'label=isCloudronManaged\' | xargs --no-run-if-empty docker stop'),
shell.exec.bind(null, 'removeAllContainers', 'docker ps -qa --filter \'label=isCloudronManaged\' | xargs --no-run-if-empty docker rm -f')
], callback);
2016-05-24 10:52:55 -07:00
}
2016-05-24 10:58:18 -07:00
2020-07-30 14:09:25 -07:00
function markApps(existingInfra, callback) {
2018-11-10 18:21:15 -08:00
if (existingInfra.version === 'none') { // cloudron is being restored from backup
2020-07-30 14:09:25 -07:00
debug('markApps: restoring installed apps');
apps.restoreInstalledApps(callback);
2018-10-16 11:04:34 -07:00
} else if (existingInfra.version !== infra.version) {
2020-07-30 14:09:25 -07:00
debug('markApps: reconfiguring installed apps');
reverseProxy.removeAppConfigs(); // should we change the cert location, nginx will not start
apps.configureInstalledApps(callback);
2018-10-16 11:04:34 -07:00
} else {
2020-05-22 16:43:16 -07:00
let changedAddons = [];
if (infra.images.mysql.tag !== existingInfra.images.mysql.tag) changedAddons.push('mysql');
if (infra.images.postgresql.tag !== existingInfra.images.postgresql.tag) changedAddons.push('postgresql');
if (infra.images.mongodb.tag !== existingInfra.images.mongodb.tag) changedAddons.push('mongodb');
if (infra.images.redis.tag !== existingInfra.images.redis.tag) changedAddons.push('redis');
if (changedAddons.length) {
// restart apps if docker image changes since the IP changes and any "persistent" connections fail
2020-07-30 14:09:25 -07:00
debug(`markApps: changedAddons: ${JSON.stringify(changedAddons)}`);
2020-05-22 16:43:16 -07:00
apps.restartAppsUsingAddons(changedAddons, callback);
} else {
2020-07-30 14:09:25 -07:00
debug('markApps: apps are already uptodate');
2020-05-22 16:43:16 -07:00
callback();
}
}
}