modules/admin-full: merge rewritten package management from luci-0.9
[project/luci.git] / modules / admin-full / luasrc / controller / admin / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15 module("luci.controller.admin.system", package.seeall)
16
17 function index()
18 luci.i18n.loadc("base")
19 local i18n = luci.i18n.translate
20
21 entry({"admin", "system"}, alias("admin", "system", "system"), i18n("System"), 30).index = true
22 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("System"), 1)
23 entry({"admin", "system", "packages"}, call("action_packages"), i18n("Software"), 10)
24 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
25 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("Admin Password"), 20)
26 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("<abbr title=\"Secure Shell\">SSH</abbr>-Keys"), 30)
27 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("Processes"), 45)
28
29 if nixio.fs.access("/etc/config/fstab") then
30 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("Mount Points"), 50)
31 end
32
33 if nixio.fs.access("/sys/class/leds") then
34 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
35 end
36
37 entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
38 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
39 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
40 end
41
42 function action_packages()
43 local ipkg = require("luci.model.ipkg")
44 local submit = luci.http.formvalue("submit")
45 local changes = false
46 local install = { }
47 local remove = { }
48
49 -- Search query
50 local query = luci.http.formvalue("query")
51 query = (query ~= '') and query or nil
52
53
54 -- Packets to be installed
55 local ninst = submit and luci.http.formvalue("install")
56 local uinst = nil
57
58 -- Install from URL
59 local url = luci.http.formvalue("url")
60 if url and url ~= '' and submit then
61 uinst = url
62 end
63
64 -- Do install
65 if ninst then
66 _, install[ninst] = ipkg.install(ninst)
67 changes = true
68 end
69
70 if uinst then
71 _, install[uinst] = ipkg.install(uinst)
72 changes = true
73 end
74
75 -- Remove packets
76 local rem = submit and luci.http.formvalue("remove")
77 if rem then
78 _, remove[rem] = ipkg.remove(rem)
79 changes = true
80 end
81
82
83 -- Update all packets
84 local update = luci.http.formvalue("update")
85 if update then
86 _, update = ipkg.update()
87 end
88
89
90 -- Upgrade all packets
91 local upgrade = luci.http.formvalue("upgrade")
92 if upgrade then
93 _, upgrade = ipkg.upgrade()
94 end
95
96
97 luci.template.render("admin_system/packages", {
98 query=query, install=install, remove=remove, update=update, upgrade=upgrade
99 })
100
101 -- Remove index cache
102 if changes then
103 nixio.fs.unlink("/tmp/luci-indexcache")
104 end
105 end
106
107 function action_backup()
108 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
109 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
110 local backup_cmd = "tar -cz %s 2>/dev/null"
111
112 local restore_fpi
113 luci.http.setfilehandler(
114 function(meta, chunk, eof)
115 if not restore_fpi then
116 restore_fpi = io.popen(restore_cmd, "w")
117 end
118 if chunk then
119 restore_fpi:write(chunk)
120 end
121 if eof then
122 restore_fpi:close()
123 end
124 end
125 )
126
127 local upload = luci.http.formvalue("archive")
128 local backup = luci.http.formvalue("backup")
129 local reset = reset_avail and luci.http.formvalue("reset")
130
131 if upload and #upload > 0 then
132 luci.template.render("admin_system/applyreboot")
133 luci.sys.reboot()
134 elseif backup then
135 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
136 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
137 luci.sys.hostname(), os.date("%Y-%m-%d")})
138 luci.http.prepare_content("application/x-targz")
139 luci.ltn12.pump.all(reader, luci.http.write)
140 elseif reset then
141 luci.template.render("admin_system/applyreboot")
142 luci.util.exec("mtd -r erase rootfs_data")
143 else
144 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
145 end
146 end
147
148 function action_passwd()
149 local p1 = luci.http.formvalue("pwd1")
150 local p2 = luci.http.formvalue("pwd2")
151 local stat = nil
152
153 if p1 or p2 then
154 if p1 == p2 then
155 stat = luci.sys.user.setpasswd("root", p1)
156 else
157 stat = 10
158 end
159 end
160
161 luci.template.render("admin_system/passwd", {stat=stat})
162 end
163
164 function action_reboot()
165 local reboot = luci.http.formvalue("reboot")
166 luci.template.render("admin_system/reboot", {reboot=reboot})
167 if reboot then
168 luci.sys.reboot()
169 end
170 end
171
172 function action_upgrade()
173 require("luci.model.uci")
174
175 local tmpfile = "/tmp/firmware.img"
176
177 local function image_supported()
178 -- XXX: yay...
179 return ( 0 == os.execute(
180 ". /etc/functions.sh; " ..
181 "include /lib/upgrade; " ..
182 "platform_check_image %q >/dev/null"
183 % tmpfile
184 ) )
185 end
186
187 local function image_checksum()
188 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
189 end
190
191 local function storage_size()
192 local size = 0
193 if nixio.fs.access("/proc/mtd") then
194 for l in io.lines("/proc/mtd") do
195 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
196 if n == "linux" then
197 size = tonumber(s, 16)
198 break
199 end
200 end
201 elseif nixio.fs.access("/proc/partitions") then
202 for l in io.lines("/proc/partitions") do
203 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
204 if b and n and not n:match('[0-9]') then
205 size = tonumber(b) * 1024
206 break
207 end
208 end
209 end
210 return size
211 end
212
213
214 -- Install upload handler
215 local file
216 luci.http.setfilehandler(
217 function(meta, chunk, eof)
218 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
219 file = io.open(tmpfile, "w")
220 end
221 if file and chunk then
222 file:write(chunk)
223 end
224 if file and eof then
225 file:close()
226 end
227 end
228 )
229
230
231 -- Determine state
232 local keep_avail = true
233 local step = tonumber(luci.http.formvalue("step") or 1)
234 local has_image = nixio.fs.access(tmpfile)
235 local has_support = image_supported()
236 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
237 local has_upload = luci.http.formvalue("image")
238
239 -- This does the actual flashing which is invoked inside an iframe
240 -- so don't produce meaningful errors here because the the
241 -- previous pages should arrange the stuff as required.
242 if step == 4 then
243 if has_platform and has_image and has_support then
244 -- Mimetype text/plain
245 luci.http.prepare_content("text/plain")
246 luci.http.write("Starting luci-flash...\n")
247
248 -- Now invoke sysupgrade
249 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
250 local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
251 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
252 })
253
254 luci.ltn12.pump.all(flash, luci.http.write)
255
256 -- Make sure the device is rebooted
257 luci.sys.reboot()
258 end
259
260
261 --
262 -- This is step 1-3, which does the user interaction and
263 -- image upload.
264 --
265
266 -- Step 1: file upload, error on unsupported image format
267 elseif not has_image or not has_support or step == 1 then
268 -- If there is an image but user has requested step 1
269 -- or type is not supported, then remove it.
270 if has_image then
271 nixio.fs.unlink(tmpfile)
272 end
273
274 luci.template.render("admin_system/upgrade", {
275 step=1,
276 bad_image=(has_image and not has_support or false),
277 keepavail=keep_avail,
278 supported=has_platform
279 } )
280
281 -- Step 2: present uploaded file, show checksum, confirmation
282 elseif step == 2 then
283 luci.template.render("admin_system/upgrade", {
284 step=2,
285 checksum=image_checksum(),
286 filesize=nixio.fs.stat(tmpfile).size,
287 flashsize=storage_size(),
288 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
289 } )
290
291 -- Step 3: load iframe which calls the actual flash procedure
292 elseif step == 3 then
293 luci.template.render("admin_system/upgrade", {
294 step=3,
295 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
296 } )
297 end
298 end
299
300 function _keep_pattern()
301 local kpattern = ""
302 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
303 if files then
304 kpattern = ""
305 for k, v in pairs(files) do
306 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
307 kpattern = kpattern .. " " .. v
308 end
309 end
310 end
311 return kpattern
312 end
313
314 function ltn12_popen(command)
315
316 local fdi, fdo = nixio.pipe()
317 local pid = nixio.fork()
318
319 if pid > 0 then
320 fdo:close()
321 local close
322 return function()
323 local buffer = fdi:read(2048)
324 local wpid, stat = nixio.waitpid(pid, "nohang")
325 if not close and wpid and stat == "exited" then
326 close = true
327 end
328
329 if buffer and #buffer > 0 then
330 return buffer
331 elseif close then
332 fdi:close()
333 return nil
334 end
335 end
336 elseif pid == 0 then
337 nixio.dup(fdo, nixio.stdout)
338 fdi:close()
339 fdo:close()
340 nixio.exec("/bin/sh", "-c", command)
341 end
342 end