summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2012-12-05 00:29:18 +0000
committerTed Trask <ttrask01@yahoo.com>2012-12-05 00:30:02 +0000
commitf8e81a44e9229f72b73582be8f8e1548632dc0f4 (patch)
tree08098853f9faed396053b61b9c089a3621db83bb
parenteb11f2e01942655779b4eb8b6952ad632d7bfb8c (diff)
downloadacf-alpine-baselayout-f8e81a44e9229f72b73582be8f8e1548632dc0f4.tar.bz2
acf-alpine-baselayout-f8e81a44e9229f72b73582be8f8e1548632dc0f4.tar.xz
Interfaces - made iface a simple structure, rather than a class
The class was only confusing things. Removed several functions and made the rest simple local functions.
-rw-r--r--interfaces-model.lua257
1 files changed, 100 insertions, 157 deletions
diff --git a/interfaces-model.lua b/interfaces-model.lua
index 548dcb1..8b7e878 100644
--- a/interfaces-model.lua
+++ b/interfaces-model.lua
@@ -6,19 +6,13 @@ require("modelfunctions")
fs = require("acf.fs")
format = require("acf.format")
-local processname = "networking"
+local servicename = "networking"
local filename = "/etc/network/interfaces"
local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin "
--- iface is a local (private) table with private methods for managing
--- the interfaces file. All low-level stuff is done here. It exposes
--- the iface.tags, file(raw), and array (parsed), as well as a number
--- of functions for managing the interface file. These are in a local
--- table because nobody outside the model should know anything about
--- the interfaces file (not even where it is - it could be in an LDAP
--- directory for all we know) The public module functions are defined
--- further below
+local array
+-- iface is a local table with cfes for the various parts of interface definitions
local iface = { tags = { comment = {type="longtext", label="Comments", seq=2},
auto = {type="boolean", value=false, label="Auto bring-up", seq=3},
name = {label="Interface Name", seq=1},
@@ -76,32 +70,10 @@ local iface = { tags = { comment = {type="longtext", label="Comments", seq=2},
ttl = {label="TTL setting"} },
},
},
- -- lowlevel functions will insert file and array here
}
--- Lowlevel functions - they do things directly to iface.
-iface.read_file = function ()
- iface.file = cfe({ type="longtext", label=filename })
- local file = io.open(filename)
- local rc = false
- if file then
- iface.file.value = file:read("*a")
- -- make sure it has a terminating \n
- iface.file.value = string.gsub (iface.file.value, "([^\n])$", "%1\n")
- file:close()
- rc = true
- end
- return rc
-end
-
-iface.write_file = function ()
- local rc = true
- fs.write_file(iface.file.label, iface.file.value)
- return rc
-end
-
-iface.iface_type = function (family, method)
+local get_iface_type = function (family, method)
local f = {}
for name,table in pairs(iface.tags) do
f[name] = cfe(table)
@@ -127,14 +99,14 @@ iface.iface_type = function (family, method)
return cfe({ type="group", value=f, label="Interface description" })
end
--- return true or false + the index of iface.array matching "name"
-iface.index = function (name)
+-- return true or false + the index of array entry matching "name"
+local arrayindex = function (name)
if name and #name > 0 then
- if iface.array == nil then
- iface.unpack_interfaces ()
+ if array == nil then
+ unpack_interfaces ()
end
- for k,v in ipairs(iface.array) do
+ for k,v in ipairs(array) do
if name == v.value.name.value then
return true, k
end
@@ -144,7 +116,7 @@ iface.index = function (name)
return false, 0
end
-iface.append = function (self, value, prefix)
+local appendentry = function (self, value, prefix)
self = self or ""
-- if we already have some values, then append a newline
if #self > 0 then self = self .. "\n" end
@@ -154,7 +126,7 @@ iface.append = function (self, value, prefix)
return self .. str
end
-iface.expand = function (self, prefix)
+local expandentry = function (self, prefix)
if #self == 0 then
return ""
end
@@ -170,26 +142,27 @@ iface.expand = function (self, prefix)
return table.concat(strings, "\n")
end
-iface.unpack_interfaces = function ()
- if iface.array == nil then
- iface.read_file()
- iface.array = {}
-
- -- call it array so we don't have to call it iface.array everywhere
- local array = iface.array
+-- This function parses the interfaces file and creates array
+local unpack_interfaces = function ()
+ if array == nil then
+ local filecontent = fs.read_file(filename)
+ -- make sure it has a terminating \n
+ filecontent = string.gsub (filecontent, "([^\n])$", "%1\n")
+
+ array = {}
local comment = ""
local auto = {}
- for line in string.gmatch ( iface.file.value, "%s*([^\n]*%S)%s*\n?") do
+ for line in string.gmatch ( filecontent, "%s*([^\n]*%S)%s*\n?") do
-- it can be #, auto, iface, or a parameter
if string.match(line, "^#") then
- comment = iface.append(comment, line , "#%s*" )
+ comment = appendentry(comment, line , "#%s*" )
elseif string.match(line, "^auto") then
local name = string.match(line, "auto%s+(%S+)")
auto[name] = true
elseif string.match(line, "^iface") then
local name, family, method = string.match(line, "%S+%s+(%S+)%s+(%S+)%s+(%S+)")
- array[#array + 1] = iface.iface_type(family, method)
+ array[#array + 1] = get_iface_type(family, method)
local interface = array[#array].value
interface.comment.value = comment
comment = ""
@@ -201,14 +174,14 @@ iface.unpack_interfaces = function ()
if (param) then
local interface = array[#array].value
if comment ~= "" then
- interface.comment.value = iface.append(interface.comment.value, comment)
+ interface.comment.value = appendentry(interface.comment.value, comment)
comment = ""
end
if not (interface[param]) then
interface[param] = cfe({label=param, errtxt = "Unknown parameter"})
end
interface[param].value =
- iface.append (interface[param].value, val)
+ appendentry(interface[param].value, val)
end
end
end
@@ -219,16 +192,17 @@ iface.unpack_interfaces = function ()
end
end
- return cfe({ type="list", value=iface.array, label="Interfaces" })
+ return array
end
-iface.pack_interfaces = function ()
+-- This function takes array and writes the interfaces file
+local commit_interfaces = function()
local str = ""
local strings = {}
- for n,int in ipairs(iface.array) do
+ for n,int in ipairs(array) do
local me = int.value
if me.comment.value ~= "" then
- strings[#strings+1] = iface.expand(me.comment.value, "#")
+ strings[#strings+1] = expandentry(me.comment.value, "#")
end
if me.auto.value then
strings[#strings+1] = "auto " .. me.name.value
@@ -238,100 +212,22 @@ iface.pack_interfaces = function ()
for name,entry in pairs(me) do
if name~="comment" and name~="name" and name~="family" and name~="method" and name~="auto"
and entry.value ~= "" then
- strings[#strings+1] = iface.expand(entry.value, "\t"..name)
+ strings[#strings+1] = expandentry(entry.value, "\t"..name)
end
end
strings[#strings+1] = ""
end
- return table.concat(strings, "\n")
-end
-
-iface.commit = function ()
- iface.file.value = iface.pack_interfaces()
- return iface.write_file()
-end
-
-iface.add_after = function (def, name)
- -- if the new def.name is already in the table, then fail
- local rc, idx = iface.index(def.value.name.value)
- if rc == true then
- def.value.name.errtxt = "This interface is already defined"
- rc = false
- else
- rc, def = iface.validate( def )
- end
-
- -- if the name to insert after doesn't exist, just add it to the end
- if rc then
- rc, idx = iface.index(name)
- if rc == false then
- idx = #iface.array
- end
- table.insert( iface.array, idx+1, def )
- rc = iface.commit()
- end
-
- if rc == false then
- def.errtxt = "Failed to create interface"
- end
-
- return def
+ local filecontent = table.concat(strings, "\n")
+ fs.write_file(filename, filecontent)
end
-iface.read = function (name)
- -- if the name is blank, then return a blank iface with error
- iface.unpack_interfaces()
- local rc, idx = iface.index(name)
- if rc == false then
- local ret = iface.iface_type()
- ret.value.name.value = name
- ret.value.name.errtxt = "Interface does not exist"
- return ret
- end
- return iface.array[idx]
-end
-
-iface.update = function (def)
- -- if the def by that name doesn't exist, fail
- local rc, idx = iface.index(def.value.name.value or "" )
- if rc == false then
- def.value.name.errtxt = "This is an invalid interface name"
- else
- rc, def = iface.validate ( def )
- end
-
- if rc then
- iface.array[idx] = def
- rc = iface.commit()
- end
-
- if rc == false then
- def.errtxt = "Failed to update interface"
- end
-
- return def
-end
-
-iface.delete = function (name)
- local rc, idx = iface.index(name)
- if rc then
- table.remove (iface.array, idx )
- rc = iface.commit()
- end
- if rc then
- return "Interface deleted"
- else
- return nil, "Interface not found"
- end
-end
-
-iface.validate = function (def)
+local validate_interface = function (def)
local success = true
-- since the structure is different depending on the family and method ...
local method = ""
if def.value.method then method = def.value.method.value end
- local newdef = iface.iface_type(def.value.family.value, method)
+ local newdef = get_iface_type(def.value.family.value, method)
for name,option in pairs(newdef.value) do
if option.errtxt then
success = false
@@ -359,10 +255,10 @@ iface.validate = function (def)
return success, newdef
end
-iface.list_interfaces = function()
+local list_interfaces = function()
local output = {}
- iface.unpack_interfaces()
- for i,int in ipairs(iface.array) do
+ unpack_interfaces()
+ for i,int in ipairs(array) do
output[#output+1] = int.value.name.value
end
table.sort(output)
@@ -373,27 +269,68 @@ end
-- Public Methods
-------------------------------------------------------------------------------
-get_all_interfaces = iface.unpack_interfaces
+get_all_interfaces = function(self, clientdata)
+ unpack_interfaces()
+ return cfe({ type="group", value=array, label="Interfaces" })
+end
get_iface_by_name = function(self, clientdata)
- return iface.read(clientdata.name or "")
+ -- if the name is blank, then return a blank iface with error
+ unpack_interfaces()
+ local rc, idx = arrayindex(clientdata.name or "")
+ if rc == false then
+ local ret = get_iface_type()
+ ret.value.name.value = name
+ ret.value.name.errtxt = "Interface does not exist"
+ return ret
+ end
+ return array[idx]
end
get_iface = function(self, clientdata)
- return iface.iface_type(clientdata.family, clientdata.method)
+ unpack_interfaces()
+ return get_iface_type(clientdata.family, clientdata.method)
end
-create_iface = function(self, value)
- return iface.add_after(value)
+create_iface = function(self, def)
+ unpack_interfaces()
+ local rc
+ rc, def = validate_interface( def )
+
+ if rc then
+ idx = #array
+ table.insert( array, idx+1, def )
+ commit_interfaces()
+ else
+ def.errtxt = "Failed to create interface"
+ end
+
+ return def
end
-update_iface = function(self, value)
- return iface.update(value)
+update_iface = function(self, def)
+ unpack_interfaces()
+ -- if the def by that name doesn't exist, fail
+ local rc, idx = arrayindex(def.value.name.value or "" )
+ if rc == false then
+ def.value.name.errtxt = "This is an invalid interface name"
+ else
+ rc, def = validate_interface( def )
+ end
+
+ if rc then
+ array[idx] = def
+ commit_interfaces()
+ else
+ def.errtxt = "Failed to update interface"
+ end
+
+ return def
end
get_delete_iface_by_name = function(self, clientdata)
local result = {}
- result.name = cfe({ type="select", value=clientdata.name or "", label="Interface Name", option=iface.list_interfaces() })
+ result.name = cfe({ type="select", value=clientdata.name or "", label="Interface Name", option=list_interfaces() })
return cfe({ type="group", value=result, label="Interface Name" })
end
@@ -402,7 +339,15 @@ delete_iface_by_name = function(self, deleterequest)
local success = modelfunctions.validateselect(deleterequest.value.name)
if success then
- deleterequest.descr, deleterequest.errtxt = iface.delete(deleterequest.value.name.value)
+ unpack_interfaces()
+ local rc, idx = arrayindex(deleterequest.value.name.value)
+ if rc then
+ table.remove (array, idx )
+ commit_interfaces()
+ deleterequest.descr = "Interface deleted"
+ else
+ deleterequest.errtxt = "Interface not found"
+ end
else
deleterequest.errtxt = "Failed to delete interface"
end
@@ -441,8 +386,7 @@ get_file = function ()
end
write_file = function (self, newfile)
- iface.array = nil
- iface.file = nil
+ array = nil
return modelfunctions.setfiledetails(self, newfile, {filename})
end
@@ -482,7 +426,7 @@ end
get_ifup_by_name = function(self, clientdata)
local result = {}
- result.name = cfe({ type="select", value=clientdata.name or "", label="Interface Name", option=iface.list_interfaces() })
+ result.name = cfe({ type="select", value=clientdata.name or "", label="Interface Name", option=list_interfaces() })
return cfe({ type="group", value=result, label="Interface Name" })
end
@@ -508,7 +452,7 @@ end
get_ifdown_by_name = function(self, clientdata)
local result = {}
- result.name = cfe({ type="select", value=clientdata.name or "", label="Interface Name", option=iface.list_interfaces() })
+ result.name = cfe({ type="select", value=clientdata.name or "", label="Interface Name", option=list_interfaces() })
return cfe({ type="group", value=result, label="Interface Name" })
end
@@ -533,11 +477,10 @@ ifdown_by_name = function (self, ifdownrequest)
end
get_restartnetworking = function(self, clientdata)
- --return modelfunctions.get_startstop(processname)
local actions = {}
actions[1] = "restart"
- local service = cfe({ type="hidden", value="networking", label="Service Name" })
- local startstop = cfe({ type="group", label="Restart Networking", value={servicename=service}, option=actions, errtxt=errtxt })
+ local service = cfe({ type="hidden", value=servicename, label="Service Name" })
+ local startstop = cfe({ type="group", label="Restart Networking", value={servicename=service}, option=actions })
return startstop
end