Deprecate dns settings api and add dns data migration

This commit is contained in:
Johannes Zellner
2017-10-29 00:49:21 +02:00
parent 3fbaa385c4
commit 0e74a6df35
2 changed files with 48 additions and 22 deletions
+26
View File
@@ -12,7 +12,10 @@ var apps = require('./apps.js'),
async = require('async'),
config = require('./config.js'),
certificates = require('./certificates.js'),
DatabaseError = require('./databaseerror.js'),
debug = require('debug')('box:platform'),
domains = require('./domains.js'),
DomainError = domains.DomainError,
fs = require('fs'),
hat = require('hat'),
infra = require('./infra_version.js'),
@@ -22,6 +25,7 @@ var apps = require('./apps.js'),
safe = require('safetydance'),
semver = require('semver'),
settings = require('./settings.js'),
settingsdb = require('./settingsdb.js'),
shell = require('./shell.js'),
subdomains = require('./subdomains.js'),
taskmanager = require('./taskmanager.js'),
@@ -64,6 +68,7 @@ function start(callback) {
debug('Updating infrastructure from %s to %s', existingInfra.version, infra.version);
async.series([
migrateDNSSettings,
stopContainers.bind(null, existingInfra),
startAddons.bind(null, existingInfra),
removeOldImages,
@@ -377,3 +382,24 @@ function startApps(existingInfra, callback) {
apps.configureInstalledApps(callback);
}
}
// This is only used when updating from single to multi domain support
// REMOVE later!
function migrateDNSSettings(callback) {
assert.strictEqual(typeof callback, 'function');
settingsdb.get('dns_config', function (error, result) {
if (error && error.reason !== DatabaseError.NOT_FOUND) return callback(error);
const dnsConfig = result ? safe.JSON.parse(result) : { provider: 'manual' };
domains.get(config.fqdn(), function (error, result) {
if (error && error.reason !== DomainError.NOT_FOUND) return callback(error);
// if domain is already in the table, nothing to do
if (result) return callback(null);
domains.add(config.fqdn(), config.zoneName(), dnsConfig, callback);
});
});
}
+22 -22
View File
@@ -21,6 +21,7 @@ exports = module.exports = {
getDeveloperMode: getDeveloperMode,
setDeveloperMode: setDeveloperMode,
// DEPRECATED in favor of domains table via domains.js
getDnsConfig: getDnsConfig,
setDnsConfig: setDnsConfig,
@@ -85,6 +86,8 @@ var assert = require('assert'),
CronJob = require('cron').CronJob,
DatabaseError = require('./databaseerror.js'),
debug = require('debug')('box:settings'),
domains = require('./domains.js'),
DomainError = domains.DomainError,
cloudron = require('./cloudron.js'),
moment = require('moment-timezone'),
paths = require('./paths.js'),
@@ -93,10 +96,8 @@ var assert = require('assert'),
EmailError = email.EmailError,
safe = require('safetydance'),
settingsdb = require('./settingsdb.js'),
subdomains = require('./subdomains.js'),
SubdomainError = subdomains.SubdomainError,
SubdomainError = require('./subdomains.js').SubdomainError,
superagent = require('superagent'),
sysinfo = require('./sysinfo.js'),
util = require('util'),
_ = require('underscore');
@@ -301,11 +302,11 @@ function setDeveloperMode(enabled, callback) {
function getDnsConfig(callback) {
assert.strictEqual(typeof callback, 'function');
settingsdb.get(exports.DNS_CONFIG_KEY, function (error, value) {
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(null, gDefaults[exports.DNS_CONFIG_KEY]);
domains.get(config.fqdn(), function (error, result) {
if (error && error.reason === DomainError.NOT_FOUND) return callback(null, gDefaults[exports.DNS_CONFIG_KEY]);
if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error));
callback(null, JSON.parse(value));
callback(null, result);
});
}
@@ -315,27 +316,26 @@ function setDnsConfig(dnsConfig, domain, zoneName, callback) {
assert.strictEqual(typeof zoneName, 'string');
assert.strictEqual(typeof callback, 'function');
sysinfo.getPublicIp(function (error, ip) {
if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, 'Error getting IP:' + error.message));
function done(error) {
if (error && error.reason === SubdomainError.ACCESS_DENIED) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Error adding A record. Access denied'));
if (error && error.reason === SubdomainError.NOT_FOUND) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Zone not found'));
if (error && error.reason === SubdomainError.EXTERNAL_ERROR) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Error adding A record:' + error.message));
if (error && error.reason === SubdomainError.BAD_FIELD) return callback(new SettingsError(SettingsError.BAD_FIELD, error.message));
if (error && error.reason === SubdomainError.INVALID_PROVIDER) return callback(new SettingsError(SettingsError.BAD_FIELD, error.message));
if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error));
subdomains.verifyDnsConfig(dnsConfig, domain, zoneName, ip, function (error, result) {
if (error && error.reason === SubdomainError.ACCESS_DENIED) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Error adding A record. Access denied'));
if (error && error.reason === SubdomainError.NOT_FOUND) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Zone not found'));
if (error && error.reason === SubdomainError.EXTERNAL_ERROR) return callback(new SettingsError(SettingsError.BAD_FIELD, 'Error adding A record:' + error.message));
if (error && error.reason === SubdomainError.BAD_FIELD) return callback(new SettingsError(SettingsError.BAD_FIELD, error.message));
if (error && error.reason === SubdomainError.INVALID_PROVIDER) return callback(new SettingsError(SettingsError.BAD_FIELD, error.message));
if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error));
exports.events.emit(exports.DNS_CONFIG_KEY, dnsConfig);
settingsdb.set(exports.DNS_CONFIG_KEY, JSON.stringify(result), function (error) {
if (error) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error));
cloudron.configureWebadmin(NOOP_CALLBACK); // do not block
exports.events.emit(exports.DNS_CONFIG_KEY, dnsConfig);
callback(null, dnsConfig);
}
cloudron.configureWebadmin(NOOP_CALLBACK); // do not block
domains.get(domain, function (error, result) {
if (error && error.reason !== DomainError.NOT_FOUND) return callback(new SettingsError(SettingsError.INTERNAL_ERROR, error));
callback(null);
});
});
if (!result) domains.add(domain, zoneName, dnsConfig, done);
else domains.update(domain, dnsConfig, done);
});
}