diff options
author | Mika Havela <mika.havela@gmail.com> | 2007-12-06 16:35:45 +0000 |
---|---|---|
committer | Mika Havela <mika.havela@gmail.com> | 2007-12-06 16:35:45 +0000 |
commit | 95f699fa380368841157fdd7043ff7730bcae50e (patch) | |
tree | e74aa62e9370a88171fcf1dac2f0e8e27dc3aa6d | |
parent | 81c1c354f6c24e0307b31309551d78f5822450d2 (diff) | |
download | acf-openntpd-95f699fa380368841157fdd7043ff7730bcae50e.tar.bz2 acf-openntpd-95f699fa380368841157fdd7043ff7730bcae50e.tar.xz |
'Set time options' works. You can add/remove a server/servers. You are informed if you miss some parameters.
git-svn-id: svn://svn.alpinelinux.org/acf/openntpd/trunk@416 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | openntpd-advanced-html.lsp | 2 | ||||
-rw-r--r-- | openntpd-controller.lua | 31 | ||||
-rw-r--r-- | openntpd-model.lua | 90 | ||||
-rw-r--r-- | openntpd-read-html.lsp | 2 | ||||
-rw-r--r-- | openntpd-settings-html.lsp | 26 |
5 files changed, 135 insertions, 16 deletions
diff --git a/openntpd-advanced-html.lsp b/openntpd-advanced-html.lsp index 4c0254e..33f59e5 100644 --- a/openntpd-advanced-html.lsp +++ b/openntpd-advanced-html.lsp @@ -35,7 +35,7 @@ <input type="submit" name="cmd" value="update"></form> <? ----[[ DEBUG INFORMATION +--[[ DEBUG INFORMATION require("debugs") io.write(debugs.variables(view)) --]] diff --git a/openntpd-controller.lua b/openntpd-controller.lua index 1d85e5f..4a74fca 100644 --- a/openntpd-controller.lua +++ b/openntpd-controller.lua @@ -21,14 +21,39 @@ end settings = function (self) local cmd = self.clientdata.cmd + errors = {} local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller + if (self.clientdata.settings_cmd ~= nil) then + if (self.clientdata.settings_startup) then + modify_opts = self.model:modify_opts("add", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s") + else + modify_opts = self.model:modify_opts("remove", "/etc/conf.d/ntpd", "NTPD_OPTS", "-s") + end + end + if (self.clientdata.hosts_cmd) then + if (string.lower(self.clientdata.hosts_cmd) == "delete") then + modify_opts = self.model:modify_config(self.clientdata.hosts_cmd, nil, self.clientdata.hosts_list) + elseif (string.lower(self.clientdata.hosts_cmd) == "add") then + if (self.clientdata.hosts_add == "") then + errors = {hosts_add = "You need to enter a server/IP" } + end + if (self.clientdata.hosts_type == nil) then + errors = {hosts_type = "You need to choose type of server" } + end + if (self.clientdata.hosts_add ~= "") and (self.clientdata.hosts_type ~= nil) then + modify_opts = self.model:modify_config(self.clientdata.hosts_cmd, nil, + self.clientdata.hosts_type .. " " .. self.clientdata.hosts_add) + end + end + end +-- DEBUG INFO +-- modify_opts = self.clientdata.hosts_cmd if ( cmd ~= nil ) then local startstop = self.model:startstop_service( cmd ) posix.sleep(1) -- Wait for the process to start|stop - local statusinfo = self.model:get() - return ( {startstop = startstop, statusinfo = statusinfo, url = url } ) + return ( {statusinfo = self.model:get(), startstop = "", modify_opts=modify_opts, url = url } ) else - return ( {statusinfo = self.model:get(), startstop = "", url = url } ) + return ( {statusinfo = self.model:get(), startstop = "", errors = errors, modify_opts=modify_opts, url = url, DEBUG=test } ) end end diff --git a/openntpd-model.lua b/openntpd-model.lua index e1bc5d3..454db5d 100644 --- a/openntpd-model.lua +++ b/openntpd-model.lua @@ -6,6 +6,7 @@ require("fs") require("posix") local ntpdconfig = "/etc/ntpd.conf" +local ntpdconfd = "/etc/conf.d/ntpd" local progname = "openntpd" local function config_content( f ) @@ -85,6 +86,74 @@ 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 ) + if (string.lower(addremove) == "remove" ) then + cmdtxt = "/bin/sed -i 's/\\(" .. variable .. ".*\\)" .. option .. "/\\1/' " .. file + local cmd, error = io.popen ( cmdtxt ) + local cmdoutput = cmd:read("*a") + cmd:close() + -- Cleanup the variable by removing unneccesary blanks + cmdtxt = "/bin/sed -i 's/\\\"\\ /\\\"/g' " .. file + cmdtxt = cmdtxt .. ";/bin/sed -i 's/\\ \\\"/\\\"/g' " .. file + local cmd, error = io.popen ( cmdtxt ) + cmd:close() + elseif (string.lower(addremove) == "add" ) then + cmdtxt = "/bin/sed -i 's/\\(" .. variable .. ".*\\)\\\"/\\1" .. option .. " \\\"/' " .. file + local cmd, error = io.popen ( cmdtxt ) + local cmdoutput = cmd:read("*a") + cmd:close() + end + return cmdtxt +end +local addremove_config = function ( 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) + if (string.lower(addremove) == "delete" ) then + cmdtxt = "/bin/sed -i '/" .. variable .. "/d' " .. file + local cmd, error = io.popen ( cmdtxt ) + local cmdoutput = cmd:read("*a") + cmd:close() + elseif (string.lower(addremove) == "add" ) then + cmdtxt = "/bin/echo '" .. variable .. "' >> " .. file + local cmd, error = io.popen ( cmdtxt ) + local cmdoutput = cmd:read("*a") + cmd:close() + end + return cmdtxt +end -- ################################################################################ -- PUBLIC FUNCTIONS @@ -102,6 +171,26 @@ function startstop_service ( self, state ) end +function modify_config (self, addremove, file, variable, opts) + if (file == nil) then file = ntpdconfig end + -- See to that only *my* configs are modyfied. + if (file == ntpdconfig) or (file == ntpdconfd) 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 + -- See to that the variable/option only exists once + if (addremove == "add") then + local remove = addremove_opts( "remove", file, variable, opts ) + end + local remove = addremove_opts( addremove, file, variable, opts ) + end +end + function get () local path = ntpdconfig local config = {} @@ -118,6 +207,7 @@ function get () config["version"] = string.match(get_version(), "^(%S*)" ) config["date"] = os.date() config["status"] = startstop + config["confd"],config["setstimeonstartup"] = get_confdopts("-s") config["listenstate"] = server config["timechanged"] = last_time_change() config["timezone"] = date.what_tz() diff --git a/openntpd-read-html.lsp b/openntpd-read-html.lsp index 4a3a9d9..9d059c0 100644 --- a/openntpd-read-html.lsp +++ b/openntpd-read-html.lsp @@ -16,7 +16,7 @@ <dt>Current time</dt> <dd><? io.write(view.statusinfo.date) ?></dd> -<dt>Latest time adjustment</dt> +<dt>Previous time adjustment</dt> <dd><? io.write(view.statusinfo.timechanged) ?></dd> <? --[[ ?> diff --git a/openntpd-settings-html.lsp b/openntpd-settings-html.lsp index f79da46..16a0eaa 100644 --- a/openntpd-settings-html.lsp +++ b/openntpd-settings-html.lsp @@ -13,38 +13,42 @@ <h2>NTPD SETTINGS</h2> -<? --[[ FIXME!!! ?> +<form name="cmd" action="" method="POST"> <dt>Set time immediately at startup</dt> -<dd><input type="checkbox" name=""></dd> +<dd><input type="checkbox" name="settings_startup" <? if (view.statusinfo.setstimeonstartup) then io.write("checked") end ?>></dd> <dt>Save the above settings</dt> -<dd><input type="submit" value="Save"> (see above)</dd> -<? --]] ?> +<dd><input name="settings_cmd" type="submit" value="Save"> (see above)</dd> +</form> <h3>'SET TIME' OPTIONS</h3> +<form name="cmd" action="" method="POST"> <dt>Timeserver hosts...</dt> <dd> -<select name="" size="3" style="width:200px;"> +<select name="hosts_list" size="3" style="width:200px;"> <? if (view.statusinfo.servers) then for i = 1, table.maxn(view.statusinfo.servers) do ?> - <option name="<? io.write(view.statusinfo.servers[i].value) ?>"><? io.write(view.statusinfo.servers[i].value) ?></option> + <option value="servers <? io.write(view.statusinfo.servers[i].value) ?>"><? io.write(view.statusinfo.servers[i].value) ?> (pool)</option> <? end end ?> <? if (view.statusinfo.server) then for i = 1, table.maxn(view.statusinfo.server) do ?> - <option name="<? io.write(view.statusinfo.server[i].value) ?>"><? io.write(view.statusinfo.server[i].value) ?></option> + <option value="server <? io.write(view.statusinfo.server[i].value) ?>"><? io.write(view.statusinfo.server[i].value) ?></option> <? end end ?> </select><BR> In most cases you could use <i><b>pool.ntp.org</b></i> or<br><i><b>[countryname].pool.ntp.org</i></b> (if listed in <i><b>http://www.pool.ntp.org/</b></i>). </dd> -<? ---[[ FIXME!!! ?> <dt>Delete selected host</dt> -<dd><input type="submit" value="Delete"> (see above)</dd> +<dd><input name="hosts_cmd" type="submit" value="Delete"> (see above)</dd> +<dt>Host to add</dt> +<dd><input type="text" name="hosts_add" value=""><? if (view.errors.hosts_add) then io.write("<p class=error>"..view.errors.hosts_add.."</p>") end ?></dd> +<dt>Type of above server</dt> +<dd><table><tr><td width="100px"><input type="radio" name="hosts_type" value="server">Single server</td><td width="100px"><input type="radio" name="hosts_type" value="servers">Server pool</td></tr></table><? if (view.errors.hosts_type) then io.write("<p class=error>"..view.errors.hosts_type.."</p>") end ?></dd> <dt>Add new host</dt> -<dd><input type="text" name="" value=""> <input type="submit" value="Add"></dd> -<? --]] ?> +<dd><input name="hosts_cmd" type="submit" value="Add"></dd> +</form> <h3>'PRESENT TIME' OPTIONS (ACT AS TIME SERVER)</h3> <dt>Listen on address...</dt> |