summaryrefslogtreecommitdiffstats
path: root/lbu-model.lua
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2008-06-05 14:06:22 +0000
committerTed Trask <ttrask01@yahoo.com>2008-06-05 14:06:22 +0000
commitab9e94b01060564436b9ba4eef0a2ab1465e7b83 (patch)
tree3fde3c8f315e234c51867fe946f6a48eb526b247 /lbu-model.lua
parentdfcf164b909dc654e42820cce73baf8aeb54a26b (diff)
downloadacf-alpine-conf-ab9e94b01060564436b9ba4eef0a2ab1465e7b83.tar.bz2
acf-alpine-conf-ab9e94b01060564436b9ba4eef0a2ab1465e7b83.tar.xz
Updated lbu to use new cfe model and components
git-svn-id: svn://svn.alpinelinux.org/acf/alpine-conf/trunk@1197 ab2d0c66-481e-0410-8bed-d214d4d58bed
Diffstat (limited to 'lbu-model.lua')
-rw-r--r--lbu-model.lua400
1 files changed, 203 insertions, 197 deletions
diff --git a/lbu-model.lua b/lbu-model.lua
index 62e008f..7a7ff1e 100644
--- a/lbu-model.lua
+++ b/lbu-model.lua
@@ -8,6 +8,8 @@ require("daemoncontrol")
-- Set variables
local configfile = "/etc/lbu/lbu.conf"
+local includefile = "/etc/lbu/include"
+local excludefile = "/etc/lbu/exclude"
-- ################################################################################
-- LOCAL FUNCTIONS
@@ -83,266 +85,270 @@ local function getincexl(state)
return incexl
end
+local function validateconfig(config)
+ -- Set errormessages when configs are wrong
+ config.value.LBU_MEDIA.errtxt = "Invalid Media"
+ for i,media in ipairs(config.value.LBU_MEDIA.option) do
+ if media == config.value.LBU_MEDIA.value then
+ config.value.LBU_MEDIA.errtxt = nil
+ break
+ end
+ end
+
+ config.value.DEFAULT_CIPHER.errtxt = "Invalid Cipher"
+ for i,cipher in ipairs(config.value.DEFAULT_CIPHER.option) do
+ if cipher == config.value.DEFAULT_CIPHER.value then
+ config.value.DEFAULT_CIPHER.errtxt = nil
+ break
+ end
+ end
+
+ if config.value.PASSWORD.value == "" and config.value.ENCRYPTION.value then
+ config.value.PASSWORD.errtxt = "Encryption without password is not allowed!\nDeactivate password protection or configure a password!"
+ end
+
+ for name,value in pairs(config.value) do
+ if value.errtxt then
+ config.errtxt = "Invalid Config"
+ break
+ end
+ end
+
+ return config
+end
+
+local function replacestring (string, find, replace)
+ local count
+ string,count = string.gsub(string, find, replace)
+ if 0 == count then
+ string = string .. replace
+ end
+ return string
+end
+
+local function validatefilelist(filelist)
+ local errors = {}
+ local files = {}
+ for line in string.gmatch(format.dostounix(filelist.value).."\n", "([^\n]*)\n") do
+ if not string.match(line, "^%s*$") then
+ local glob = posix.glob("/"..line)
+ if not glob then
+ errors[#errors+1] = '"' .. tostring(line) .. '" not a valid file/directory'
+ files[#files+1] = line
+ else
+ for i,value in pairs(glob) do
+ files[#files+1] = string.gsub(value, "^/+", "")
+ end
+ end
+ end
+ end
+ filelist.value = table.concat(files, "\n")
+ if #errors ~= 0 then
+ filelist.errtxt = table.concat(errors, "\n")
+ end
+ return filelist
+end
+
+local function validatefilecontent (filecontent)
+ local config = getconfig(filecontent.value)
+ local errors = {}
+ for name,value in pairs(config.value) do
+ if value.errtxt then
+ errors[#errors+1] = value.errtxt
+ end
+ end
+ if #errors > 0 then
+ filecontent.errtxt = table.concat(errors, "\n")
+ end
+
+ return filecontent
+end
+
-- ################################################################################
-- PUBLIC FUNCTIONS
function getstatus ()
- local path = configfile
local status = {}
- local statustxt
- local descrtxt
- if (#list() == 0) then
- statustxt = "There is no uncommited files"
- else
- statustxt = "WARNING!"
- descrtxt = "Until you commit, you will lose your changes at next reboot/shutdown!"
- end
- local config = getconfig()
status["version"] = cfe({
- name="status",
value=get_version(),
label="Program version",
})
- status["status"] = cfe({
- name="status",
- value=statustxt,
+ status["committed"] = cfe({
+ type="boolean",
+ value=(#list().value==0),
descr=descrtxt,
label="Program status",
})
- status["LBU_MEDIA"] = config["LBU_MEDIA"]
- if (config["ENCRYPTION"]["checked"]) then
- status["ENCRYPTION"] = config["ENCRYPTION"]
- status["ENCRYPTION"]["descr"] = "Using cipher '".. config.DEFAULT_CIPHER.value .. "' for encryption"
+ local config = getconfig()
+ status["LBU_MEDIA"] = config.value.LBU_MEDIA
+ if (config.value.ENCRYPTION.value) then
+ status["DEFAULT_CIPHER"] = config.value.DEFAULT_CIPHER
end
- return status
+ return cfe({ type="group", value=status, label="LBU Status" })
end
-function list(self)
+function list()
local ret = {}
local lbuStatus = getLbuStatus()
for k,v in pairs(lbuStatus) do
ret[#ret + 1] = { name=k, status=v }
end
table.sort(ret, function(a,b) return (a.name < b.name) end)
- return ret
-end
-
-function getcommit(self, flag)
- --See to that only allowed flags are passed to the process
- flag = string.match(flag or "", "%-%a") or ""
- return getLbuCommit("-v " .. flag)
-end
-function getsimulate(self, flag)
- --See to that only allowed flags are passed to the process
- flag = string.match(flag or "", "%-%a") or ""
- return getLbuCommit("-n " .. flag)
+ return cfe({ type="structure", value=ret, label="List of changes" })
end
-function getconfig ()
- local path = configfile
+function getconfig (configcontents)
local config = {}
local configopts = {}
- local errors = {}
- if (fs.is_file(path)) then
- configopts = getopts.getoptsfromfile(path, "") or {}
- end
- local lbumedias = availablemedias()
- if not (configopts.LBU_MEDIA) then
- local lbumessage = "No default"
- table.insert(lbumedias, lbumessage)
- config.LBU_MEDIA = lbumessage
- else
- config.LBU_MEDIA = configopts.LBU_MEDIA
+ if configcontents then
+ configopts = getopts.getoptsfromfile(configcontents, "") or {}
+ elseif (fs.is_file(configfile)) then
+ configopts = getopts.getoptsfromfile(configfile, "") or {}
end
- config["debug"] = configopts
config["LBU_MEDIA"] = cfe({
- name="LBU_MEDIA",
- value=config["LBU_MEDIA"],
- label="Default media for commit",
- type="select",
- option=lbumedias,
- })
-
- config["lbu_included"]= cfe({
- name="lbu_included",
- option=getincexl("include") or {},
- label="Included item(s)",
+ value=configopts.LBU_MEDIA or "",
+ label="Media for commit",
type="select",
- size=table.maxn(getincexl("include"))+1,
+ option=availablemedias() or {},
})
- if (config["lbu_included"]["size"] == 1) then config["lbu_included"]["size"] = 2 end
-
- config["lbu_excluded"]= cfe({
- name="lbu_excluded",
- option=getincexl("exclude") or {},
- label="Excluded item(s)",
- type="select",
- size=table.maxn(getincexl("exclude"))+1,
- })
- if (config["lbu_excluded"]["size"] == 1) then config["lbu_excluded"]["size"] = 2 end
config["ENCRYPTION"] = cfe({
- name="ENCRYPTION",
- value="yes",
+ value=(configopts.ENCRYPTION ~= nil),
label="Password protected commits",
- type="checkbox",
+ type="boolean",
})
- if (configopts["ENCRYPTION"]) and (#configopts["ENCRYPTION"] ~= 0) then
- config["ENCRYPTION"]["checked"] = "yes"
- end
config["DEFAULT_CIPHER"] = cfe({
- name="DEFAULT_CIPHER",
- value=configopts["DEFAULT_CIPHER"],
- label="Cipher to use at encryption",
+ value=configopts.DEFAULT_CIPHER or "",
+ label="Cipher to use for encryption",
option=getciphers() or {},
type="select",
})
config["PASSWORD"] = cfe({
- name="PASSWORD",
- value=configopts["PASSWORD"],
+ value=configopts.PASSWORD or "",
label="Password when encrypting",
})
- -- Next section is to print errormessages when configs are wrong
- if (configopts["LBU_MEDIA"] == "") or (configopts["LBU_MEDIA"] == nil) then
- config.LBU_MEDIA.errtxt = "'Media' needs to be configured!"
- end
- if (configopts["PASSWORD"] == nil) and (configopts["ENCRYPTION"] ~= nil) then
- config.PASSWORD.errtxt = "Encryption without password is not allowed!<BR> Deactivate 'Password protection' or configure a password!"
- end
- for k,v in pairs(errors) do
- config["errors"] = v
+ retval = cfe({ type="group", value=config, label="LBU Config" })
+ validateconfig(retval)
+
+ return retval
+end
+
+function setconfig (config)
+ validateconfig(config)
+
+ if not config.errtxt then
+ local content = (fs.read_file(configfile) or "").."\n"
+
+ -- LBU_MEDIA, ENCRYPTION, DEFAULT_CIPHER, PASSWORD
+ content = replacestring(content, "#*LBU_MEDIA=[^\n]*\n", "LBU_MEDIA="..config.value.LBU_MEDIA.value.."\n")
+ local newstring = "ENCRYPTION=$DEFAULT_CIPHER\n"
+ if not config.value.ENCRYPTION.value then
+ newstring = "#" .. newstring
+ end
+ content = replacestring(content, "#*ENCRYPTION=[^\n]*\n", newstring)
+ content = replacestring(content, "#*DEFAULT_CIPHER=[^\n]*\n", "DEFAULT_CIPHER="..config.value.DEFAULT_CIPHER.value.."\n")
+ content = replacestring(content, "#*PASSWORD=[^\n]*\n", "PASSWORD="..config.value.PASSWORD.value.."\n")
+ content = string.gsub(content,"\n*$","")
+
+ -- Write changes to file
+ fs.write_file(configfile,content)
end
return config
end
-function lbuincexcl(self,state,value,addremove)
- local incexl = nil
- if (string.lower(addremove or "") == "add") then
- addremove = ""
- elseif (string.lower(addremove or "") == "remove") then
- addremove = "-r"
- else
- return "Function setincexl() - Invalid option! Use add|remove when calling this function!"
- end
- if (string.lower(state) == "include") or (string.lower(state) == "exclude") then
- local f = io.popen("/sbin/lbu " .. string.lower(state) .. " " .. addremove .. " -v " .. value .." 2>&1", "r")
- if not (f) then return incexl end
- incexl = f:read("*a")
- f:close()
- else
- return "Function setincexl() - Invalid command! Use included|excluded when calling this function!"
- end
- return incexl
+function getincluded ()
+ local included = cfe({
+ value=fs.read_file(includefile) or "",
+ label="Included item(s)",
+ type="longtext",
+ descr="List one file/directory per line\nAssumes root directory, no leading '/' necessary",
+ })
+
+ validatefilelist(included)
+
+ return included
end
-function editconfig (self,variable,value)
- local configfilecontent = nil
- local path = configfile
- local cmdoutput = nil
- if not (fs.is_file(path)) then
- fs.write_file(path,"")
- end
- local deactivate = ""
- if not (value) or (value == "") then
- deactivate = "#"
- end
- if (variable == "LBU_MEDIA") then
- local errtxt
- local lbumedias = availablemedias()
- for k,v in pairs(lbumedias) do
- errtxt = ""
- if (value == v) then break end
- errtxt = "Not a valid media!"
- end
- if (#errtxt > 0) then
- return errtxt
- end
+function setincluded (included)
+ validatefilelist(included)
+ if not included.errtxt then
+ fs.write_file(includefile, included.value)
end
+ return included
+end
- -- Hardcode some parameters (e.g for some checkboxes)
- if (variable == "ENCRYPTION") then value = "DEFAULT_CIPHER" end
- configfilecontent = fs.read_file_as_array(path) or {}
- -- Search if the variable is defined in the file
- for k,v in pairs(configfilecontent) do
- if (string.match(v,"^%s*%#*%s*" .. variable)) then
- cmdoutput = format.search_replace(configfilecontent,"^%s*%#*%s*" .. variable .. ".*$", deactivate .. variable .. "=" .. value)
- break
- end
- end
- --If variable is not changed (didn't exist) then create a new row with this variable
- if not (cmdoutput) then
- cmdoutput = configfilecontent
- table.insert(cmdoutput,deactivate .. variable .. "=" .. (value or ""))
+function getexcluded ()
+ local excluded = cfe({
+ value=fs.read_file(excludefile) or "",
+ label="Excluded item(s)",
+ type="longtext",
+ descr="List one file/directory per line\nAssumes root directory, no leading '/' necessary",
+ })
+
+ validatefilelist(excluded)
+
+ return excluded
+end
+
+function setexcluded (excluded)
+ validatefilelist(excluded)
+ if not excluded.errtxt then
+ fs.write_file(excludefile, excluded.value)
end
- -- Write changes to file
- fs.write_file(path,table.concat(cmdoutput,"\n"))
- return value
+ return excluded
end
+
function get_filedetails()
- local path = configfile
- local file = {}
- local filedetails = {}
- local config = {}
- local filenameerrtxt
- if (fs.is_file(path)) then
- filedetails = fs.stat(path)
- config = getconfig(path)
+ local filename = cfe({ value=configfile, label="File name" })
+ local filecontent = cfe({ type="longtext", label="Config file" })
+ local filesize = cfe({ value="0", label="File size" })
+ local mtime = cfe({ value="---", label="File date" })
+
+ if fs.is_file(configfile) then
+ local filedetails = fs.stat(configfile)
+ filecontent.value = fs.read_file(configfile)
+ filesize.value = filedetails.size
+ mtime.value = filedetails.mtime
+
+ validatefilecontent(filecontent)
else
- config = {}
- config.filename = {}
- config["filename"]["errtxt"]="Config file '".. path .. "' is missing!"
+ filename.errtxt = "File not found"
end
- file["filename"] = cfe({
- name="filename",
- label="File name",
- value=path,
- errtxt=filenameerrtxt
- })
- file["filesize"] = cfe({
- name="filesize",
- label="File size",
- value=filedetails.size or 0,
- })
- file["mtime"] = cfe({
- name="mtime",
- label="File date",
- value=filedetails.mtime or "---",
- })
- file["filecontent"] = cfe({
- type="longtext",
- name="filecontent",
- label="File content",
- value=fs.read_file(path),
- })
+ return cfe({ type="group", value={filename=filename, filecontent=filecontent, filesize=filesize, mtime=mtime}, label="Config file details" })
+end
- -- Sum all errors into one cfe
- local sumerrors = ""
- for k,v in pairs(config) do
- if (config[k]) and (config[k]["errtxt"]) and (config[k]["errtxt"] ~= "") then
- sumerrors = sumerrors .. config[k]["errtxt"] .. "\n"
- end
- end
- if (sumerrors ~= "") then
- file["sumerrors"] = cfe ({
- name="sumerrors",
- label = "Configuration errors",
- errtxt = string.match(sumerrors, "(.-)\n$"),
- })
+function set_filecontent (filecontent)
+ filecontent.value = format.dostounix(filecontent.value)
+ validatefilecontent(filecontent)
+ if not filecontent.errtxt then
+ fs.write_file(configfile, filecontent.value)
end
-
- return file
+ return filecontent
end
-function update_filecontent (self, modifications)
- local path = configfile
- local file_result,err = fs.write_file(path, format.dostounix(modifications))
- return file_result, err
+
+function getcommit()
+ local simulate = cfe({ type="boolean", value=false, label="Simulate a commit" })
+ local delete = cfe({ type="boolean", value=false, label="Remove existing overlays on commit" })
+ return cfe({ type="group", value={simulate=simulate, delete=delete}, label="Commit changes" })
end
+function commit(input)
+ if input.value.simulate.value and input.value.delete.value then
+ input.errtxt = "Cannot delete overlays when simulating"
+ else
+ local flag = "-v"
+ if input.value.simulate.value then flag=flag.." -n" end
+ if input.value.delete.value then flag=flag.." -d" end
+ input.descr = getLbuCommit(flag)
+ end
+
+ return input
+end