summaryrefslogtreecommitdiffstats
path: root/chrony-model.lua
blob: 7f5e642c98a1612033346663c7dedec4b5579f2d (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
95
96
97
98
99
module(..., package.seeall)

-- Load libraries
require("modelfunctions")
require("format")
require("fs")

-- Set variables
local configfile = "/etc/chrony/chrony.conf"
local processname = "chronyd"
local packagename = "chrony"
local keyfile = "/etc/chrony/chrony.keys"

-- ################################################################################
-- LOCAL FUNCTIONS

-- ################################################################################
-- PUBLIC FUNCTIONS

function startstop_service(action)
	return modelfunctions.startstop_service(processname, action)
end

function getconfig()
        local output = {}
	output.SERVER = cfe({ value="0.pool.ntp.org", label="server" })
        output.ALLOW = cfe({ value="all", label="allow" })
	output.DRIFTFILE = cfe({ value="/var/log/chrony/chrony.drift", label="driftfile"})
	output.KEYFILE = cfe({value="/etc/chrony/chrony.keys", label="keyfile"})
	output.LOGDIR = cfe({value="/var/log/chrony", label="logdir"})

	local config = format.parse_configfile(fs.read_file(configfile), "[!;#%%]")
	if config then
		output.SERVER.value = config.server or output.SERVER.value
		output.ALLOW.value = config.allow or output.ALLOW.value
		output.DRIFTFILE.value = config.driftfile or output.DRIFTFILE.value
		output.KEYFILE.value = config.keyfile or output.KEYFILE.value
		output.LOGDIR.value = config.logdir or output.LOGDIR.value
	end
	
	return cfe({ type="group", value=output, label="Chrony Config" })
end


function update_config(config)
        local success, config = validate_config(config)

        if success then
                for name,val in pairs(config.value) do
                        val.line = name.." "..config_value(val.value)
                end

                local lines = {}
                for line in string.gmatch(fs.read_file(configfile) or "", "([^\n]*)\n?") do
                        for name,val in pairs(config.value) do
                                if val.line and string.find(line, "^%s*#?%s*"..name) then
                                        if string.find(line, "^%s*#") then
                                                lines[#lines+1] = val.line
                                        else
                                                line = val.line
                                        end
                                        val.line = nil
                                end
                        end
                        lines[#lines+1] = line
                end

                for name,val in pairs(config.value) do
                        if val.line then
                                lines[#lines+1] = val.line
                                val.line = nil
                        end
                end
                fs.write_file(configfile, string.gsub(table.concat(lines, "\n"), "\n+$", ""))
        else
                config.errtxt = "Failed to save config"
        end

        return config
end

function getstatus()
	return modelfunctions.getstatus(processname, packagename, "Chrony Status")
end

function gettime()
	return cfe({ value=os.date(), label="Current time" })
end

function get_filedetails()
	-- FIXME validate
	return modelfunctions.getfiledetails(configfile)
end

function update_filedetails(filedetails)
	-- FIXME validate
	return modelfunctions.setfiledetails(filedetails, {configfile})
end