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

require("processinfo")
require("validator")

local config

status = function()
	config = config or processinfo.read_startupsequence()
	return cfe({ type="structure", value=config, label="Startup Sequence Status" })
end

read_sequence = function(servicename)
	local value = {}
	value.servicename = cfe({ value=servicename or "", label="Service Name" })
	value.sequence = cfe({ label="Startup Sequence" })
	value.kill = cfe({ type="boolean", value=false, label="Add kill link for shutdown" })
	value.system = cfe({ type="boolean", value=false, label="System init service" })

	-- read in the value for the servicename
	config = config or processinfo.read_startupsequence()
	for i,conf in ipairs(config) do
		if conf.servicename == servicename then
			value.sequence.value = conf.sequence
			value.kill.value = conf.kill
			value.system.value = conf.system
			break
		end
	end
	
	return cfe({ type="group", value=value, label="Service Startup Sequence"})
end


update_sequence = function(sequence)
	local success = false
	sequence.value.servicename.errtxt = "Invalid service"
	for name in posix.files("/etc/init.d") do
		if name == sequence.value.servicename.value then
			success = true
			sequence.value.servicename.errtxt = nil
		end
	end
	if sequence.value.sequence.value ~= "" and not validator.is_integer_in_range(sequence.value.sequence.value, 1, 99) then
		success = false
		sequence.value.sequence.errtxt = "Invalid sequence number"
	end
	if success then
		if sequence.value.sequence.value == "" then
			sequence.descr, sequence.errtxt = processinfo.delete_startupsequence(sequence.value.servicename.value)
		else
			sequence.descr, sequence.errtxt = processinfo.add_startupsequence(sequence.value.servicename.value, sequence.value.sequence.value, sequence.value.kill.value, sequence.value.system.value)
		end
	else
		sequence.errtxt = "Failed to set sequence"
	end

	return sequence
end