modules/admin-mini: merge new upgrade pages
[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                 end
110                 return size
111         end
112
113
114         -- Install upload handler
115         local file
116         luci.http.setfilehandler(
117                 function(meta, chunk, eof)
118                         if not luci.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
119                                 file = io.open(tmpfile, "w")
120                         end
121                         if file and chunk then
122                                 file:write(chunk)
123                         end
124                         if file and eof then
125                                 file:close()
126                         end
127                 end
128         )
129
130
131         -- Determine state
132         local keep_avail   = true
133         local step         = tonumber(luci.http.formvalue("step") or 1)
134         local has_image    = luci.fs.access(tmpfile)
135         local has_support  = image_supported()
136         local has_platform = luci.fs.access("/lib/upgrade/platform.sh")
137         local has_upload   = luci.http.formvalue("image")
138         
139         -- This does the actual flashing which is invoked inside an iframe
140         -- so don't produce meaningful errors here because the the 
141         -- previous pages should arrange the stuff as required.
142         if step == 4 then
143                 if has_platform and has_image and has_support then
144                         -- Next line is to bypass luci.http layer
145                         luci.http.context.eoh = true
146
147                         -- Now invoke sysupgrade
148                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
149                         os.execute("/sbin/luci-flash %s %q" %{
150                                 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
151                         })
152
153                         -- Make sure the device is rebooted
154                         luci.sys.reboot()
155                 end
156
157
158         --
159         -- This is step 1-3, which does the user interaction and
160         -- image upload.
161         --
162
163         -- Step 1: file upload, error on unsupported image format
164         elseif not has_image or not has_support or step == 1 then
165                 -- If there is an image but user has requested step 1
166                 -- or type is not supported, then remove it.
167                 if has_image then
168                         luci.fs.unlink(tmpfile)
169                 end
170                         
171                 luci.template.render("admin_system/upgrade", {
172                         step=1,
173                         bad_image=(has_image and not has_support or false),
174                         keepavail=keep_avail,
175                         supported=has_platform
176                 } )
177
178         -- Step 2: present uploaded file, show checksum, confirmation
179         elseif step == 2 then
180                 luci.template.render("admin_system/upgrade", {
181                         step=2,
182                         checksum=image_checksum(),
183                         filesize=luci.fs.stat(tmpfile).size,
184                         flashsize=storage_size(),
185                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
186                 } )
187         
188         -- Step 3: load iframe which calls the actual flash procedure
189         elseif step == 3 then
190                 luci.template.render("admin_system/upgrade", {
191                         step=3,
192                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
193                 } )
194         end     
195 end
196
197 function _keep_pattern()
198         local kpattern = ""
199         local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
200         if files then
201                 kpattern = ""
202                 for k, v in pairs(files) do
203                         if k:sub(1,1) ~= "." and luci.fs.glob(v) then
204                                 kpattern = kpattern .. " " ..  v
205                         end
206                 end
207         end
208         return kpattern
209 end