docker.js and services.js: async'ify

This commit is contained in:
Girish Ramakrishnan
2021-08-25 19:41:46 -07:00
parent 1cc11fece8
commit 42774eac8c
24 changed files with 1618 additions and 2130 deletions

View File

@@ -87,13 +87,13 @@ const assert = require('assert'),
nodemailer = require('nodemailer'),
path = require('path'),
paths = require('./paths.js'),
request = require('request'),
reverseProxy = require('./reverseproxy.js'),
safe = require('safetydance'),
services = require('./services.js'),
settings = require('./settings.js'),
shell = require('./shell.js'),
smtpTransport = require('nodemailer-smtp-transport'),
superagent = require('superagent'),
sysinfo = require('./sysinfo.js'),
system = require('./system.js'),
tasks = require('./tasks.js'),
@@ -716,9 +716,7 @@ async function configureMail(mailFqdn, mailDomain, serviceConfig) {
}
async function getMailAuth() {
const dockerInspect = util.promisify(docker.inspect);
const data = await dockerInspect('mail');
const data = await docker.inspect('mail');
const ip = safe.query(data, 'NetworkSettings.Networks.cloudron.IPAddress');
if (!ip) throw new BoxError(BoxError.MAIL_ERROR, 'Error querying mail server IP');
@@ -737,18 +735,14 @@ async function getMailAuth() {
};
}
function restartMail(callback) {
assert.strictEqual(typeof callback, 'function');
async function restartMail() {
if (process.env.BOX_ENV === 'test' && !process.env.TEST_CREATE_INFRA) return;
if (process.env.BOX_ENV === 'test' && !process.env.TEST_CREATE_INFRA) return callback();
const servicesConfig = await settings.getServicesConfig();
const mailConfig = servicesConfig['mail'] || {};
services.getServiceConfig('mail', async function (error, serviceConfig) {
if (error) return callback(error);
debug(`restartMail: restarting mail container with mailFqdn:${settings.mailFqdn()} dashboardDomain:${settings.dashboardDomain()}`);
[error] = await safe(configureMail(settings.mailFqdn(), settings.dashboardDomain(), serviceConfig));
callback(error);
});
debug(`restartMail: restarting mail container with mailFqdn:${settings.mailFqdn()} dashboardDomain:${settings.dashboardDomain()}`);
await configureMail(settings.mailFqdn(), settings.dashboardDomain(), mailConfig);
}
async function restartMailIfActivated() {
@@ -759,7 +753,7 @@ async function restartMailIfActivated() {
return; // not provisioned yet, do not restart container after dns setup
}
await util.promisify(restartMail)();
await restartMail();
}
async function handleCertChanged() {
@@ -1034,11 +1028,10 @@ function onDomainAdded(domain, callback) {
], callback);
}
function onDomainRemoved(domain, callback) {
async function onDomainRemoved(domain) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof callback, 'function');
restartMail(callback);
await restartMail();
}
async function clearDomains() {
@@ -1061,7 +1054,7 @@ async function setMailFromValidation(domain, enabled) {
await updateDomain(domain, { mailFromValidation: enabled });
restartMail(NOOP_CALLBACK); // have to restart mail container since haraka cannot watch symlinked config files (mail.ini)
safe(restartMail()); // have to restart mail container since haraka cannot watch symlinked config files (mail.ini)
}
async function setBanner(domain, banner) {
@@ -1070,7 +1063,7 @@ async function setBanner(domain, banner) {
await updateDomain(domain, { banner });
restartMail(NOOP_CALLBACK);
safe(restartMail());
}
async function setCatchAllAddress(domain, addresses) {
@@ -1079,7 +1072,7 @@ async function setCatchAllAddress(domain, addresses) {
await updateDomain(domain, { catchAll: addresses });
restartMail(NOOP_CALLBACK); // have to restart mail container since haraka cannot watch symlinked config files (mail.ini)
safe(restartMail()); // have to restart mail container since haraka cannot watch symlinked config files (mail.ini)
}
async function setMailRelay(domain, relay, options) {
@@ -1100,7 +1093,7 @@ async function setMailRelay(domain, relay, options) {
await updateDomain(domain, { relay: relay });
restartMail(NOOP_CALLBACK);
safe(restartMail());
}
async function setMailEnabled(domain, enabled, auditSource) {
@@ -1110,7 +1103,7 @@ async function setMailEnabled(domain, enabled, auditSource) {
await updateDomain(domain, { enabled: enabled });
restartMail(NOOP_CALLBACK);
safe(restartMail());
await eventlog.add(enabled ? eventlog.ACTION_MAIL_ENABLED : eventlog.ACTION_MAIL_DISABLED, auditSource, { domain });
}
@@ -1252,21 +1245,20 @@ async function updateMailbox(name, domain, data, auditSource) {
eventlog.add(eventlog.ACTION_MAIL_MAILBOX_UPDATE, auditSource, { name, domain, oldUserId: mailbox.userId, ownerId, ownerType, active });
}
function removeSolrIndex(mailbox, callback) {
async function removeSolrIndex(mailbox) {
assert.strictEqual(typeof mailbox, 'string');
assert.strictEqual(typeof callback, 'function');
services.getContainerDetails('mail', 'CLOUDRON_MAIL_TOKEN', function (error, addonDetails) {
if (error) return callback(error);
const addonDetails = await services.getContainerDetails('mail', 'CLOUDRON_MAIL_TOKEN');
request.post(`https://${addonDetails.ip}:3000/solr_delete_index?access_token=${addonDetails.token}`, { timeout: 2000, rejectUnauthorized: false, json: { mailbox } }, function (error, response) {
if (error) return callback(error);
const [error, response] = await safe(superagent.post(`https://${addonDetails.ip}:3000/solr_delete_index?access_token=${addonDetails.token}`)
.timeout(2000)
.disableTLSCerts()
.send({ mailbox })
.ok(() => true));
if (response.statusCode !== 200) return callback(new Error(`Error removing solr index - ${response.statusCode} ${JSON.stringify(response.body)}`));
if (error) throw new BoxError(BoxError.MAIL_ERROR, `Could not remove solr index: ${error.message}`);
callback(null);
});
});
if (response.status !== 200) throw new BoxError(BoxError.MAIL_ERROR, `Error removing solr index - ${response.status} ${JSON.stringify(response.body)}`);
}
async function delMailbox(name, domain, options, auditSource) {
@@ -1285,7 +1277,9 @@ async function delMailbox(name, domain, options, auditSource) {
const result = await database.query('DELETE FROM mailboxes WHERE ((name=? AND domain=?) OR (aliasName = ? AND aliasDomain=?))', [ name, domain, name, domain ]);
if (result.affectedRows === 0) throw new BoxError(BoxError.NOT_FOUND, 'Mailbox not found');
removeSolrIndex(mailbox, NOOP_CALLBACK);
const [error] = await safe(removeSolrIndex(mailbox));
if (error) debug(`delMailbox: failed to remove solr index: ${error.message}`);
eventlog.add(eventlog.ACTION_MAIL_MAILBOX_REMOVE, auditSource, { name, domain });
}