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
|
|
|
|
2022-11-30 19:54:32 +01:00
|
|
|
getStatus
|
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'),
|
2021-11-17 10:33:28 -08:00
|
|
|
AuditSource = require('./auditsource.js'),
|
2021-11-02 22:30:38 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2023-02-21 12:03:58 +01:00
|
|
|
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'),
|
2016-05-24 10:52:55 -07:00
|
|
|
fs = require('fs'),
|
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'),
|
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'),
|
2021-01-21 11:31:35 -08:00
|
|
|
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'),
|
2021-09-28 11:51:01 -07:00
|
|
|
volumes = require('./volumes.js'),
|
2016-07-24 23:19:11 -07:00
|
|
|
_ = 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');
|
2016-05-24 10:52:55 -07:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
let 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);
|
2021-11-02 22:30:38 -07:00
|
|
|
await onPlatformReady(false /* !infraChanged */);
|
2021-09-07 09:57:49 -07:00
|
|
|
return;
|
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
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
const error = locker.lock(locker.OP_PLATFORM_START);
|
|
|
|
|
if (error) throw error;
|
2017-11-28 23:18:43 -08:00
|
|
|
|
2021-11-02 22:30:38 -07:00
|
|
|
for (let attempt = 0; attempt < 5; attempt++) {
|
|
|
|
|
try {
|
2022-02-10 09:32:15 -08:00
|
|
|
if (existingInfra.version !== infra.version) {
|
2022-11-30 19:54:32 +01:00
|
|
|
gStatusMessage = 'Removing containers for upgrade';
|
2022-02-10 09:32:15 -08:00
|
|
|
await removeAllContainers();
|
|
|
|
|
await createDockerNetwork();
|
|
|
|
|
}
|
2021-11-02 22:30:38 -07:00
|
|
|
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';
|
2021-11-02 22:30:38 -07:00
|
|
|
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}`);
|
2022-02-09 19:23:18 -08:00
|
|
|
if (!retry) throw error; // refuse to start
|
2021-11-02 22:30:38 -07:00
|
|
|
await delay(10000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-28 23:18:43 -08:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
locker.unlock(locker.OP_PLATFORM_START);
|
2016-07-24 22:59:47 -07:00
|
|
|
|
2021-11-02 22:30:38 -07:00
|
|
|
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) {
|
2020-09-02 18:13:08 -07:00
|
|
|
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
|
|
|
|
2021-11-02 22:30:38 -07:00
|
|
|
if (infraChanged) await safe(pruneInfraImages(), { debug }); // ignore error
|
2018-11-17 19:34:34 -08:00
|
|
|
|
2021-11-17 10:33:28 -08:00
|
|
|
await apps.schedulePendingTasks(AuditSource.PLATFORM);
|
2018-11-12 21:43:53 -08:00
|
|
|
}
|
|
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
async function pruneInfraImages() {
|
2018-10-27 13:04:13 -07:00
|
|
|
debug('pruneInfraImages: checking existing images');
|
2022-11-30 19:57:25 +01: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]; }));
|
|
|
|
|
|
|
|
|
|
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' });
|
2022-11-30 19:57:25 +01:00
|
|
|
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) {
|
2022-11-30 19:57:25 +01:00
|
|
|
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
|
2022-11-30 19:57:25 +01:00
|
|
|
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
|
2022-11-30 19:57:25 +01:00
|
|
|
if (result === null) debug(`Error removing image ${parts[0]}: ${safe.error.mesage}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-24 13:16:31 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-09 17:28:46 -08:00
|
|
|
async function createDockerNetwork() {
|
|
|
|
|
debug('createDockerNetwork: recreating docker network');
|
|
|
|
|
|
2022-02-09 17:47:48 -08:00
|
|
|
await shell.promises.exec('createDockerNetwork', 'docker network rm cloudron || true');
|
|
|
|
|
// the --ipv6 option will work even in ipv6 is disabled. fd00 is IPv6 ULA
|
2023-02-21 12:03:58 +01:00
|
|
|
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`);
|
2022-02-09 17:28:46 -08:00
|
|
|
}
|
|
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
async function removeAllContainers() {
|
2020-07-30 14:36:11 -07:00
|
|
|
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:52:55 -07:00
|
|
|
}
|
2016-05-24 10:58:18 -07:00
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
async function markApps(existingInfra, options) {
|
2021-02-24 14:56:09 -08:00
|
|
|
assert.strictEqual(typeof existingInfra, 'object');
|
|
|
|
|
assert.strictEqual(typeof options, 'object');
|
|
|
|
|
|
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');
|
2021-11-17 10:33:28 -08:00
|
|
|
await apps.restoreInstalledApps(options, AuditSource.PLATFORM);
|
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');
|
2018-01-30 12:23:27 -08:00
|
|
|
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);
|
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)}`);
|
2021-11-17 10:33:28 -08:00
|
|
|
await apps.restartAppsUsingAddons(changedAddons, AuditSource.PLATFORM);
|
2020-05-22 16:43:16 -07:00
|
|
|
} else {
|
2020-07-30 14:09:25 -07:00
|
|
|
debug('markApps: apps are already uptodate');
|
2020-05-22 16:43:16 -07:00
|
|
|
}
|
2016-07-25 18:57:54 -07:00
|
|
|
}
|
|
|
|
|
}
|