3aae6fa2f7b6a0d0efbf8225a4be7b7bff053a9a
[project/luci.git] / modules / admin-mini / luasrc / controller / mini / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
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.mini.system", package.seeall)
17
18 function index()
19         luci.i18n.loadc("admin-core")
20         local i18n = luci.i18n.translate
21
22         entry({"mini", "system"}, alias("mini", "system", "index"), i18n("system"), 40).index = true
23         entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), i18n("general"), 1)
24         entry({"mini", "system", "passwd"}, form("mini/passwd"), i18n("a_s_changepw"), 10)
25         entry({"mini", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 80)
26         entry({"mini", "system", "upgrade"}, call("action_upgrade"), i18n("admin_upgrade"), 90)
27         entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 100)
28 end
29
30 function action_backup()
31         local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
32         local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
33         local backup_cmd  = "tar -c %s | gzip 2>/dev/null"
34         
35         local restore_fpi 
36         luci.http.setfilehandler(
37                 function(meta, chunk, eof)
38                         if not restore_fpi then
39                                 restore_fpi = io.popen(restore_cmd, "w")
40                         end
41                         if chunk then
42                                 restore_fpi:write(chunk)
43                         end
44                         if eof then
45                                 restore_fpi:close()
46                         end
47                 end
48         )
49                   
50         local upload = luci.http.formvalue("archive")
51         local backup = luci.http.formvalue("backup")
52         local reset  = reset_avail and luci.http.formvalue("reset")
53         
54         if upload and #upload > 0 then
55                 luci.template.render("mini/applyreboot")
56                 luci.sys.reboot()
57         elseif backup then
58                 luci.util.perror(backup_cmd:format(_keep_pattern()))
59                 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
60                 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
61                         luci.sys.hostname(), os.date("%Y-%m-%d")})
62                 luci.http.prepare_content("application/x-targz")
63                 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
64         elseif reset then
65                 luci.template.render("mini/applyreboot")
66                 luci.util.exec("mtd -r erase rootfs_data")
67         else
68                 luci.template.render("mini/backup", {reset_avail = reset_avail})
69         end
70 end
71
72 function action_reboot()
73         local reboot = luci.http.formvalue("reboot")
74         luci.template.render("mini/reboot", {reboot=reboot})
75         if reboot then
76                 luci.sys.reboot()
77         end
78 end
79
80 function action_upgrade()
81         require("luci.model.uci")
82
83         local tmpfile = "/tmp/firmware.img"
84         
85         local function image_supported()
86                 -- XXX: yay...
87                 return ( 0 == os.execute(
88                         ". /etc/functions.sh; " ..
89                         "include /lib/upgrade; " ..
90                         "platform_check_image %q >/dev/null"
91                                 % tmpfile
92                 ) )
93         end
94         
95         local function image_checksum()
96                 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
97         end
98         
99         local function storage_size()
100                 local size = 0
101                 if luci.fs.access("/proc/mtd") then
102                         for l in io.lines("/proc/mtd") do
103                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
104                                 if n == "linux" then
105                                         size = tonumber(s, 16)
106                                         break
107                                 end
108                         end
109                 elseif luci.fs.access("/proc/partitions") then
110                         for l in io.lines("/proc/partitions") do
111                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
112                                 if b and n and not n:match('[0-9]') then
113                                         size = tonumber(b) * 1024
114                                         break
115                                 end
116                         end
117                 end
118                 return size
119         end
120
121
122         -- Install upload handler
123         local file
124         luci.http.setfilehandler(
125                 function(meta, chunk, eof)
126                         if not luci.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
127                                 file = io.open(tmpfile, "w")
128                         end
129                         if file and chunk then
130                                 file:write(chunk)
131                         end
132                         if file and eof then
133                                 file:close()
134                         end
135                 end
136         )
137
138
139         -- Determine state
140         local keep_avail   = true
141         local step         = tonumber(luci.http.formvalue("step") or 1)
142         local has_image    = luci.fs.access(tmpfile)
143         local has_support  = image_supported()
144         local has_platform = luci.fs.access("/lib/upgrade/platform.sh")
145         local has_upload   = luci.http.formvalue("image")
146         
147         -- This does the actual flashing which is invoked inside an iframe
148         -- so don't produce meaningful errors here because the the 
149         -- previous pages should arrange the stuff as required.
150         if step == 4 then
151                 if has_platform and has_image and has_support then
152                         -- Now invoke sysupgrade
153                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
154                         local fd = io.popen("/sbin/luci-flash %s %q" %{
155                                 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
156                         })
157
158                         if fd then
159                                 while true do
160                                         local ln = fd:read("*l")
161                                         if not ln then break end
162                                         luci.http.write(ln)
163                                 end
164                                 fd:close()
165                         end
166
167                         -- Make sure the device is rebooted
168                         luci.sys.reboot()
169                 end
170
171
172         --
173         -- This is step 1-3, which does the user interaction and
174         -- image upload.
175         --
176
177         -- Step 1: file upload, error on unsupported image format
178         elseif not has_image or not has_support or step == 1 then
179                 -- If there is an image but user has requested step 1
180                 -- or type is not supported, then remove it.
181                 if has_image then
182                         luci.fs.unlink(tmpfile)
183                 end
184                         
185                 luci.template.render("admin_system/upgrade", {
186                         step=1,
187                         bad_image=(has_image and not has_support or false),
188                         keepavail=keep_avail,
189                         supported=has_platform
190                 } )
191
192         -- Step 2: present uploaded file, show checksum, confirmation
193         elseif step == 2 then
194                 luci.template.render("admin_system/upgrade", {
195                         step=2,
196                         checksum=image_checksum(),
197                         filesize=luci.fs.stat(tmpfile).size,
198                         flashsize=storage_size(),
199                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
200                 } )
201         
202         -- Step 3: load iframe which calls the actual flash procedure
203         elseif step == 3 then
204                 luci.template.render("admin_system/upgrade", {
205                         step=3,
206                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
207                 } )
208         end     
209 end
210
211 function _keep_pattern()
212         local kpattern = ""
213         local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
214         if files then
215                 kpattern = ""
216                 for k, v in pairs(files) do
217                         if k:sub(1,1) ~= "." and luci.fs.glob(v) then
218                                 kpattern = kpattern .. " " ..  v
219                         end
220                 end
221         end
222         return kpattern
223 end