From ae507b734acf3c14397d680e36425a467c829ab8 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Tue, 11 Jan 2011 15:59:23 +0000 Subject: Added script files to be run after edit device and after edit device params. --- provisioning-model.lua | 95 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 93 insertions(+), 2 deletions(-) (limited to 'provisioning-model.lua') diff --git a/provisioning-model.lua b/provisioning-model.lua index 848c7ad..3618194 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -16,9 +16,14 @@ local DatabasePassword local path = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin " local baseurl = "/etc/provisioning/templates/" +local updatedevicescriptfile = "/etc/provisioning/update_device.lua" +local updatedeviceparamsscriptfile = "/etc/provisioning/update_device_params.lua" local env local con +local saved_devices = {} +local saved_device_params = {} + -- if a table_creation_script does not create the named table or throw an exception then you will get an infinite loop, so be careful local table_creation_scripts = { -- List of all available templates @@ -308,6 +313,62 @@ validateparam = function(p) return true end +local function callscript(script, value, oldvalue) + local functions = { + getselectresponse=getselectresponse, + runsqlcommand=runsqlcommand, + get_device=get_device, + get_device_params=get_device_params, + } + local env = {} + setmetatable (env, {__index = _G}) + -- loadfile loads into the global environment + -- so we set env 0, not env 1 + setfenv (0, env) + local f = loadfile(script) + if (f) then f(functions, value, oldvalue) end + setfenv (0, _G) +end + +local function validatefiledetails(filedetails) + local success = true + + -- Validate that contents are valid lua code + local env = {} + setmetatable (env, {__index = _G}) + -- loadfile loads into the global environment + -- so we set env 0, not env 1 + setfenv (0, env) + local f,err = loadstring(filedetails.value.filecontent.value) + if not (f) then + success = false + filedetails.value.filecontent.errtxt = "Invalid Lua code\n"..(err or "") + end + setfenv (0, _G) + -- setmetatable (self.conf.app_hooks, {}) + + return success, filedetails +end + +local duplicatestructure +duplicatestructure = function(value, saved) + saved = saved or {} + if type(value) == "table" then + if saved[value] then + return saved[value] + else + local output = {} + saved[value] = output + for k,v in pairs(value) do + output[k] = duplicatestructure(v, saved) + end + return output + end + else + return value + end +end + -- ################################################################################ -- PUBLIC FUNCTIONS @@ -1053,7 +1114,10 @@ get_device = function(device_id) errtxt = err end - return cfe({ type="group", value=retval, label="Provisioning Device", errtxt=errtxt }) + -- Save the device for later use + local output = cfe({ type="group", value=retval, label="Provisioning Device", errtxt=errtxt }) + if device_id and device_id ~= "" then saved_devices[device_id] = duplicatestructure(output) end + return output end create_device = function(device) @@ -1110,6 +1174,8 @@ update_device = function(device, create) sql = "COMMIT" runsqlcommand(sql) + + callscript(updatedevicescriptfile, device, saved_devices[device.value.device_id.value]) end if connected then databasedisconnect() end end) @@ -1229,7 +1295,11 @@ get_device_params = function(device_id, editable) errtxt = err end end - return cfe({ type="group", value=retval, label="Provisioning Device Parameters", errtxt=errtxt }) + + -- Save the device for later use + local output = cfe({ type="group", value=retval, label="Provisioning Device Parameters", errtxt=errtxt }) + if device_id and device_id ~= "" then saved_device_params[device_id] = duplicatestructure(output) end + return output end set_editable_device_params = function(params) @@ -1277,6 +1347,8 @@ set_device_params = function(params, editable) sql = "COMMIT" runsqlcommand(sql) + + callscript(updatedeviceparamsscriptfile, params, saved_device_params[params.value.device_id.value]) end if connected then databasedisconnect() end end) @@ -1508,3 +1580,22 @@ set_param_options = function(options) end return options end + +function get_filedetails(filename) + return modelfunctions.getfiledetails(filename, {updatedevicescriptfile, updatedeviceparamsscriptfile}) +end + +function update_filedetails(filedetails) + return modelfunctions.setfiledetails(filedetails, {updatedevicescriptfile, updatedeviceparamsscriptfile}, validatefiledetails) +end + +function list_files() + local retval = {} + for i,file in ipairs({updatedevicescriptfile, updatedeviceparamsscriptfile}) do + local details = fs.stat(file) or {} + details.filename = file + table.insert(retval, details) + end + table.sort(retval, function(a,b) return a.filename < b.filename end) + return cfe({ type="structure", value=retval, label="List of Provisioning Script files" }) +end -- cgit v1.2.3