summaryrefslogtreecommitdiffstats
path: root/rc-model.lua
blob: c213e1f2220c4124f5379e720e70319e4d26ab4f (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
module (..., package.seeall)

require("processinfo")
require("modelfunctions")
require("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()
	config = config or processinfo.read_initrunlevels()
	return cfe({ type="structure", value=config, label="Init Runlevels" })
end

read_runlevels = function(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