NIU: More pages
[project/luci.git] / modules / niu / luasrc / controller / niu / system.lua
1 --[[
2 LuCI - Lua Development Framework
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 local require, pairs, unpack, tonumber = require, pairs, unpack, tonumber
16 module "luci.controller.niu.system"
17
18 function index()
19         entry({"niu", "system"}, nil, "System", 20).dbtemplate = "niu/system"
20
21         entry({"niu", "system", "general"}, 
22         cbi("niu/system/general", {on_success_to={"niu"}}), "Configure Device", 10)
23         
24         entry({"niu", "system", "backup"}, call("backup"), "Backup or Restore Settings", 20)
25         entry({"niu", "system", "upgrade"}, call("upgrade"), "Upgrade Firmware", 40)
26 end
27
28 function backup()
29         local dsp = require "luci.dispatcher"
30         local os, io = require "os", require "io"
31         local uci = require "luci.model.uci".inst
32         local nixio, nutl = require "nixio", require "nixio.util"
33         local fs = require "nixio.fs"
34         local http = require "luci.http"
35         local tpl = require "luci.template"
36         
37         local restore_fpi 
38         http.setfilehandler(
39                 function(meta, chunk, eof)
40                         if not restore_fpi then
41                                 restore_fpi = io.popen("tar -xzC/ >/dev/null 2>&1", "w")
42                         end
43                         if chunk then
44                                 restore_fpi:write(chunk)
45                         end
46                         if eof then
47                                 restore_fpi:close()
48                         end
49                 end
50         )
51         
52         local reset_avail = (fs.readfile("/proc/mtd") or ""):find('"rootfs_data"')
53         local upload = http.formvalue("archive")
54         local backup = http.formvalue("backup")
55         local reset  = reset_avail and http.formvalue("reset")
56         local backup_cmd  = "tar -cz %s 2>/dev/null"
57         
58         if http.formvalue("cancel") then
59                 return http.redirect(dsp.build_url("niu"))
60         end
61         
62         if backup then
63                 local call = {"/bin/tar", "-cz"}
64                 for k, v in pairs(uci:get_all("luci", "flash_keep")) do
65                         if k:byte() ~= 46 then  -- k[1] ~= "."
66                                 nutl.consume(fs.glob(v), call)
67                         end
68                 end
69                 
70                 
71                 http.header(
72                         'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
73                                 nixio.uname().nodename, os.date("%Y-%m-%d")
74                         }
75                 )
76                 http.prepare_content("application/x-targz")
77                 
78                 
79                 local fdin, fdout = nixio.pipe()
80                 local devnull = nixio.open("/dev/null", "r+")
81                 local proc = nixio.fork()
82                 
83                 if proc == 0 then
84                         fdin:close()
85                         nixio.dup(devnull, nixio.stdin)
86                         nixio.dup(devnull, nixio.stderr)
87                         nixio.dup(fdout, nixio.stdout)
88                         nixio.exec(unpack(call))
89                         os.exit(1)
90                 end
91                 
92                 fdout:close()
93                 http.splice(fdin)
94                 http.close()
95         elseif (upload and #upload > 0) or reset then
96                 tpl.render("niu/system/reboot")
97                 if nixio.fork() == 0 then
98                         nixio.nanosleep(1)
99                         if reset then
100                                 nixio.execp("mtd", "-r", "erase", "rootfs_data")
101                         else
102                                 nixio.execp("reboot")
103                         end
104                         os.exit(1)
105                 end
106         else
107                 tpl.render("niu/system/backup", {reset_avail = reset_avail})
108         end
109 end
110
111 function upgrade()
112         local io, os, table = require "io", require "os", require "table"
113         local uci = require "luci.store".uci_state
114         local http = require "luci.http"
115         local util = require "luci.util"
116         local tpl = require "luci.template"
117         local nixio = require "nixio", require "nixio.util", require "nixio.fs"
118
119
120         local tmpfile = "/tmp/firmware.img"
121         
122         local function image_supported()
123                 -- XXX: yay...
124                 return ( 0 == os.execute(
125                         ". /etc/functions.sh; " ..
126                         "include /lib/upgrade; " ..
127                         "platform_check_image %q >/dev/null"
128                                 % tmpfile
129                 ) )
130         end
131         
132         local function image_checksum()
133                 return (util.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
134         end
135         
136         local function storage_size()
137                 local size = 0
138                 if nixio.fs.access("/proc/mtd") then
139                         for l in io.lines("/proc/mtd") do
140                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
141                                 if n == "linux" then
142                                         size = tonumber(s, 16)
143                                         break
144                                 end
145                         end
146                 elseif nixio.fs.access("/proc/partitions") then
147                         for l in io.lines("/proc/partitions") do
148                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
149                                 if b and n and not n:match('[0-9]') then
150                                         size = tonumber(b) * 1024
151                                         break
152                                 end
153                         end
154                 end
155                 return size
156         end
157
158
159         -- Install upload handler
160         local file
161         http.setfilehandler(
162                 function(meta, chunk, eof)
163                         if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
164                                 file = io.open(tmpfile, "w")
165                         end
166                         if file and chunk then
167                                 file:write(chunk)
168                         end
169                         if file and eof then
170                                 file:close()
171                         end
172                 end
173         )
174
175
176         -- Determine state
177         local keep_avail   = true
178         local step         = tonumber(http.formvalue("step") or 1)
179         local has_image    = nixio.fs.access(tmpfile)
180         local has_support  = image_supported()
181         local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
182         local has_upload   = http.formvalue("image")
183         
184         -- This does the actual flashing which is invoked inside an iframe
185         -- so don't produce meaningful errors here because the the 
186         -- previous pages should arrange the stuff as required.
187         if step == 4 then
188                 if has_platform and has_image and has_support then
189                         -- Mimetype text/plain
190                         http.prepare_content("text/plain")
191                         
192                         local call = {}
193                         for k, v in pairs(uci:get_all("luci", "flash_keep")) do
194                                 if k:byte() ~= 46 then  -- k[1] ~= "."
195                                         nixio.util.consume(nixio.fs.glob(v), call)
196                                 end
197                         end
198
199                         -- Now invoke sysupgrade
200                         local keepcfg = keep_avail and http.formvalue("keepcfg") == "1"
201                         local fd = io.popen("/sbin/luci-flash %s %q" %{
202                                 keepcfg and "-k %q" % table.concat(call, " ") or "", tmpfile
203                         })
204
205                         if fd then
206                                 while true do
207                                         local ln = fd:read("*l")
208                                         if not ln then break end
209                                         http.write(ln .. "\n")
210                                 end
211                                 fd:close()
212                         end
213
214                         -- Make sure the device is rebooted
215                         if nixio.fork() == 0 then
216                                 nixio.nanosleep(1)
217                                 nixio.execp("reboot")
218                                 os.exit(1)
219                         end
220                 end
221
222
223         --
224         -- This is step 1-3, which does the user interaction and
225         -- image upload.
226         --
227
228         -- Step 1: file upload, error on unsupported image format
229         elseif not has_image or not has_support or step == 1 then
230                 -- If there is an image but user has requested step 1
231                 -- or type is not supported, then remove it.
232                 if has_image then
233                         nixio.fs.unlink(tmpfile)
234                 end
235                         
236                 tpl.render("niu/system/upgrade", {
237                         step=1,
238                         bad_image=(has_image and not has_support or false),
239                         keepavail=keep_avail,
240                         supported=has_platform
241                 } )
242
243         -- Step 2: present uploaded file, show checksum, confirmation
244         elseif step == 2 then
245                 tpl.render("niu/system/upgrade", {
246                         step=2,
247                         checksum=image_checksum(),
248                         filesize=nixio.fs.stat(tmpfile).size,
249                         flashsize=storage_size(),
250                         keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")
251                 } )
252         
253         -- Step 3: load iframe which calls the actual flash procedure
254         elseif step == 3 then
255                 tpl.render("niu/system/upgrade", {
256                         step=3,
257                         keepconfig=(keep_avail and http.formvalue("keepcfg") == "1")
258                 } )
259         end     
260 end