diff options
author | Ted Trask <ttrask01@yahoo.com> | 2013-09-23 18:13:03 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2013-09-23 18:13:03 +0000 |
commit | fb00ff8f05dae3c110beceb929ea036e4cd14ae1 (patch) | |
tree | 51d124ce1c65e3a600a7da7b9dd57ffafd83f910 | |
parent | 60ef6e20019e046aa801325d29893ada12b5d5af (diff) | |
download | acf-provisioning-fb00ff8f05dae3c110beceb929ea036e4cd14ae1.tar.bz2 acf-provisioning-fb00ff8f05dae3c110beceb929ea036e4cd14ae1.tar.xz |
Change function prototypes to pass self and use self.model instead of functions table
Changed the order of the first two parameters passed to validate function in provisioning_params
Replaced all APP references with self, now that self is available
Still need to implement swapvalidateparams script
-rw-r--r-- | config/delete_device.lua | 10 | ||||
-rw-r--r-- | config/determine_class.lua | 6 | ||||
-rw-r--r-- | config/process_put.lua | 10 | ||||
-rw-r--r-- | config/update_device.lua | 6 | ||||
-rw-r--r-- | config/update_device_params.lua | 18 | ||||
-rw-r--r-- | provisioning-model.lua | 88 | ||||
-rw-r--r-- | provisioning-scripts.lua | 4 | ||||
-rwxr-xr-x | upgradeprovisioning | 9 |
8 files changed, 74 insertions, 77 deletions
diff --git a/config/delete_device.lua b/config/delete_device.lua index 4f5fee7..81c2afb 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 functions, olddevice, oldparams = ... +local self, olddevice, oldparams = ... require("posix") local root = "/var/www/provisioning/htdocs/" ---APP.logevent("got to delete_device script") +--self.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 - --APP.logevent("Deleting files for "..oldparams.value.device.value.mac.value) + --self.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 - --APP.logevent("deleting "..path..d) + --self.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(functions, params, oldparams) end +if (f) then f(self, params, oldparams) end setfenv (0, _G) diff --git a/config/determine_class.lua b/config/determine_class.lua index 675e6e8..53a1884 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 functions, agent, classes = ... +local self, agent, classes = ... ---APP.logevent("got to determine_class script") +--self.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 ---APP.logevent("Found "..(manufacture or "").." model "..(model or "")) +--self.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 4eabb1a..1a34042 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 functions, mac, data, device_id = ... +local self, mac, data, device_id = ... ---APP.logevent("got to process_put script") +--self.logevent("got to process_put script") -- Get the params -local params = functions.get_device_params(device_id) +local params = self.get_device_params(self, device_id) function process_polycom() local before, xml, after = string.match(data, "(.*<OVERRIDES)([^/]*)(/>.*)") @@ -18,7 +18,7 @@ function process_polycom() end -- for n,v in pairs(attrs) do --- APP.logevent("name "..n.." val "..(v or "")) +-- self.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 = functions.set_device_params(params) +local params = self.set_device_params(self, params) -- Return the updated data return data diff --git a/config/update_device.lua b/config/update_device.lua index 7295062..9823e95 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 functions, device, olddevice, params, oldparams = ... +local self, device, olddevice, params, oldparams = ... ---APP.logevent("got to update_device script") +--self.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(functions, params, oldparams) end +if (f) then f(self, params, oldparams) end setfenv (0, _G) diff --git a/config/update_device_params.lua b/config/update_device_params.lua index 6d7ae23..42560c3 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 functions, params, oldparams = ... +local self, params, oldparams = ... require("posix") local root = "/var/www/provisioning/htdocs/" ---APP.logevent("got to update_device_params script") +--self.logevent("got to update_device_params script") local function findip(mac) if not mac or mac == "" then return nil end - local ipaddr = functions.getselectresponse("SELECT ip FROM provisioning_requests WHERE mac~*'"..mac.."'") + local ipaddr = self.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 - --APP.logevent("Notifying "..ipaddr.." to update for "..(mac or "")) + --self.logevent("Notifying "..ipaddr.." to update for "..(mac or "")) os.execute("/etc/provisioning/notify_device "..ipaddr.." "..extension) else - --APP.logevent("Warning - could not find IP address for "..(mac or "")) + --self.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 - --APP.logevent("Moving files for "..oldparams.value.device.value.mac.value) + --self.logevent("Moving files for "..oldparams.value.device.value.mac.value) deletefiles = false else - --APP.logevent("Deleting files for "..oldparams.value.device.value.mac.value) + --self.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 - --APP.logevent("deleting "..path..d) + --self.logevent("deleting "..path..d) os.remove(path..d) else - --APP.logevent("moving "..path..d.." to "..path..newfile) + --self.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 6db5b98..fd20d7e 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -100,10 +100,6 @@ local deletedatabase = function() 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() if not con then -- create environment object @@ -146,6 +142,7 @@ local runscript = function(script, in_transaction) end end +-- This function is used by scripts, do not change prototype runsqlcommand = function(sql, in_transaction) logevent(sql) if in_transaction then assert(con:execute("SAVEPOINT before_command")) end @@ -166,7 +163,7 @@ logevent(err) end end -local getselectresponse +-- This function is used by scripts, do not change prototype getselectresponse = function(sql, in_transaction) local retval = {} if in_transaction then assert(con:execute("SAVEPOINT before_select")) end @@ -222,17 +219,13 @@ 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(p, top) +validateparamcoded = function(self, p, top) top = top or p local success = true if p.type == "group" then for n,p2 in pairs(p.value) do - success = validateparamcoded(p2, top) and success + success = validateparamcoded(self, p2, top) and success end return success end @@ -246,7 +239,7 @@ validateparamcoded = function(p, top) local f = loadstring(p.validate) if (f) then local res, err = pcall(function() - p.value, p.errtxt = f(p.value, functions, top) + p.value, p.errtxt = f(self.model, p.value, top) if p.errtxt then success = false end end) if not res and err then @@ -256,10 +249,11 @@ validateparamcoded = function(p, top) end setfenv (0, _G) end + --self.logevent(p.label.." validation "..tostring(success)) return success end -local function callscript(script, ...) +local function callscript(self, script, ...) local result={} local env = {} setmetatable (env, {__index = _G}) @@ -269,7 +263,7 @@ local function callscript(script, ...) local f = loadfile(script) if f then local res, err = pcall(function(...) - result = { f(functions, ...) } + result = { f(self.model, ...) } end, ...) if not res and err then assert(res, "Exception in "..script.." script\n"..err) @@ -1269,15 +1263,16 @@ end get_existing_device = function(self, clientdata) clientdata = clientdata or {} - return get_device(clientdata.device_id, false) + return get_device(self, clientdata.device_id, false) end get_new_device = function(self, clientdata) clientdata = clientdata or {} - return get_device(clientdata.device_id, true) + return get_device(self, clientdata.device_id, true) end -get_device = function(device_id, create) +-- This function is used by scripts, do not change prototype +get_device = function(self, 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}) @@ -1346,8 +1341,8 @@ update_device = function(self, device, action, create) end end if success then - 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 + if not saved_devices[device.value.device_id.value] then get_device(self, device.value.device_id.value) end + if not saved_device_params[device.value.device_id.value] then get_device_params(self, device.value.device_id.value) end local sql = "BEGIN TRANSACTION" runsqlcommand(sql) @@ -1372,7 +1367,7 @@ update_device = function(self, device, action, create) runsqlcommand(sql) local s = saved_device_params[device.value.device_id.value] - callscript(updatedevicescriptfile, device, saved_devices[device.value.device_id.value], get_device_params(device.value.device_id.value), s) + callscript(self, updatedevicescriptfile, device, saved_devices[device.value.device_id.value], get_device_params(self, device.value.device_id.value), s) saved_devices[device.value.device_id.value] = device end if connected then databasedisconnect() end @@ -1410,8 +1405,8 @@ delete_device = function(self, delreq) if #tmp == 0 then delreq.value.device_id.errtxt = "Device does not exist" else - 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 + if not saved_device_params[device_id] then get_device_params(self, device_id) end + if not saved_devices[device_id] then get_device(self, device_id) end sql = "BEGIN TRANSACTION" runsqlcommand(sql) @@ -1423,7 +1418,7 @@ delete_device = function(self, delreq) runsqlcommand(sql) delreq.errtxt = nil - callscript(deletedevicescriptfile, saved_devices[device_id], saved_device_params[device_id]) + callscript(self, deletedevicescriptfile, saved_devices[device_id], saved_device_params[device_id]) saved_devices[device_id] = nil saved_device_params[device_id] = nil end @@ -1439,15 +1434,16 @@ end get_editable_device_params = function(self, clientdata, action) clientdata = clientdata or {} - return get_device_params(clientdata.device_id, true) + return get_device_params(self, clientdata.device_id, true) end get_all_device_params = function(self, clientdata, action) clientdata = clientdata or {} - return get_device_params(clientdata.device_id, false) + return get_device_params(self, clientdata.device_id, false) end -get_device_params = function(device_id, editable) +-- This function is used by scripts, do not change prototype +get_device_params = function(self, device_id, editable) local retval = {} retval.device_id = cfe({value=device_id or "", label="Device ID", seq=0}) local errtxt = "Cannot find device" @@ -1568,21 +1564,22 @@ get_class_values = function(self, retval) end set_editable_device_params = function(self, params) - return set_device_params(params, true) + return set_device_params(self, params, true) end set_all_device_params = function(self, params) - return set_device_params(params, false) + return set_device_params(self, params, false) end -set_device_params = function(params, editable) +-- This function is used by scripts, do not change prototype +set_device_params = function(self, params, editable) -- Validate the settings local success = validateparam(params) local errtxt if success then local res, err = pcall(function() local connected = databaseconnect() - success = validateparamcoded(params) + success = validateparamcoded(self, params) local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..escape(params.value.device_id.value).."' LIMIT 1" local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then @@ -1590,7 +1587,7 @@ set_device_params = function(params, editable) errtxt = "Device does not exist" end if success then - if not saved_device_params[params.value.device_id.value] then get_device_params(params.value.device_id.value) end + if not saved_device_params[params.value.device_id.value] then get_device_params(self, params.value.device_id.value) end local sql = "BEGIN TRANSACTION" runsqlcommand(sql) @@ -1623,9 +1620,9 @@ set_device_params = function(params, editable) if not editable then saved_device_params[params.value.device_id.value] = params else - p = get_device_params(params.value.device_id.value) + p = get_device_params(self, params.value.device_id.value) end - callscript(updatedeviceparamsscriptfile, p, tmp) + callscript(self, updatedeviceparamsscriptfile, p, tmp) end if connected then databasedisconnect() end end) @@ -1652,7 +1649,7 @@ fetch_device_values = function(self, search) elseif #search.value.result.value > 1 then search.errtxt = "Multiple devices found" else - search.value.values = get_device_values(search.value.result.value[1].device_id) + search.value.values = get_device_values(self, search.value.result.value[1].device_id) search.value.values.seq = 5 end if connected then databasedisconnect() end @@ -1665,7 +1662,7 @@ fetch_device_values = function(self, search) return search end -get_device_values = function(device_id) +get_device_values = function(self, device_id) local retval = {} local errtxt if device_id and device_id ~= "" then @@ -1989,7 +1986,7 @@ function 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 = list_classes() - local class = callscript(determineclassscriptfile, agent, c) + local class = callscript(self, determineclassscriptfile, agent, c) if class then local options = get_class_options(self, {}) @@ -2036,7 +2033,7 @@ function put_file(self, clientdata) elseif #search.value.result.value > 1 then retval.errtxt = "Multiple devices found" else - retval.value, retval.errtxt = callscript(processputscriptfile, mac, data, search.value.result.value[1].device_id) + retval.value, retval.errtxt = callscript(self, 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 @@ -2048,15 +2045,6 @@ function 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, -} - list_requests = function() local retval = {} local errtxt @@ -2134,10 +2122,10 @@ create_from_request = function(self, request) else -- Determine which class to use (need a class that specifies a template) local c = list_classes() - local class, group = callscript(determineclassscriptfile, tmp[1].agent, c) + local class, group = callscript(self, determineclassscriptfile, tmp[1].agent, c) -- Create the device - local device = get_device(nil, true) + local device = get_device(self, nil, true) if class and group and device.value.classes.value[group] then device.value.classes.value[group].value = class device = create_device(self, device) @@ -2150,11 +2138,11 @@ create_from_request = function(self, request) end request.errtxt = table.concat(request.errtxt, "\n") else - local params = get_device_params(device.value.device_id.value) + local params = get_device_params(self, 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 = set_device_params(params) + params = set_device_params(self, params) end if params.errtxt then request.errtxt = params.errtxt diff --git a/provisioning-scripts.lua b/provisioning-scripts.lua index 713c8c9..4741062 100644 --- a/provisioning-scripts.lua +++ b/provisioning-scripts.lua @@ -684,7 +684,7 @@ param_groups_to_params = { 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 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, '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, '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 @@ 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!', '1', '999', '', null)" + "INSERT INTO provisioning_params VALUES(default, 'databaseversion', 'text', 'Provisioning Database Version', 'Do not edit or delete!', '2', '999', '', null)" } -- All of the (non-default) parameter values for all devices are stored here diff --git a/upgradeprovisioning b/upgradeprovisioning index a4436eb..ada0d4f 100755 --- a/upgradeprovisioning +++ b/upgradeprovisioning @@ -239,5 +239,14 @@ psql -U postgres -c "INSERT INTO provisioning_params VALUES(default, 'databaseve 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 |