mirror of https://github.com/aredn/aredn.git
Migrate services files into uci setup config (#1650)
This commit is contained in:
parent
4483384b8a
commit
07d4b8d3e0
|
@ -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: [] };
|
||||
|
|
|
@ -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 += ` <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;
|
||||
}
|
||||
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 += ` <div class="icon warning" title="Service cannot be reached"></div>`;
|
||||
}
|
||||
else {
|
||||
const k = match(trim(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>`);
|
||||
}
|
||||
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 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) {
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
--
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue