ed1eabe1f97a903ed1bd206a32f503dcb4cc491e
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_system / startup.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2010-2011 Jo-Philipp Wich <xm@subsignal.org>
6 Copyright 2010 Manuel Munz <freifunk at somakoma dot de>
7
8 Licensed under the Apache License, Version 2.0 (the "License");
9 you may not use this file except in compliance with the License.
10 You may obtain a copy of the License at
11
12         http://www.apache.org/licenses/LICENSE-2.0
13
14 $Id$
15 ]]--
16
17 require "luci.fs"
18 require "luci.sys"
19 require "luci.util"
20
21 local inits = { }
22
23 for _, name in ipairs(luci.sys.init.names()) do
24         local index   = luci.sys.init.index(name)
25         local enabled = luci.sys.init.enabled(name)
26
27         inits["%02i.%s" % { index, name }] = {
28                 name    = name,
29                 index   = tostring(index),
30                 enabled = enabled
31         }
32 end
33
34
35 m = SimpleForm("initmgr", translate("Initscripts"), translate("You can enable or disable installed init scripts here. Changes will applied after a device reboot.<br /><strong>Warning: If you disable essential init scripts like \"network\", your device might become inaccesable!</strong>"))
36 m.reset = false
37 m.submit = false
38
39
40 s = m:section(Table, inits)
41
42 i = s:option(DummyValue, "index", translate("Start priority"))
43 n = s:option(DummyValue, "name", translate("Initscript"))
44
45
46 e = s:option(Button, "endisable", translate("Enable/Disable"))
47
48 e.render = function(self, section, scope)
49         if inits[section].enabled then
50                 self.title = translate("Enabled")
51                 self.inputstyle = "save"
52         else
53                 self.title = translate("Disabled")
54                 self.inputstyle = "reset"
55         end
56
57         Button.render(self, section, scope)
58 end
59
60 e.write = function(self, section)
61         if inits[section].enabled then
62                 inits[section].enabled = false
63                 return luci.sys.init.disable(inits[section].name)
64         else
65                 inits[section].enabled = true
66                 return luci.sys.init.enable(inits[section].name)
67         end
68 end
69
70
71 start = s:option(Button, "start", translate("Start"))
72 start.inputstyle = "apply"
73 start.write = function(self, section)
74         luci.sys.call("/etc/init.d/%s %s >/dev/null" %{ inits[section].name, self.option })
75 end
76
77 restart = s:option(Button, "restart", translate("Restart"))
78 restart.inputstyle = "reload"
79 restart.write = start.write
80
81 stop = s:option(Button, "stop", translate("Stop"))
82 stop.inputstyle = "remove"
83 stop.write = start.write
84
85
86
87 f = SimpleForm("rc", translate("Local Startup"),
88         translate("This is the content of /etc/rc.local. Insert your own commands here (in front of 'exit 0') to execute them at the end of the boot process."))
89
90 t = f:field(TextValue, "rcs")
91 t.rmempty = true
92 t.rows = 20
93
94 function t.cfgvalue()
95         return luci.fs.readfile("/etc/rc.local") or ""
96 end
97
98 function f.handle(self, state, data)
99         if state == FORM_VALID then
100                 if data.rcs then
101                         luci.fs.writefile("/etc/rc.local", data.rcs:gsub("\r\n", "\n"))
102                 end
103         end
104         return true
105 end
106
107 return m, f