Files
cloudron-box/src/platform.js

169 lines
7.3 KiB
JavaScript
Raw Normal View History

2016-05-24 09:40:26 -07:00
'use strict';
exports = module.exports = {
start,
stopAllTasks,
2022-11-30 19:54:32 +01:00
getStatus
2016-05-24 09:40:26 -07:00
};
const apps = require('./apps.js'),
2016-05-24 10:58:18 -07:00
assert = require('assert'),
2021-11-17 10:33:28 -08:00
AuditSource = require('./auditsource.js'),
BoxError = require('./boxerror.js'),
constants = require('./constants.js'),
2016-05-24 09:40:26 -07:00
debug = require('debug')('box:platform'),
2022-04-15 07:52:35 -05:00
delay = require('./delay.js'),
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'),
services = require('./services.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'),
volumes = require('./volumes.js'),
_ = require('underscore');
2016-05-24 09:40:26 -07:00
2022-11-30 19:54:32 +01:00
let gStatusMessage = 'Initializing';
function getStatus() {
return { message: gStatusMessage };
}
2021-09-07 09:57:49 -07:00
async function start(options) {
if (process.env.BOX_ENV === 'test' && !process.env.TEST_CREATE_INFRA) return;
2016-05-24 09:40:26 -07:00
2022-11-30 19:54:32 +01:00
debug('initializing platform');
2021-09-07 09:57:49 -07:00
let 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);
await onPlatformReady(false /* !infraChanged */);
2021-09-07 09:57:49 -07:00
return;
}
2016-05-24 13:10:18 -07:00
debug('Updating infrastructure from %s to %s', existingInfra.version, infra.version);
2021-09-07 09:57:49 -07:00
const error = locker.lock(locker.OP_PLATFORM_START);
if (error) throw error;
for (let attempt = 0; attempt < 5; attempt++) {
try {
if (existingInfra.version !== infra.version) {
2022-11-30 19:54:32 +01:00
gStatusMessage = 'Removing containers for upgrade';
await removeAllContainers();
await createDockerNetwork();
}
if (existingInfra.version === 'none') await volumes.mountAll(); // when restoring, mount all volumes
await markApps(existingInfra, options); // mark app state before we start addons. this gives the db import logic a chance to mark an app as errored
2022-11-30 19:54:32 +01:00
gStatusMessage = 'Starting services, this can take a while';
await services.startServices(existingInfra);
await fs.promises.writeFile(paths.INFRA_VERSION_FILE, JSON.stringify(infra, null, 4));
break;
} catch (error) {
// for some reason, mysql arbitrary restarts making startup tasks fail. this makes the box update stuck
// LOST is when existing connection breaks. REFUSED is when new connection cannot connect at all
const retry = error.reason === BoxError.DATABASE_ERROR && (error.code === 'PROTOCOL_CONNECTION_LOST' || error.code === 'ECONNREFUSED');
debug(`Failed to start services. retry=${retry} (attempt ${attempt}): ${error.message}`);
if (!retry) throw error; // refuse to start
await delay(10000);
}
}
2021-09-07 09:57:49 -07:00
locker.unlock(locker.OP_PLATFORM_START);
await onPlatformReady(true /* infraChanged */);
2016-06-21 10:37:12 -05:00
}
2021-07-12 23:35:30 -07:00
async function stopAllTasks() {
await tasks.stopAllTasks();
2016-06-21 10:37:12 -05:00
}
2021-09-07 09:57:49 -07:00
async function onPlatformReady(infraChanged) {
debug(`onPlatformReady: platform is ready. infra changed: ${infraChanged}`);
2022-11-30 19:54:32 +01:00
gStatusMessage = 'Ready';
2019-09-24 20:29:01 -07:00
if (infraChanged) await safe(pruneInfraImages(), { debug }); // ignore error
2021-11-17 10:33:28 -08:00
await apps.schedulePendingTasks(AuditSource.PLATFORM);
}
2021-09-07 09:57:49 -07:00
async function pruneInfraImages() {
2018-10-27 13:04:13 -07:00
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]; }));
for (const image of images) {
2022-11-30 20:59:14 +01:00
const output = safe.child_process.execSync(`docker images --digests ${image.repo} --format "{{.ID}} {{.Repository}}:{{.Tag}}@{{.Digest}}"`, { encoding: 'utf8' });
if (output === null) {
debug(`Failed to list images of ${image}`, safe.error);
throw safe.error;
}
2022-11-30 20:59:14 +01:00
const lines = output.trim().split('\n');
for (const line of lines) {
if (!line) continue;
2022-11-30 20:59:14 +01:00
const parts = line.split(' '); // [ ID, Repo:Tag@Digest ]
const normalizedTag = parts[1].replace('registry.ipv6.docker.com/', '').replace('registry-1.docker.io/', '');
if (image.tag === normalizedTag) continue; // keep
debug(`pruneInfraImages: removing unused image of ${image.repo}: tag: ${parts[1]} id: ${parts[0]}`);
2022-11-30 20:59:14 +01:00
let result = safe.child_process.execSync(`docker rmi ${parts[1].replace(':<none>', '')}`, { encoding: 'utf8' }); // the none tag has to be removed
if (result === null) debug(`Error removing image ${parts[0]}: ${safe.error.mesage}`);
}
}
2016-05-24 13:16:31 -07:00
}
async function createDockerNetwork() {
debug('createDockerNetwork: recreating docker network');
await shell.promises.exec('createDockerNetwork', 'docker network rm cloudron || true');
// the --ipv6 option will work even in ipv6 is disabled. fd00 is IPv6 ULA
await shell.promises.exec('createDockerNetwork', `docker network create --subnet=${constants.DOCKER_IPv4_SUBNET} --ip-range=${constants.DOCKER_IPv4_RANGE} --gateway ${constants.DOCKER_IPv4_GATEWAY} --ipv6 --subnet=fd00:c107:d509::/64 cloudron`);
}
2021-09-07 09:57:49 -07:00
async function removeAllContainers() {
debug('removeAllContainers: removing all containers for infra upgrade');
2016-07-25 00:39:57 -07:00
2021-09-07 09:57:49 -07:00
await shell.promises.exec('removeAllContainers', 'docker ps -qa --filter \'label=isCloudronManaged\' | xargs --no-run-if-empty docker stop');
await shell.promises.exec('removeAllContainers', 'docker ps -qa --filter \'label=isCloudronManaged\' | xargs --no-run-if-empty docker rm -f');
}
2016-05-24 10:58:18 -07:00
2021-08-20 09:19:44 -07:00
async function markApps(existingInfra, options) {
assert.strictEqual(typeof existingInfra, 'object');
assert.strictEqual(typeof options, 'object');
if (existingInfra.version === 'none') { // cloudron is being restored from backup
2020-07-30 14:09:25 -07:00
debug('markApps: restoring installed apps');
2021-11-17 10:33:28 -08:00
await apps.restoreInstalledApps(options, AuditSource.PLATFORM);
} 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
2021-11-17 10:33:28 -08:00
await apps.configureInstalledApps(AuditSource.PLATFORM);
} else {
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)}`);
2021-11-17 10:33:28 -08:00
await apps.restartAppsUsingAddons(changedAddons, AuditSource.PLATFORM);
} else {
2020-07-30 14:09:25 -07:00
debug('markApps: apps are already uptodate');
}
}
}