move startup logic to platform.js

This commit is contained in:
Girish Ramakrishnan
2023-08-12 19:28:07 +05:30
parent 559125cd3c
commit 070f6e5de3
7 changed files with 155 additions and 172 deletions

View File

@@ -1,14 +1,9 @@
'use strict';
exports = module.exports = {
initialize,
uninitialize,
getStatus,
getConfig,
onActivated,
setupDnsAndCert,
prepareDashboardDomain,
@@ -25,7 +20,6 @@ exports = module.exports = {
const apps = require('./apps.js'),
appstore = require('./appstore.js'),
assert = require('assert'),
AuditSource = require('./auditsource.js'),
BoxError = require('./boxerror.js'),
branding = require('./branding.js'),
constants = require('./constants.js'),
@@ -33,106 +27,20 @@ const apps = require('./apps.js'),
debug = require('debug')('box:cloudron'),
dashboard = require('./dashboard.js'),
dns = require('./dns.js'),
dockerProxy = require('./dockerproxy.js'),
eventlog = require('./eventlog.js'),
mailServer = require('./mailserver.js'),
moment = require('moment-timezone'),
network = require('./network.js'),
oidc = require('./oidc.js'),
paths = require('./paths.js'),
platform = require('./platform.js'),
reverseProxy = require('./reverseproxy.js'),
safe = require('safetydance'),
services = require('./services.js'),
settings = require('./settings.js'),
system = require('./system.js'),
tasks = require('./tasks.js'),
timers = require('timers/promises'),
translation = require('./translation.js'),
users = require('./users.js');
async function initialize() {
safe(runStartupTasks(), { debug }); // background
await notifyUpdate();
}
async function uninitialize() {
await cron.stopJobs();
await dockerProxy.stop();
await platform.stopAllTasks();
}
async function onActivated(options) {
assert.strictEqual(typeof options, 'object');
debug('onActivated: running post activation tasks');
// Starting the platform after a user is available means:
// 1. mail bounces can now be sent to the cloudron owner
// 2. the restore code path can run without sudo (since mail/ is non-root)
await platform.start(options);
await cron.startJobs();
await dockerProxy.start(); // this relies on the 'cloudron' docker network interface to be available
await oidc.start(); // this requires dashboardFqdn to be set
// disable responding to api calls via IP to not leak domain info. this is carefully placed as the last item, so it buys
// the UI some time to query the dashboard domain in the restore code path
await timers.setTimeout(30000);
await reverseProxy.writeDefaultConfig({ activated :true });
}
async function notifyUpdate() {
const version = safe.fs.readFileSync(paths.VERSION_FILE, 'utf8');
if (version === constants.VERSION) return;
if (!version) {
await eventlog.add(eventlog.ACTION_INSTALL_FINISH, AuditSource.CRON, { version: constants.VERSION });
} else {
await eventlog.add(eventlog.ACTION_UPDATE_FINISH, AuditSource.CRON, { errorMessage: '', oldVersion: version || 'dev', newVersion: constants.VERSION });
const [error] = await safe(tasks.setCompletedByType(tasks.TASK_UPDATE, { error: null }));
if (error && error.reason !== BoxError.NOT_FOUND) throw error; // when hotfixing, task may not exist
}
safe.fs.writeFileSync(paths.VERSION_FILE, constants.VERSION, 'utf8');
}
// each of these tasks can fail. we will add some routes to fix/re-run them
async function runStartupTasks() {
const tasks = [];
// stop all the systemd tasks
tasks.push(platform.stopAllTasks);
// always generate webadmin config since we have no versioning mechanism for the ejs
tasks.push(async function () {
if (!await dashboard.getDomain()) return;
await reverseProxy.writeDashboardConfig(await dashboard.getDomain());
});
tasks.push(async function () {
// check activation state and start the platform
const activated = await users.isActivated();
// configure nginx to be reachable by IP when not activated. for the moment, the IP based redirect exists even after domain is setup
// just in case user forgot or some network error happenned in the middle (then browser refresh takes you to activation page)
// we remove the config as a simple security measure to not expose IP <-> domain
if (!activated) {
debug('runStartupTasks: not activated. generating IP based redirection config');
return await reverseProxy.writeDefaultConfig({ activated: false });
}
await onActivated({});
});
// we used to run tasks in parallel but simultaneous nginx reloads was causing issues
for (let i = 0; i < tasks.length; i++) {
const [error] = await safe(tasks[i]());
if (error) debug(`Startup task at index ${i} failed: ${error.message} ${error.stack}`);
}
}
async function getStatus() {
return {
version: constants.VERSION,