sysinfo: async'ify

in the process, provision, dyndns, mail, dns also got further asyncified
This commit is contained in:
Girish Ramakrishnan
2021-08-27 09:52:24 -07:00
parent 1856caf972
commit 51d067cbe3
26 changed files with 782 additions and 1082 deletions

View File

@@ -5,41 +5,44 @@ exports = module.exports = {
testConfig
};
var assert = require('assert'),
async = require('async'),
const assert = require('assert'),
BoxError = require('../boxerror.js'),
debug = require('debug')('box:sysinfo/generic'),
promiseRetry = require('../promise-retry.js'),
safe = require('safetydance'),
superagent = require('superagent');
function getServerIp(config, callback) {
async function getServerIp(config) {
assert.strictEqual(typeof config, 'object');
assert.strictEqual(typeof callback, 'function');
if (process.env.BOX_ENV === 'test') return callback(null, '127.0.0.1');
if (process.env.BOX_ENV === 'test') return '127.0.0.1';
async.retry({ times: 10, interval: 5000 }, function (callback) {
superagent.get('https://api.cloudron.io/api/v1/helper/public_ip').timeout(30 * 1000).end(function (error, result) {
if (error || result.statusCode !== 200) {
debug('Error getting IP', error);
return callback(new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. API server unreachable'));
}
if (!result.body && !result.body.ip) {
debug('Unexpected answer. No "ip" found in response body.', result.body);
return callback(new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. No IP found in response'));
}
let attempt = 0;
callback(null, result.body.ip);
});
}, function (error, result) {
if (error) return callback(error);
await promiseRetry({ times: 10, interval: 5000 }, async () => {
if (attempt) debug(`getServerIp: getting server IP. attempt ${attempt}`);
++attempt;
callback(null, result);
const [networkError, response] = await safe(superagent.get('https://api.cloudron.io/api/v1/helper/public_ip')
.timeout(30 * 1000)
.ok(() => true));
if (networkError || response.status !== 200) {
debug('Error getting IP', networkError);
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. API server unreachable');
}
if (!response.body && !response.body.ip) {
debug('Unexpected answer. No "ip" found in response body.', response.body);
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Unable to detect IP. No IP found in response');
}
return response.body.ip;
});
}
function testConfig(config, callback) {
async function testConfig(config) {
assert.strictEqual(typeof config, 'object');
assert.strictEqual(typeof callback, 'function');
callback(null);
return null;
}