diff options
author | Ted Trask <ttrask01@yahoo.com> | 2009-01-07 21:13:10 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2009-01-07 21:13:10 +0000 |
commit | 03af194d9cd96d5cc840dc780fab76d741bf4b50 (patch) | |
tree | a702346346a3dd0ecb33fda63f3cdc246b423c0e | |
parent | 97986db9b4f5b531dd9310a4466ac0a196274ca3 (diff) | |
download | acf-chrony-03af194d9cd96d5cc840dc780fab76d741bf4b50.tar.bz2 acf-chrony-03af194d9cd96d5cc840dc780fab76d741bf4b50.tar.xz |
Modified chrony to make config work and replace time with details.
git-svn-id: svn://svn.alpinelinux.org/acf/chrony/trunk@1668 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | chrony-config-html.lsp | 4 | ||||
-rw-r--r-- | chrony-controller.lua | 10 | ||||
-rw-r--r-- | chrony-details-html.lsp (renamed from chrony-time-html.lsp) | 5 | ||||
-rw-r--r-- | chrony-model.lua | 151 | ||||
-rw-r--r-- | chrony.menu | 9 | ||||
-rw-r--r-- | chrony.roles | 4 |
6 files changed, 129 insertions, 54 deletions
diff --git a/chrony-config-html.lsp b/chrony-config-html.lsp index 9272f40..3768699 100644 --- a/chrony-config-html.lsp +++ b/chrony-config-html.lsp @@ -17,8 +17,8 @@ end %> <H1>Config</H1> <% - form.action = page_info.script .. page_info.prefix .. page_info.controller .. "/" .. page_info.action - displayform(form) + local order = { "server", "allow", "driftfile", "keyfile", "commandkey" } + displayform(form, order, nil, page_info) %> <% if viewlibrary and viewlibrary.dispatch_component then diff --git a/chrony-controller.lua b/chrony-controller.lua index 4b02a8d..4870690 100644 --- a/chrony-controller.lua +++ b/chrony-controller.lua @@ -8,18 +8,18 @@ function status(self) return self.model.getstatus() end +function details(self) + return self.model.getdetails() +end + function startstop(self) return controllerfunctions.handle_startstop(self, self.model.startstop_service, self.model.getstatus, self.clientdata) end function config(self) - return controllerfunctions.handle_form(self, self.model.getconfig, self.model.setconfig, self.clientdata, "Save", "Edit Config", "Configuration Set") + return controllerfunctions.handle_form(self, self.model.get_config, self.model.update_config, self.clientdata, "Save", "Edit Config", "Configuration Set") end function expert(self) return controllerfunctions.handle_form(self, self.model.get_filedetails, self.model.update_filedetails, self.clientdata, "Save", "Edit Config File", "Configuration Set") end - -function time(self) - return self.model.gettime() -end diff --git a/chrony-time-html.lsp b/chrony-details-html.lsp index 24a3245..43e00d6 100644 --- a/chrony-time-html.lsp +++ b/chrony-details-html.lsp @@ -13,5 +13,8 @@ io.write("</span>") <H2><%= data.label %></H2> <DL> -<% displayitem(data) %> +<% displayitem(data.value.time) %> +<% displayitem(data.value.sources) %> +<% displayitem(data.value.sourcestats) %> +<% displayitem(data.value.tracking) %> </DL> diff --git a/chrony-model.lua b/chrony-model.lua index 7f5e642..517efa4 100644 --- a/chrony-model.lua +++ b/chrony-model.lua @@ -4,6 +4,7 @@ module(..., package.seeall) require("modelfunctions") require("format") require("fs") +require("validator") -- Set variables local configfile = "/etc/chrony/chrony.conf" @@ -11,9 +12,43 @@ local processname = "chronyd" local packagename = "chrony" local keyfile = "/etc/chrony/chrony.keys" +local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin " + -- ################################################################################ -- LOCAL FUNCTIONS +function validate_config(config) + local success = true + for i,val in ipairs(config.value.server.value) do + if string.find(val, "[^%w%s.-]") then + config.value.server.errtxt = "Invalid entry on line "..i + success = false + break + end + end + for i,val in ipairs(config.value.allow.value) do + if string.find(val, "[^%w%s.-/]") then + config.value.allow.errtxt = "Invalid entry on line "..i + success = false + break + end + end + if not validator.is_valid_filename(config.value.driftfile.value) then + config.value.driftfile.errtxt = "Invalid file name" + success = false + end + if not validator.is_valid_filename(config.value.keyfile.value) then + config.value.keyfile.errtxt = "Invalid file name" + success = false + end + if not validator.is_integer(config.value.commandkey.value) then + config.value.commandkey.errtxt = "Must be an integer" + success = false + end + + return success, config +end + -- ################################################################################ -- PUBLIC FUNCTIONS @@ -21,57 +56,103 @@ function startstop_service(action) return modelfunctions.startstop_service(processname, action) end -function getconfig() +function getstatus() + return modelfunctions.getstatus(processname, packagename, "Chrony Status") +end + +function getdetails() + local details = {} + details.time = cfe({ value=os.date(), label="Current Time" }) + details.sources = cfe({ type="longtext", value="Unavailable", label="Sources" }) + details.sourcestats = cfe({ type="longtext", value="Unavailable", label="Source Stats" }) + details.tracking = cfe({ type="longtext", value="Unavailable", label="Tracking" }) + + local pid = processinfo.pidof(processname) + if pid and #pid > 0 then + local cmd = path.."chronyc sources" + local f = io.popen(cmd) + details.sources.value = f:read("*a") or "" + f:close() + cmd = path.."chronyc sourcestats" + f = io.popen(cmd) + details.sourcestats.value = f:read("*a") or "" + f:close() + cmd = path.."chronyc tracking" + f = io.popen(cmd) + details.tracking.value = f:read("*a") or "" + f:close() + end + + return cfe({ type="group", value=details, label="Chrony Status Details" }) +end + +function get_config() 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"}) + output.server = cfe({ type="list", value={}, label="Servers", descr="List of NTP servers by name or IP (ie. 0.pool.ntp.org). If infrequent Internet connection, follow name/IP with 'offline'." }) + output.allow = cfe({ type="list", value={}, label="Allow", descr="List of allowed clients by name/subnet/IP or 'all'."}) + output.driftfile = cfe({ label="Drift File", descr="Name of drift file (ie. /var/log/chrony/chrony.drift)" }) + output.keyfile = cfe({ label="Key File", descr="Name of key file (ie. /etc/chrony/chrony.keys)" }) + output.commandkey = cfe({ label="Command Key", descr="Number of key in Key File for commands." }) - local config = format.parse_configfile(fs.read_file(configfile), "[!;#%%]") + local config = format.parse_linesandwords(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 + for i,entry in ipairs(config) do + if output[entry[1]] then + if type(output[entry[1]].value) == "table" then + table.insert(output[entry[1]].value, table.concat(entry, " ", 2)) + else + output[entry[1]].value = table.concat(entry, " ", 2) + end + end + end 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 + if type(val.value) == "table" then + if #val.value > 0 then + val.line = name.." "..table.concat(val.value, "\n"..name.." ") + end + else + if val.value ~= "" then + val.line = name .. " " .. val.value + end + end end + local lines = fs.read_file_as_array(configfile) or {} + local conf = format.parse_linesandwords(lines, "[!;#%%]") + for i,entry in ipairs(conf) do + if config.value[entry[1]] then + if config.value[entry[1]].line then + lines[entry.linenum] = config.value[entry[1]].line + else + lines[entry.linenum] = nil + end + config.value[entry[1]].line = nil + end + end + + -- remove the holes in the lines array (sparse array due to removing entries) + local newlines = {} + for i=1,table.maxn(lines) do + table.insert(newlines, lines[i]) + end + + -- add in missing entries to end for name,val in pairs(config.value) do if val.line then - lines[#lines+1] = val.line + newlines[#newlines+1] = val.line val.line = nil end end - fs.write_file(configfile, string.gsub(table.concat(lines, "\n"), "\n+$", "")) + fs.write_file(configfile, table.concat(newlines, "\n")) else config.errtxt = "Failed to save config" end @@ -79,14 +160,6 @@ function update_config(config) 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) diff --git a/chrony.menu b/chrony.menu index 9a63b6d..03a0722 100644 --- a/chrony.menu +++ b/chrony.menu @@ -1,6 +1,5 @@ #CAT GROUP/DESC TAB ACTION -Networking 20NTP(chrony) Status time -Networking 46NTP(chrony) Config config -Networking 46NTP(chrony) Expert expert -Networking 46NTP(chrony) Logfile logfile - +Networking 20NTP(chrony) Status details +Networking 20NTP(chrony) Config config +Networking 20NTP(chrony) Expert expert +Networking 20NTP(chrony) Logfile logfile diff --git a/chrony.roles b/chrony.roles index c217729..7d7bad9 100644 --- a/chrony.roles +++ b/chrony.roles @@ -1,4 +1,4 @@ -USER=chrony:status,chrony:logfile,chrony:time,chrony:startstop +USER=chrony:status,chrony:logfile,chrony:details,chrony:startstop EDITOR=chrony:config EXPERT=chrony:expert -ADMIN=chrony:status,chrony:config,chrony:logfile,chrony:time,chrony:startstop,chrony:expert +ADMIN=chrony:status,chrony:logfile,chrony:details,chrony:startstop,chrony:config,chrony:expert |