Files
cloudron-box/src/sysinfo/generic.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2019-10-29 20:08:45 -07:00
getServerIp,
testConfig
};
const assert = require('assert'),
2019-10-22 14:09:44 -07:00
BoxError = require('../boxerror.js'),
debug = require('debug')('box:sysinfo/generic'),
promiseRetry = require('../promise-retry.js'),
safe = require('safetydance'),
2019-10-22 14:09:44 -07:00
superagent = require('superagent');
async function getServerIp(config) {
2019-10-29 15:46:33 -07:00
assert.strictEqual(typeof config, 'object');
if (process.env.BOX_ENV === 'test') return '127.0.0.1';
let attempt = 0;
await promiseRetry({ times: 10, interval: 5000 }, async () => {
if (attempt) debug(`getServerIp: getting server IP. attempt ${attempt}`);
++attempt;
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;
});
}
2019-10-29 20:08:45 -07:00
async function testConfig(config) {
2019-10-29 20:08:45 -07:00
assert.strictEqual(typeof config, 'object');
return null;
2019-10-29 20:08:45 -07:00
}