* Merged Luci to use native UCI-library
[project/luci.git] / modules / admin-core / luasrc / controller / admin / uci.lua
1 module("luci.controller.admin.uci", package.seeall)
2 require("luci.util")
3 require("luci.sys")
4
5 function index()
6 node("admin", "uci", "changes").target = call("action_changes")
7 node("admin", "uci", "revert").target = call("action_revert")
8 node("admin", "uci", "apply").target = call("action_apply")
9 end
10
11 function convert_changes(changes)
12 local ret = {}
13 for r, tbl in pairs(changes) do
14 for s, os in pairs(tbl) do
15 for o, v in pairs(os) do
16 local val, str
17 if (v == "") then
18 str = "-"
19 val = ""
20 else
21 str = ""
22 val = "="..v
23 end
24 str = r.."."..s
25 if o ~= ".type" then
26 str = str.."."..o
27 end
28 table.insert(ret, str..val)
29 end
30 end
31 end
32 return table.concat(ret, "\n")
33 end
34
35 function action_changes()
36 local changes = convert_changes(luci.model.uci.changes())
37 luci.template.render("admin_uci/changes", {changes=changes})
38 end
39
40 function action_apply()
41 local changes = luci.model.uci.changes()
42 local output = ""
43
44 if changes then
45 local com = {}
46 local run = {}
47
48 -- Collect files to be applied and commit changes
49 for r, tbl in pairs(changes) do
50 if r then
51 luci.model.uci.load(r)
52 luci.model.uci.commit(r)
53 luci.model.uci.unload(r)
54 if luci.config.uci_oncommit and luci.config.uci_oncommit[r] then
55 run[luci.config.uci_oncommit[r]] = true
56 end
57 end
58 end
59
60 -- Search for post-commit commands
61 for cmd, i in pairs(run) do
62 output = output .. cmd .. ":" .. luci.sys.exec(cmd) .. "\n"
63 end
64 end
65
66
67 luci.template.render("admin_uci/apply", {changes=convert_changes(changes), output=output})
68 end
69
70
71 function action_revert()
72 local changes = luci.model.uci.changes()
73 if changes then
74 local revert = {}
75
76 -- Collect files to be reverted
77 for r, tbl in pairs(changes) do
78 luci.model.uci.load(r)
79 luci.model.uci.revert(r)
80 luci.model.uci.unload(r)
81 end
82 end
83
84 luci.template.render("admin_uci/revert", {changes=convert_changes(changes)})
85 end