* Completed Firmware-Upgrade page
[project/luci.git] / src / ffluci / controller / admin / system.lua
1 module("ffluci.controller.admin.system", package.seeall)
2
3 require("ffluci.sys")
4 require("ffluci.http")
5 require("ffluci.util")
6 require("ffluci.fs")
7 require("ffluci.model.ipkg")
8 require("ffluci.model.uci")
9
10 function action_editor()
11 local file = ffluci.http.formvalue("file", "")
12 local data = ffluci.http.formvalue("data")
13 local err = nil
14 local msg = nil
15 local stat = true
16
17 if file and data then
18 stat, err = ffluci.fs.writefile(file, data)
19 end
20
21 if not stat then
22 err = ffluci.util.split(err, " ")
23 table.remove(err, 1)
24 msg = table.concat(err, " ")
25 end
26
27 local cnt, err = ffluci.fs.readfile(file)
28 if cnt then
29 cnt = ffluci.util.pcdata(cnt)
30 end
31 ffluci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
32 end
33
34 function action_ipkg()
35 local file = "/etc/ipkg.conf"
36 local data = ffluci.http.formvalue("data")
37 local stat = nil
38 local err = nil
39
40 if data then
41 stat, err = ffluci.fs.writefile(file, data)
42 end
43
44 local cnt = ffluci.fs.readfile(file)
45 if cnt then
46 cnt = ffluci.util.pcdata(cnt)
47 end
48
49 ffluci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
50 end
51
52 function action_packages()
53 local ipkg = ffluci.model.ipkg
54 local void = nil
55 local submit = ffluci.http.formvalue("submit")
56
57
58 -- Search query
59 local query = ffluci.http.formvalue("query")
60 query = (query ~= '') and query or nil
61
62
63 -- Packets to be installed
64 local install = ffluci.http.formvalue("install")
65 install = (type(install) == "table" and submit) and install or nil
66
67 -- Install from URL
68 local url = ffluci.http.formvalue("url")
69 if url and url ~= '' and submit then
70 if not install then
71 install = {}
72 end
73 install[url] = 1
74 end
75
76 -- Do install
77 if install then
78 for k, v in pairs(install) do
79 void, install[k] = ipkg.install(k)
80 end
81 end
82
83
84 -- Remove packets
85 local remove = ffluci.http.formvalue("remove")
86 remove = (type(remove) == "table" and submit) and remove or nil
87 if remove then
88 for k, v in pairs(remove) do
89 void, remove[k] = ipkg.remove(k)
90 end
91 end
92
93
94 -- Update all packets
95 local update = ffluci.http.formvalue("update")
96 if update then
97 void, update = ipkg.update()
98 end
99
100
101 -- Upgrade all packets
102 local upgrade = ffluci.http.formvalue("upgrade")
103 if upgrade then
104 void, upgrade = ipkg.upgrade()
105 end
106
107
108 -- Package info
109 local info = ffluci.model.ipkg.info(query)
110 info = info or {}
111 local pkgs = {}
112
113 -- Sort after status and name
114 for k, v in pairs(info) do
115 local x = 0
116 for i, j in pairs(pkgs) do
117 local vins = (v.Status and v.Status.installed)
118 local jins = (j.Status and j.Status.installed)
119 if vins ~= jins then
120 if vins then
121 break
122 end
123 else
124 if j.Package > v.Package then
125 break
126 end
127 end
128 x = i
129 end
130 table.insert(pkgs, x+1, v)
131 end
132
133 ffluci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
134 install=install, remove=remove, update=update, upgrade=upgrade})
135 end
136
137 function action_passwd()
138 local p1 = ffluci.http.formvalue("pwd1")
139 local p2 = ffluci.http.formvalue("pwd2")
140 local stat = nil
141
142 if p1 or p2 then
143 if p1 == p2 then
144 stat = ffluci.sys.user.setpasswd("root", p1)
145 else
146 stat = 10
147 end
148 end
149
150 ffluci.template.render("admin_system/passwd", {stat=stat})
151 end
152
153 function action_reboot()
154 local reboot = ffluci.http.formvalue("reboot")
155 ffluci.template.render("admin_system/reboot", {reboot=reboot})
156 if reboot then
157 ffluci.sys.reboot()
158 end
159 end
160
161 function action_sshkeys()
162 local file = "/etc/dropbear/authorized_keys"
163 local data = ffluci.http.formvalue("data")
164 local stat = nil
165 local err = nil
166
167 if data then
168 stat, err = ffluci.fs.writefile(file, data)
169 end
170
171 local cnt = ffluci.fs.readfile(file)
172 if cnt then
173 cnt = ffluci.util.pcdata(cnt)
174 end
175
176 ffluci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})
177 end
178
179 function action_upgrade()
180 local ret = nil
181 local plat = ffluci.fs.mtime("/lib/upgrade/platform.sh")
182
183 local image = ffluci.http.formvalue("image")
184 local imgname = ffluci.http.formvalue("image_name")
185 local keepcfg = ffluci.http.formvalue("keepcfg")
186
187 if plat and imgname then
188 local kpattern = nil
189 if keepcfg then
190 local files = ffluci.model.uci.show("luci", "flash_keep")
191 if files.luci and files.luci.flash_keep then
192 kpattern = ""
193 for k,v in pairs(files.luci.flash_keep) do
194 kpattern = kpattern .. " " .. v
195 end
196 end
197 end
198 ret = ffluci.sys.flash(image, kpattern)
199 end
200
201 ffluci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret})
202 end