Skip to content

Commit

Permalink
Added store_unmanaged.lua #16
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Mar 4, 2016
1 parent 64e2687 commit 999af06
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 1 deletion.
6 changes: 5 additions & 1 deletion openwisp-config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ define Package/openwisp-config
TITLE:=OpenWISP Configuration Agent
MAINTAINER:=OpenWISP Project [email protected]
PKG_ARCH:=all
DEPENDS:=+libpolarssl +curl +ca-certificates
DEPENDS:=+libpolarssl +curl +ca-certificates +libuci-lua
endef

define Package/openwisp-config/description
Expand Down Expand Up @@ -50,6 +50,10 @@ define Package/openwisp-config/install
files/apply_config \
$(1)/usr/sbin/apply_config

$(INSTALL_BIN) \
files/store_unmanaged.lua \
$(1)/usr/sbin/store_unmanaged

$(INSTALL_BIN) \
files/openwisp.init \
$(1)/etc/init.d/openwisp_config
Expand Down
138 changes: 138 additions & 0 deletions openwisp-config/files/store_unmanaged.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/usr/bin/env lua

require('os')
require('io')
require('uci')
local blocks
local arg={...}

-- parse arguments
for key, value in pairs(arg) do
-- test argument
if value == '--test=1' then test = true; end
-- blocks argument
if string.sub(value, 1, 3) == '-o=' then
blocks = value:gsub('%-o=', '')
blocks = blocks:gsub('%"', '')
end
end

local standard_prefix = test and "../tests/" or "/etc/"
local unmanaged_prefix = test and "../tests/" or "/tmp/openwisp/"
local standard_path = standard_prefix .. "config/"
local unmanaged_path = unmanaged_prefix .. "unmanaged/"
local uci_tmp_path = "/tmp/openwisp/.uci"

function split(input, sep)
if input == "" or input == nil then
return {}
end
if sep == nil then
sep = "%s"
end
local t={}; i=1
for str in string.gmatch(input, "([^" .. sep .. "]+)") do
t[i] = str
i = i + 1
end
return t
end

function empty_file(path)
local file = io.open(path, 'w')
file:write('')
file:close()
end

-- writes uci block, eg:
--
-- config interface 'wan'
-- option proto 'none'
-- option ifname 'eth0.2'
--
function write_uci_block(cursor, config, block)
local name
-- add named block
if not block['.anonymous'] then
name = block['.name']
cursor:set(config, name, block['.type'])
-- add anonymous block
else
name = cursor:add(config, block['.type'])
end
-- add options for block
for key, value in pairs(block) do
-- ignore properties starting with .
if string.sub(key, 1, 1) ~= '.' then
cursor:set(config, name, key, value)
end
end
end

-- convert list of blocks in a table with a structure like:
-- {
-- network = {
-- {name = "loopback"},
-- {name = "globals"},
-- {type = "switch"},
-- {type = "switch_vlan"}
-- },
-- system = {
-- {name = "ntp"},
-- {type = "led"}
-- }
-- }
local unmanaged_map = {}
local block_list = split(blocks)
for i, block in pairs(block_list) do
local parts = split(block, '.')
-- skip unrecognized strings
if parts[1] and parts[2] then
local config = parts[1]
local block_type = nil
local block_name = nil
-- anonymous block
if string.sub(parts[2], 1, 1) == '@' then
block_type = string.sub(parts[2], 2, -1)
-- named block
else
block_name = parts[2]
end
if config and (block_name or block_type) then
if not unmanaged_map[config] then
unmanaged_map[config] = {}
end
el = {name=block_name, type=block_type}
table.insert(unmanaged_map[config], el)
end
end
end

os.execute("mkdir -p " .. uci_tmp_path)
os.execute("mkdir -p " .. unmanaged_path)
-- standard cursor
local standard = uci.cursor(standard_path)
-- unmanaged cursor
local unmanaged = uci.cursor(unmanaged_path, uci_tmp_path)

-- loop over standard sections and store a copy in unmanaged
for config_name, config_values in pairs(unmanaged_map) do
local uci_table = standard:get_all(config_name)
if uci_table then
-- create empty file
empty_file(unmanaged_path .. config_name)
for i, element in pairs(config_values) do
if element.name then
local block = uci_table[element.name]
if block then
write_uci_block(unmanaged, config_name, block)
end
else
standard:foreach(config_name, element.type, function(block)
write_uci_block(unmanaged, config_name, block)
end)
end
end
end
unmanaged:commit(config_name)
end

0 comments on commit 999af06

Please sign in to comment.