fc35f774a94cc9f098f2b59ec4eae2cb779b275d
[project/luci.git] / modules / luci-mod-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-2012 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         if index < 255 then
28                 inits["%02i.%s" % { index, name }] = {
29                         name    = name,
30                         index   = tostring(index),
31                         enabled = enabled
32                 }
33         end
34 end
35
36
37 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 inaccessible!</strong>"))
38 m.reset = false
39 m.submit = false
40
41
42 s = m:section(Table, inits)
43
44 i = s:option(DummyValue, "index", translate("Start priority"))
45 n = s:option(DummyValue, "name", translate("Initscript"))
46
47
48 e = s:option(Button, "endisable", translate("Enable/Disable"))
49
50 e.render = function(self, section, scope)
51         if inits[section].enabled then
52                 self.title = translate("Enabled")
53                 self.inputstyle = "save"
54         else
55                 self.title = translate("Disabled")
56                 self.inputstyle = "reset"
57         end
58
59         Button.render(self, section, scope)
60 end
61
62 e.write = function(self, section)
63         if inits[section].enabled then
64                 inits[section].enabled = false
65                 return luci.sys.init.disable(inits[section].name)
66         else
67                 inits[section].enabled = true
68                 return luci.sys.init.enable(inits[section].name)
69         end
70 end
71
72
73 start = s:option(Button, "start", translate("Start"))
74 start.inputstyle = "apply"
75 start.write = function(self, section)
76         luci.sys.call("/etc/init.d/%s %s >/dev/null" %{ inits[section].name, self.option })
77 end
78
79 restart = s:option(Button, "restart", translate("Restart"))
80 restart.inputstyle = "reload"
81 restart.write = start.write
82
83 stop = s:option(Button, "stop", translate("Stop"))
84 stop.inputstyle = "remove"
85 stop.write = start.write
86
87
88
89 f = SimpleForm("rc", translate("Local Startup"),
90         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."))
91
92 t = f:field(TextValue, "rcs")
93 t.rmempty = true
94 t.rows = 20
95
96 function t.cfgvalue()
97         return luci.fs.readfile("/etc/rc.local") or ""
98 end
99
100 function f.handle(self, state, data)
101         if state == FORM_VALID then
102                 if data.rcs then
103                         luci.fs.writefile("/etc/rc.local", data.rcs:gsub("\r\n", "\n"))
104                 end
105         end
106         return true
107 end
108
109 return m, f