summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile4
-rw-r--r--lib/cfe.lua15
-rw-r--r--lib/controllerfunctions.lua51
-rw-r--r--lib/modelfunctions.lua62
4 files changed, 131 insertions, 1 deletions
diff --git a/lib/Makefile b/lib/Makefile
index 62a34d6..855e551 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -19,7 +19,9 @@ LIB_DIST=fs.lua\
roles.lua\
processinfo.lua\
viewfunctions.lua\
-
+ controllerfunctions.lua\
+ modelfunctions.lua\
+ cfe.lua\
EXTRA_DIST=README Makefile
DISTFILES=$(LIB_DIST) $(EXTRA_DIST)
diff --git a/lib/cfe.lua b/lib/cfe.lua
new file mode 100644
index 0000000..34a249d
--- /dev/null
+++ b/lib/cfe.lua
@@ -0,0 +1,15 @@
+module(..., package.seeall)
+
+-- create a Configuration Framework Entity (cfe)
+-- returns a table with at least "value", "type", and "label"
+cfe = function ( optiontable )
+ optiontable = optiontable or {}
+ me = { value="",
+ type="text",
+ label="" }
+ for key,value in pairs(optiontable) do
+ me[key] = value
+ end
+ return me
+end
+_G.cfe = cfe
diff --git a/lib/controllerfunctions.lua b/lib/controllerfunctions.lua
new file mode 100644
index 0000000..23e57d4
--- /dev/null
+++ b/lib/controllerfunctions.lua
@@ -0,0 +1,51 @@
+module(..., package.seeall)
+
+function handle_form(self, getFunction, setFunction, clientdata, option, label, descr)
+ local form = getFunction()
+
+ if clientdata[option] then
+ form.errtxt = nil
+ for name,value in pairs(form.value) do
+ value.errtxt = nil
+ if value.type == "boolean" then
+ value.value = (clientdata[name] ~= nil)
+ elseif value.type == "list" then
+ value.value = {}
+ if clientdata[name] and clientdata[name] ~= "" then
+ for ip in string.gmatch(clientdata[name].."\n", "%s*(%S[^\n]*%S)%s*\n") do
+ table.insert(value.value, ip)
+ end
+ end
+ else
+ value.value = clientdata[name] or value.value
+ end
+ end
+ form = setFunction(form)
+ if not form.errtxt then
+ form.descr = descr
+ end
+ form = self:redirect_to_referrer(form)
+ else
+ form = self:redirect_to_referrer() or form
+ end
+
+ form.type = "form"
+ form.option = option
+ form.label = label
+
+ return form
+end
+
+function handle_startstop(self, startstopfunction, getstatusfunction, clientdata)
+ local result
+ if clientdata.action then
+ result = startstopfunction(clientdata.action)
+ end
+ result = self:redirect_to_referrer(result)
+
+ local status = getstatusfunction()
+ status = status.value.status
+
+ return cfe({ type="group", value={status=status, result=result} })
+end
+
diff --git a/lib/modelfunctions.lua b/lib/modelfunctions.lua
new file mode 100644
index 0000000..84c690b
--- /dev/null
+++ b/lib/modelfunctions.lua
@@ -0,0 +1,62 @@
+module(..., package.seeall)
+
+-- Load libraries
+require("procps")
+require("daemoncontrol")
+require("processinfo")
+
+local function process_status_text(procname)
+ local t = procps.pidof(procname)
+ if (t) and (#t > 0) then
+ return "Enabled"
+ else
+ return "Disabled"
+ end
+end
+
+function startstop_service(processname, action)
+ -- action is validated in daemoncontrol
+ local cmdresult,cmdmessage,cmderror,cmdaction = daemoncontrol.daemoncontrol(processname, action)
+ return cfe({ type="boolean", value=cmdresult, descr=cmdmessage, errtxt=cmderror, label="Start/Stop result" })
+end
+
+function getstatus(processname, packagename, label)
+ local status = {}
+
+ local value, errtxt = processinfo.package_version(packagename)
+ status.version = cfe({
+ label="Program version",
+ value=value,
+ errtxt=errtxt,
+ })
+
+ status.status = cfe({
+ label="Program status",
+ value=process_status_text(processname),
+ })
+
+ local autostart_sequence, autostart_errtxt = processinfo.process_botsequence(processname)
+ status.autostart = cfe({
+ label="Autostart sequence",
+ value=autostart_sequence,
+ errtxt=autostart_errtxt,
+ })
+
+ return cfe({ type="group", value=status, label=label })
+end
+
+function getfiledetails(file)
+ local filename = cfe({ value=file, label="File name" })
+ local filecontent = cfe({ type="longtext", label="File content" })
+ local filesize = cfe({ value="0", label="File size" })
+ local mtime = cfe({ value="---", label="File date" })
+ if fs.is_file(file) then
+ local filedetails = fs.stat(file)
+ filecontent.value = fs.read_file(file)
+ filesize.value = filedetails.size
+ mtime.value = filedetails.mtime
+ else
+ filename.errtxt = "File not found"
+ end
+ return cfe({ type="group", value={filename=filename, filecontent=filecontent, filesize=filesize, mtime=mtime}, label="Config file details" })
+end