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