1a26280725558c72e7864f3e048b6c487c781598
[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("a_s_flash"), 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 ret = nil
84 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
85 local tmpfile = "/tmp/firmware.img"
86 local keep_avail = true
87
88 local file
89 luci.http.setfilehandler(
90 function(meta, chunk, eof)
91 if not file then
92 file = io.open(tmpfile, "w")
93 end
94 if chunk then
95 file:write(chunk)
96 end
97 if eof then
98 file:close()
99 end
100 end
101 )
102
103 local fname = luci.http.formvalue("image")
104 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
105
106 if plat and fname then
107 ret = function()
108 return luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
109 end
110 end
111
112 luci.http.prepare_content("text/html")
113 luci.template.render("mini/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
114 end
115
116 function _keep_pattern()
117 local kpattern = ""
118 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
119 if files then
120 kpattern = ""
121 for k, v in pairs(files) do
122 if k:sub(1,1) ~= "." and luci.fs.glob(v) then
123 kpattern = kpattern .. " " .. v
124 end
125 end
126 end
127 return kpattern
128 end