modules/admin-full: make software page tabbed, show available list broken down by...
[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
16 module("luci.controller.admin.system", package.seeall)
17
18 function index()
19         entry({"admin", "system"}, alias("admin", "system", "system"), _("System"), 30).index = true
20         entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
21         entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
22
23         if nixio.fs.access("/bin/opkg") then
24                 entry({"admin", "system", "packages"}, call("action_packages"), _("Software"), 10)
25                 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
26         end
27
28         entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
29
30         if nixio.fs.access("/etc/config/fstab") then
31                 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
32                 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
33                 entry({"admin", "system", "fstab", "swap"},  cbi("admin_system/fstab/swap"),  nil).leaf = true
34         end
35
36         if nixio.fs.access("/sys/class/leds") then
37                 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
38         end
39
40         entry({"admin", "system", "backup"}, call("action_backup"), _("Backup / Restore"), 70)
41         entry({"admin", "system", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 80)
42         entry({"admin", "system", "reboot"}, call("action_reboot"), _("Reboot"), 90)
43 end
44
45 function action_packages()
46         local ipkg = require("luci.model.ipkg")
47         local submit = luci.http.formvalue("submit")
48         local changes = false
49         local install = { }
50         local remove  = { }
51         local stdout  = { "" }
52         local stderr  = { "" }
53         local out, err
54
55         -- Display
56         local display = luci.http.formvalue("display") or "installed"
57
58         -- Letter
59         local letter = string.byte(luci.http.formvalue("letter") or "A", 1)
60         letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65
61
62         -- Search query
63         local query = luci.http.formvalue("query")
64         query = (query ~= '') and query or nil
65
66
67         -- Packets to be installed
68         local ninst = submit and luci.http.formvalue("install")
69         local uinst = nil
70
71         -- Install from URL
72         local url = luci.http.formvalue("url")
73         if url and url ~= '' and submit then
74                 uinst = url
75         end
76
77         -- Do install
78         if ninst then
79                 install[ninst], out, err = ipkg.install(ninst)
80                 stdout[#stdout+1] = out
81                 stderr[#stderr+1] = err
82                 changes = true
83         end
84
85         if uinst then
86                 install[uinst], out, err = ipkg.install(uinst)
87                 stdout[#stdout+1] = out
88                 stderr[#stderr+1] = err
89                 changes = true
90         end
91
92         -- Remove packets
93         local rem = submit and luci.http.formvalue("remove")
94         if rem then
95                 remove[rem], out, err = ipkg.remove(rem)
96                 stdout[#stdout+1] = out
97                 stderr[#stderr+1] = err
98                 changes = true
99         end
100
101
102         -- Update all packets
103         local update = luci.http.formvalue("update")
104         if update then
105                 update, out, err = ipkg.update()
106                 stdout[#stdout+1] = out
107                 stderr[#stderr+1] = err
108         end
109
110
111         -- Upgrade all packets
112         local upgrade = luci.http.formvalue("upgrade")
113         if upgrade then
114                 upgrade, out, err = ipkg.upgrade()
115                 stdout[#stdout+1] = out
116                 stderr[#stderr+1] = err
117         end
118
119
120         luci.template.render("admin_system/packages", {
121                 display = display,
122                 letter  = letter,
123                 query   = query,
124                 install = install,
125                 remove  = remove,
126                 update  = update,
127                 upgrade = upgrade,
128                 stdout  = table.concat(stdout, ""),
129                 stderr  = table.concat(stderr, "")
130         })
131
132         -- Remove index cache
133         if changes then
134                 nixio.fs.unlink("/tmp/luci-indexcache")
135         end
136 end
137
138 function action_backup()
139         local sys = require "luci.sys"
140         local fs  = require "luci.fs"
141
142         local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
143         local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
144         local backup_cmd  = "tar -czT %s 2>/dev/null"
145
146         local restore_fpi
147         luci.http.setfilehandler(
148                 function(meta, chunk, eof)
149                         if not restore_fpi then
150                                 restore_fpi = io.popen(restore_cmd, "w")
151                         end
152                         if chunk then
153                                 restore_fpi:write(chunk)
154                         end
155                         if eof then
156                                 restore_fpi:close()
157                         end
158                 end
159         )
160
161         local upload = luci.http.formvalue("archive")
162         local backup = luci.http.formvalue("backup")
163         local reset  = reset_avail and luci.http.formvalue("reset")
164
165         if upload and #upload > 0 then
166                 luci.template.render("admin_system/applyreboot")
167                 luci.sys.reboot()
168         elseif backup then
169                 local filelist = "/tmp/luci-backup-list.%d" % os.time()
170
171                 sys.call(
172                         "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
173                         "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
174                         "opkg list-changed-conffiles ) | sort -u > %s" % filelist
175                 )
176
177                 if fs.access(filelist) then
178                         local reader = ltn12_popen(backup_cmd:format(filelist))
179                         luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
180                                 luci.sys.hostname(), os.date("%Y-%m-%d")})
181                         luci.http.prepare_content("application/x-targz")
182                         luci.ltn12.pump.all(reader, luci.http.write)
183                         fs.unlink(filelist)
184                 end
185         elseif reset then
186                 luci.template.render("admin_system/applyreboot")
187                 luci.util.exec("mtd -r erase rootfs_data")
188         else
189                 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
190         end
191 end
192
193 function action_passwd()
194         local p1 = luci.http.formvalue("pwd1")
195         local p2 = luci.http.formvalue("pwd2")
196         local stat = nil
197
198         if p1 or p2 then
199                 if p1 == p2 then
200                         stat = luci.sys.user.setpasswd("root", p1)
201                 else
202                         stat = 10
203                 end
204         end
205
206         luci.template.render("admin_system/passwd", {stat=stat})
207 end
208
209 function action_reboot()
210         local reboot = luci.http.formvalue("reboot")
211         luci.template.render("admin_system/reboot", {reboot=reboot})
212         if reboot then
213                 luci.sys.reboot()
214         end
215 end
216
217 function action_upgrade()
218         require("luci.model.uci")
219
220         local tmpfile = "/tmp/firmware.img"
221
222         local function image_supported()
223                 -- XXX: yay...
224                 return ( 0 == os.execute(
225                         ". /etc/functions.sh; " ..
226                         "include /lib/upgrade; " ..
227                         "platform_check_image %q >/dev/null"
228                                 % tmpfile
229                 ) )
230         end
231
232         local function image_checksum()
233                 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
234         end
235
236         local function storage_size()
237                 local size = 0
238                 if nixio.fs.access("/proc/mtd") then
239                         for l in io.lines("/proc/mtd") do
240                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
241                                 if n == "linux" then
242                                         size = tonumber(s, 16)
243                                         break
244                                 end
245                         end
246                 elseif nixio.fs.access("/proc/partitions") then
247                         for l in io.lines("/proc/partitions") do
248                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
249                                 if b and n and not n:match('[0-9]') then
250                                         size = tonumber(b) * 1024
251                                         break
252                                 end
253                         end
254                 end
255                 return size
256         end
257
258
259         -- Install upload handler
260         local file
261         luci.http.setfilehandler(
262                 function(meta, chunk, eof)
263                         if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
264                                 file = io.open(tmpfile, "w")
265                         end
266                         if file and chunk then
267                                 file:write(chunk)
268                         end
269                         if file and eof then
270                                 file:close()
271                         end
272                 end
273         )
274
275
276         -- Determine state
277         local keep_avail   = true
278         local step         = tonumber(luci.http.formvalue("step") or 1)
279         local has_image    = nixio.fs.access(tmpfile)
280         local has_support  = image_supported()
281         local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
282         local has_upload   = luci.http.formvalue("image")
283
284         -- This does the actual flashing which is invoked inside an iframe
285         -- so don't produce meaningful errors here because the the
286         -- previous pages should arrange the stuff as required.
287         if step == 4 then
288                 if has_platform and has_image and has_support then
289                         -- Mimetype text/plain
290                         luci.http.prepare_content("text/plain")
291                         luci.http.write("Starting sysupgrade...\n")
292
293                         io.flush()
294
295                         -- Now invoke sysupgrade
296                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
297                         local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
298                                 keepcfg and "" or "-n", tmpfile
299                         })
300
301                         luci.ltn12.pump.all(flash, luci.http.write)
302                 end
303
304
305         --
306         -- This is step 1-3, which does the user interaction and
307         -- image upload.
308         --
309
310         -- Step 1: file upload, error on unsupported image format
311         elseif not has_image or not has_support or step == 1 then
312                 -- If there is an image but user has requested step 1
313                 -- or type is not supported, then remove it.
314                 if has_image then
315                         nixio.fs.unlink(tmpfile)
316                 end
317
318                 luci.template.render("admin_system/upgrade", {
319                         step=1,
320                         bad_image=(has_image and not has_support or false),
321                         keepavail=keep_avail,
322                         supported=has_platform
323                 } )
324
325         -- Step 2: present uploaded file, show checksum, confirmation
326         elseif step == 2 then
327                 luci.template.render("admin_system/upgrade", {
328                         step=2,
329                         checksum=image_checksum(),
330                         filesize=nixio.fs.stat(tmpfile).size,
331                         flashsize=storage_size(),
332                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
333                 } )
334
335         -- Step 3: load iframe which calls the actual flash procedure
336         elseif step == 3 then
337                 luci.template.render("admin_system/upgrade", {
338                         step=3,
339                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
340                 } )
341         end
342 end
343
344 function ltn12_popen(command)
345
346         local fdi, fdo = nixio.pipe()
347         local pid = nixio.fork()
348
349         if pid > 0 then
350                 fdo:close()
351                 local close
352                 return function()
353                         local buffer = fdi:read(2048)
354                         local wpid, stat = nixio.waitpid(pid, "nohang")
355                         if not close and wpid and stat == "exited" then
356                                 close = true
357                         end
358
359                         if buffer and #buffer > 0 then
360                                 return buffer
361                         elseif close then
362                                 fdi:close()
363                                 return nil
364                         end
365                 end
366         elseif pid == 0 then
367                 nixio.dup(fdo, nixio.stdout)
368                 fdi:close()
369                 fdo:close()
370                 nixio.exec("/bin/sh", "-c", command)
371         end
372 end