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