From 1a49da92ea883bbf8b0d0cc6010caf5310aa2c05 Mon Sep 17 00:00:00 2001 From: Tim Wilkinson Date: Tue, 29 Oct 2024 09:48:00 -0700 Subject: [PATCH] Fix migration of services on nodes without any services (#1660) * Fix migration of services on nodes without any services * Correctly handle first creation and last deletion. --- files/app/main/status/e/local-services.ut | 12 ++++++++++-- files/app/partial/local-services.ut | 2 +- files/usr/lib/lua/aredn/services.lua | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/files/app/main/status/e/local-services.ut b/files/app/main/status/e/local-services.ut index 92de813c..5f3ca75f 100755 --- a/files/app/main/status/e/local-services.ut +++ b/files/app/main/status/e/local-services.ut @@ -37,7 +37,15 @@ if (request.env.REQUEST_METHOD === "PUT") { configuration.prepareChanges(); if ("services" in request.args) { const services = json(request.args.services); - uciMesh.set("setup", "services", "service", services); + if (!uciMesh.get("setup", "services")) { + uciMesh.set("setup", "services", "services"); + } + if (length(services)) { + uciMesh.set("setup", "services", "service", services); + } + else { + uciMesh.delete("setup", "services", "service"); + } uciMesh.commit("setup"); } if ("aliases" in request.args) { @@ -108,7 +116,7 @@ const ae = iptoarr(dhcp.end); const reService = regexp("^([^|]+)\\|1\\|([^|]+)\\|([^|]+)\\|(\\d+)\\|(.*)$"); const reLink = regexp("^([^|]+)\\|0\\|\\|([^|]+)\\|\\|$"); const reType = regexp("^(.+) \\[([a-z]+)\\]$"); -const svcs = uciMesh.get("setup", "services", "service"); +const svcs = uciMesh.get("setup", "services", "service") || []; for (let i = 0; i < length(svcs); i++) { const l = trim(svcs[i]); const v = match(l, reService); diff --git a/files/app/partial/local-services.ut b/files/app/partial/local-services.ut index 0b6147f6..24374e1e 100755 --- a/files/app/partial/local-services.ut +++ b/files/app/partial/local-services.ut @@ -55,7 +55,7 @@ } f.close(); } - const svcs = uciMesh.get("setup", "services", "service"); + const svcs = uciMesh.get("setup", "services", "service") || []; for (let i = 0; i < length(svcs); i++) { const l = trim(svcs[i]); const v = match(l, reService); diff --git a/files/usr/lib/lua/aredn/services.lua b/files/usr/lib/lua/aredn/services.lua index 5a0abfbc..48e2b002 100755 --- a/files/usr/lib/lua/aredn/services.lua +++ b/files/usr/lib/lua/aredn/services.lua @@ -127,7 +127,7 @@ local function get(validate) end -- load the services - local svcs = uci.cursor("/etc/config.mesh"):get_all("setup", "services", "service") + local svcs = uci.cursor("/etc/config.mesh"):get_all("setup", "services", "service") or {} for _, svc in ipairs(svcs) do local name, link, proto, host, port, sffx = svc:match("(.*)|(.*)|(.*)|(.*)|(.*)|(.*)")