From 07d4b8d3e0adbad97401f85d9fc81c5339b51754 Mon Sep 17 00:00:00 2001 From: Tim Wilkinson Date: Mon, 28 Oct 2024 16:37:06 -0700 Subject: [PATCH] Migrate services files into uci setup config (#1650) --- files/app/main/status/e/local-services.ut | 60 ++++++------- files/app/partial/local-services.ut | 72 ++++++++-------- files/etc/uci-defaults/20_setup_migrate | 88 ++++++++++++++++++++ files/usr/lib/lua/aredn/services.lua | 29 +++---- files/usr/local/bin/aredn_init | 26 ++---- files/usr/local/bin/node-setup | 3 - files/usr/share/ucode/aredn/configuration.uc | 2 - 7 files changed, 166 insertions(+), 114 deletions(-) create mode 100755 files/etc/uci-defaults/20_setup_migrate diff --git a/files/app/main/status/e/local-services.ut b/files/app/main/status/e/local-services.ut index 1f19e46d..92de813c 100755 --- a/files/app/main/status/e/local-services.ut +++ b/files/app/main/status/e/local-services.ut @@ -37,14 +37,8 @@ if (request.env.REQUEST_METHOD === "PUT") { configuration.prepareChanges(); if ("services" in request.args) { const services = json(request.args.services); - const dhcp = configuration.getDHCP(); - let f = fs.open(dhcp.services, "w"); - if (f) { - for (let i = 0; i < length(services); i++) { - f.write(`${services[i]}\n`); - } - f.close(); - } + uciMesh.set("setup", "services", "service", services); + uciMesh.commit("setup"); } if ("aliases" in request.args) { const aliases = json(request.args.aliases); @@ -111,36 +105,34 @@ const services = []; const dhcp = configuration.getDHCP(); const as = iptoarr(dhcp.start); const ae = iptoarr(dhcp.end); -let f = fs.open(dhcp.services); -if (f) { - const reService = regexp("^([^|]+)\\|1\\|([^|]+)\\|([^|]+)\\|(\\d+)\\|(.*)$"); - const reLink = regexp("^([^|]+)\\|0\\|\\|([^|]+)\\|\\|$"); - const reType = regexp("^(.+) \\[([a-z]+)\\]$"); - for (let l = f.read("line"); length(l); l = f.read("line")) { - const v = match(trim(l), reService); - if (v) { - let type = null; - const v2 = match(v[1], reType); - if (v2) { - v[1] = v2[1]; - type = v2[2]; - } - push(services, { name: v[1], type: type, link: true, protocol: v[2], hostname: v[3], port: v[4], path: v[5] }); +const reService = regexp("^([^|]+)\\|1\\|([^|]+)\\|([^|]+)\\|(\\d+)\\|(.*)$"); +const reLink = regexp("^([^|]+)\\|0\\|\\|([^|]+)\\|\\|$"); +const reType = regexp("^(.+) \\[([a-z]+)\\]$"); +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); + if (v) { + let type = null; + const v2 = match(v[1], reType); + if (v2) { + v[1] = v2[1]; + type = v2[2]; } - else { - const k = match(trim(l), reLink); - if (k) { - let type = null; - const k2 = match(k[1], reType); - if (k2) { - k[1] = k2[1]; - type = k2[2]; - } - push(services, { name: k[1], type: type, link: false, hostname: k[2] }); + push(services, { name: v[1], type: type, link: true, protocol: v[2], hostname: v[3], port: v[4], path: v[5] }); + } + else { + const k = match(l, reLink); + if (k) { + let type = null; + const k2 = match(k[1], reType); + if (k2) { + k[1] = k2[1]; + type = k2[2]; } + push(services, { name: k[1], type: type, link: false, hostname: k[2] }); } } - f.close(); } const hosts = [ configuration.getName() ]; const aliases = { start: dhcp.start, end: dhcp.end, leases: {}, map: [] }; diff --git a/files/app/partial/local-services.ut b/files/app/partial/local-services.ut index 72bcfec0..0b6147f6 100755 --- a/files/app/partial/local-services.ut +++ b/files/app/partial/local-services.ut @@ -55,46 +55,44 @@ } f.close(); } - f = fs.open(dhcp.services); - if (f) { - for (let l = f.read("line"); length(l); l = f.read("line")) { - const v = match(trim(l), reService); - if (v) { - let type = ""; - if (!activesvc[v[1]]) { - type += `
`; - } - const v2 = match(v[1], reType); - if (v2) { - v[1] = v2[1]; - type += `
`; - } - switch (v[4]) { - case "80": - push(services, `
${v[1]}${type}
`); - break; - case "443": - push(services, `
${v[1]}${type}
`); - break; - default: - push(services, `
${v[1]}${type}
`); - break; - } + 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); + if (v) { + let type = ""; + if (!activesvc[v[1]]) { + type += `
`; } - else { - const k = match(trim(l), reLink); - if (k) { - let type = ""; - const k2 = match(k[1], reType); - if (k2) { - k[1] = k2[1]; - type = `
`; - } - push(services, `
${k[1]}${type}
`); - } + const v2 = match(v[1], reType); + if (v2) { + v[1] = v2[1]; + type += `
`; + } + switch (v[4]) { + case "80": + push(services, `
${v[1]}${type}
`); + break; + case "443": + push(services, `
${v[1]}${type}
`); + break; + default: + push(services, `
${v[1]}${type}
`); + break; + } + } + else { + const k = match(l, reLink); + if (k) { + let type = ""; + const k2 = match(k[1], reType); + if (k2) { + k[1] = k2[1]; + type = `
`; + } + push(services, `
${k[1]}${type}
`); } } - f.close(); } if (dhcp.enabled) { diff --git a/files/etc/uci-defaults/20_setup_migrate b/files/etc/uci-defaults/20_setup_migrate new file mode 100755 index 00000000..681410b6 --- /dev/null +++ b/files/etc/uci-defaults/20_setup_migrate @@ -0,0 +1,88 @@ +#!/bin/sh +true <<'LICENSE' + Part of AREDN -- Used for creating Amateur Radio Emergency Data Networks + Copyright (C) 2024 Tim Wilkinson + See Contributors file for additional contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Additional Terms: + + Additional use restrictions exist on the AREDN(TM) trademark and logo. + See AREDNLicense.txt for more info. + + Attributions to the AREDN Project must be retained in the source code. + If importing this code into a new or existing project attribution + to the AREDN project must be added to the source code. + + You must not misrepresent the origin of the material contained within. + + Modified versions must be modified to attribute to the original source + and be marked in reasonable ways as differentiate it from the original + version. + +LICENSE + +cat > /tmp/setup_migrate << __EOF__ + +require("nixio") +require("uci") + +io.open("/etc/config.mesh/setup", "a"):close() +local c = uci.cursor("/etc/config.mesh") + +-- Migrate the old school _setup file +if not c:get("setup", "globals") and nixio.fs.stat("/etc/config.mesh/_setup") then + c:set("setup", "globals", "globals") + for line in io.lines("/etc/config.mesh/_setup") + do + if not (line:match("^%s*#") or line:match("^%s*$")) then + local k, v = line:match("^(%S+)%s*=%s*(.*)%s*$") + if v then + c:set("setup", "globals", k, v) + end + end + end + c:commit("setup") +end + +-- Migrate the old school _setup.services.{nat,dmz} files +if not c:get("setup", "services") then + local file = "/etc/config.mesh/_setup.services." + if (nixio.fs.stat(file .. "nat", "size") or -1) > 0 then + file = file .. "nat" + elseif (nixio.fs.stat(file .. "dmz", "size") or -1) > 0 then + file = file .. "dmz" + else + file = nil + end + if file then + local services = {} + for line in io.lines(file) + do + services[#services + 1] = line + end + c:set("setup", "services", "services") + c:set("setup", "services", "service", services) + c:commit("setup") + end +end + +-- Dont remove this yet otherwise we cannot revert this node +-- os.remove("/etc/config.mesh/_setup") +-- os.remove("/etc/config.mesh/_setup.service.dmz") +-- os.remove("/etc/config.mesh/_setup.service.nat") + +__EOF__ +/usr/bin/lua /tmp/setup_migrate +rm -f /tmp/setup_migrate diff --git a/files/usr/lib/lua/aredn/services.lua b/files/usr/lib/lua/aredn/services.lua index e901986a..5a0abfbc 100755 --- a/files/usr/lib/lua/aredn/services.lua +++ b/files/usr/lib/lua/aredn/services.lua @@ -127,25 +127,18 @@ local function get(validate) end -- load the services - local servfile = "/etc/config.mesh/_setup.services.nat" - if dmz_mode ~= "0" then - servfile = "/etc/config.mesh/_setup.services.dmz" - end - if nixio.fs.access(servfile) then - for line in io.lines(servfile) - do - if not (line:match("^%s*#") or line:match("^%s*$")) then - local name, link, proto, host, port, sffx = line:match("(.*)|(.*)|(.*)|(.*)|(.*)|(.*)") - if name and name ~= "" and host ~= "" then - if proto == "" then - proto = "http" - end - if link == "0" then - port = "0" - end - services[#services + 1] = string.format("%s://%s:%s/%s|tcp|%s", proto, host, port, sffx, name) - end + local svcs = uci.cursor("/etc/config.mesh"):get_all("setup", "services", "service") + for _, svc in ipairs(svcs) + do + local name, link, proto, host, port, sffx = svc:match("(.*)|(.*)|(.*)|(.*)|(.*)|(.*)") + if name and name ~= "" and host ~= "" then + if proto == "" then + proto = "http" end + if link == "0" then + port = "0" + end + services[#services + 1] = string.format("%s://%s:%s/%s|tcp|%s", proto, host, port, sffx, name) end end -- diff --git a/files/usr/local/bin/aredn_init b/files/usr/local/bin/aredn_init index 9bb313e4..f7543aea 100755 --- a/files/usr/local/bin/aredn_init +++ b/files/usr/local/bin/aredn_init @@ -162,28 +162,14 @@ do end -- Override with the current config -if nixio.fs.stat("/etc/config.mesh/setup") then - local all = uci.cursor("/etc/config.mesh"):get_all("setup", "globals") - for k, v in pairs(all) - do - if not k:match("^%.") then - cfg[k] = v - end +local all = uci.cursor("/etc/config.mesh"):get_all("setup", "globals") +for k, v in pairs(all) +do + if not k:match("^%.") then + cfg[k] = v end -else - -- Migrate the old school _setup file and then delete it - for line in io.lines("/etc/config.mesh/_setup") - do - if not (line:match("^%s*#") or line:match("^%s*$")) then - local k, v = line:match("^(%S+)%s*=%s*(.*)%s*$") - if cfg[k] then - cfg[k] = v - end - end - end - -- Dont remove this yet otherwise we cannot revert this node - -- os.remove("/etc/config.mesh/_setup") end + -- Override wifi setting if wifi_enable == 0 then cfg.wifi_enable = wifi_enable diff --git a/files/usr/local/bin/node-setup b/files/usr/local/bin/node-setup index faab630e..ed7f1e64 100755 --- a/files/usr/local/bin/node-setup +++ b/files/usr/local/bin/node-setup @@ -310,21 +310,18 @@ local dhcpfile = "/etc/config.mesh/_setup.dhcp" local dhcptagsfile = "/etc/config.mesh/_setup.dhcptags" local dhcpoptionsfile = "/etc/config.mesh/_setup.dhcpoptions" local aliasfile = "/etc/config.mesh/aliases" -local servfile = "/etc/config.mesh/_setup.services" if is_nat_mode() then portfile = portfile .. ".nat" dhcpfile = dhcpfile .. ".nat" dhcptagsfile = dhcptagsfile .. ".nat" dhcpoptionsfile = dhcpoptionsfile .. ".nat" aliasfile = aliasfile .. ".nat" - servfile = servfile .. ".nat" else portfile = portfile .. ".dmz" dhcpfile = dhcpfile .. ".dmz" dhcptagsfile = dhcptagsfile .. ".dmz" dhcpoptionsfile = dhcpoptionsfile .. ".dmz" aliasfile = aliasfile .. ".dmz" - servfile = servfile .. ".dmz" end -- check for old aliases file, copy it to .dmz and create symlink diff --git a/files/usr/share/ucode/aredn/configuration.uc b/files/usr/share/ucode/aredn/configuration.uc index 0186285c..c542118e 100755 --- a/files/usr/share/ucode/aredn/configuration.uc +++ b/files/usr/share/ucode/aredn/configuration.uc @@ -222,7 +222,6 @@ export function getDHCP(mode) cidr: network.netmaskToCIDR(setup.lan_mask), leases: "/tmp/dhcp.leases", reservations: "/etc/config.mesh/_setup.dhcp.nat", - services: "/etc/config.mesh/_setup.services.nat", ports: "/etc/config.mesh/_setup.ports.nat", dhcptags: "/etc/config.mesh/_setup.dhcptags.nat", dhcpoptions: "/etc/config.mesh/_setup.dhcpoptions.nat", @@ -246,7 +245,6 @@ export function getDHCP(mode) cidr: network.netmaskToCIDR(setup.lan_mask), leases: "/tmp/dhcp.leases", reservations: "/etc/config.mesh/_setup.dhcp.dmz", - services: "/etc/config.mesh/_setup.services.dmz", ports: "/etc/config.mesh/_setup.ports.dmz", dhcptags: "/etc/config.mesh/_setup.dhcptags.dmz", dhcpoptions: "/etc/config.mesh/_setup.dhcpoptions.dmz",