all: change most translate statements to new format, some need manual cleanup
[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("Admin Password"), 10)
25         entry({"mini", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 80)
26         entry({"mini", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 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 nixio.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 nixio.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 nixio.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    = nixio.fs.access(tmpfile)
143         local has_support  = image_supported()
144         local has_platform = nixio.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                         -- Mimetype text/plain
153                         luci.http.prepare_content("text/plain")
154
155                         -- Now invoke sysupgrade
156                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
157                         local fd = io.popen("/sbin/luci-flash %s %q" %{
158                                 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
159                         })
160
161                         if fd then
162                                 while true do
163                                         local ln = fd:read("*l")
164                                         if not ln then break end
165                                         luci.http.write(ln .. "\n")
166                                 end
167                                 fd:close()
168                         end
169
170                         -- Make sure the device is rebooted
171                         luci.sys.reboot()
172                 end
173
174
175         --
176         -- This is step 1-3, which does the user interaction and
177         -- image upload.
178         --
179
180         -- Step 1: file upload, error on unsupported image format
181         elseif not has_image or not has_support or step == 1 then
182                 -- If there is an image but user has requested step 1
183                 -- or type is not supported, then remove it.
184                 if has_image then
185                         nixio.fs.unlink(tmpfile)
186                 end
187                         
188                 luci.template.render("admin_system/upgrade", {
189                         step=1,
190                         bad_image=(has_image and not has_support or false),
191                         keepavail=keep_avail,
192                         supported=has_platform
193                 } )
194
195         -- Step 2: present uploaded file, show checksum, confirmation
196         elseif step == 2 then
197                 luci.template.render("admin_system/upgrade", {
198                         step=2,
199                         checksum=image_checksum(),
200                         filesize=nixio.fs.stat(tmpfile).size,
201                         flashsize=storage_size(),
202                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
203                 } )
204         
205         -- Step 3: load iframe which calls the actual flash procedure
206         elseif step == 3 then
207                 luci.template.render("admin_system/upgrade", {
208                         step=3,
209                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
210                 } )
211         end     
212 end
213
214 function _keep_pattern()
215         local kpattern = ""
216         local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
217         if files then
218                 kpattern = ""
219                 for k, v in pairs(files) do
220                         if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
221                                 kpattern = kpattern .. " " ..  v
222                         end
223                 end
224         end
225         return kpattern
226 end