module (..., package.seeall) -- Load libraries require("posix") require("validator") -- ################################################################################ -- LOCAL FUNCTIONS local function list_redir(self) self.conf.action = "read" self.conf.type = "redir" error (self.conf) end local function displaycmdmanagement(pidofstatus) -- Add a management buttons local management = {} management.start = cfe({ name="cmdmanagement", label="Program control-panel", value="Start", type="submit", }) management.stop = cfe({ name="cmdmanagement", label="Program control-panel", value="Stop", type="submit", }) management.restart = cfe({ name="cmdmanagement", label="Program control-panel", 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 (pidofstatus) then management.start.disabled = "yes" else management.stop.disabled = "yes" management.restart.disabled = "yes" end return management end local function displaycmdsave(self) -- Add a cmd button to the view local cmdsave = cfe({ name="cmdsave", label="Save/Apply above settings", value="Save", type="submit", }) return cmdsave 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 config(self) local errors = {} local modify_opts = nil local cmdsavereply = {} local cmdsaveresult = {} -- Start/Stop/Restart process local cmdmanagement, actionresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", label="Previous action result", action=cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) }), }) actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) end if (self.clientdata.cmdsavesetstimeonstartup) then if (self.clientdata.setstimeonstartup) 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_delete) then local variables="hosts_cmd_delete" for var in string.gmatch(variables, "%S+") do -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file -- if (self.clientdata[var] == "") then self.clientdata[var] = nil end cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "delete", "server%S?" , string.match(self.clientdata["hosts_list"], "^%s*(%S*)") ) end end if ( self.clientdata.listen_cmd_delete) then local variables="listen_cmd_delete" for var in string.gmatch(variables, "%S+") do -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file -- if (self.clientdata[var] == "") then self.clientdata[var] = nil end cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "delete", "listen on" , string.match(self.clientdata["listen_list"], "^%s*(%S*)") ) end end if ( self.clientdata.hosts_cmd_add) then local variables="hosts_cmd_add" for var in string.gmatch(variables, "%S+") do local freetocommit -- Do some checking before executing the command if not (self.clientdata["hosts_type"]) then cmdsavereply["hosts_type"] = cfe ({ errtxt="You need chose type of host to add!", }) cmdsavereply["hosts_add"] = cfe ({ value=self.clientdata["hosts_add"], }) end if (#self.clientdata["hosts_add"] == 0) then cmdsavereply["hosts_add"] = cfe ({ errtxt="You need to add a valid hostname/IP!", }) end if not ( cmdsavereply["hosts_type"] and cmdsavereply["hosts_type"]["errtxt"]) and not (cmdsavereply["hosts_add"] and cmdsavereply["hosts_add"]["errtxt"]) then -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file -- if (self.clientdata[var] == "") then self.clientdata[var] = nil end cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "add", self.clientdata["hosts_type"] , self.clientdata["hosts_add"] ) end end end if ( self.clientdata.listen_cmd_add) then local variables="listen_cmd_add" for var in string.gmatch(variables, "%S+") do local freetocommit -- Do some checking before executing the command if (#self.clientdata["listen_add"] == 0) then cmdsavereply["listen_add"] = cfe ({ errtxt="You need to add a valid hostname/IP!", }) end if not ( cmdsavereply["listen_add"] and cmdsavereply["listen_add"]["errtxt"]) then -- Send nil instead of "" causes the parameter to be removed/deleted/empty/unset the variable in the config-file -- if (self.clientdata[var] == "") then self.clientdata[var] = nil end cmdsaveresult[var], cmdsavereply[var] = self.model:modify_config( "add", "listen on", self.clientdata["listen_add"] ) end end end local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller local status = self.model:getstatus(self) local config = self.model:getconfig() -- Add buttons config.hosts_cmd_delete = cfe({ name = "hosts_cmd_delete", label="Delete selected host", value="Delete", type="submit", }) config.hosts_cmd_delete.descr="Mark a item in '" .. config.hosts_list.label .. "'-list before pressing [" .. config.hosts_cmd_delete.value .. "]." if (#config.hosts_list.option == 0) then config.hosts_cmd_delete.disabled = "yes" config.hosts_cmd_delete.descr = "" end config.listen_cmd_delete = cfe({ name = "listen_cmd_delete", label="Delete selected host", value="Delete", type="submit", }) config.listen_cmd_delete.descr="Mark a item in '" .. config.listen_list.label .. "'-list before pressing [" .. config.listen_cmd_delete.value .. "]." if (#config.listen_list.option == 0) then config.listen_cmd_delete.disabled = "yes" config.listen_cmd_delete.descr = "" end config.hosts_cmd_add = cfe({ name = "hosts_cmd_add", label="Add selected host", value="Add", type="submit", }) config.hosts_cmd_add.descr="Enter your values in '" .. config.hosts_add.label .. "' and chose '" .. config.hosts_type.label .. "' before pressing [" .. config.hosts_cmd_add.value .. "]." config.listen_cmd_add = cfe({ name = "listen_cmd_add", label="Add new listen address", value="Add", type="submit", }) config.listen_cmd_add.descr="Enter your values in '" .. config.listen_add.label .. "' before pressing [" .. config.listen_cmd_add.value .. "]." -- 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 if (status) and (status.version) and (#status.version.value == 0) then management.start.disabled = "yes" management.stop.disabled = "yes" management.restart.disabled = "yes" end -- Write out erros and descriptions from previous actions for k,v in pairs(cmdsavereply) do if (config[k]) and (cmdsavereply[k]["errtxt"]) and (#cmdsavereply[k]["errtxt"] > 0) then config[k]["errtxt"] = tostring(cmdsavereply[k]["errtxt"]) end if (config[k]) and (cmdsavereply[k]["descr"]) and (#cmdsavereply[k]["descr"] > 0) then config[k]["descr"] = tostring(cmdsavereply[k]["descr"]) end if (config[k]) and (cmdsavereply[k]["value"]) and (#cmdsavereply[k]["value"] > 0) then config[k]["value"] = tostring(cmdsavereply[k]["value"]) end end return ( {status = status, config = config, management = management, url = url, errors = errors, cmdsavereply = cmdsavereply, cmdsaveresult = cmdsaveresult, modify_opts = modify_opts, clientdata = self.clientdata, } ) end function logfile(self) local status = self.model:getstatus(self) local logfile = self.model:get_logfile() return ({ status = status, logfile = logfile, url = url, }) end function status (self) local cmd = self.clientdata.cmd local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller return ( {status = self.model:getstatus(self), url = url } ) end function expert (self) local modifications = self.clientdata.filecontent or "" if ( self.clientdata.cmdsave ) then modifications = self.model:update_filecontent(modifications) end local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller -- Start/Stop/Restart process local cmdmanagement, actionresult if ( self.clientdata.cmdmanagement) then cmdmanagement = cfe({ name="cmdmanagement", label="Previous action result", action=cfe({ name="cmdmanagement", value=string.lower(self.clientdata.cmdmanagement), -- This row contains start/stop/restart (one of these commands) }), }) actionresult, cmdmanagement = self.model:startstop_service( cmdmanagement.action ) end local status = self.model:getstatus(self) local file = self.model:get_filedetails() -- Add buttons file.cmdsave = cfe ({ name="cmdsave", label="Apply settings", value="Apply", type="submit", }) if (self.clientdata.cmdsave) then file.cmdsave.descr="* Changes has been saved!" end -- 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 if (status) and (status.version) and (#status.version.value == 0) then management.start.disabled = "yes" management.stop.disabled = "yes" management.restart.disabled = "yes" end return ( { status = status, file = file, modifications = modifications, management = management, url = url, } ) end