Generalize node-setup variable expansion (#653)

* Turn variable expansion into a function so we can reuse it

* Fix not including missing include
This commit is contained in:
Tim Wilkinson 2023-01-15 21:50:39 -08:00 committed by GitHub
parent fdeda7d0cc
commit 45ac6c583f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 34 deletions

View File

@ -116,6 +116,40 @@ local cfg = {
wifi_network_config = "" wifi_network_config = ""
} }
function expand_vars(lines)
local nlines = {}
for line in lines:gmatch("([^\n]*\n?)")
do
local inc = line:match("^include%s+(%S+)%s*")
if inc then
if nixio.fs.stat(inc) then
line = expand_vars(read_all(inc))
else
line = nil
end
elseif line:match("^[^#]") then
for parm in line:gmatch("<([^%s]*)>")
do
if deleteme[parm] then
line = nil
elseif parm == "NODE" then
line = line:gsub("<NODE>", node)
elseif parm == "MAC2" then
line = line:gsub("<MAC2>", mac2)
elseif parm == "DTDMAC" then
line = line:gsub("<DTDMAC>", dtdmac)
else
line = line:gsub("<" .. parm .. ">", cfg[parm])
end
end
end
if line then
nlines[#nlines + 1] = line
end
end
return table.concat(nlines, "")
end
if not auto then if not auto then
-- Is this used now? -- Is this used now?
print "Non-auto mode no longer supported." print "Non-auto mode no longer supported."
@ -231,7 +265,7 @@ if do_basic then
-- generate the new school bridge configuration -- generate the new school bridge configuration
if nixio.fs.stat("/etc/aredn_include/bridge.network.user") then if nixio.fs.stat("/etc/aredn_include/bridge.network.user") then
cfg.bridge_network_config = read_all("/etc/aredn_include/bridge.network.user") cfg.bridge_network_config = expand_vars(read_all("/etc/aredn_include/bridge.network.user"))
else else
local list = {} local list = {}
for _, net in ipairs({ "lan", "wan", "dtdlink" }) for _, net in ipairs({ "lan", "wan", "dtdlink" })
@ -257,7 +291,7 @@ if do_basic then
local config = "" local config = ""
-- user override -- user override
if nixio.fs.stat("/etc/aredn_include/" .. net .. ".network.user") then if nixio.fs.stat("/etc/aredn_include/" .. net .. ".network.user") then
config = read_all("/etc/aredn_include/" .. net .. ".network.user") config = expand_vars(read_all("/etc/aredn_include/" .. net .. ".network.user"))
else else
-- generate a complete config -- generate a complete config
local vlan = nil local vlan = nil
@ -395,38 +429,7 @@ if do_basic then
if not (bfile:match("^_setup") or bfile:match("^firewall.user") or bfile:match("^olsrd")) then if not (bfile:match("^_setup") or bfile:match("^firewall.user") or bfile:match("^olsrd")) then
local f = io.open("/tmp/new_config/" .. bfile, "w") local f = io.open("/tmp/new_config/" .. bfile, "w")
if f then if f then
for line in io.lines(file) f:write(expand_vars(read_all(file)))
do
local out = true
local inc = line:match("^include%s+(.*)%s*")
if inc then
if nixio.fs.stat(inc) then
for iline in io.lines(inc)
do
f:write(iline .. "\n")
end
end
out = false
elseif line:match("^[^#]") then
for parm in line:gmatch("<([^%s]*)>")
do
if deleteme[parm] then
out = false
elseif parm == "NODE" then
line = line:gsub("<NODE>", node)
elseif parm == "MAC2" then
line = line:gsub("<MAC2>", mac2)
elseif parm == "DTDMAC" then
line = line:gsub("<DTDMAC>", dtdmac)
else
line = line:gsub("<" .. parm .. ">", cfg[parm])
end
end
end
if out then
f:write(line .. "\n")
end
end
f:close() f:close()
end end
end end