Merge pull request #2223 from freifunk-gluon/wizard-reconfigure

Simplify save/commit handling of config wizard sections
This commit is contained in:
David Bauer 2021-05-19 13:39:03 +02:00 committed by GitHub
commit 17dd5abd75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 91 deletions

View File

@ -21,7 +21,6 @@ return function(form, uci)
o.optional = true o.optional = true
function o:write(data) function o:write(data)
uci:set("gluon-node-info", owner, "contact", data) uci:set("gluon-node-info", owner, "contact", data)
uci:save("gluon-node-info")
end end
return {'gluon-node-info'}
end end

View File

@ -1,15 +1,6 @@
local util = require "gluon.util" local util = require "gluon.util"
local uci = require("simple-uci").cursor() local uci = require("simple-uci").cursor()
local wizard = {}
for _, entry in ipairs(util.glob('/lib/gluon/config-mode/wizard/*')) do
local f = assert(loadfile(entry))
setfenv(f, getfenv())
local w = f()
table.insert(wizard, w)
end
local f = Form(translate("Welcome!")) local f = Form(translate("Welcome!"))
f.submit = translate('Save & restart') f.submit = translate('Save & restart')
f.reset = false f.reset = false
@ -18,21 +9,10 @@ local s = f:section(Section)
s.template = "wizard/welcome" s.template = "wizard/welcome"
s.package = "gluon-config-mode-core" s.package = "gluon-config-mode-core"
local commit = {'gluon-setup-mode'} for _, entry in ipairs(util.glob('/lib/gluon/config-mode/wizard/*')) do
local run = {} local section = assert(loadfile(entry))
setfenv(section, getfenv())
for _, w in ipairs(wizard) do section()(f, uci)
for _, c in ipairs(w(f, uci) or {}) do
if type(c) == 'string' then
if not util.contains(commit, c) then
table.insert(commit, c)
end
elseif type(c) == 'function' then
table.insert(run, c)
else
error('invalid wizard module return')
end
end
end end
function f:write() function f:write()
@ -40,13 +20,9 @@ function f:write()
local unistd = require 'posix.unistd' local unistd = require 'posix.unistd'
uci:set("gluon-setup-mode", uci:get_first("gluon-setup-mode", "setup_mode"), "configured", true) uci:set("gluon-setup-mode", uci:get_first("gluon-setup-mode", "setup_mode"), "configured", true)
uci:save("gluon-setup-mode")
for _, c in ipairs(commit) do os.execute('gluon-reconfigure')
uci:commit(c)
end
for _, r in ipairs(run) do
r()
end
f.template = "wizard/reboot" f.template = "wizard/reboot"
f.package = "gluon-config-mode-core" f.package = "gluon-config-mode-core"

View File

@ -49,20 +49,8 @@ return function(form, uci)
o:value(domain.domain_code, domain.domain_name) o:value(domain.domain_code, domain.domain_name)
end end
local domain_changed = false
function o:write(data) function o:write(data)
if data ~= selected_domain then uci:set('gluon', 'core', 'domain', data)
domain_changed = true uci:save('gluon')
uci:set('gluon', 'core', 'domain', data)
end
end end
local function reconfigure()
if domain_changed then
os.execute('gluon-reconfigure')
end
end
return {'gluon', reconfigure}
end end

View File

@ -100,5 +100,7 @@ return function(form, uci)
end end
end end
return {'gluon-node-info'} function s:write()
uci:save("gluon-node-info")
end
end end

View File

@ -30,7 +30,6 @@ return function(form, uci)
function o:write(data) function o:write(data)
pretty_hostname.set(uci, data or default_hostname) pretty_hostname.set(uci, data or default_hostname)
uci:save('system')
end end
return {'system'}
end end

View File

@ -58,11 +58,7 @@ return function(form, uci)
uci:set("gluon", "mesh_vpn", "limit_egress", data * 1000) uci:set("gluon", "mesh_vpn", "limit_egress", data * 1000)
end end
function s:handle() function s:write()
Section.handle(s)
uci:save('gluon') uci:save('gluon')
os.execute('exec /lib/gluon/mesh-vpn/update-config')
end end
return {'gluon', 'fastd', 'tunneldigger', 'simple-tc'}
end end

View File

@ -44,10 +44,6 @@ return function(form, uci)
end end
uci:save('wireless') uci:save('wireless')
end end
os.execute('/lib/gluon/upgrade/200-wireless')
end end
end end
return {'gluon', 'network', 'wireless'}
end end

View File

@ -53,6 +53,7 @@ function Node:__init__(name, title, description)
self.name = name self.name = name
self.index = nil self.index = nil
self.parent = nil self.parent = nil
self.state = M.FORM_NODATA
self.package = 'gluon-web-model' self.package = 'gluon-web-model'
end end
@ -73,17 +74,32 @@ function Node:id()
end end
function Node:reset_node() function Node:reset_node()
self.state = M.FORM_NODATA
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
child:reset_node() child:reset_node()
end end
end end
function Node:parse(http) function Node:parse(http)
self.state = M.FORM_VALID
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
child:parse(http) child:parse(http)
end end
end end
function Node:propagate_state()
if self.state == M.FORM_NODATA then
return
end
for _, child in ipairs(self.children) do
child:propagate_state()
if child.state == M.FORM_INVALID then
self.state = M.FORM_INVALID
end
end
end
function Node:render(renderer, scope) function Node:render(renderer, scope)
if self.template then if self.template then
local env = setmetatable({ local env = setmetatable({
@ -158,9 +174,16 @@ function Node:resolve_node_depends()
return true return true
end end
-- will be overridden: write(value)
function Node:write()
end
function Node:handle() function Node:handle()
for _, node in ipairs(self.children) do if self.state == M.FORM_VALID then
node:handle() for _, node in ipairs(self.children) do
node:handle()
end
self:write(self.data)
end end
end end
@ -187,7 +210,6 @@ function AbstractValue:__init__(...)
self.template = "model/valuewrapper" self.template = "model/valuewrapper"
self.error = false self.error = false
self.state = M.FORM_NODATA
end end
function AbstractValue:defaultvalue() function AbstractValue:defaultvalue()
@ -250,16 +272,6 @@ function AbstractValue:validate()
end end
function AbstractValue:handle()
if self.state == M.FORM_VALID then
self:write(self.data)
end
end
-- will be overridden: write(value)
function AbstractValue:write()
end
local Value = class(AbstractValue) local Value = class(AbstractValue)
M.Value = Value M.Value = Value
@ -438,26 +450,7 @@ function Form:parse(http)
while self:resolve_depends() do end while self:resolve_depends() do end
for _, s in ipairs(self.children) do self:propagate_state()
for _, v in ipairs(s.children) do
if v.state == M.FORM_INVALID then
self.state = M.FORM_INVALID
return
end
end
end
self.state = M.FORM_VALID
end
function Form:handle()
if self.state == M.FORM_VALID then
Node.handle(self)
self:write()
end
end
function Form:write()
end end
function Form:section(t, ...) function Form:section(t, ...)