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