summaryrefslogtreecommitdiffstats
path: root/openntpd-model.lua
diff options
context:
space:
mode:
authorMika Havela <mika.havela@gmail.com>2008-01-14 19:30:29 +0000
committerMika Havela <mika.havela@gmail.com>2008-01-14 19:30:29 +0000
commit6f3662eb2e408ef2e281626f52419bc966d638cd (patch)
tree67403c1d48ab4dc9a5c147ffe452c77fe1c42f6d /openntpd-model.lua
parentacba7201c5e2680a092dee1314cfa70cc85896ce (diff)
downloadacf-openntpd-6f3662eb2e408ef2e281626f52419bc966d638cd.tar.bz2
acf-openntpd-6f3662eb2e408ef2e281626f52419bc966d638cd.tar.xz
Cleaned up the code. Moved functions to library instead (to be able to reuse code)
git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@569 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'openntpd-model.lua')
-rw-r--r--openntpd-model.lua160
1 files changed, 44 insertions, 116 deletions
diff --git a/openntpd-model.lua b/openntpd-model.lua
index 4960534..ff7c470 100644
--- a/openntpd-model.lua
+++ b/openntpd-model.lua
@@ -1,14 +1,21 @@
module (..., package.seeall)
+--require("date")
+--require("posix")
require("format")
-require("date")
require("fs")
-require("posix")
+require("procps")
+require("getopts")
+require("daemoncontrol")
-local ntpdconfig = "/etc/ntpd.conf"
-local ntpdconfd = "/etc/conf.d/ntpd"
-local progname = "openntpd"
+local configfile = "/etc/ntpd.conf"
+local confdfile = "/etc/conf.d/ntpd"
+local processname = "openntpd"
+-- ################################################################################
+-- LOCAL FUNCTIONS
+
+-- This function is used to get config_content.
local function config_content( f )
local config = {}
config.name = f
@@ -33,28 +40,6 @@ local function config_content( f )
return config
end
-local function file_info ( path )
- require("posix")
- local filedetails = posix.stat(path)
- filedetails["owner"]=rawget((posix.getpasswd(filedetails["uid"])),"name")
- filedetails["group"]=rawget((posix.getgroup(filedetails["gid"])),"name")
- filedetails["atimelong"]=os.date("%c", filedetails["atime"])
- filedetails["mtimelong"]=os.date("%c", filedetails["mtime"])
- filedetails["path"]=path
- filedetails["name"]=basename(path)
-
- if ( filedetails["size"] > 1073741824 ) then
- filedetails["size"]=((filedetails["size"]/1073741824) - (filedetails["size"]/1073741824%0.1)) .. "G"
- elseif ( filedetails["size"] > 1048576 ) then
- filedetails["size"]=((filedetails["size"]/1048576) - (filedetails["size"]/1048576%0.1)) .. "M"
- elseif ( filedetails["size"] > 1024 ) then
- filedetails["size"]=((filedetails["size"]/1024) - (filedetails["size"]/1024%0.1)) .. "k"
- else
- filedetails["size"]=filedetails["size"]
- end
- return filedetails
-end
-
local function get_version ()
local f,error = io.popen("/sbin/apk_version -v -s openntpd")
local programversion = f:read("*a")
@@ -62,19 +47,7 @@ local function get_version ()
return programversion
end
-local is_running = function( process )
- local statusreport = nil
- local cmdoutput = {}
- local cmd, error = io.popen("pidof " .. process ,r)
- local cmdoutput = string.gsub(cmd:read("*a"), "%s", "")
- cmd:close()
- if (cmdoutput ~= "") then
- statusreport = "Yes"
- end
- return statusreport
-end
-
-local last_time_change = function()
+local function last_time_change()
local cmdoutput = {}
local cmd, error = io.popen("cat /var/log/messages | grep ntpd | grep adjusting | tail -1" ,r)
local cmdoutput1,cmdoutput2 = string.match(cmd:read("*a"), "^%s*(%S+%s+%S+%s+%S+%s+).*: (.*)$")
@@ -84,39 +57,13 @@ local last_time_change = function()
return cmdoutput1 .. cmdoutput2
end
-local get_confdopts = function(search)
- local opts = {}
- local searchresult = nil
- local conf_file = fs.read_file_as_array ( ntpdconfd )
- for i=1,table.maxn(conf_file) do
- local l = conf_file[i]
- -- Filter out commented lines
- if not string.find ( l, "^[;#].*" ) then
- local a = string.match ( l, "^%s*(%S+)%=" )
- if (a) then
- if not (opts[string.lower(a)]) then
- opts[string.lower(a)] = {}
- end
- local b = string.gsub(string.match ( l, '^%s*%S+%=(.*)' ), '"', '')
- for i=1,table.maxn(format.string_to_table(" ", b)) do
- local option = rawget(format.string_to_table(" ", b),i)
- table.insert (opts[string.lower(a)], i, option)
- if (option == search) then
- searchresult = "Yes"
- end
- end
- end
- end
- end
- return opts,searchresult
-end
-- This function needs:
-- addremove = [add|remove]
-- file = Path to the file
-- variable = e.g. "NTPD_OPTS"
-- option = What value to look for to add or remove from the variable.
-local addremove_opts = function ( addremove, file, variable, option )
+local function addremove_opts( addremove, file, variable, option )
if (string.lower(addremove) == "remove" ) then
cmdtxt = "/bin/sed -i 's/\\(" .. variable .. ".*\\)" .. option .. "/\\1/' " .. file
local cmd, error = io.popen ( cmdtxt )
@@ -135,7 +82,7 @@ local addremove_opts = function ( addremove, file, variable, option )
end
return cmdtxt
end
-local addremove_config = function ( addremove, file, variable )
+local function addremove_config( addremove, file, variable )
-- Notes on known/unknown bugs: Remove 'server www.test.org' wont work if config has multiple space/tab e.g. 'server www.test.org'
-- FIXME: Make num-space unsensetive.
-- FIXME: Can hold multiple rows with same values (when deleting... every equal row is deleted)
@@ -160,30 +107,21 @@ end
-- PUBLIC FUNCTIONS
function startstop_service ( self, state )
- local status = {}
- -- This is a strange hack to get the init.d script working
- local f,err = io.popen("PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin /etc/init.d/ntpd " .. state)
- local config = f:read("*a")
- f:close()
- if (config == "") then
- config = "OK!"
- end
- status.status = config
- return status
+ return daemoncontrol.daemoncontrol("ntpd", state)
end
function modify_config (self, addremove, file, variable)
- if (file == nil) then file = ntpdconfig end
+ if (file == nil) then file = configfile end
-- See to that only *my* configs are modyfied.
- if (file == ntpdconfig) or (file == ntpdconfd) then
+ if (file == configfile) or (file == confdfile) then
return addremove_config(addremove, file, variable)
end
end
function modify_opts (self, addremove, file, variable, opts)
-- See to that only *my* configs are modyfied.
- if (file == ntpdconfig) or (file == ntpdconfd) then
+ if (file == configfile) or (file == confdfile) then
-- See to that the variable/option only exists once
if (addremove == "add") then
local remove = addremove_opts( "remove", file, variable, opts )
@@ -192,26 +130,26 @@ function modify_opts (self, addremove, file, variable, opts)
end
end
-function get ()
- local path = ntpdconfig
- local config = {}
+function getstatus ()
+ local path = configfile
+ local status = {}
if not (fs.is_file(path)) then
local file_result,err = fs.write_file(path, "")
end
- local startstop = is_running ("ntpd")
- local config = config_content ( path )
- if (config["listen"]) then
- server = "Yes"
- else
- server = "No"
- end
- config["version"] = string.match(get_version(), "^(%S*)" )
- config["date"] = os.date()
- config["enabled"] = startstop
- config["confd"],config["setstimeonstartup"] = get_confdopts("-s")
- config["listenstate"] = server
- config["timechanged"] = last_time_change()
- config["timezone"] = date.what_tz()
+ status["version"] = string.match(get_version(), "^(%S*)" )
+ status["date"] = os.date()
+ status["enabled"] = procps.pidof("ntpd")
+ status["setstimeonstartup"] = getopts.getoptsfromfile(confdfile,"NTPD_OPTS", "-s")
+ status["listenstate"] = server
+ status["timechanged"] = last_time_change()
+ return status
+end
+
+function getconfig ()
+ local path = configfile
+ local config = {}
+ config = getopts.getoptsfromfile(confdfile)
+ config["variables"] = config_content(path)
return config
end
@@ -225,27 +163,17 @@ function get_logfile ()
me.cmd = cmdtxt
return me
end
-function get_filecontent (self)
- local path = ntpdconfig
- local file_content = get()
- if (fs.is_file(path)) then
- local filedetails = file_info(path)
- local configstatus = get()
- local file = io.open( path )
- local file_result = file:read("*a") or "unknown"
- file_content["details"]=filedetails
- file_content["content"]=file_result
- file:close()
- else
- file_content = {value="", errtxt="File is missing, but will be created when you save your new settings",filedetails={longname=path, size="0", mtimelong=""}}
- end
- return file_content
+function get_filedetails()
+ local filedetails = {}
+ local path = configfile
+ filedetails.details = fs.stat(path)
+ filedetails.content = fs.read_file(path)
+ return filedetails
end
function update_filecontent (self, modifications)
- local path = ntpdconfig
+ local path = configfile
local file_result,err = fs.write_file(path, format.dostounix(modifications))
- file_content = get_filecontent()
- return file_content
+ return file_result
end