f86fa04042aa3d3ca70aa866886a28efee5b4c84
[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("admin-core")
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("a_s_packages"), 10)
24         entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
25         entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("a_s_changepw"), 20)
26         entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("a_s_sshkeys"), 30)
27         entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("process_head"), 45)
28         entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
29
30         if nixio.fs.access("/sys/class/leds") then
31                 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
32         end
33
34         entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
35         entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("admin_upgrade"), 80)
36         entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
37 end
38
39 function action_packages()
40         local ipkg = require("luci.model.ipkg")
41         local submit = luci.http.formvalue("submit")
42         local changes = false
43         local install = { }
44         local remove  = { }
45
46         -- Search query
47         local query = luci.http.formvalue("query")
48         query = (query ~= '') and query or nil
49         
50         
51         -- Packets to be installed
52         local ninst = submit and luci.http.formvalue("install")
53         local uinst = nil       
54
55         -- Install from URL
56         local url = luci.http.formvalue("url")
57         if url and url ~= '' and submit then
58                 uinst = url
59         end
60
61         -- Do install
62         if ninst then
63                 _, install[ninst] = ipkg.install(ninst)
64                 changes = true
65         end
66
67         if uinst then
68                 _, install[uinst] = ipkg.install(uinst)
69                 changes = true
70         end
71
72         -- Remove packets
73         local rem = submit and luci.http.formvalue("remove")
74         if rem then     
75                 _, remove[rem] = ipkg.remove(rem)
76                 changes = true
77         end
78
79         
80         -- Update all packets
81         local update = luci.http.formvalue("update")
82         if update then
83                 _, update = ipkg.update()
84         end
85         
86         
87         -- Upgrade all packets
88         local upgrade = luci.http.formvalue("upgrade")
89         if upgrade then
90                 _, upgrade = ipkg.upgrade()
91         end
92
93         
94         luci.template.render("admin_system/packages", {
95                 query=query, install=install, remove=remove, update=update, upgrade=upgrade
96         })
97  
98         -- Remove index cache
99         if changes then
100                 nixio.fs.unlink("/tmp/luci-indexcache")
101         end     
102 end
103
104 function action_backup()
105         local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
106         local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
107         local backup_cmd  = "tar -c %s | gzip 2>/dev/null"
108         
109         local restore_fpi 
110         luci.http.setfilehandler(
111                 function(meta, chunk, eof)
112                         if not restore_fpi then
113                                 restore_fpi = io.popen(restore_cmd, "w")
114                         end
115                         if chunk then
116                                 restore_fpi:write(chunk)
117                         end
118                         if eof then
119                                 restore_fpi:close()
120                         end
121                 end
122         )
123                   
124         local upload = luci.http.formvalue("archive")
125         local backup = luci.http.formvalue("backup")
126         local reset  = reset_avail and luci.http.formvalue("reset")
127         
128         if upload and #upload > 0 then
129                 luci.template.render("admin_system/applyreboot")
130                 luci.sys.reboot()
131         elseif backup then
132                 luci.util.perror(backup_cmd:format(_keep_pattern()))
133                 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
134                 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
135                         luci.sys.hostname(), os.date("%Y-%m-%d")})
136                 luci.http.prepare_content("application/x-targz")
137                 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
138         elseif reset then
139                 luci.template.render("admin_system/applyreboot")
140                 luci.util.exec("mtd -r erase rootfs_data")
141         else
142                 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
143         end
144 end
145
146 function action_passwd()
147         local p1 = luci.http.formvalue("pwd1")
148         local p2 = luci.http.formvalue("pwd2")
149         local stat = nil
150         
151         if p1 or p2 then
152                 if p1 == p2 then
153                         stat = luci.sys.user.setpasswd("root", p1)
154                 else
155                         stat = 10
156                 end
157         end
158         
159         luci.template.render("admin_system/passwd", {stat=stat})
160 end
161
162 function action_reboot()
163         local reboot = luci.http.formvalue("reboot")
164         luci.template.render("admin_system/reboot", {reboot=reboot})
165         if reboot then
166                 luci.sys.reboot()
167         end
168 end
169
170 function action_upgrade()
171         require("luci.model.uci")
172
173         local tmpfile = "/tmp/firmware.img"
174         
175         local function image_supported()
176                 -- XXX: yay...
177                 return ( 0 == os.execute(
178                         ". /etc/functions.sh; " ..
179                         "include /lib/upgrade; " ..
180                         "platform_check_image %q >/dev/null"
181                                 % tmpfile
182                 ) )
183         end
184         
185         local function image_checksum()
186                 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
187         end
188         
189         local function storage_size()
190                 local size = 0
191                 if nixio.fs.access("/proc/mtd") then
192                         for l in io.lines("/proc/mtd") do
193                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
194                                 if n == "linux" then
195                                         size = tonumber(s, 16)
196                                         break
197                                 end
198                         end
199                 elseif nixio.fs.access("/proc/partitions") then
200                         for l in io.lines("/proc/partitions") do
201                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
202                                 if b and n and not n:match('[0-9]') then
203                                         size = tonumber(b) * 1024
204                                         break
205                                 end
206                         end
207                 end
208                 return size
209         end
210
211
212         -- Install upload handler
213         local file
214         luci.http.setfilehandler(
215                 function(meta, chunk, eof)
216                         if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
217                                 file = io.open(tmpfile, "w")
218                         end
219                         if file and chunk then
220                                 file:write(chunk)
221                         end
222                         if file and eof then
223                                 file:close()
224                         end
225                 end
226         )
227
228
229         -- Determine state
230         local keep_avail   = true
231         local step         = tonumber(luci.http.formvalue("step") or 1)
232         local has_image    = nixio.fs.access(tmpfile)
233         local has_support  = image_supported()
234         local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
235         local has_upload   = luci.http.formvalue("image")
236         
237         -- This does the actual flashing which is invoked inside an iframe
238         -- so don't produce meaningful errors here because the the 
239         -- previous pages should arrange the stuff as required.
240         if step == 4 then
241                 if has_platform and has_image and has_support then
242                         -- Mimetype text/plain
243                         luci.http.prepare_content("text/plain")
244
245                         -- Now invoke sysupgrade
246                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
247                         local fd = io.popen("/sbin/luci-flash %s %q" %{
248                                 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
249                         })
250
251                         if fd then
252                                 while true do
253                                         local ln = fd:read("*l")
254                                         if not ln then break end
255                                         luci.http.write(ln .. "\n")
256                                 end
257                                 fd:close()
258                         end
259
260                         -- Make sure the device is rebooted
261                         luci.sys.reboot()
262                 end
263
264
265         --
266         -- This is step 1-3, which does the user interaction and
267         -- image upload.
268         --
269
270         -- Step 1: file upload, error on unsupported image format
271         elseif not has_image or not has_support or step == 1 then
272                 -- If there is an image but user has requested step 1
273                 -- or type is not supported, then remove it.
274                 if has_image then
275                         nixio.fs.unlink(tmpfile)
276                 end
277                         
278                 luci.template.render("admin_system/upgrade", {
279                         step=1,
280                         bad_image=(has_image and not has_support or false),
281                         keepavail=keep_avail,
282                         supported=has_platform
283                 } )
284
285         -- Step 2: present uploaded file, show checksum, confirmation
286         elseif step == 2 then
287                 luci.template.render("admin_system/upgrade", {
288                         step=2,
289                         checksum=image_checksum(),
290                         filesize=nixio.fs.stat(tmpfile).size,
291                         flashsize=storage_size(),
292                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
293                 } )
294         
295         -- Step 3: load iframe which calls the actual flash procedure
296         elseif step == 3 then
297                 luci.template.render("admin_system/upgrade", {
298                         step=3,
299                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
300                 } )
301         end     
302 end
303
304 function _keep_pattern()
305         local kpattern = ""
306         local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
307         if files then
308                 kpattern = ""
309                 for k, v in pairs(files) do
310                         if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
311                                 kpattern = kpattern .. " " ..  v
312                         end
313                 end
314         end
315         return kpattern
316 end