get rid of all the NOOP_CALLBACKs

This commit is contained in:
Girish Ramakrishnan
2021-09-17 09:22:46 -07:00
parent b3fe2a4b84
commit c5794b5ecd
8 changed files with 88 additions and 122 deletions

View File

@@ -25,7 +25,6 @@ exports = module.exports = {
const apps = require('./apps.js'),
appstore = require('./appstore.js'),
assert = require('assert'),
async = require('async'),
auditSource = require('./auditsource.js'),
backups = require('./backups.js'),
BoxError = require('./boxerror.js'),
@@ -52,15 +51,12 @@ const apps = require('./apps.js'),
split = require('split'),
sysinfo = require('./sysinfo.js'),
tasks = require('./tasks.js'),
users = require('./users.js'),
util = require('util');
users = require('./users.js');
const REBOOT_CMD = path.join(__dirname, 'scripts/reboot.sh');
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
async function initialize() {
runStartupTasks();
safe(runStartupTasks(), { debug });
await notifyUpdate();
}
@@ -70,25 +66,21 @@ async function uninitialize() {
await platform.stopAllTasks();
}
function onActivated(options, callback) {
async function onActivated(options) {
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
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)
async.series([
platform.start.bind(null, options),
cron.startJobs,
// 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
async () => {
await delay(30000);
await reverseProxy.writeDefaultConfig({ activated :true });
}
], callback);
await platform.start(options);
await cron.startJobs();
// 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 delay(30000);
await reverseProxy.writeDefaultConfig({ activated :true });
}
async function notifyUpdate() {
@@ -104,46 +96,45 @@ async function notifyUpdate() {
}
// each of these tasks can fail. we will add some routes to fix/re-run them
function runStartupTasks() {
const tasks = [
// stop all the systemd tasks
platform.stopAllTasks,
async function runStartupTasks() {
const tasks = [];
// this configures collectd to collect backup storage metrics if filesystem is used. This is also triggerd when the settings change with the rest api
async function () {
const backupConfig = await settings.getBackupConfig();
await backups.configureCollectd(backupConfig);
},
// stop all the systemd tasks
tasks.push(platform.stopAllTasks);
// always generate webadmin config since we have no versioning mechanism for the ejs
async function () {
if (!settings.dashboardDomain()) return;
// this configures collectd to collect backup storage metrics if filesystem is used. This is also triggerd when the settings change with the rest api
tasks.push(async function () {
const backupConfig = await settings.getBackupConfig();
await backups.configureCollectd(backupConfig);
});
await reverseProxy.writeDashboardConfig(settings.dashboardDomain());
},
// always generate webadmin config since we have no versioning mechanism for the ejs
tasks.push(async function () {
if (!settings.dashboardDomain()) return;
await reverseProxy.writeDashboardConfig(settings.dashboardDomain());
});
tasks.push(async function () {
// check activation state and start the platform
async function () {
const activated = await users.isActivated();
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 util.promisify(onActivated)({});
// 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
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}`);
});
});
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}`);
}
}
async function getConfig() {
@@ -183,17 +174,16 @@ async function isRebootRequired() {
return fs.existsSync('/var/run/reboot-required');
}
// called from cron.js
function runSystemChecks(callback) {
assert.strictEqual(typeof callback, 'function');
async function runSystemChecks() {
debug('runSystemChecks: checking status');
async.parallel([
checkMailStatus,
checkRebootRequired,
checkUbuntuVersion
], callback);
const checks = [
checkMailStatus(),
checkRebootRequired(),
checkUbuntuVersion()
];
await Promise.allSettled(checks);
}
async function checkMailStatus() {
@@ -276,7 +266,7 @@ async function prepareDashboardDomain(domain, auditSource) {
const taskId = await tasks.add(tasks.TASK_SETUP_DNS_AND_CERT, [ constants.DASHBOARD_LOCATION, domain, auditSource ]);
tasks.startTask(taskId, {}, NOOP_CALLBACK);
tasks.startTask(taskId, {});
return taskId;
}
@@ -312,7 +302,7 @@ async function updateDashboardDomain(domain, auditSource) {
await setDashboardDomain(domain, auditSource);
services.rebuildService('turn', NOOP_CALLBACK); // to update the realm variable
safe(services.rebuildService('turn'), { debug }); // to update the realm variable
}
async function renewCerts(options, auditSource) {
@@ -320,7 +310,7 @@ async function renewCerts(options, auditSource) {
assert.strictEqual(typeof auditSource, 'object');
const taskId = await tasks.add(tasks.TASK_CHECK_CERTS, [ options, auditSource ]);
tasks.startTask(taskId, {}, NOOP_CALLBACK);
tasks.startTask(taskId, {});
return taskId;
}
@@ -347,6 +337,6 @@ async function syncDnsRecords(options) {
assert.strictEqual(typeof options, 'object');
const taskId = await tasks.add(tasks.TASK_SYNC_DNS_RECORDS, [ options ]);
tasks.startTask(taskId, {}, NOOP_CALLBACK);
tasks.startTask(taskId, {});
return taskId;
}