From 8a180f8a449ab86e1f7e22459e0512ee3ac613f1 Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Mon, 18 Jan 2016 15:44:40 +0000 Subject: Modify CyberData template to work with any init.cfg or none at all Previously, init.cfg had to follow a specific format to work --- config/templates/cyberdata-template.lua | 59 ++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/config/templates/cyberdata-template.lua b/config/templates/cyberdata-template.lua index 515cceb..11206ba 100644 --- a/config/templates/cyberdata-template.lua +++ b/config/templates/cyberdata-template.lua @@ -6,32 +6,61 @@ local values = ... xml = require("LuaXml") -local init_cfg = xml.load("/var/www/provisioning/htdocs/CyberData/init.cfg") +local InitFile = "/var/www/provisioning/htdocs/CyberData/init.cfg" + +local function findSection(xmlobj, name) + -- Cannot use xmlobj:find() because we don't want recursion + for i,s in ipairs(xmlobj) do + if s:tag() == name then + return s + end + end + return null +end + +local function findOrAppendSection(xmlobj, name) + return findSection(xmlobj, name) or xmlobj:append(name) +end + +local function setValue(xmlobj, name, value) + if value then + findOrAppendSection(xmlobj, name)[1] = value + end +end + +-- Load the initial config +local init_cfg +local res, err = pcall(function() + init_cfg = xml.load(InitFile) +end) +if not res and err then + init_cfg = xml.new("specific") +end -- -- -for pg, pg_t in pairs(values) do - -- Is it of the form regX ? - local num = string.match(pg, 'reg(%d+)') - if num then - if pg_t.extension ~= "" then - init_cfg[2]:append("SIPServer")[1] = values.device.registrar - init_cfg[2]:append("SIPUserID")[1] = pg_t.extension - init_cfg[2]:append("SIPAuthID")[1] = pg_t.extension - init_cfg[2]:append("SIPAuthPassword")[1] = pg_t.password - init_cfg[2]:append("DialoutExtension0")[1] = pg_t.hotlinedestination - init_cfg[2]:append("DialoutID0")[1] = pg_t.callerid - end +if values.reg1 then + local sipsettings = findOrAppendSection(init_cfg, "SIPSettings") + if values.device then + setValue(sipsettings, "SIPServer", values.device.registrar) end + setValue(sipsettings, "SIPUserID", values.reg1.extension) + setValue(sipsettings, "SIPAuthID", values.reg1.extension) + setValue(sipsettings, "SIPAuthPassword", values.reg1.password) + setValue(sipsettings, "DialoutExtension0", values.reg1.hotlinedestination) + setValue(sipsettings, "DialoutID0", values.reg1.callerid) end -- -init_cfg[3]:append("NTPServer")[1] = values.device.sntpserver -init_cfg[3]:append("NTPTimezone")[1] = values.device.timezone +if values.device then + local clocksettings = findOrAppendSection(init_cfg, "ClockSettings") + setValue(clocksettings, "NTPServer", values.device.sntpserver) + setValue(clocksettings, "NTPTimezone", values.device.timezone) +end -- init_cfg:save("TEST-cyberdata.xml") -- cgit v1.2.3