From 148d568b3db38ada9cf0c5085d2c71584d588598 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Sun, 27 Oct 2013 01:43:30 +0000 Subject: Revert "Change function prototypes to pass self and use self.model instead of functions table" This reverts commit fb00ff8f05dae3c110beceb929ea036e4cd14ae1. Conflicts: provisioning-model.lua Conflict was resolved by removing "mymodule." from function calls that are once again made local --- config/delete_device.lua | 10 +- config/determine_class.lua | 6 +- config/process_put.lua | 10 +- config/update_device.lua | 6 +- config/update_device_params.lua | 18 +- provisioning-model.lua | 356 +++++++++++++++++++++------------------- provisioning-scripts.lua | 4 +- upgradeprovisioning | 9 - 8 files changed, 211 insertions(+), 208 deletions(-) diff --git a/config/delete_device.lua b/config/delete_device.lua index 3b04894..e009e74 100644 --- a/config/delete_device.lua +++ b/config/delete_device.lua @@ -1,20 +1,20 @@ -- This is the script run after deleting a device (and all of its params) -local self, olddevice, oldparams = ... +local functions, olddevice, oldparams = ... posix = require("posix") local root = "/var/www/provisioning/htdocs/" ---self.logevent("got to delete_device script") +--APP.logevent("got to delete_device script") -- First, we delete the associated config files for Polycom with valid MAC (not blank or all 0's) if oldparams.value.device and string.match(oldparams.value.device.label, "Polycom") and oldparams.value.device.value.mac and string.match(oldparams.value.device.value.mac.value, "[1-9A-F]") then - --self.logevent("Deleting files for "..oldparams.value.device.value.mac.value) + --APP.logevent("Deleting files for "..oldparams.value.device.value.mac.value) local path = root.."Polycom/" if posix.stat(path, "type") == "directory" then for d in posix.files(path) do if string.match(d, string.lower(oldparams.value.device.value.mac.value)) and posix.stat(path..d, "type") == "regular" then - --self.logevent("deleting "..path..d) + --APP.logevent("deleting "..path..d) os.remove(path..d) end end @@ -56,5 +56,5 @@ setmetatable (env, {__index = _G}) -- so we set env 0, not env 1 setfenv (0, env) local f = loadfile("/etc/provisioning/update_device_params.lua") -if (f) then f(self, params, oldparams) end +if (f) then f(functions, params, oldparams) end setfenv (0, _G) diff --git a/config/determine_class.lua b/config/determine_class.lua index 53a1884..675e6e8 100644 --- a/config/determine_class.lua +++ b/config/determine_class.lua @@ -1,7 +1,7 @@ -- This is the script run to determine the device class from the HTTP user agent -local self, agent, classes = ... +local functions, agent, classes = ... ---self.logevent("got to determine_class script") +--APP.logevent("got to determine_class script") local manufacture, model @@ -16,7 +16,7 @@ elseif string.match(agent, "snom") then model = string.match(agent, "snom(%d+)") end ---self.logevent("Found "..(manufacture or "").." model "..(model or "")) +--APP.logevent("Found "..(manufacture or "").." model "..(model or "")) if manufacture and model then for i,c in ipairs(classes.value) do diff --git a/config/process_put.lua b/config/process_put.lua index 1a34042..4eabb1a 100644 --- a/config/process_put.lua +++ b/config/process_put.lua @@ -1,10 +1,10 @@ -- This is the script run to process uploaded config files -local self, mac, data, device_id = ... +local functions, mac, data, device_id = ... ---self.logevent("got to process_put script") +--APP.logevent("got to process_put script") -- Get the params -local params = self.get_device_params(self, device_id) +local params = functions.get_device_params(device_id) function process_polycom() local before, xml, after = string.match(data, "(.*.*)") @@ -18,7 +18,7 @@ function process_polycom() end -- for n,v in pairs(attrs) do --- self.logevent("name "..n.." val "..(v or "")) +-- APP.logevent("name "..n.." val "..(v or "")) -- end -- Read attributes from attrs and generate paramaters for ACF @@ -57,7 +57,7 @@ if string.match(params.value.device.label, "Polycom") then end -- Update the params -local params = self.set_device_params(self, params) +local params = functions.set_device_params(params) -- Return the updated data return data diff --git a/config/update_device.lua b/config/update_device.lua index 9823e95..7295062 100644 --- a/config/update_device.lua +++ b/config/update_device.lua @@ -1,7 +1,7 @@ -- This is the script run after editing a device - the label, classes -local self, device, olddevice, params, oldparams = ... +local functions, device, olddevice, params, oldparams = ... ---self.logevent("got to update_device script") +--APP.logevent("got to update_device script") -- We'll handle the changing of the device by handling the resulting changing of the params local env = {} @@ -10,5 +10,5 @@ setmetatable (env, {__index = _G}) -- so we set env 0, not env 1 setfenv (0, env) local f = loadfile("/etc/provisioning/update_device_params.lua") -if (f) then f(self, params, oldparams) end +if (f) then f(functions, params, oldparams) end setfenv (0, _G) diff --git a/config/update_device_params.lua b/config/update_device_params.lua index 775385c..c5ec144 100644 --- a/config/update_device_params.lua +++ b/config/update_device_params.lua @@ -1,17 +1,17 @@ -- This is the script run after editing device params -local self, params, oldparams = ... +local functions, params, oldparams = ... posix = require("posix") local root = "/var/www/provisioning/htdocs/" ---self.logevent("got to update_device_params script") +--APP.logevent("got to update_device_params script") local function findip(mac) if not mac or mac == "" then return nil end - local ipaddr = self.getselectresponse("SELECT ip FROM provisioning_requests WHERE mac~*'"..mac.."'") + local ipaddr = functions.getselectresponse("SELECT ip FROM provisioning_requests WHERE mac~*'"..mac.."'") if ipaddr and ipaddr[1] then return ipaddr[1].ip end @@ -20,10 +20,10 @@ end local notify_device = function(mac, extension) local ipaddr = findip(mac) if ipaddr then - --self.logevent("Notifying "..ipaddr.." to update for "..(mac or "")) + --APP.logevent("Notifying "..ipaddr.." to update for "..(mac or "")) os.execute("/etc/provisioning/notify_device "..ipaddr.." "..extension) else - --self.logevent("Warning - could not find IP address for "..(mac or "")) + --APP.logevent("Warning - could not find IP address for "..(mac or "")) end end @@ -34,10 +34,10 @@ if oldparams.value.device and oldparams.value.device.value.mac and oldparams.val if string.match(oldparams.value.device.label, "Polycom") and string.match(oldparams.value.device.value.mac.value, "[1-9A-F]") then local deletefiles = true if string.match(params.value.device.value.mac.value, "[1-9A-F]") then - --self.logevent("Moving files for "..oldparams.value.device.value.mac.value) + --APP.logevent("Moving files for "..oldparams.value.device.value.mac.value) deletefiles = false else - --self.logevent("Deleting files for "..oldparams.value.device.value.mac.value) + --APP.logevent("Deleting files for "..oldparams.value.device.value.mac.value) end local path = root.."Polycom/" if posix.stat(path, "type") == "directory" then @@ -45,10 +45,10 @@ if oldparams.value.device and oldparams.value.device.value.mac and oldparams.val if string.match(d, string.lower(oldparams.value.device.value.mac.value)) and posix.stat(path..d, "type") == "regular" then local newfile = string.gsub(d, string.lower(oldparams.value.device.value.mac.value), string.lower(params.value.device.value.mac.value)) if deletefiles then - --self.logevent("deleting "..path..d) + --APP.logevent("deleting "..path..d) os.remove(path..d) else - --self.logevent("moving "..path..d.." to "..path..newfile) + --APP.logevent("moving "..path..d.." to "..path..newfile) os.rename(path..d, path..newfile) end end diff --git a/provisioning-model.lua b/provisioning-model.lua index 7cf425d..f4ac16f 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -62,7 +62,7 @@ local createdatabase = function() table.insert(result, errtxt) table.insert(result, cmdresult) - logevent(table.concat(result, "\n")) + mymodule.logevent(table.concat(result, "\n")) return table.concat(result, "\n") end @@ -83,11 +83,15 @@ local deletedatabase = function() table.insert(result, cmdresult) end - logevent(table.concat(result, "\n")) + mymodule.logevent(table.concat(result, "\n")) return table.concat(result, "\n") end +-- Declare runsqlcommand first because it's recursive +-- we also have recursion when runsqlcommand calls runscript, so we have to be careful in creating table_creation_scripts +local runsqlcommand + local databaseconnect = function() local result = false local res, err = pcall(function() @@ -100,7 +104,7 @@ local databaseconnect = function() -- Let's create all the tables from the start for n,v in pairs(provdb.table_creation_scripts) do if not string.match(n, "^_") then - mymodule.runsqlcommand("SELECT * FROM "..provdb.escape(n).." LIMIT 1") + runsqlcommand("SELECT * FROM "..provdb.escape(n).." LIMIT 1") end end else @@ -110,15 +114,14 @@ local databaseconnect = function() return result end --- This function is used by scripts, do not change prototype -mymodule.runsqlcommand = function(sql, transaction) -logevent(sql) +runsqlcommand = function(sql, transaction) +mymodule.logevent(sql) return provdb.runsqlcommand(sql, transaction) end --- This function is used by scripts, do not change prototype -mymodule.getselectresponse = function(sql, in_transaction) -logevent(sql) +local getselectresponse +getselectresponse = function(sql, transaction) +mymodule.logevent(sql) return provdb.getselectresponse(sql, transaction) end @@ -143,13 +146,17 @@ validateparam = function(p, allowdefault) return true end +-- These are the functions that may be called from within loaded Lua code +-- The actual functions are added at the end of the file, after they're declared +local functions = {} + local validateparamcoded -validateparamcoded = function(self, p, top) +validateparamcoded = function(p, top) top = top or p local success = true if p.type == "group" then for n,p2 in pairs(p.value) do - success = validateparamcoded(self, p2, top) and success + success = validateparamcoded(p2, top) and success end return success end @@ -163,7 +170,7 @@ validateparamcoded = function(self, p, top) local f = loadstring(p.validate) if (f) then local res, err = pcall(function() - p.value, p.errtxt = f(self.model, p.value, top) + p.value, p.errtxt = f(p.value, functions, top) if p.errtxt then success = false end end) if not res and err then @@ -173,11 +180,10 @@ validateparamcoded = function(self, p, top) end setfenv (0, _G) end - --self.logevent(p.label.." validation "..tostring(success)) return success end -local function callscript(self, script, ...) +local function callscript(script, ...) local result={} local env = {} setmetatable (env, {__index = _G}) @@ -187,7 +193,7 @@ local function callscript(self, script, ...) local f = loadfile(script) if f then local res, err = pcall(function(...) - result = { f(self.model, ...) } + result = { f(functions, ...) } end, ...) if not res and err then assert(res, "Exception in "..script.." script\n"..err) @@ -251,7 +257,7 @@ local checkgroupdefaultoverride = function(device_id) "FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING (class_id) JOIN provisioning_groups g USING(group_id) ".. "JOIN param_groups_to_params g2p USING(group_id) JOIN provisioning_params p USING(param_id) JOIN provisioning_values v USING(param_id))".. "WHERE d2t.device_id='"..provdb.escape(device_id).."' AND v.value IS NOT NULL AND g2p.value IS NOT NULL AND v.value!=g2p.value" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) return (#tmp ~= 0) end @@ -265,7 +271,7 @@ mymodule.list_templates = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT value AS filename, label, seq FROM provisioning_options WHERE param_id = (SELECT param_id FROM provisioning_params WHERE name = 'template') ORDER BY seq ASC, label ASC, value ASC" - retval = mymodule.getselectresponse(sql) + retval = getselectresponse(sql) if connected then provdb.databasedisconnect() end end) if not res and err then @@ -309,7 +315,7 @@ mymodule.get_template = function(self, clientdata) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_options WHERE param_id=(SELECT param_id FROM provisioning_params WHERE name = 'template') AND value='"..provdb.escape(filename).."' ORDER BY seq ASC, label ASC, value ASC" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if tmp and #tmp > 0 then for n,v in pairs(tmp[1]) do if n == "value" then @@ -360,7 +366,7 @@ mymodule.update_template = function(self, template, action, create) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_options WHERE param_id=(SELECT param_id FROM provisioning_params WHERE name = 'template') AND value='"..provdb.escape(template.value.filename.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not create and (not tmp or #tmp == 0) then success = false errtxt = "Template does not exist" @@ -374,7 +380,7 @@ mymodule.update_template = function(self, template, action, create) else sql = "UPDATE provisioning_options SET (label, seq) = ('"..provdb.escape(template.value.label.value).."', '"..provdb.escape(template.value.seq.value).."') WHERE param_id=(SELECT param_id FROM provisioning_params WHERE name = 'template') AND value='"..provdb.escape(template.value.filename.value).."'" end - mymodule.runsqlcommand(sql) + runsqlcommand(sql) fs.write_file(template.value.filename.value, string.gsub(format.dostounix(template.value.filecontent.value), "\n+$", "")) end @@ -408,13 +414,13 @@ mymodule.delete_template = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_options WHERE param_id=(SELECT param_id FROM provisioning_params WHERE name = 'template') AND value='"..provdb.escape(filename).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.filename.errtxt = "Template does not exist" else -- Remove the template sql = "DELETE FROM provisioning_options WHERE param_id=(SELECT param_id FROM provisioning_params WHERE name = 'template') AND value='"..provdb.escape(filename).."'" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) -- Delete the template file os.remove(filename) delreq.errtxt = nil @@ -435,7 +441,7 @@ mymodule.list_class_groups = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_class_groups ORDER BY seq ASC, label ASC" - retval = mymodule.getselectresponse(sql) + retval = getselectresponse(sql) if connected then provdb.databasedisconnect() end end) if not res and err then @@ -458,7 +464,7 @@ mymodule.get_class_group = function(self, clientdata) local connected = databaseconnect() if class_group_id and class_group_id ~= "" then sql = "SELECT * FROM provisioning_class_groups WHERE class_group_id='"..provdb.escape(class_group_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) if tmp and #tmp > 0 then for n,v in pairs(tmp[1]) do if retval[n] then @@ -506,7 +512,7 @@ mymodule.update_class_group = function(self, group, action, create) local connected = databaseconnect() if not create then local sql = "SELECT * FROM provisioning_class_groups WHERE class_group_id='"..provdb.escape(group.value.class_group_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false errtxt = "Group does not exist" @@ -514,12 +520,12 @@ mymodule.update_class_group = function(self, group, action, create) end if success then local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) if create then sql = "INSERT INTO provisioning_class_groups VALUES(DEFAULT, '"..provdb.escape(group.value.name.value).."', '"..provdb.escape(group.value.label.value).."', '"..provdb.escape(group.value.seq.value).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "SELECT class_group_id FROM provisioning_class_groups WHERE label='"..provdb.escape(group.value.label.value).."'" - local tmp = mymodule.getselectresponse(sql, true) + local tmp = getselectresponse(sql, true) if tmp and #tmp>0 then group.value.class_group_id = cfe({value=tmp[1].class_group_id, label="Class Group ID", readonly=true, seq=1}) else @@ -527,11 +533,11 @@ mymodule.update_class_group = function(self, group, action, create) end else sql = "UPDATE provisioning_class_groups SET (name, label, seq) = ('"..provdb.escape(group.value.name.value).."', '"..provdb.escape(group.value.label.value).."', '"..provdb.escape(group.value.seq.value).."') WHERE class_group_id='"..provdb.escape(group.value.class_group_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) end if connected then provdb.databasedisconnect() end end) @@ -564,12 +570,12 @@ mymodule.delete_class_group = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_class_groups WHERE class_group_id='"..provdb.escape(class_group_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.class_group_id.errtxt = "Group does not exist" else sql = "DELETE FROM provisioning_class_groups WHERE class_group_id='"..provdb.escape(class_group_id).."'" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) delreq.errtxt = nil end if connected then provdb.databasedisconnect() end @@ -588,7 +594,7 @@ mymodule.list_classes = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT class_id, g.label AS group, g.name, c.label, c.seq FROM provisioning_classes c JOIN provisioning_class_groups g USING(class_group_id) ORDER BY g.seq ASC, g.label ASC, c.seq ASC, c.label ASC" - retval = mymodule.getselectresponse(sql) + retval = getselectresponse(sql) if connected then provdb.databasedisconnect() end end) if not res and err then @@ -613,7 +619,7 @@ mymodule.get_class = function(self, clientdata) local connected = databaseconnect() if class_id and class_id ~= "" then local sql = "SELECT * FROM provisioning_classes WHERE class_id='"..provdb.escape(class_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if tmp and #tmp > 0 then for n,v in pairs(tmp[1]) do if retval[n] then @@ -623,7 +629,7 @@ mymodule.get_class = function(self, clientdata) end -- Now, get the class-to-paramgroup mappings sql = "SELECT group_id FROM classes_to_param_groups WHERE class_id='"..provdb.escape(class_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,g in ipairs(tmp) do groups[g.group_id] = true end @@ -632,13 +638,13 @@ mymodule.get_class = function(self, clientdata) end -- Get the class_group_id options sql = "SELECT * from provisioning_class_groups ORDER BY seq ASC, label ASC" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,g in ipairs(tmp) do retval.class_group_id.option[#retval.class_group_id.option + 1] = {value=g.class_group_id, label=g.label} end -- Finally, get the paramgroup options sql = "SELECT group_id, name, label FROM provisioning_groups ORDER BY seq ASC, name ASC" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,g in ipairs(tmp) do if not retval.groups.value[g.name] then retval.groups.value[g.name] = cfe({type="select", label=g.name, option={{value="", label=""}}, seq=i}) @@ -683,7 +689,7 @@ mymodule.update_class = function(self, class, action, create) local connected = databaseconnect() if not create then local sql = "SELECT * FROM provisioning_classes WHERE class_id='"..provdb.escape(class.value.class_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false errtxt = "Class does not exist" @@ -691,12 +697,12 @@ mymodule.update_class = function(self, class, action, create) end if success then local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) if create then sql = "INSERT INTO provisioning_classes VALUES(DEFAULT, '"..provdb.escape(class.value.class_group_id.value).."', '"..provdb.escape(class.value.label.value).."', '"..provdb.escape(class.value.seq.value).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "SELECT class_id FROM provisioning_classes WHERE class_group_id='"..provdb.escape(class.value.class_group_id.value).."' AND label='"..provdb.escape(class.value.label.value).."'" - local tmp = mymodule.getselectresponse(sql, true) + local tmp = getselectresponse(sql, true) if tmp and #tmp>0 then class.value.class_id = cfe({value=tmp[1].class_id, label="Class ID", readonly=true, seq=1}) else @@ -704,20 +710,20 @@ mymodule.update_class = function(self, class, action, create) end else sql = "UPDATE provisioning_classes SET (class_group_id, label, seq) = ('"..provdb.escape(class.value.class_group_id.value).."', '"..provdb.escape(class.value.label.value).."', '"..provdb.escape(class.value.seq.value).."') WHERE class_id='"..provdb.escape(class.value.class_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "DELETE FROM classes_to_param_groups WHERE class_id='"..provdb.escape(class.value.class_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end -- Insert the class to group entries for n,g in pairs(class.value.groups.value) do if g.value ~= "" then sql = "INSERT INTO classes_to_param_groups VALUES('"..provdb.escape(class.value.class_id.value).."', '"..provdb.escape(g.value).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) end if connected then provdb.databasedisconnect() end end) @@ -750,18 +756,18 @@ mymodule.delete_class = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_classes WHERE class_id='"..provdb.escape(class_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.class_id.errtxt = "Class does not exist" else sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) sql = "DELETE FROM classes_to_param_groups WHERE class_id='"..provdb.escape(class_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "DELETE FROM provisioning_classes WHERE class_id='"..provdb.escape(class_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) delreq.errtxt = nil end if connected then provdb.databasedisconnect() end @@ -781,7 +787,7 @@ mymodule.list_groups = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_groups ORDER BY seq ASC, name ASC, label ASC" - retval = mymodule.getselectresponse(sql) + retval = getselectresponse(sql) if connected then provdb.databasedisconnect() end end) if not res and err then @@ -807,14 +813,14 @@ mymodule.get_group = function(self, clientdata) local connected = databaseconnect() -- First, let's get all the parameters to set up the params.options and defaults local sql = "SELECT * FROM provisioning_params ORDER BY seq ASC, name ASC" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) for i,p in ipairs(tmp) do retval.params.option[#retval.params.option + 1] = {value=p.param_id, label=p.label} retval.editable.option[#retval.editable.option + 1] = {value=p.param_id, label=p.label} p.seq = i if p.type == "select" then sql = "SELECT * FROM provisioning_options WHERE param_id='"..provdb.escape(p.param_id).."' ORDER BY seq ASC" - p.option = mymodule.getselectresponse(sql) or {} + p.option = getselectresponse(sql) or {} end if p.type == "boolean" then p.value = (p.value == "true") @@ -824,7 +830,7 @@ mymodule.get_group = function(self, clientdata) end if group_id and group_id ~= "" then sql = "SELECT * FROM provisioning_groups WHERE group_id='"..provdb.escape(group_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) if tmp and #tmp > 0 then for n,v in pairs(tmp[1]) do if retval[n] then @@ -834,7 +840,7 @@ mymodule.get_group = function(self, clientdata) end -- Now, get the paramgroup-to-param mappings sql = "SELECT * FROM param_groups_to_params WHERE group_id='"..provdb.escape(group_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,p in ipairs(tmp) do retval.params.value[#retval.params.value + 1] = p.param_id if (p.editable == "t") then @@ -899,7 +905,7 @@ mymodule.update_group = function(self, group, action, create) local devices = {} if not create then local sql = "SELECT * FROM provisioning_groups WHERE group_id='"..provdb.escape(group.value.group_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false errtxt = "Group does not exist" @@ -909,20 +915,20 @@ mymodule.update_group = function(self, group, action, create) sql = "SELECT d2t.device_id ".. "FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING (class_id)) ".. "WHERE t2g.group_id='"..provdb.escape(group.value.group_id.value).."'" - devices = mymodule.getselectresponse(sql) + devices = getselectresponse(sql) -- Make sure the current params are saved for each device for i,d in ipairs(devices) do - if not saved_device_params[d.device_id] then mymodule.get_device_params(self, d.device_id) end + if not saved_device_params[d.device_id] then get_device_params(d.device_id) end end end if success then local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) if create then sql = "INSERT INTO provisioning_groups VALUES(DEFAULT, '"..provdb.escape(group.value.name.value).."', '"..provdb.escape(group.value.label.value).."', '"..provdb.escape(group.value.seq.value).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "SELECT group_id FROM provisioning_groups WHERE name='"..provdb.escape(group.value.name.value).."' AND label='"..provdb.escape(group.value.label.value).."'" - local tmp = mymodule.getselectresponse(sql, true) + local tmp = getselectresponse(sql, true) if tmp and #tmp>0 then group.value.group_id = cfe({value=tmp[1].group_id, label="Group ID", readonly=true, seq=1}) else @@ -930,9 +936,9 @@ mymodule.update_group = function(self, group, action, create) end else sql = "UPDATE provisioning_groups SET (name, label, seq) = ('"..provdb.escape(group.value.name.value).."', '"..provdb.escape(group.value.label.value).."', '"..provdb.escape(group.value.seq.value).."') WHERE group_id='"..provdb.escape(group.value.group_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "DELETE FROM param_groups_to_params WHERE group_id='"..provdb.escape(group.value.group_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end -- Reverse the editable table for ease of use below local reverseeditable = {} @@ -948,17 +954,17 @@ mymodule.update_group = function(self, group, action, create) sql = sql.."null" end sql = sql..", '"..provdb.escape(tostring(reverseeditable[p] ~= nil)).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) -- Notify the devices that their params might have changed for i,d in ipairs(devices) do local tmp = saved_device_params[d.device_id] - local p = mymodule.get_device_params(self, d.device_id) - callscript(self, updatedeviceparamsscriptfile, p, tmp) + local p = get_device_params(d.device_id) + callscript(updatedeviceparamsscriptfile, p, tmp) end end if connected then provdb.databasedisconnect() end @@ -992,18 +998,18 @@ mymodule.delete_group = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_groups WHERE group_id='"..provdb.escape(group_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.group_id.errtxt = "Group does not exist" else sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) sql = "DELETE FROM param_groups_to_params WHERE group_id='"..provdb.escape(group_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "DELETE FROM provisioning_groups WHERE group_id='"..provdb.escape(group_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) delreq.errtxt = nil end if connected then provdb.databasedisconnect() end @@ -1023,7 +1029,7 @@ mymodule.list_params = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_params ORDER BY seq ASC, name ASC, label ASC" - retval = mymodule.getselectresponse(sql) + retval = getselectresponse(sql) if connected then provdb.databasedisconnect() end end) if not res and err then @@ -1051,7 +1057,7 @@ mymodule.get_param = function(self, clientdata) local connected = databaseconnect() if param_id and param_id ~= "" then sql = "SELECT * FROM provisioning_params WHERE param_id='"..provdb.escape(param_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) if tmp and #tmp > 0 then for n,v in pairs(tmp[1]) do if retval[n] then @@ -1106,7 +1112,7 @@ mymodule.update_param = function(self, param, action, create) local devices = {} if not create then local sql = "SELECT * FROM provisioning_params WHERE param_id='"..provdb.escape(param.value.param_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false errtxt = "Param does not exist" @@ -1116,20 +1122,20 @@ mymodule.update_param = function(self, param, action, create) sql = "SELECT d2t.device_id FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING (class_id) ".. "JOIN provisioning_groups g USING(group_id) JOIN param_groups_to_params g2p USING(group_id))".. "WHERE g2p.param_id='"..provdb.escape(param.value.param_id.value).."'" - devices = mymodule.getselectresponse(sql) + devices = getselectresponse(sql) -- Make sure the current params are saved for each device for i,d in ipairs(devices) do - if not saved_device_params[d.device_id] then mymodule.get_device_params(self, d.device_id) end + if not saved_device_params[d.device_id] then get_device_params(d.device_id) end end end if success then local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) if create then sql = "INSERT INTO provisioning_params VALUES(DEFAULT, '"..provdb.escape(param.value.name.value).."', '"..provdb.escape(param.value.type.value).."', '"..provdb.escape(param.value.label.value).."', '"..provdb.escape(param.value.descr.value).."', '"..provdb.escape(param.value.value.value).."', '"..provdb.escape(param.value.seq.value).."', '"..provdb.escape(param.value.regexp.value).."', '"..provdb.escape(format.dostounix(param.value.validate.value)).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "SELECT param_id FROM provisioning_params WHERE name='"..provdb.escape(param.value.name.value).."' AND label='"..provdb.escape(param.value.label.value).."'" - local tmp = mymodule.getselectresponse(sql, true) + local tmp = getselectresponse(sql, true) if tmp and #tmp>0 then param.value.param_id = cfe({value=tmp[1].param_id, label="Param ID", readonly=true, seq=1}) else @@ -1137,17 +1143,17 @@ mymodule.update_param = function(self, param, action, create) end else sql = "UPDATE provisioning_params SET (name, type, label, descr, value, seq, regexp, validate) = ('"..provdb.escape(param.value.name.value).."', '"..provdb.escape(param.value.type.value).."', '"..provdb.escape(param.value.label.value).."', '"..provdb.escape(param.value.descr.value).."', '"..provdb.escape(param.value.value.value).."', '"..provdb.escape(param.value.seq.value).."', '"..provdb.escape(param.value.regexp.value).."', '"..provdb.escape(format.dostounix(param.value.validate.value)).."') WHERE param_id='"..provdb.escape(param.value.param_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) -- Notify the devices that their params might have changed for i,d in ipairs(devices) do local tmp = saved_device_params[d.device_id] - local p = mymodule.get_device_params(self, d.device_id) - callscript(self, updatedeviceparamsscriptfile, p, tmp) + local p = get_device_params(d.device_id) + callscript(updatedeviceparamsscriptfile, p, tmp) end end if connected then provdb.databasedisconnect() end @@ -1181,18 +1187,18 @@ mymodule.delete_param = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_params WHERE param_id='"..provdb.escape(param_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.param_id.errtxt = "Parameter does not exist" else sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) sql = "DELETE FROM provisioning_options WHERE param_id='"..provdb.escape(param_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "DELETE FROM provisioning_params WHERE param_id='"..provdb.escape(param_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) delreq.errtxt = nil end if connected then provdb.databasedisconnect() end @@ -1212,7 +1218,7 @@ mymodule.list_devices = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT device_id, c.label AS class, g.label AS group FROM devices_to_classes d2c JOIN provisioning_classes c USING(class_id) JOIN provisioning_class_groups g USING(class_group_id) ORDER BY device_id" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) local reverse_device_id = {} for i,d in ipairs(tmp) do if not reverse_device_id[d.device_id] then @@ -1232,16 +1238,15 @@ end mymodule.get_existing_device = function(self, clientdata) clientdata = clientdata or {} - return mymodule.get_device(self, clientdata.device_id, false) + return get_device(clientdata.device_id, false) end mymodule.get_new_device = function(self, clientdata) clientdata = clientdata or {} - return mymodule.get_device(self, clientdata.device_id, true) + return get_device(clientdata.device_id, true) end --- This function is used by scripts, do not change prototype -mymodule.get_device = function(self, device_id, create) +get_device = function(device_id, create) local retval = {} retval.device_id = cfe({value=device_id or "", label="Device ID", seq=1}) retval.classes = cfe({type="group", value={}, label="Classes", seq=2}) @@ -1252,14 +1257,14 @@ mymodule.get_device = function(self, device_id, create) if not create and device_id and device_id ~= "" then -- Get the device-to-class mappings local sql = "SELECT class_id FROM devices_to_classes WHERE device_id='"..provdb.escape(device_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) for i,g in ipairs(tmp) do classes[g.class_id] = true end end -- Finally, get the class options sql = "SELECT class_id, g.name, g.label AS group, c.label, c.seq FROM provisioning_classes c JOIN provisioning_class_groups g USING(class_group_id) ORDER BY g.seq ASC, g.label ASC, c.seq ASC, c.label ASC" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,c in ipairs(tmp) do if not retval.classes.value[c.name] then retval.classes.value[c.name] = cfe({type="select", label=c.group, option={{value="", label=""}}, seq=i}) @@ -1303,40 +1308,40 @@ mymodule.update_device = function(self, device, action, create) local connected = databaseconnect() if not create then local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..provdb.escape(device.value.device_id.value).."' LIMIT 1" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false device.value.device_id.errtxt = "Device does not exist" end end if success then - if not saved_devices[device.value.device_id.value] then mymodule.get_device(self, device.value.device_id.value) end - if not saved_device_params[device.value.device_id.value] then mymodule.get_device_params(self, device.value.device_id.value) end + if not saved_devices[device.value.device_id.value] then get_device(device.value.device_id.value) end + if not saved_device_params[device.value.device_id.value] then get_device_params(device.value.device_id.value) end local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) if create then sql = "SELECT nextval('provisioning_device_seq')" - local tmp = mymodule.getselectresponse(sql, true) + local tmp = getselectresponse(sql, true) if tmp and #tmp>0 then device.value.device_id.value = tmp[1].nextval end end sql = "DELETE FROM devices_to_classes WHERE device_id='"..provdb.escape(device.value.device_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) -- Insert the device to class entries for n,c in pairs(device.value.classes.value) do if c.value ~= "" then sql = "INSERT INTO devices_to_classes VALUES('"..provdb.escape(device.value.device_id.value).."', '"..provdb.escape(c.value).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) local s = saved_device_params[device.value.device_id.value] - callscript(self, updatedevicescriptfile, device, saved_devices[device.value.device_id.value], mymodule.get_device_params(self, device.value.device_id.value), s) + callscript(updatedevicescriptfile, device, saved_devices[device.value.device_id.value], get_device_params(device.value.device_id.value), s) saved_devices[device.value.device_id.value] = device end if connected then provdb.databasedisconnect() end @@ -1370,24 +1375,24 @@ mymodule.delete_device = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..provdb.escape(device_id).."' LIMIT 1" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.device_id.errtxt = "Device does not exist" else - if not saved_device_params[device_id] then mymodule.get_device_params(self, device_id) end - if not saved_devices[device_id] then mymodule.get_device(self, device_id) end + if not saved_device_params[device_id] then get_device_params(device_id) end + if not saved_devices[device_id] then get_device(device_id) end sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(device_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "DELETE FROM devices_to_classes WHERE device_id='"..provdb.escape(device_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) delreq.errtxt = nil - callscript(self, deletedevicescriptfile, saved_devices[device_id], saved_device_params[device_id]) + callscript(deletedevicescriptfile, saved_devices[device_id], saved_device_params[device_id]) saved_devices[device_id] = nil saved_device_params[device_id] = nil end @@ -1403,16 +1408,15 @@ end mymodule.get_editable_device_params = function(self, clientdata, action) clientdata = clientdata or {} - return mymodule.get_device_params(self, clientdata.device_id, true) + return get_device_params(clientdata.device_id, true) end mymodule.get_all_device_params = function(self, clientdata, action) clientdata = clientdata or {} - return mymodule.get_device_params(self, clientdata.device_id, false) + return get_device_params(clientdata.device_id, false) end --- This function is used by scripts, do not change prototype -mymodule.get_device_params = function(self, device_id, editable) +get_device_params = function(device_id, editable) local retval = {} retval.device_id = cfe({value=device_id or "", label="Device ID", seq=0}) local errtxt = "Cannot find device" @@ -1421,12 +1425,12 @@ mymodule.get_device_params = function(self, device_id, editable) local connected = databaseconnect() -- First, just check to see if device_id exists local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..provdb.escape(device_id).."' LIMIT 1" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if tmp and #tmp > 0 then errtxt = nil -- Next, get all of the param groups sql = "SELECT * FROM provisioning_groups" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) -- Loop through the groups and put them into the result for i,g in ipairs(tmp) do retval[g.name] = g @@ -1441,13 +1445,13 @@ mymodule.get_device_params = function(self, device_id, editable) if editable then sql = sql.." AND g2p.editable='t'" end - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) -- Loop through the params to figure out options and put them into the groups for i,p in ipairs(tmp) do -- Options if (p.type == "select") then sql = "SELECT * FROM provisioning_options WHERE param_id='"..provdb.escape(p.param_id).."' ORDER BY seq ASC" - p.option = mymodule.getselectresponse(sql) or {} + p.option = getselectresponse(sql) or {} end -- Groups if not retval[p.group].value then @@ -1503,7 +1507,7 @@ mymodule.get_class_values = function(self, retval) local connected = databaseconnect() -- First, just check to see if class_id exists local sql = "SELECT * FROM provisioning_classes WHERE class_id='"..provdb.escape(retval.value.class_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if tmp and #tmp > 0 then retval.errtxt = nil retval.value.label = cfe({ value=tmp[1].label or "", label="Label", seq=3}) @@ -1513,7 +1517,7 @@ mymodule.get_class_values = function(self, retval) "FROM (provisioning_classes t JOIN classes_to_param_groups t2g USING(class_id) JOIN provisioning_groups g USING(group_id) ".. "JOIN param_groups_to_params g2p USING(group_id) JOIN provisioning_params p USING(param_id)) ".. "WHERE t.class_id='"..provdb.escape(retval.value.class_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) -- Loop through the params and put them into the groups for i,p in ipairs(tmp) do if p.type == "boolean" then @@ -1538,37 +1542,36 @@ mymodule.get_class_values = function(self, retval) end mymodule.set_editable_device_params = function(self, params) - return mymodule.set_device_params(self, params, true) + return set_device_params(params, true) end mymodule.set_all_device_params = function(self, params) - return mymodule.set_device_params(self, params, false) + return set_device_params(params, false) end --- This function is used by scripts, do not change prototype -mymodule.set_device_params = function(self, params, editable) +set_device_params = function(params, editable) -- Validate the settings local success = validateparam(params) local errtxt if success then local res, err = pcall(function() local connected = databaseconnect() - success = validateparamcoded(self, params) + success = validateparamcoded(params) local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..provdb.escape(params.value.device_id.value).."' LIMIT 1" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false errtxt = "Device does not exist" end if success then - if not saved_device_params[params.value.device_id.value] then mymodule.get_device_params(self, params.value.device_id.value) end + if not saved_device_params[params.value.device_id.value] then get_device_params(params.value.device_id.value) end local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) if not editable then -- Delete all values for this device (can't do this if only updating editable) sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(params.value.device_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end -- Loop through the groups and params for group,v in pairs(params.value) do @@ -1576,27 +1579,27 @@ mymodule.set_device_params = function(self, params, editable) for name,param in pairs(v.value) do if editable then sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(params.value.device_id.value).."' AND group_name='"..provdb.escape(group).."' AND param_id='"..provdb.escape(param.param_id).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end if param.value ~= param.default then sql = "INSERT INTO provisioning_values VALUES('"..provdb.escape(params.value.device_id.value).."', '"..provdb.escape(group).."', '"..provdb.escape(param.param_id).."', '"..provdb.escape(tostring(param.value)).."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end end end end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) local tmp = saved_device_params[params.value.device_id.value] local p = params if not editable then saved_device_params[params.value.device_id.value] = params else - p = mymodule.get_device_params(self, params.value.device_id.value) + p = get_device_params(params.value.device_id.value) end - callscript(self, updatedeviceparamsscriptfile, p, tmp) + callscript(updatedeviceparamsscriptfile, p, tmp) end if connected then provdb.databasedisconnect() end end) @@ -1623,7 +1626,7 @@ mymodule.fetch_device_values = function(self, search) elseif #search.value.result.value > 1 then search.errtxt = "Multiple devices found" else - search.value.values = mymodule.get_device_values(self, search.value.result.value[1].device_id) + search.value.values = get_device_values(search.value.result.value[1].device_id) search.value.values.seq = 5 end if connected then provdb.databasedisconnect() end @@ -1636,7 +1639,7 @@ mymodule.fetch_device_values = function(self, search) return search end -mymodule.get_device_values = function(self, device_id) +get_device_values = function(device_id) local retval = {} local errtxt if device_id and device_id ~= "" then @@ -1646,7 +1649,7 @@ mymodule.get_device_values = function(self, device_id) "FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING(class_id) JOIN provisioning_groups g USING(group_id) ".. "JOIN param_groups_to_params g2p USING(group_id) JOIN provisioning_params p USING(param_id)) LEFT JOIN provisioning_values v ON(d2t.device_id=v.device_id AND p.param_id=v.param_id AND g.name=v.group_name ) ".. "WHERE d2t.device_id='"..provdb.escape(device_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) -- Loop through the params and put them into the groups for i,p in ipairs(tmp) do if p.type == "boolean" then @@ -1679,7 +1682,7 @@ mymodule.get_search_options = function() local connected = databaseconnect() -- Get the group/parameter options local sql = "SELECT g.name AS group_name, p.name AS param_name FROM provisioning_groups g JOIN param_groups_to_params USING(group_id) JOIN provisioning_params p USING(param_id) GROUP BY g.name, p.name ORDER BY g.name ASC, p.name ASC" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) local blankopt = {} local blankexists = {} for i,v in ipairs(tmp) do @@ -1738,7 +1741,7 @@ mymodule.search_device_values = function(self, search) sql = sql.." p.name='"..provdb.escape(param).."' AND CASE WHEN v.value IS NOT NULL THEN v.value WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END"..provdb.escape(search.value.comparison.value).."'"..provdb.escape(search.value.value.value).."'" end sql = sql.." ORDER BY d2t.device_id ASC" - search.value.result.value = mymodule.getselectresponse(sql) + search.value.result.value = getselectresponse(sql) if connected then provdb.databasedisconnect() end end) if not res and err then @@ -1764,14 +1767,14 @@ mymodule.get_param_options = function(self, clientdata) local connected = databaseconnect() -- First, just check to see if param_id exists local sql = "SELECT * FROM provisioning_params WHERE param_id='"..provdb.escape(param_id).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if tmp and #tmp > 0 then errtxt = nil retval.name.value = tmp[1].name retval.label.value = tmp[1].label -- Next, get all of the param options sql = "SELECT * FROM provisioning_options WHERE param_id='"..provdb.escape(param_id).."' ORDER BY seq ASC" - local tmp = mymodule.getselectresponse(sql) or {} + local tmp = getselectresponse(sql) or {} for i,t in ipairs(tmp) do retval.options.value[#retval.options.value + 1] = t.value..","..t.label end @@ -1794,17 +1797,17 @@ mymodule.set_param_options = function(self, options) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_params WHERE param_id='"..provdb.escape(options.value.param_id.value).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then success = false errtxt = "Parameter does not exist" end if success then local sql = "BEGIN TRANSACTION" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) -- Delete all options for this device sql = "DELETE FROM provisioning_options WHERE param_id='"..provdb.escape(options.value.param_id.value).."'" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) -- Loop through the options for i,o in ipairs(options.value.options.value) do local v,l = string.match(o, "^%s*([^,]+),%s*(.*%S)%s*$") @@ -1815,10 +1818,10 @@ mymodule.set_param_options = function(self, options) l = v end sql = "INSERT INTO provisioning_options VALUES('"..provdb.escape(options.value.param_id.value).."', '"..provdb.escape(l).."', '"..provdb.escape(v).."', '"..i.."')" - mymodule.runsqlcommand(sql, true) + runsqlcommand(sql, true) end sql = "COMMIT" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) end if connected then provdb.databasedisconnect() end end) @@ -1864,51 +1867,51 @@ function mymodule.dump_database(self, db) local connected = databaseconnect() local lines = {} local sql = "SELECT name, label, seq FROM provisioning_class_groups ORDER BY name, label" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO provisioning_class_groups VALUES(default, '"..provdb.escape(t.name).."', '"..provdb.escape(t.label).."', '"..provdb.escape(t.seq).."');" end sql = "SELECT g.name AS group, c.label, c.seq FROM provisioning_classes c JOIN provisioning_class_groups g USING(class_group_id) ORDER BY g.name, c.label" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO provisioning_classes VALUES(default, (SELECT class_group_id FROM provisioning_class_groups WHERE name='"..provdb.escape(t.group).."'), '"..provdb.escape(t.label).."', '"..provdb.escape(t.seq).."');" end sql = "SELECT name, label, seq FROM provisioning_groups ORDER BY name, label" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO provisioning_groups VALUES(default, '"..provdb.escape(t.name).."', '"..provdb.escape(t.label).."', '"..provdb.escape(t.seq).."');" end sql = "SELECT name, type, label, descr, value, seq, regexp, validate FROM provisioning_params ORDER BY name" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO provisioning_params VALUES(default, '"..provdb.escape(t.name).."', '"..provdb.escape(t.type).."', '"..provdb.escape(t.label).."', '"..provdb.escape(t.descr).."', '"..provdb.escape(t.value).."', '"..provdb.escape(t.seq).."', '"..provdb.escape(t.regexp).."', '"..provdb.escape(t.validate).."');" end sql = "SELECT p.name AS param, o.label, o.value, o.seq FROM provisioning_options o JOIN provisioning_params p USING(param_id) ORDER BY p.name, o.seq, o.label" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO provisioning_options VALUES((SELECT param_id FROM provisioning_params WHERE name='"..provdb.escape(t.param).."'), '"..provdb.escape(t.label).."', '"..provdb.escape(t.value).."', '"..provdb.escape(t.seq).."');" end sql = "SELECT c.label AS class, g.label AS group FROM provisioning_classes c JOIN classes_to_param_groups USING(class_id) JOIN provisioning_groups g USING(group_id) ORDER BY c.label, g.label" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO classes_to_param_groups VALUES((SELECT class_id FROM provisioning_classes WHERE label='"..provdb.escape(t.class).."'), (SELECT group_id FROM provisioning_groups WHERE label='"..provdb.escape(t.group).."'));" end sql = "SELECT p.name AS param, g.label AS group, t.value, t.editable FROM provisioning_params p JOIN param_groups_to_params t USING(param_id) JOIN provisioning_groups g USING(group_id) ORDER BY p.name, g.label" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for i,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO param_groups_to_params VALUES((SELECT group_id FROM provisioning_groups WHERE label='"..provdb.escape(t.group).."'), (SELECT param_id FROM provisioning_params WHERE name='"..provdb.escape(t.param).."'), '"..provdb.escape(t.value).."', '"..provdb.escape(t.editable).."');" end if db.value.devices and db.value.devices.value then sql = "SELECT device_id FROM devices_to_classes GROUP BY device_id ORDER BY device_id ASC" - devices = mymodule.getselectresponse(sql) + devices = getselectresponse(sql) for i,d in ipairs(devices) do sql = "SELECT label FROM devices_to_classes JOIN provisioning_classes USING(class_id) WHERE device_id='"..provdb.escape(d.device_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for j,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO devices_to_classes VALUES("..provdb.escape(i)..", (SELECT class_id FROM provisioning_classes WHERE label='"..provdb.escape(t.label).."'));" end sql = "SELECT group_name, p.name AS param, v.value FROM provisioning_values v JOIN provisioning_params p USING(param_id) WHERE device_id='"..provdb.escape(d.device_id).."'" - tmp = mymodule.getselectresponse(sql) + tmp = getselectresponse(sql) for j,t in ipairs(tmp) do lines[#lines+1] = "INSERT INTO provisioning_values VALUES("..provdb.escape(i)..", '"..provdb.escape(t.group_name).."', (SELECT param_id FROM provisioning_params WHERE name='"..provdb.escape(t.param).."'), '"..provdb.escape(t.value).."');" end @@ -1942,13 +1945,13 @@ function mymodule.get_file(self, clientdata) -- Add the device to the table of requests local sql = "SELECT * FROM provisioning_requests WHERE mac='"..provdb.escape(string.upper(mac)).."'" - local requests = mymodule.getselectresponse(sql) + local requests = getselectresponse(sql) if requests and #requests > 0 then sql = "UPDATE provisioning_requests SET ip='"..provdb.escape(ip).."', agent='"..provdb.escape(agent).."', date=now() WHERE mac='"..provdb.escape(string.upper(mac)).."'" else sql = "INSERT INTO provisioning_requests VALUES('"..provdb.escape(string.upper(mac)).."', '"..provdb.escape(ip).."', '"..provdb.escape(agent).."', now())" end - mymodule.runsqlcommand(sql) + runsqlcommand(sql) -- Now, let's see if this device exists result = mymodule.get_search_options() @@ -1960,7 +1963,7 @@ function mymodule.get_file(self, clientdata) if #result.value.result.value == 0 then -- Determine which class to use (need a class that specifies a template) local c = mymodule.list_classes() - local class = callscript(self, determineclassscriptfile, agent, c) + local class = callscript(determineclassscriptfile, agent, c) if class then local options = mymodule.get_class_options(self, {}) @@ -2007,7 +2010,7 @@ function mymodule.put_file(self, clientdata) elseif #search.value.result.value > 1 then retval.errtxt = "Multiple devices found" else - retval.value, retval.errtxt = callscript(self, processputscriptfile, mac, data, search.value.result.value[1].device_id) + retval.value, retval.errtxt = callscript(processputscriptfile, mac, data, search.value.result.value[1].device_id) -- If the script doesn't exist, allow the write retval.value = retval.value or data end @@ -2019,6 +2022,15 @@ function mymodule.put_file(self, clientdata) return retval end +-- The functions must be added after they're declared +functions = { + getselectresponse=getselectresponse, + runsqlcommand=runsqlcommand, + get_device=get_device, + get_device_params=get_device_params, + set_device_params=set_device_params, +} + mymodule.list_requests = function() local retval = {} local errtxt @@ -2026,10 +2038,10 @@ mymodule.list_requests = function() local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_requests ORDER BY date DESC" - retval = mymodule.getselectresponse(sql) + retval = getselectresponse(sql) -- Get the corresponding device_id's for each request sql = "SELECT * FROM provisioning_values WHERE param_id=(SELECT param_id FROM provisioning_params WHERE name='mac')" - local ids = mymodule.getselectresponse(sql) + local ids = getselectresponse(sql) local reverseids = {} for i,v in ipairs(ids) do reverseids[v.value] = v.device_id @@ -2059,13 +2071,13 @@ mymodule.delete_request = function(self, delreq) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_requests WHERE mac='"..provdb.escape(string.upper(mac)).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then delreq.value.mac.errtxt = "Request does not exist" else -- Remove the request sql = "DELETE FROM provisioning_requests WHERE mac='"..provdb.escape(string.upper(mac)).."'" - mymodule.runsqlcommand(sql) + runsqlcommand(sql) delreq.errtxt = nil end if connected then provdb.databasedisconnect() end @@ -2090,16 +2102,16 @@ mymodule.create_from_request = function(self, request) local res, err = pcall(function() local connected = databaseconnect() local sql = "SELECT * FROM provisioning_requests WHERE mac='"..provdb.escape(string.upper(request.value.mac.value)).."'" - local tmp = mymodule.getselectresponse(sql) + local tmp = getselectresponse(sql) if #tmp == 0 then request.value.mac.errtxt = "Request does not exist" else -- Determine which class to use (need a class that specifies a template) local c = mymodule.list_classes() - local class, group = callscript(self, determineclassscriptfile, tmp[1].agent, c) + local class, group = callscript(determineclassscriptfile, tmp[1].agent, c) -- Create the device - local device = mymodule.get_device(self, nil, true) + local device = get_device(nil, true) if class and group and device.value.classes.value[group] then device.value.classes.value[group].value = class device = mymodule.create_device(self, device) @@ -2112,11 +2124,11 @@ mymodule.create_from_request = function(self, request) end request.errtxt = table.concat(request.errtxt, "\n") else - local params = mymodule.get_device_params(self, device.value.device_id.value) + local params = get_device_params(device.value.device_id.value) -- Set the MAC Address if params.value.device and params.value.device.value.mac then params.value.device.value.mac.value = string.upper(request.value.mac.value) - params = mymodule.set_device_params(self, params) + params = set_device_params(params) end if params.errtxt then request.errtxt = params.errtxt diff --git a/provisioning-scripts.lua b/provisioning-scripts.lua index 325b07b..212e825 100644 --- a/provisioning-scripts.lua +++ b/provisioning-scripts.lua @@ -684,7 +684,7 @@ mymodule.param_groups_to_params = { mymodule.provisioning_params = { "CREATE TABLE provisioning_params (param_id SERIAL PRIMARY KEY, name VARCHAR(255) UNIQUE, type VARCHAR(255), label VARCHAR(255), descr VARCHAR(255), value VARCHAR(255), seq INTEGER, regexp VARCHAR(255), validate text)", "CREATE INDEX params_name_idx ON provisioning_params (name)", - "INSERT INTO provisioning_params VALUES(default, 'mac', 'text', 'MAC Address', 'Capitalized hex digits with no puncuation', '', '1', '^%x%x%x%x%x%x%x%x%x%x%x%x$', E'local self, value, params = ...\nvalue = string.upper(value)\nlocal others = self.getselectresponse(\"SELECT count(*) FROM provisioning_values WHERE param_id=\\'\"..params.value.device.value.mac.param_id..\"\\' AND device_id!=\\'\"..params.value.device_id.value..\"\\' AND value=\\'\"..value..\"\\'\")\nif tonumber(others[1].count) > 0 then\n\treturn value, \"MAC Address must be unique\"\nend\nreturn value')", + "INSERT INTO provisioning_params VALUES(default, 'mac', 'text', 'MAC Address', 'Capitalized hex digits with no puncuation', '', '1', '^%x%x%x%x%x%x%x%x%x%x%x%x$', E'local value, functions, params = ...\nvalue = string.upper(value)\nlocal others = functions.getselectresponse(\"SELECT count(*) FROM provisioning_values WHERE param_id=\\'\"..params.value.device.value.mac.param_id..\"\\' AND device_id!=\\'\"..params.value.device_id.value..\"\\' AND value=\\'\"..value..\"\\'\")\nif tonumber(others[1].count) > 0 then\n\treturn value, \"MAC Address must be unique\"\nend\nreturn value')", "INSERT INTO provisioning_params VALUES(default, 'template', 'select', 'Template', '', '', '2', '', null)", "INSERT INTO provisioning_params VALUES(default, 'registrar', 'text', 'SIP Registrar', '', '', '3', '', null)", "INSERT INTO provisioning_params VALUES(default, 'digitmap', 'text', 'Digit Map', 'Phone dial pattern based on section 2.1.5 of RFC 3435, plus a comma to turn dialtone back on', '', '4', '^[*#0-9xT|,.%[%]-]*$', null)", @@ -715,7 +715,7 @@ mymodule.provisioning_params = { "INSERT INTO provisioning_params VALUES(default, 'speeddialenable', 'boolean', 'Speed Dial Enable', '', 'true', '206', '', null)", "INSERT INTO provisioning_params VALUES(default, 'mailbox', 'text', 'Voice Mailbox', 'Mailbox extension or URL', '', '207', '', null)", "INSERT INTO provisioning_params VALUES(default, 'mailcallback', 'text', 'Voice Mailbox Callback', 'Extension or URL for mailbox message retrieval', '', '208', '', null)", - "INSERT INTO provisioning_params VALUES(default, 'databaseversion', 'text', 'Provisioning Database Version', 'Do not edit or delete!', '2', '999', '', null)" + "INSERT INTO provisioning_params VALUES(default, 'databaseversion', 'text', 'Provisioning Database Version', 'Do not edit or delete!', '1', '999', '', null)" } -- All of the (non-default) parameter values for all devices are stored here diff --git a/upgradeprovisioning b/upgradeprovisioning index ada0d4f..a4436eb 100755 --- a/upgradeprovisioning +++ b/upgradeprovisioning @@ -238,15 +238,6 @@ done psql -U postgres -c "INSERT INTO provisioning_params VALUES(default, 'databaseversion', 'text', 'Provisioning Database Version', 'Do not edit or delete!', '1', '999', '', null)" provisioning psql -U postgres -c "UPDATE provisioning_params SET value='1' WHERE name='databaseversion'" provisioning -fi -if [ "$version" -lt "2" ]; then -echo "Upgrading to database version 2" - -# provisioning_params validate field changed the order of the first two Lua parameters -$(pwd)/swapvalidateparams - -psql -U postgres -c "UPDATE provisioning_params SET value='2' WHERE name='databaseversion'" provisioning - fi exit 0 -- cgit v1.2.3