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