make sysinfo provider a setting

This commit is contained in:
Girish Ramakrishnan
2019-10-29 15:46:33 -07:00
parent 7a25187bee
commit 7d987d7c79
19 changed files with 132 additions and 56 deletions

View File

@@ -1,35 +1,25 @@
'use strict';
exports = module.exports = {
getPublicIp: getPublicIp,
getServerIp: getServerIp,
hasIPv6: hasIPv6,
provider: provider
hasIPv6: hasIPv6
};
var assert = require('assert'),
ec2 = require('./sysinfo/ec2.js'),
fs = require('fs'),
generic = require('./sysinfo/generic.js'),
paths = require('./paths.js'),
scaleway = require('./sysinfo/scaleway.js'),
safe = require('safetydance');
settings = require('./settings.js');
let gProvider = null;
function provider() {
if (gProvider) return gProvider;
gProvider = safe.fs.readFileSync(paths.PROVIDER_FILE, 'utf8');
if (!gProvider) return gProvider = 'generic';
return gProvider.trim();
}
function getApi(callback) {
function getApi(config, callback) {
assert.strictEqual(typeof config, 'object');
assert.strictEqual(typeof callback, 'function');
switch (provider()) {
const provider = config.provider || 'generic';
switch (provider) {
case 'ec2': return callback(null, ec2);
case 'lightsail': return callback(null, ec2);
case 'ami': return callback(null, ec2);
@@ -38,13 +28,17 @@ function getApi(callback) {
}
}
function getPublicIp(callback) {
function getServerIp(callback) {
assert.strictEqual(typeof callback, 'function');
getApi(function (error, api) {
settings.getSysinfoConfig(function (error, config) {
if (error) return callback(error);
api.getPublicIp(callback);
getApi(config, function (error, api) {
if (error) return callback(error);
api.getServerIp(config, callback);
});
});
}