all: remove references to old i18n files
[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 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
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 module("luci.controller.admin.system", package.seeall)
16
17 function index()
18 luci.i18n.loadc("base")
19 local i18n = luci.i18n.translate
20
21 entry({"admin", "system"}, alias("admin", "system", "system"), i18n("System"), 30).index = true
22 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("System"), 1)
23 entry({"admin", "system", "packages"}, call("action_packages"), i18n("Software"), 10)
24 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
25 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("Admin Password"), 20)
26 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("<abbr title=\"Secure Shell\">SSH</abbr>-Keys"), 30)
27 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("Processes"), 45)
28 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("Mount Points"), 50)
29
30 if nixio.fs.access("/sys/class/leds") then
31 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
32 end
33
34 entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
35 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
36 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
37 end
38
39 function action_packages()
40 local ipkg = require("luci.model.ipkg")
41 local void = nil
42 local submit = luci.http.formvalue("submit")
43 local changes = false
44
45
46 -- Search query
47 local query = luci.http.formvalue("query")
48 query = (query ~= '') and query or nil
49
50
51 -- Packets to be installed
52 local install = submit and luci.http.formvaluetable("install")
53
54 -- Install from URL
55 local url = luci.http.formvalue("url")
56 if url and url ~= '' and submit then
57 if not install then
58 install = {}
59 end
60 install[url] = 1
61 changes = true
62 end
63
64 -- Do install
65 if install then
66 for k, v in pairs(install) do
67 void, install[k] = ipkg.install(k)
68 end
69 changes = true
70 end
71
72
73 -- Remove packets
74 local remove = submit and luci.http.formvaluetable("remove")
75 if remove then
76 for k, v in pairs(remove) do
77 void, remove[k] = ipkg.remove(k)
78 end
79 changes = true
80 end
81
82
83 -- Update all packets
84 local update = luci.http.formvalue("update")
85 if update then
86 void, update = ipkg.update()
87 end
88
89
90 -- Upgrade all packets
91 local upgrade = luci.http.formvalue("upgrade")
92 if upgrade then
93 void, upgrade = ipkg.upgrade()
94 end
95
96
97 -- Package info
98 local info = luci.model.ipkg.info(query and "*"..query.."*")
99 info = info or {}
100 local pkgs = {}
101
102 -- Sort after status and name
103 for k, v in pairs(info) do
104 local x = 0
105 for i, j in pairs(pkgs) do
106 local vins = (v.Status and v.Status.installed)
107 local jins = (j.Status and j.Status.installed)
108 if vins ~= jins then
109 if vins then
110 break
111 end
112 else
113 if j.Package > v.Package then
114 break
115 end
116 end
117 x = i
118 end
119 table.insert(pkgs, x+1, v)
120 end
121
122 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
123 install=install, remove=remove, update=update, upgrade=upgrade})
124
125 -- Remove index cache
126 if changes then
127 nixio.fs.unlink("/tmp/luci-indexcache")
128 end
129 end
130
131 function action_backup()
132 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
133 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
134 local backup_cmd = "tar -cz %s 2>/dev/null"
135
136 local restore_fpi
137 luci.http.setfilehandler(
138 function(meta, chunk, eof)
139 if not restore_fpi then
140 restore_fpi = io.popen(restore_cmd, "w")
141 end
142 if chunk then
143 restore_fpi:write(chunk)
144 end
145 if eof then
146 restore_fpi:close()
147 end
148 end
149 )
150
151 local upload = luci.http.formvalue("archive")
152 local backup = luci.http.formvalue("backup")
153 local reset = reset_avail and luci.http.formvalue("reset")
154
155 if upload and #upload > 0 then
156 luci.template.render("admin_system/applyreboot")
157 luci.sys.reboot()
158 elseif backup then
159 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
160 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
161 luci.sys.hostname(), os.date("%Y-%m-%d")})
162 luci.http.prepare_content("application/x-targz")
163 luci.ltn12.pump.all(reader, luci.http.write)
164 elseif reset then
165 luci.template.render("admin_system/applyreboot")
166 luci.util.exec("mtd -r erase rootfs_data")
167 else
168 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
169 end
170 end
171
172 function action_passwd()
173 local p1 = luci.http.formvalue("pwd1")
174 local p2 = luci.http.formvalue("pwd2")
175 local stat = nil
176
177 if p1 or p2 then
178 if p1 == p2 then
179 stat = luci.sys.user.setpasswd("root", p1)
180 else
181 stat = 10
182 end
183 end
184
185 luci.template.render("admin_system/passwd", {stat=stat})
186 end
187
188 function action_reboot()
189 local reboot = luci.http.formvalue("reboot")
190 luci.template.render("admin_system/reboot", {reboot=reboot})
191 if reboot then
192 luci.sys.reboot()
193 end
194 end
195
196 function action_upgrade()
197 require("luci.model.uci")
198
199 local tmpfile = "/tmp/firmware.img"
200
201 local function image_supported()
202 -- XXX: yay...
203 return ( 0 == os.execute(
204 ". /etc/functions.sh; " ..
205 "include /lib/upgrade; " ..
206 "platform_check_image %q >/dev/null"
207 % tmpfile
208 ) )
209 end
210
211 local function image_checksum()
212 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
213 end
214
215 local function storage_size()
216 local size = 0
217 if nixio.fs.access("/proc/mtd") then
218 for l in io.lines("/proc/mtd") do
219 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
220 if n == "linux" then
221 size = tonumber(s, 16)
222 break
223 end
224 end
225 elseif nixio.fs.access("/proc/partitions") then
226 for l in io.lines("/proc/partitions") do
227 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
228 if b and n and not n:match('[0-9]') then
229 size = tonumber(b) * 1024
230 break
231 end
232 end
233 end
234 return size
235 end
236
237
238 -- Install upload handler
239 local file
240 luci.http.setfilehandler(
241 function(meta, chunk, eof)
242 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
243 file = io.open(tmpfile, "w")
244 end
245 if file and chunk then
246 file:write(chunk)
247 end
248 if file and eof then
249 file:close()
250 end
251 end
252 )
253
254
255 -- Determine state
256 local keep_avail = true
257 local step = tonumber(luci.http.formvalue("step") or 1)
258 local has_image = nixio.fs.access(tmpfile)
259 local has_support = image_supported()
260 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
261 local has_upload = luci.http.formvalue("image")
262
263 -- This does the actual flashing which is invoked inside an iframe
264 -- so don't produce meaningful errors here because the the
265 -- previous pages should arrange the stuff as required.
266 if step == 4 then
267 if has_platform and has_image and has_support then
268 -- Mimetype text/plain
269 luci.http.prepare_content("text/plain")
270
271 -- Now invoke sysupgrade
272 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
273 local fd = io.popen("/sbin/luci-flash %s %q" %{
274 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
275 })
276
277 if fd then
278 while true do
279 local ln = fd:read("*l")
280 if not ln then break end
281 luci.http.write(ln .. "\n")
282 end
283 fd:close()
284 end
285
286 -- Make sure the device is rebooted
287 luci.sys.reboot()
288 end
289
290
291 --
292 -- This is step 1-3, which does the user interaction and
293 -- image upload.
294 --
295
296 -- Step 1: file upload, error on unsupported image format
297 elseif not has_image or not has_support or step == 1 then
298 -- If there is an image but user has requested step 1
299 -- or type is not supported, then remove it.
300 if has_image then
301 nixio.fs.unlink(tmpfile)
302 end
303
304 luci.template.render("admin_system/upgrade", {
305 step=1,
306 bad_image=(has_image and not has_support or false),
307 keepavail=keep_avail,
308 supported=has_platform
309 } )
310
311 -- Step 2: present uploaded file, show checksum, confirmation
312 elseif step == 2 then
313 luci.template.render("admin_system/upgrade", {
314 step=2,
315 checksum=image_checksum(),
316 filesize=nixio.fs.stat(tmpfile).size,
317 flashsize=storage_size(),
318 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
319 } )
320
321 -- Step 3: load iframe which calls the actual flash procedure
322 elseif step == 3 then
323 luci.template.render("admin_system/upgrade", {
324 step=3,
325 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
326 } )
327 end
328 end
329
330 function _keep_pattern()
331 local kpattern = ""
332 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
333 if files then
334 kpattern = ""
335 for k, v in pairs(files) do
336 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
337 kpattern = kpattern .. " " .. v
338 end
339 end
340 end
341 return kpattern
342 end
343
344 function ltn12_popen(command)
345
346 local fdi, fdo = nixio.pipe()
347 local pid = nixio.fork()
348
349 if pid > 0 then
350 fdo:close()
351 local close
352 return function()
353 local buffer = fdi:read(2048)
354 local wpid, stat = nixio.waitpid(pid, "nohang")
355 if not close and wpid and stat == "exited" then
356 close = true
357 end
358
359 if buffer and #buffer > 0 then
360 return buffer
361 elseif close then
362 fdi:close()
363 return nil
364 end
365 end
366 elseif pid == 0 then
367 nixio.dup(fdo, nixio.stdout)
368 fdi:close()
369 fdo:close()
370 nixio.exec("/bin/sh", "-c", command)
371 end
372 end