diff options
author | Mika Havela <mika.havela@gmail.com> | 2008-04-08 15:12:27 +0000 |
---|---|---|
committer | Mika Havela <mika.havela@gmail.com> | 2008-04-08 15:12:27 +0000 |
commit | 814afcfdeeb9e92f7c7ce02f8176b9a9562580ef (patch) | |
tree | 3cac821f2855252067766998f55a142a6bd7728f | |
parent | 45f598e8eb50479e76bd9db538c8babfe0b2e2ef (diff) | |
download | acf-ipsec-tools-814afcfdeeb9e92f7c7ce02f8176b9a9562580ef.tar.bz2 acf-ipsec-tools-814afcfdeeb9e92f7c7ce02f8176b9a9562580ef.tar.xz |
Cleaning up code.
git-svn-id: svn://svn.alpinelinux.org/acf/ipsec-tools/trunk@957 ab2d0c66-481e-0410-8bed-d214d4d58bed
-rw-r--r-- | ipsectools-controller.lua | 65 | ||||
-rw-r--r-- | ipsectools-expert-html.lsp | 80 | ||||
-rw-r--r-- | ipsectools-logfile-html.lsp | 49 | ||||
-rw-r--r-- | ipsectools-model.lua | 44 | ||||
-rw-r--r-- | ipsectools-status-html.lsp | 45 |
5 files changed, 63 insertions, 220 deletions
diff --git a/ipsectools-controller.lua b/ipsectools-controller.lua index ba0f667..9fa758c 100644 --- a/ipsectools-controller.lua +++ b/ipsectools-controller.lua @@ -1,23 +1,18 @@ module(..., package.seeall) --- This is the object/text used when we want to add a new record - +-- Load libraries require("format") -local list_redir = function (self) +-- ################################################################################ +-- LOCAL FUNCTIONS + +local function list_redir (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", @@ -35,22 +30,39 @@ 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 "" local modifications2 = self.clientdata.filecontent2 or "" if ( self.clientdata.cmdsave ) then @@ -61,7 +73,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", @@ -71,7 +83,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() @@ -89,16 +101,14 @@ expert = function (self) file.cmdsave.descr="* Changes has been saved!" 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, @@ -106,11 +116,10 @@ expert = function (self) file2 = file2, 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/ipsectools-expert-html.lsp b/ipsectools-expert-html.lsp index 39c45cd..f7ecb4e 100644 --- a/ipsectools-expert-html.lsp +++ b/ipsectools-expert-html.lsp @@ -1,43 +1,6 @@ <? local 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>") ---]] -?> -<? -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 +require("viewfunctions") ?> <H1>SYSTEM INFO</H1> @@ -49,7 +12,6 @@ displayinfo(myform,tags,"viewonly") ?> </DL> - <form name="myform" action="" method="POST"> <h1>CONFIGURATION</h1> <H2>Expert config - Config</H2> @@ -92,37 +54,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/ipsectools-logfile-html.lsp b/ipsectools-logfile-html.lsp index 6f8f8f7..f0da163 100644 --- a/ipsectools-logfile-html.lsp +++ b/ipsectools-logfile-html.lsp @@ -1,44 +1,6 @@ <? local 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>") ---]] -?> <? -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 +require("viewfunctions") ?> <H1>SYSTEM INFO</H1> @@ -50,7 +12,6 @@ displayinfo(myform,tags,"viewonly") ?> </DL> - <? local myform = form.logfile ?> <form name="myform" action="" method="POST"> <h1>LOGFILE</h1> @@ -68,11 +29,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/ipsectools-model.lua b/ipsectools-model.lua index d46f476..49f259b 100644 --- a/ipsectools-model.lua +++ b/ipsectools-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/racoon/racoon.conf" local configfile2 = "/etc/ipsec.conf" local processname = "racoon" -local pkgname = "ipsec-tools" +local packagename = "ipsec-tools" local baseurl = "/etc/racoon/" local descr = { @@ -28,34 +31,9 @@ local descr = { }, } -local function get_version() - local cmd_output_result, cmd_output_error - local cmd = "/sbin/apk_version -vs " .. pkgname .." 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 - 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 - -end +-- ################################################################################ +-- LOCAL FUNCTIONS local function ip_xfrm(mode) local cmd_output_result @@ -156,21 +134,25 @@ 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 = processinfo.package_version(packagename) status.version = cfe({ name = "version", label="Program version", - value=get_version(), + value=value, + errtxt=errtxt, }) + status.status = cfe({ name="status", 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/ipsectools-status-html.lsp b/ipsectools-status-html.lsp index 463474c..b74f5d5 100644 --- a/ipsectools-status-html.lsp +++ b/ipsectools-status-html.lsp @@ -1,28 +1,6 @@ <? local 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>") ---]] -?> - -<? -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 +require("viewfunctions") ?> <H1>SYSTEM INFO</H1> @@ -30,7 +8,7 @@ end <? local myform = form.status local tags = { "status", "version", "autostart", } -informationform(myform,tags) +displayinfo(myform,tags,"viewonly") ?> </DL> @@ -67,22 +45,3 @@ for k,v in pairs(myform) do end ?> </DL> -<H3>Policy information</H3> -<DL> -<? -local myform = form.status.ip_xfrm_policy -io.write("<DT>"..myform.label .."</DT>\n") -io.write("<DD><PRE>"..myform.value .."</PRE></DT>\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>") ---]] -?> - |