Include backup date and hostname in backup archive name
[project/luci.git] / modules / admin-full / luasrc / controller / admin / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 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 module("luci.controller.admin.system", package.seeall)
15
16 function index()
17         luci.i18n.loadc("admin-core")
18         local i18n = luci.i18n.translate
19         
20         entry({"admin", "system"}, alias("admin", "system", "system"), i18n("system"), 30).index = true
21         entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("system"), 1)
22         entry({"admin", "system", "packages"}, call("action_packages"), i18n("a_s_packages"), 10)
23         entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"), i18n("a_s_p_ipkg"))
24         entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("a_s_changepw"), 20)
25         entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("a_s_sshkeys"), 30)
26         entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("process_head"), 45)
27         entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
28         entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
29         entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
30         entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 80)
31         entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
32 end
33
34 function action_packages()
35         local ipkg = require("luci.model.ipkg")
36         local void = nil
37         local submit = luci.http.formvalue("submit")
38         
39         
40         -- Search query
41         local query = luci.http.formvalue("query")
42         query = (query ~= '') and query or nil
43         
44         
45         -- Packets to be installed
46         local install = submit and luci.http.formvaluetable("install")
47         
48         -- Install from URL
49         local url = luci.http.formvalue("url")
50         if url and url ~= '' and submit then
51                 if not install then
52                         install = {}
53                 end
54                 install[url] = 1
55         end
56         
57         -- Do install
58         if install then
59                 for k, v in pairs(install) do
60                         void, install[k] = ipkg.install(k)
61                 end
62         end
63         
64         
65         -- Remove packets
66         local remove = submit and luci.http.formvaluetable("remove")
67         if remove then  
68                 for k, v in pairs(remove) do
69                         void, remove[k] = ipkg.remove(k)
70                 end     
71         end
72         
73         
74         -- Update all packets
75         local update = luci.http.formvalue("update")
76         if update then
77                 void, update = ipkg.update()
78         end
79         
80         
81         -- Upgrade all packets
82         local upgrade = luci.http.formvalue("upgrade")
83         if upgrade then
84                 void, upgrade = ipkg.upgrade()
85         end
86         
87         
88         -- Package info
89         local info = luci.model.ipkg.info(query and "*"..query.."*")
90         info = info or {}
91         local pkgs = {}
92         
93         -- Sort after status and name
94         for k, v in pairs(info) do
95                 local x = 0
96                 for i, j in pairs(pkgs) do
97                         local vins = (v.Status and v.Status.installed)
98                         local jins = (j.Status and j.Status.installed)
99                         if vins ~= jins then
100                                 if vins then
101                                         break
102                                 end
103                         else
104                                 if j.Package > v.Package then
105                                         break
106                                 end
107                         end
108                         x = i
109                 end
110                 table.insert(pkgs, x+1, v)
111         end 
112         
113         luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
114          install=install, remove=remove, update=update, upgrade=upgrade})       
115 end
116
117 function action_backup()
118         local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
119         local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
120         local backup_cmd  = "tar -c %s | gzip 2>/dev/null"
121         
122         local restore_fpi 
123         luci.http.setfilehandler(
124                 function(meta, chunk, eof)
125                         if not restore_fpi then
126                                 restore_fpi = io.popen(restore_cmd, "w")
127                         end
128                         if chunk then
129                                 restore_fpi:write(chunk)
130                         end
131                         if eof then
132                                 restore_fpi:close()
133                         end
134                 end
135         )
136                   
137         local upload = luci.http.formvalue("archive")
138         local backup = luci.http.formvalue("backup")
139         local reset  = reset_avail and luci.http.formvalue("reset")
140         
141         if upload and #upload > 0 then
142                 luci.template.render("admin_system/applyreboot")
143                 luci.sys.reboot()
144         elseif backup then
145                 luci.util.perror(backup_cmd:format(_keep_pattern()))
146                 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
147                 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
148                         luci.sys.hostname(), os.date("%Y-%m-%d")})
149                 luci.http.prepare_content("application/x-targz")
150                 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
151         elseif reset then
152                 luci.template.render("admin_system/applyreboot")
153                 luci.util.exec("mtd -r erase rootfs_data")
154         else
155                 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
156         end
157 end
158
159 function action_passwd()
160         local p1 = luci.http.formvalue("pwd1")
161         local p2 = luci.http.formvalue("pwd2")
162         local stat = nil
163         
164         if p1 or p2 then
165                 if p1 == p2 then
166                         stat = luci.sys.user.setpasswd("root", p1)
167                 else
168                         stat = 10
169                 end
170         end
171         
172         luci.template.render("admin_system/passwd", {stat=stat})
173 end
174
175 function action_reboot()
176         local reboot = luci.http.formvalue("reboot")
177         luci.template.render("admin_system/reboot", {reboot=reboot})
178         if reboot then
179                 luci.sys.reboot()
180         end
181 end
182
183 function action_upgrade()
184         require("luci.model.uci")
185
186         local ret
187         local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
188         local tmpfile = "/tmp/firmware.img"
189         local keep_avail = true
190
191         local file
192         luci.http.setfilehandler(
193                 function(meta, chunk, eof)
194                         if not file then
195                                 file = io.open(tmpfile, "w")
196                         end
197                         if chunk then
198                                 file:write(chunk)
199                         end
200                         if eof then
201                                 file:close()
202                         end
203                 end
204         )
205
206         local fname   = luci.http.formvalue("image")
207         local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
208
209         if plat and fname then
210                 ret = function()
211                         return luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
212                 end
213         end
214
215         luci.http.prepare_content("text/html")
216         luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
217 end
218
219 function _keep_pattern()
220         local kpattern = ""
221         local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
222         if files then
223                 kpattern = ""
224                 for k, v in pairs(files) do
225                         if k:sub(1,1) ~= "." and luci.fs.glob(v) then
226                                 kpattern = kpattern .. " " ..  v
227                         end
228                 end
229         end
230         return kpattern
231 end