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,13 +105,13 @@ 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);
|
||||
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);
|
||||
|
@ -128,7 +122,7 @@ if (f) {
|
|||
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(trim(l), reLink);
|
||||
const k = match(l, reLink);
|
||||
if (k) {
|
||||
let type = null;
|
||||
const k2 = match(k[1], reType);
|
||||
|
@ -139,8 +133,6 @@ if (f) {
|
|||
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,10 +55,10 @@
|
|||
}
|
||||
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);
|
||||
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]]) {
|
||||
|
@ -82,7 +82,7 @@
|
|||
}
|
||||
}
|
||||
else {
|
||||
const k = match(trim(l), reLink);
|
||||
const k = match(l, reLink);
|
||||
if (k) {
|
||||
let type = "";
|
||||
const k2 = match(k[1], reType);
|
||||
|
@ -94,8 +94,6 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
if (dhcp.enabled) {
|
||||
f = fs.open(dhcp.reservations);
|
||||
|
|
|
@ -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,15 +127,10 @@ 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)
|
||||
local svcs = uci.cursor("/etc/config.mesh"):get_all("setup", "services", "service")
|
||||
for _, svc in ipairs(svcs)
|
||||
do
|
||||
if not (line:match("^%s*#") or line:match("^%s*$")) then
|
||||
local name, link, proto, host, port, sffx = line:match("(.*)|(.*)|(.*)|(.*)|(.*)|(.*)")
|
||||
local name, link, proto, host, port, sffx = svc:match("(.*)|(.*)|(.*)|(.*)|(.*)|(.*)")
|
||||
if name and name ~= "" and host ~= "" then
|
||||
if proto == "" then
|
||||
proto = "http"
|
||||
|
@ -146,8 +141,6 @@ local function get(validate)
|
|||
services[#services + 1] = string.format("%s://%s:%s/%s|tcp|%s", proto, host, port, sffx, name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
--
|
||||
|
||||
-- validation
|
||||
|
|
|
@ -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
|
||||
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
|
||||
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