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