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