summaryrefslogtreecommitdiffstats
path: root/rc-model.lua
blob: 1046a969e1314feaf0530fe62ebff5d9853315b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module (..., package.seeall)

require("processinfo")
require("modelfunctions")
fs = require("acf.fs")
require("posix")

local configpath = "/etc/runlevels/"
local runlevels = {}
for f in posix.files(configpath) do
	if f~="." and f~=".." and fs.is_dir(configpath..f) then
		runlevels[#runlevels+1] = f
	end
end
table.sort(runlevels)

local config

status = function()
	if not config then
		config = processinfo.read_initrunlevels()
		for i,c in pairs(config) do
			c.actions, c.description = processinfo.daemon_actions(c.servicename)
			c.status = processinfo.daemoncontrol(c.servicename, "status")
			if c.status then
				c.status = string.match(c.status, "status: (.*)")
			end
		end
	end
	return cfe({ type="structure", value=config, label="Init Runlevels" })
end

read_runlevels = function(clientdata)
	local servicename = clientdata.servicename
	local value = {}
	value.servicename = cfe({ value=servicename or "", label="Service Name" })
	value.runlevels = cfe({ type="multi", value={}, label="Service Runlevels", option=runlevels })

	-- read in the value for the servicename
	config = config or processinfo.read_initrunlevels()
	for i,conf in ipairs(config) do
		if conf.servicename == servicename then
			value.runlevels.value = conf.runlevels
			break
		end
	end
	
	return cfe({ type="group", value=value, label="Service Runlevels"})
end

update_runlevels = function(service)
	local success = modelfunctions.validatemulti(service.value.runlevels)
	service.value.servicename.errtxt = "Invalid service"
	for name in posix.files("/etc/init.d") do
		if name == service.value.servicename.value then
			success = true
			service.value.servicename.errtxt = nil
		end
	end
	if success then
		local reverserunlevels = {}
		for i,lev in ipairs(service.value.runlevels.value) do
			reverserunlevels[lev] = i
		end
		local delrunlevels = {}
		for i,lev in ipairs(runlevels) do
			if not reverserunlevels[lev] then
				delrunlevels[#delrunlevels+1] = lev
			end
		end
		service.descr = {}
		service.errtxt = {}
		service.descr[#service.descr+1], service.errtxt[#service.errtxt+1] = processinfo.delete_runlevels(service.value.servicename.value, delrunlevels)
		service.descr[#service.descr+1], service.errtxt[#service.errtxt+1] = processinfo.add_runlevels(service.value.servicename.value, service.value.runlevels.value)
		service.descr = table.concat(service.descr, "\n")
		service.errtxt = table.concat(service.errtxt, "\n")
		if service.errtxt and string.find(service.errtxt, "^%s*$") then
			service.errtxt = nil
		end
	else
		service.errtxt = "Failed to set runlevels"
	end

	return service
end

function get_startstop(clientdata)
	return modelfunctions.get_startstop(clientdata.servicename)	
end

function startstop_service(startstop)
	return modelfunctions.startstop_service(startstop, clientdata.action)
end