Migrate services files into uci setup config (#1650)

This commit is contained in:
Tim Wilkinson 2024-10-28 16:37:06 -07:00 committed by GitHub
parent 4483384b8a
commit 07d4b8d3e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 166 additions and 114 deletions

View File

@ -37,14 +37,8 @@ if (request.env.REQUEST_METHOD === "PUT") {
configuration.prepareChanges(); configuration.prepareChanges();
if ("services" in request.args) { if ("services" in request.args) {
const services = json(request.args.services); const services = json(request.args.services);
const dhcp = configuration.getDHCP(); uciMesh.set("setup", "services", "service", services);
let f = fs.open(dhcp.services, "w"); uciMesh.commit("setup");
if (f) {
for (let i = 0; i < length(services); i++) {
f.write(`${services[i]}\n`);
}
f.close();
}
} }
if ("aliases" in request.args) { if ("aliases" in request.args) {
const aliases = json(request.args.aliases); const aliases = json(request.args.aliases);
@ -111,36 +105,34 @@ const services = [];
const dhcp = configuration.getDHCP(); const dhcp = configuration.getDHCP();
const as = iptoarr(dhcp.start); const as = iptoarr(dhcp.start);
const ae = iptoarr(dhcp.end); const ae = iptoarr(dhcp.end);
let f = fs.open(dhcp.services); const reService = regexp("^([^|]+)\\|1\\|([^|]+)\\|([^|]+)\\|(\\d+)\\|(.*)$");
if (f) { const reLink = regexp("^([^|]+)\\|0\\|\\|([^|]+)\\|\\|$");
const reService = regexp("^([^|]+)\\|1\\|([^|]+)\\|([^|]+)\\|(\\d+)\\|(.*)$"); const reType = regexp("^(.+) \\[([a-z]+)\\]$");
const reLink = regexp("^([^|]+)\\|0\\|\\|([^|]+)\\|\\|$"); const svcs = uciMesh.get("setup", "services", "service");
const reType = regexp("^(.+) \\[([a-z]+)\\]$"); for (let i = 0; i < length(svcs); i++) {
for (let l = f.read("line"); length(l); l = f.read("line")) { const l = trim(svcs[i]);
const v = match(trim(l), reService); const v = match(l, reService);
if (v) { if (v) {
let type = null; let type = null;
const v2 = match(v[1], reType); const v2 = match(v[1], reType);
if (v2) { if (v2) {
v[1] = v2[1]; v[1] = v2[1];
type = v2[2]; type = v2[2];
}
push(services, { name: v[1], type: type, link: true, protocol: v[2], hostname: v[3], port: v[4], path: v[5] });
} }
else { push(services, { name: v[1], type: type, link: true, protocol: v[2], hostname: v[3], port: v[4], path: v[5] });
const k = match(trim(l), reLink); }
if (k) { else {
let type = null; const k = match(l, reLink);
const k2 = match(k[1], reType); if (k) {
if (k2) { let type = null;
k[1] = k2[1]; const k2 = match(k[1], reType);
type = k2[2]; if (k2) {
} k[1] = k2[1];
push(services, { name: k[1], type: type, link: false, hostname: k[2] }); type = k2[2];
} }
push(services, { name: k[1], type: type, link: false, hostname: k[2] });
} }
} }
f.close();
} }
const hosts = [ configuration.getName() ]; const hosts = [ configuration.getName() ];
const aliases = { start: dhcp.start, end: dhcp.end, leases: {}, map: [] }; const aliases = { start: dhcp.start, end: dhcp.end, leases: {}, map: [] };

View File

@ -55,46 +55,44 @@
} }
f.close(); f.close();
} }
f = fs.open(dhcp.services); const svcs = uciMesh.get("setup", "services", "service");
if (f) { for (let i = 0; i < length(svcs); i++) {
for (let l = f.read("line"); length(l); l = f.read("line")) { const l = trim(svcs[i]);
const v = match(trim(l), reService); const v = match(l, reService);
if (v) { if (v) {
let type = ""; let type = "";
if (!activesvc[v[1]]) { if (!activesvc[v[1]]) {
type += ` <div class="icon warning" title="Service cannot be reached"></div>`; type += ` <div class="icon warning" title="Service cannot be reached"></div>`;
}
const v2 = match(v[1], reType);
if (v2) {
v[1] = v2[1];
type += ` <div class="icon ${v2[2]}" title="${v2[2]}"></div>`;
}
switch (v[4]) {
case "80":
push(services, `<div class="service"><a target="_blank" href="http://${v[3]}.local.mesh/${v[5]}" onclick="event.stopPropagation()">${v[1]}${type}</a></div>`);
break;
case "443":
push(services, `<div class="service"><a target="_blank" href="https://${v[3]}.local.mesh/${v[5]}" onclick="event.stopPropagation()">${v[1]}${type}</a></div>`);
break;
default:
push(services, `<div class="service"><a target="_blank" href="${v[2]}://${v[3]}.local.mesh:${v[4]}/${v[5]}" onclick="event.stopPropagation()">${v[1]}${type}</a></div>`);
break;
}
} }
else { const v2 = match(v[1], reType);
const k = match(trim(l), reLink); if (v2) {
if (k) { v[1] = v2[1];
let type = ""; type += ` <div class="icon ${v2[2]}" title="${v2[2]}"></div>`;
const k2 = match(k[1], reType); }
if (k2) { switch (v[4]) {
k[1] = k2[1]; case "80":
type = ` <div class="icon ${k2[2]}" title="${k2[2]}"></div>`; push(services, `<div class="service"><a target="_blank" href="http://${v[3]}.local.mesh/${v[5]}" onclick="event.stopPropagation()">${v[1]}${type}</a></div>`);
} break;
push(services, `<div class="service">${k[1]}${type}</div>`); case "443":
} push(services, `<div class="service"><a target="_blank" href="https://${v[3]}.local.mesh/${v[5]}" onclick="event.stopPropagation()">${v[1]}${type}</a></div>`);
break;
default:
push(services, `<div class="service"><a target="_blank" href="${v[2]}://${v[3]}.local.mesh:${v[4]}/${v[5]}" onclick="event.stopPropagation()">${v[1]}${type}</a></div>`);
break;
}
}
else {
const k = match(l, reLink);
if (k) {
let type = "";
const k2 = match(k[1], reType);
if (k2) {
k[1] = k2[1];
type = ` <div class="icon ${k2[2]}" title="${k2[2]}"></div>`;
}
push(services, `<div class="service">${k[1]}${type}</div>`);
} }
} }
f.close();
} }
if (dhcp.enabled) { if (dhcp.enabled) {

View File

@ -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 <http://www.gnu.org/licenses/>.
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

View File

@ -127,25 +127,18 @@ local function get(validate)
end end
-- load the services -- load the services
local servfile = "/etc/config.mesh/_setup.services.nat" local svcs = uci.cursor("/etc/config.mesh"):get_all("setup", "services", "service")
if dmz_mode ~= "0" then for _, svc in ipairs(svcs)
servfile = "/etc/config.mesh/_setup.services.dmz" do
end local name, link, proto, host, port, sffx = svc:match("(.*)|(.*)|(.*)|(.*)|(.*)|(.*)")
if nixio.fs.access(servfile) then if name and name ~= "" and host ~= "" then
for line in io.lines(servfile) if proto == "" then
do proto = "http"
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
end 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
end end
-- --

View File

@ -162,28 +162,14 @@ do
end end
-- Override with the current config -- 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")
local all = uci.cursor("/etc/config.mesh"):get_all("setup", "globals") for k, v in pairs(all)
for k, v in pairs(all) do
do if not k:match("^%.") then
if not k:match("^%.") then cfg[k] = v
cfg[k] = v
end
end 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 end
-- Override wifi setting -- Override wifi setting
if wifi_enable == 0 then if wifi_enable == 0 then
cfg.wifi_enable = wifi_enable cfg.wifi_enable = wifi_enable

View File

@ -310,21 +310,18 @@ local dhcpfile = "/etc/config.mesh/_setup.dhcp"
local dhcptagsfile = "/etc/config.mesh/_setup.dhcptags" local dhcptagsfile = "/etc/config.mesh/_setup.dhcptags"
local dhcpoptionsfile = "/etc/config.mesh/_setup.dhcpoptions" local dhcpoptionsfile = "/etc/config.mesh/_setup.dhcpoptions"
local aliasfile = "/etc/config.mesh/aliases" local aliasfile = "/etc/config.mesh/aliases"
local servfile = "/etc/config.mesh/_setup.services"
if is_nat_mode() then if is_nat_mode() then
portfile = portfile .. ".nat" portfile = portfile .. ".nat"
dhcpfile = dhcpfile .. ".nat" dhcpfile = dhcpfile .. ".nat"
dhcptagsfile = dhcptagsfile .. ".nat" dhcptagsfile = dhcptagsfile .. ".nat"
dhcpoptionsfile = dhcpoptionsfile .. ".nat" dhcpoptionsfile = dhcpoptionsfile .. ".nat"
aliasfile = aliasfile .. ".nat" aliasfile = aliasfile .. ".nat"
servfile = servfile .. ".nat"
else else
portfile = portfile .. ".dmz" portfile = portfile .. ".dmz"
dhcpfile = dhcpfile .. ".dmz" dhcpfile = dhcpfile .. ".dmz"
dhcptagsfile = dhcptagsfile .. ".dmz" dhcptagsfile = dhcptagsfile .. ".dmz"
dhcpoptionsfile = dhcpoptionsfile .. ".dmz" dhcpoptionsfile = dhcpoptionsfile .. ".dmz"
aliasfile = aliasfile .. ".dmz" aliasfile = aliasfile .. ".dmz"
servfile = servfile .. ".dmz"
end end
-- check for old aliases file, copy it to .dmz and create symlink -- check for old aliases file, copy it to .dmz and create symlink

View File

@ -222,7 +222,6 @@ export function getDHCP(mode)
cidr: network.netmaskToCIDR(setup.lan_mask), cidr: network.netmaskToCIDR(setup.lan_mask),
leases: "/tmp/dhcp.leases", leases: "/tmp/dhcp.leases",
reservations: "/etc/config.mesh/_setup.dhcp.nat", reservations: "/etc/config.mesh/_setup.dhcp.nat",
services: "/etc/config.mesh/_setup.services.nat",
ports: "/etc/config.mesh/_setup.ports.nat", ports: "/etc/config.mesh/_setup.ports.nat",
dhcptags: "/etc/config.mesh/_setup.dhcptags.nat", dhcptags: "/etc/config.mesh/_setup.dhcptags.nat",
dhcpoptions: "/etc/config.mesh/_setup.dhcpoptions.nat", dhcpoptions: "/etc/config.mesh/_setup.dhcpoptions.nat",
@ -246,7 +245,6 @@ export function getDHCP(mode)
cidr: network.netmaskToCIDR(setup.lan_mask), cidr: network.netmaskToCIDR(setup.lan_mask),
leases: "/tmp/dhcp.leases", leases: "/tmp/dhcp.leases",
reservations: "/etc/config.mesh/_setup.dhcp.dmz", reservations: "/etc/config.mesh/_setup.dhcp.dmz",
services: "/etc/config.mesh/_setup.services.dmz",
ports: "/etc/config.mesh/_setup.ports.dmz", ports: "/etc/config.mesh/_setup.ports.dmz",
dhcptags: "/etc/config.mesh/_setup.dhcptags.dmz", dhcptags: "/etc/config.mesh/_setup.dhcptags.dmz",
dhcpoptions: "/etc/config.mesh/_setup.dhcpoptions.dmz", dhcpoptions: "/etc/config.mesh/_setup.dhcpoptions.dmz",