dyndns: run as a task

this lets us display logs
This commit is contained in:
Girish Ramakrishnan
2023-07-08 19:48:12 +05:30
parent 56b7cc4041
commit 23f0eba1bd
11 changed files with 108 additions and 31 deletions
+3 -2
View File
@@ -245,8 +245,9 @@ function dynamicDnsChanged(enabled) {
if (enabled) {
gJobs.dynamicDns = new CronJob({
cronTime: '5 * * * * *', // we only update the records if the ip has changed.
onTick: async () => await safe(dyndns.sync(AuditSource.CRON), { debug }),
// often home IPs change at the full hour, so we give it 5min to settle
cronTime: '00 5 * * * *',
onTick: async () => { await safe(dyndns.refreshDns(AuditSource.CRON), { debug }); },
start: true
});
} else {
+40 -19
View File
@@ -1,6 +1,7 @@
'use strict';
exports = module.exports = {
refreshDns,
sync
};
@@ -13,50 +14,70 @@ const apps = require('./apps.js'),
paths = require('./paths.js'),
safe = require('safetydance'),
settings = require('./settings.js'),
sysinfo = require('./sysinfo.js');
sysinfo = require('./sysinfo.js'),
tasks = require('./tasks.js');
// called for dynamic dns setups where we have to update the IP
// FIXME: this races with apptask. can result in a conflict if apptask is doing some dns operation and this code changes entries
async function sync(auditSource) {
async function refreshDns(auditSource) {
assert.strictEqual(typeof auditSource, 'object');
const ipv4 = await sysinfo.getServerIPv4();
const ipv6 = await sysinfo.getServerIPv6();
const info = safe.JSON.parse(safe.fs.readFileSync(paths.DYNDNS_INFO_FILE, 'utf8')) || { ipv4: null, ipv6: null };
if (info.ip) { // legacy cache file
info.ipv4 = info.ip;
delete info.ip;
}
const ipv4Changed = info.ipv4 !== ipv4;
const ipv6Changed = ipv6 && info.ipv6 !== ipv6; // both should be RFC 5952 format
if (!ipv4Changed && !ipv6Changed) {
debug(`refreshDNS: no change in IP ipv4: ${ipv4} ipv6: ${ipv6}`);
debug(`refreshDns: no change in IP. ipv4: ${ipv4} ipv6: ${ipv6}`);
return;
}
debug(`refreshDNS: updating IP from ${info.ipv4} to ipv4: ${ipv4} (changed: ${ipv4Changed}) ipv6: ${ipv6} (changed: ${ipv6Changed})`);
if (ipv4Changed) await dns.upsertDnsRecords(constants.DASHBOARD_SUBDOMAIN, settings.dashboardDomain(), 'A', [ ipv4 ]);
if (ipv6Changed) await dns.upsertDnsRecords(constants.DASHBOARD_SUBDOMAIN, settings.dashboardDomain(), 'AAAA', [ ipv6 ]);
debug(`refreshDns: updating IP from ${info.ipv4} to ipv4: ${ipv4} (changed: ${ipv4Changed}) ipv6: ${ipv6} (changed: ${ipv6Changed})`);
// update dns in a separate task so we can have trackable logs
const taskId = await tasks.add(tasks.TASK_SYNC_DYNDNS, [ ipv4Changed ? ipv4 : null, ipv6Changed ? ipv6 : null, auditSource ]);
tasks.startTask(taskId, {}, async function (error) {
if (error) {
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { taskId, fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6, errorMessage: error.message });
return;
}
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { taskId, fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6 });
info.ipv4 = ipv4;
info.ipv6 = ipv6;
safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
});
return taskId;
}
async function sync(ipv4, ipv6, auditSource, progressCallback) {
assert(ipv4 === null || typeof ipv4 === 'string');
assert(ipv6 === null || typeof ipv6 === 'string');
assert.strictEqual(typeof auditSource, 'object');
assert.strictEqual(typeof progressCallback, 'function');
let percent = 10;
progressCallback({ percent, message: `Updating dashboard domain ${settings.dashboardFqdn()}`});
if (ipv4) await dns.upsertDnsRecords(constants.DASHBOARD_SUBDOMAIN, settings.dashboardDomain(), 'A', [ ipv4 ]);
if (ipv6) await dns.upsertDnsRecords(constants.DASHBOARD_SUBDOMAIN, settings.dashboardDomain(), 'AAAA', [ ipv6 ]);
const result = await apps.list();
for (const app of result) {
percent += Math.round(90/result.length);
progressCallback({ percent, message: `Updating app ${app.fqdn}`});
const locations = [{ domain: app.domain, subdomain: app.subdomain }]
.concat(app.secondaryDomains)
.concat(app.redirectDomains)
.concat(app.aliasDomains);
for (const location of locations) {
if (ipv4Changed) await dns.upsertDnsRecords(location.subdomain, location.domain, 'A', [ ipv4 ]);
if (ipv6Changed) await dns.upsertDnsRecords(location.subdomain, location.domain, 'AAAA', [ ipv6 ]);
if (ipv4) await dns.upsertDnsRecords(location.subdomain, location.domain, 'A', [ ipv4 ]);
if (ipv6) await dns.upsertDnsRecords(location.subdomain, location.domain, 'AAAA', [ ipv6 ]);
}
}
debug('refreshDNS: updated apps');
await eventlog.add(eventlog.ACTION_DYNDNS_UPDATE, auditSource, { fromIpv4: info.ipv4, fromIpv6: info.ipv6, toIpv4: ipv4, toIpv6: ipv6 });
info.ipv4 = ipv4;
info.ipv6 = ipv6;
safe.fs.writeFileSync(paths.DYNDNS_INFO_FILE, JSON.stringify(info), 'utf8');
progressCallback({ percent: 100, message: 'refreshDNS: updated apps' });
}
+1
View File
@@ -23,6 +23,7 @@ exports = module.exports = {
TASK_BACKUP: 'backup',
TASK_UPDATE: 'update',
TASK_CHECK_CERTS: 'checkCerts',
TASK_SYNC_DYNDNS: 'syncDyndns',
TASK_SETUP_DNS_AND_CERT: 'setupDnsAndCert',
TASK_CLEAN_BACKUPS: 'cleanBackups',
TASK_SYNC_EXTERNAL_LDAP: 'syncExternalLdap',
+2
View File
@@ -9,6 +9,7 @@ const apptask = require('./apptask.js'),
cloudron = require('./cloudron.js'),
database = require('./database.js'),
dns = require('./dns.js'),
dyndns = require('./dyndns.js'),
externalLdap = require('./externalldap.js'),
fs = require('fs'),
mail = require('./mail.js'),
@@ -29,6 +30,7 @@ const TASKS = { // indexed by task type
syncExternalLdap: externalLdap.sync,
changeMailLocation: mail.changeLocation,
syncDnsRecords: dns.syncDnsRecords,
syncDyndns: dyndns.sync,
updateDiskUsage: system.updateDiskUsage,
_identity: async (arg, progressCallback) => { progressCallback(); return arg; },