diff options
author | Mika Havela <mika.havela@gmail.com> | 2008-04-08 15:12:44 +0000 |
---|---|---|
committer | Mika Havela <mika.havela@gmail.com> | 2008-04-08 15:12:44 +0000 |
commit | 910f45e7c9889172631a6554fbf90c70e850984c (patch) | |
tree | 4032e8840233c301746a42cdcc3d914ca32eb2ab | |
parent | bb074fac16b8227a0bbb35208261635fed1b0891 (diff) | |
download | acf-opennhrp-910f45e7c9889172631a6554fbf90c70e850984c.tar.bz2 acf-opennhrp-910f45e7c9889172631a6554fbf90c70e850984c.tar.xz |
Cleaning up code.
git-svn-id: svn://svn.alpinelinux.org/acf/opennhrp/trunk@958 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | opennhrp-controller.lua | 65 | ||||
-rw-r--r-- | opennhrp-expert-html.lsp | 79 | ||||
-rw-r--r-- | opennhrp-logfile-html.lsp | 46 | ||||
-rw-r--r-- | opennhrp-model.lua | 41 | ||||
-rw-r--r-- | opennhrp-status-html.lsp | 32 |
5 files changed, 66 insertions, 197 deletions
diff --git a/opennhrp-controller.lua b/opennhrp-controller.lua index ac216e7..e90c030 100644 --- a/opennhrp-controller.lua +++ b/opennhrp-controller.lua @@ -1,25 +1,21 @@ module(..., package.seeall) --- This is the object/text used when we want to add a new record - +-- Load libraries require("format") +-- Set variables local newrecordtxt = "[New]" +-- ################################################################################ +-- LOCAL FUNCTIONS + local list_redir = function (self) self.conf.action = "status" self.conf.type = "redir" error (self.conf) end -mvc = {} -mvc.on_load = function(self, parent) - if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then - self.worker[self.conf.action] = list_redir(self) - end -end - -local function displaycmdmanagement(disablestart,disablestop,disablerestart) +local function displaycmdmanagement(pidofstatus) -- Add a management buttons local management = {} management.start = cfe({ name="cmdmanagement", @@ -37,22 +33,38 @@ local function displaycmdmanagement(disablestart,disablestop,disablerestart) value="Restart", type="submit", }) + -- next CFE can be used to present the result of the previous action + management.actionresult = cfe({ name="actionresult", + label="Previous action result", + descr="", --Content of this variable is displayed as <PRE> ... </PRE> in BLACK text + errtxt="", --Content of this variable is displayed as <PRE> ... </PRE> in RED text + }) -- Disable management buttons based on if the process is running or not - if (disablestart) then management.start.disabled = "yes" end - if (disablestop) then management.stop.disabled = "yes" end - if (disablerestart) then management.restart.disabled = "yes" end + if (pidofstatus) then + management.start.disabled = "yes" + else + management.stop.disabled = "yes" + management.restart.disabled = "yes" + end return management end - -- ################################################################################ -- PUBLIC FUNCTIONS + +mvc = {} +function mvc.on_load (self, parent) + if (self.worker[self.conf.action] == nil ) or ( self.conf.action == "init" ) then + self.worker[self.conf.action] = list_redir(self) + end +end + function status(self) return { status=self.model.getstatus() } end -expert = function (self) +function expert (self) local modifications = self.clientdata.filecontent or "" if ( self.clientdata.cmdsave ) then modifications = self.model:update_filecontent(modifications) @@ -60,7 +72,7 @@ expert = function (self) local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller -- Start/Stop/Restart process - local cmdmanagement + local cmdmanagement, actionresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", @@ -70,7 +82,7 @@ expert = function (self) value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) }), }) - local actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) + actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) end local status=self.model.getstatus() @@ -88,26 +100,23 @@ expert = function (self) end - -- Management buttons - local disablestart,disablestop,disablerestart - -- Disable management buttons based on if the process is running or not - if (string.lower(status.status.value) == "enabled" ) then - disablestart = "yes" - else - disablestop = "yes" + -- Management buttons (Hide/show buttons + local pidofstatus + if (string.lower(status.status.value) == "enabled" ) then pidofstatus = true end + management = displaycmdmanagement(pidofstatus) + if (actionresult) then + management.actionresult.descr=cmdmanagement.descr + management.actionresult.errtxt=cmdmanagement.errtxt end - -- Display management buttons - management = displaycmdmanagement(disablestart,disablestop,disablerestart) return ( { status = status, file = file, modifications = modifications, management = management, - cmdmanagement = cmdmanagement, url = url, } ) end -logfile = function (self) +function logfile(self) local status=self.model.getstatus() local logfile = self.model:get_logfile() diff --git a/opennhrp-expert-html.lsp b/opennhrp-expert-html.lsp index cf70091..4f77e5e 100644 --- a/opennhrp-expert-html.lsp +++ b/opennhrp-expert-html.lsp @@ -1,4 +1,6 @@ -<? local form = ... ?> +<? local form = ... +require("viewfunctions") +?> <? --[[ DEBUG INFORMATION io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") @@ -6,39 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("</span>") --]] ?> -<? -function displayinfo(myform,tags,viewtype) - for k,v in pairs(tags) do - if (myform[v]) and (myform[v]["value"]) then - local val = myform[v] - io.write("\n\t<DT") - if (#val.errtxt > 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "</DT>") - io.write("\n\t\t<DD>") - if (viewtype == "viewonly") then - io.write(val.value) - elseif (val.type == "radio") and (type(val.option) == "table") and (#val.option > 0) then - io.write("<span style='display:inline' class='" .. ( val.class or "") .. "'>") - for k1,v1 in pairs(val.option) do - io.write(tostring(v1.label) .. ":") - io.write("<input style='margin-right:20px;margin-left:5px;' type='radio' class='" .. ( val.class or "") .. "' name='" .. val.name .. "'") - if (tostring(val.value) == tostring(v1.value)) then io.write(" checked='yes'") end - io.write(" value='" .. v1.value .. "'>") - end - io.write("</input></span>") - else - io.write(html.form[val.type](val)) - end - if (val.descr) and (#val.descr > 0) then io.write("\n\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>") end - if (#val.errtxt > 0) then io.write("\n\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>") end - io.write("\n\t\t</DD>\n") - end - end -end -?> <H1>SYSTEM INFO</H1> <DL> @@ -49,7 +18,6 @@ displayinfo(myform,tags,"viewonly") ?> </DL> - <form name="myform" action="" method="POST"> <h1>CONFIGURATION</h1> <H2>Expert config</H2> @@ -76,38 +44,15 @@ displayinfo(myform,tags) ?> </DL> -</form> - -<? -- MANAGEMENT BUTTONS -local cmdform = form.management -local cmdresult = form.cmdmanagement -local tags = { "start", "stop", "restart" } -if (cmdform) and (cmdform[tags[1]]) then - - io.write('<form name="management" action="" method="POST">') - io.write('<H1>MANAGEMENT</H1>') - io.write('<dl>') - io.write('<dt>' .. cmdform[tags[1]]["label"] .. '</dt>') - io.write('<dd>') - for k,v in pairs(tags) do - if (cmdform[v]) then - io.write(html.form[cmdform[v].type](cmdform[v])) - end - end - io.write('</dd>') - - if (cmdresult) and (cmdresult.action) and (#cmdresult.action.descr > 0) then - io.write('<dt>' .. cmdresult.label .. '</dt>') - io.write('<dd><pre>' .. cmdresult.action.descr .. '</pre></dd>') - end - io.write('</dl></form>') -end ?> - <? ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] +-- Management buttons +local myform = form.management +local tags = { "start", "stop", "restart" } +if (myform) then + io.write("<H1>MANAGEMENT</H1>\n<DL>") + displaymanagement(myform,tags) + io.write("</DL>") +end ?> +</form> diff --git a/opennhrp-logfile-html.lsp b/opennhrp-logfile-html.lsp index 6f8f8f7..934494a 100644 --- a/opennhrp-logfile-html.lsp +++ b/opennhrp-logfile-html.lsp @@ -1,5 +1,6 @@ -<? local form = ... ?> - +<? local form = ... +require("viewfunctions") +?> <? --[[ DEBUG INFORMATION io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") @@ -7,39 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("</span>") --]] ?> -<? -function displayinfo(myform,tags,viewtype) - for k,v in pairs(tags) do - if (myform[v]) and (myform[v]["value"]) then - local val = myform[v] - io.write("\n\t<DT") - if (#val.errtxt > 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "</DT>") - io.write("\n\t\t<DD>") - if (viewtype == "viewonly") then - io.write(val.value) - elseif (val.type == "radio") and (type(val.option) == "table") and (#val.option > 0) then - io.write("<span style='display:inline' class='" .. ( val.class or "") .. "'>") - for k1,v1 in pairs(val.option) do - io.write(tostring(v1.label) .. ":") - io.write("<input style='margin-right:20px;margin-left:5px;' type='radio' class='" .. ( val.class or "") .. "' name='" .. val.name .. "'") - if (tostring(val.value) == tostring(v1.value)) then io.write(" checked='yes'") end - io.write(" value='" .. v1.value .. "'>") - end - io.write("</input></span>") - else - io.write(html.form[val.type](val)) - end - if (val.descr) and (#val.descr > 0) then io.write("\n\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>") end - if (#val.errtxt > 0) then io.write("\n\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>") end - io.write("\n\t\t</DD>\n") - end - end -end -?> <H1>SYSTEM INFO</H1> <DL> @@ -68,11 +36,3 @@ io.write(html.form[myform.filecontent.type](myform.filecontent)) ?> </form> - -<? ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] -?> diff --git a/opennhrp-model.lua b/opennhrp-model.lua index 41c1569..224155a 100644 --- a/opennhrp-model.lua +++ b/opennhrp-model.lua @@ -1,16 +1,19 @@ module(..., package.seeall) +-- Load libraries require("fs") require("procps") require("getopts") require("format") require("daemoncontrol") require("validator") +require("processinfo") +-- Set variables local configfile = "/etc/opennhrp/opennhrp.conf" local processname = "opennhrp" +local packagename = "opennhrp" local baseurl = "/etc/opennhrp/" - local descr = { Type = { ['incomplete']="The protocol address is being resolved", @@ -27,33 +30,8 @@ local descr = { }, } -local function get_version() - local cmd_output_result, cmd_output_error - local cmd = "/sbin/apk_version -vs " .. processname .." 2>/dev/null" - local f = io.popen( cmd ) - local cmdresult = f:read("*l") - if (cmdresult) and (#cmdresult > 0) then - cmd_output_result = string.match(cmdresult,"^%S*") or "Unknown" - else - cmd_output_error = "Program not installed" - end - f:close() - return cmd_output_result,cmd_output_error -end - -local function autostarts() - local cmd_output_result, cmd_output_error - local cmd = "/sbin/rc_status | egrep '^S' | egrep '" .. processname .."' 2>/dev/null" - local f = io.popen( cmd ) - local cmdresult = f:read("*a") - if (cmdresult) and (#cmdresult > 0) then - cmd_output_result = "Process will autostart at next boot (at sequence '" .. string.match(cmdresult,"^%a+(%d%d)") .. "')" - else - cmd_output_error = "Not programmed to autostart" - end - f:close() - return cmd_output_result,cmd_output_error -end +-- ################################################################################ +-- LOCAL FUNCTIONS local function opennhrpctl_show() local cmd_output_result={} @@ -114,13 +92,13 @@ function startstop_service ( self, action ) local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, cmd) action.descr=cmdmessage action.errtxt=cmderror - -- Reporting back (true|false, the original acition) return cmdresult,action end function getstatus() local status = {} - local value, errtxt = get_version() + + local value, errtxt = processinfo.package_version(packagename) status.version = cfe({ name = "version", label="Program version", value=value, @@ -131,7 +109,8 @@ function getstatus() label="Program status", value=process_status_text(processname), }) - local autostart_sequense, autostart_errtxt = autostarts() + + local autostart_sequense, autostart_errtxt = processinfo.process_botsequence(processname) status.autostart = cfe({ name="autostart", label="Autostart sequence", value=autostart_sequense, diff --git a/opennhrp-status-html.lsp b/opennhrp-status-html.lsp index e47e942..5c16c8f 100644 --- a/opennhrp-status-html.lsp +++ b/opennhrp-status-html.lsp @@ -1,4 +1,6 @@ -<? local form = ... ?> +<? local form = ... +require("viewfunctions") +?> <? --[[ DEBUG INFORMATION io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") @@ -7,30 +9,12 @@ io.write("</span>") --]] ?> -<? -function informationform(myform,tags) - for k,v in pairs(tags) do - if (myform[v]) then - local val = myform[v] - io.write("\t<DT") - if (#val.errtxt > 0) then io.write(" class='error'") end - io.write(">" .. val.label .. "</DT>\n") - - io.write("\t\t<DD>" .. val.value .. "\n") - if (val.descr) and (#val.descr > 0) then io.write("\t\t<P CLASS='descr'>" .. string.gsub(val.descr, "\n", "<BR>") .. "</P>\n") end - if (#val.errtxt > 0) then io.write("\t\t<P CLASS='error'>" .. string.gsub(val.errtxt, "\n", "<BR>") .. "</P>\n") end - io.write("\t\t</DD>\n") - end - end -end -?> - <H1>SYSTEM INFO</H1> <DL> <? local myform = form.status local tags = { "status", "version", "autostart", } -informationform(myform,tags) +displayinfo(myform,tags,"viewonly") ?> </DL> @@ -85,11 +69,3 @@ io.write("\t\t</DD>\n") ?> </DL> -<? ---[[ DEBUG INFORMATION -io.write("<H1>DEBUGGING</H1><span style='color:red'><H2>DEBUG INFO: CFE</H2>") -io.write(html.cfe_unpack(form)) -io.write("</span>") ---]] -?> - |