From 910f45e7c9889172631a6554fbf90c70e850984c Mon Sep 17 00:00:00 2001 From: Mika Havela Date: Tue, 8 Apr 2008 15:12:44 +0000 Subject: Cleaning up code. git-svn-id: svn://svn.alpinelinux.org/acf/opennhrp/trunk@958 ab2d0c66-481e-0410-8bed-d214d4d58bed --- opennhrp-controller.lua | 65 +++++++++++++++++++++----------------- opennhrp-expert-html.lsp | 79 +++++++---------------------------------------- opennhrp-logfile-html.lsp | 46 ++------------------------- opennhrp-model.lua | 41 ++++++------------------ 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
 ... 
in BLACK text + errtxt="", --Content of this variable is displayed as
 ... 
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 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -6,39 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("
") --]] ?> - 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "") - io.write("\n\t\t
") - if (viewtype == "viewonly") then - io.write(val.value) - elseif (val.type == "radio") and (type(val.option) == "table") and (#val.option > 0) then - io.write("") - for k1,v1 in pairs(val.option) do - io.write(tostring(v1.label) .. ":") - io.write("") - end - io.write("") - else - io.write(html.form[val.type](val)) - end - if (val.descr) and (#val.descr > 0) then io.write("\n\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

") end - if (#val.errtxt > 0) then io.write("\n\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

") end - io.write("\n\t\t
\n") - end - end -end -?>

SYSTEM INFO

@@ -49,7 +18,6 @@ displayinfo(myform,tags,"viewonly") ?>
-

CONFIGURATION

Expert config

@@ -76,38 +44,15 @@ displayinfo(myform,tags) ?> -
- -') - io.write('

MANAGEMENT

') - io.write('
') - io.write('
' .. cmdform[tags[1]]["label"] .. '
') - io.write('
') - for k,v in pairs(tags) do - if (cmdform[v]) then - io.write(html.form[cmdform[v].type](cmdform[v])) - end - end - io.write('
') - - if (cmdresult) and (cmdresult.action) and (#cmdresult.action.descr > 0) then - io.write('
' .. cmdresult.label .. '
') - io.write('
' .. cmdresult.action.descr .. '
') - end - io.write('
') -end ?> - DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] +-- Management buttons +local myform = form.management +local tags = { "start", "stop", "restart" } +if (myform) then + io.write("

MANAGEMENT

\n
") + displaymanagement(myform,tags) + io.write("
") +end ?> + 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 @@ - - + DEBUGGING

DEBUG INFO: CFE

") @@ -7,39 +8,6 @@ io.write(html.cfe_unpack(form)) io.write("
") --]] ?> - 0) then - val.class = "error" - io.write(" class='error'") - end - io.write(">" .. val.label .. "") - io.write("\n\t\t
") - if (viewtype == "viewonly") then - io.write(val.value) - elseif (val.type == "radio") and (type(val.option) == "table") and (#val.option > 0) then - io.write("") - for k1,v1 in pairs(val.option) do - io.write(tostring(v1.label) .. ":") - io.write("") - end - io.write("") - else - io.write(html.form[val.type](val)) - end - if (val.descr) and (#val.descr > 0) then io.write("\n\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

") end - if (#val.errtxt > 0) then io.write("\n\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

") end - io.write("\n\t\t
\n") - end - end -end -?>

SYSTEM INFO

@@ -68,11 +36,3 @@ io.write(html.form[myform.filecontent.type](myform.filecontent)) ?> - -DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] -?> 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 @@ - + DEBUGGING

DEBUG INFO: CFE

") @@ -7,30 +9,12 @@ io.write("
") --]] ?> - 0) then io.write(" class='error'") end - io.write(">" .. val.label .. "\n") - - io.write("\t\t
" .. val.value .. "\n") - if (val.descr) and (#val.descr > 0) then io.write("\t\t

" .. string.gsub(val.descr, "\n", "
") .. "

\n") end - if (#val.errtxt > 0) then io.write("\t\t

" .. string.gsub(val.errtxt, "\n", "
") .. "

\n") end - io.write("\t\t
\n") - end - end -end -?> -

SYSTEM INFO

@@ -85,11 +69,3 @@ io.write("\t\t\n") ?>
-DEBUGGING

DEBUG INFO: CFE

") -io.write(html.cfe_unpack(form)) -io.write("
") ---]] -?> - -- cgit v1.2.3