Skip to content

Commit

Permalink
Added name_anonymous_uci script #21
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed May 23, 2016
1 parent 9634b80 commit 603fc50
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 45 deletions.
3 changes: 2 additions & 1 deletion openwisp-config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ define Package/openwisp-config
TITLE:=OpenWISP Configuration Agent
MAINTAINER:=OpenWISP Project info@openwisp.org
PKG_ARCH:=all
DEPENDS:=+libpolarssl +curl +ca-certificates +lua +libuci-lua
DEPENDS:=+libpolarssl +curl +ca-certificates
+lua +libuci-lua +luafilesystem
endef

define Package/openwisp-config/description
Expand Down
45 changes: 45 additions & 0 deletions openwisp-config/files/lib/openwisp/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- openwisp uci utils

-- 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
-- write options for block
for key, value in pairs(block) do
write_uci_option(cursor, config, name, key, value)
end
end

-- abstraction for "uci set" which handles corner cases
function write_uci_option(cursor, config, name, key, value)
-- ignore properties starting with .
if string.sub(key, 1, 1) == '.' then
return
end
-- avoid duplicate list settings
if type(value) == 'table' then
-- create set with unique values
set = {}
for i, el in pairs(value) do
set[el] = true
end
-- reset value var with set contents
value = {}
for item_value, present in pairs(set) do
table.insert(value, item_value)
end
end
cursor:set(config, name, key, value)
end
55 changes: 55 additions & 0 deletions openwisp-config/files/name_anonymous_uci.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env lua
-- ensures anonymous configurations are named

require('io')
require('uci')
require('lfs')
require('openwisp.utils')
local arg={...}

-- parse arguments
for key, value in pairs(arg) do
-- test argument
if value == '--test=1' then test = true; end
end

local input_prefix = test and '../tests/' or '/etc/'
local input_path = input_prefix .. 'config'
local input = uci.cursor(input_path) -- read operations
local output = input -- write operations
local stdout = '' -- result

-- if test mode
if test then
-- use different write cursor in test mode
local uci_tmp_path = '/tmp/openwisp/.uci'
os.execute('mkdir -p ' .. uci_tmp_path)
output = uci.cursor('../tests/anonymous/', uci_tmp_path)
end

for file in lfs.dir(input_path) do
if lfs.attributes(file, 'mode') ~= 'directory' then
local changed = false
input:foreach(file, nil, function(block)
if block['.anonymous'] then
output:delete(file, block['.name'])
if file == 'system' and block['.type'] == 'system' then
block['.name'] = 'system'
end
block['.anonymous'] = false
write_uci_block(output, file, block)
changed = true
-- append new named block to stdout var
stdout = stdout .. file .. '.' .. block['.name'] .. ', '
end
end)
if changed then
output:commit(file)
end
end
end

if stdout ~= '' then
-- print changed UCI elements to standard output
print(string.sub(stdout, 0, -3)) -- remove trailing comma
end
12 changes: 12 additions & 0 deletions openwisp-config/files/openwisp.agent
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ merge_unmanaged() {
uci commit
}

# ensures there are no anonymous UCI blocks
call_name_anonymous_uci() {
output=$(/usr/sbin/name_anonymous_uci)
if [ -n "$output" ]; then
logger "The following uci configs have been renamed: $output" \
-t openwisp \
-p daemon.info
fi
}

# downloads configuration from controller
# performs test (if testing enabled)
# and applies it
Expand All @@ -326,6 +336,8 @@ update_configuration() {
return 3
fi

call_name_anonymous_uci

# control file to avoid reloading the agent while
# configuration is still being applied
touch $APPLYING_CONF
Expand Down
45 changes: 1 addition & 44 deletions openwisp-config/files/store_unmanaged.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require('os')
require('io')
require('uci')
require('openwisp.utils')
local blocks
local arg={...}

Expand Down Expand Up @@ -46,50 +47,6 @@ function empty_file(path)
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
-- write options for block
for key, value in pairs(block) do
write_uci_option(cursor, config, name, key, value)
end
end

-- abstraction for "uci set" which handles corner cases
function write_uci_option(cursor, config, name, key, value)
-- ignore properties starting with .
if string.sub(key, 1, 1) == '.' then
return
end
-- avoid duplicate list settings
if type(value) == 'table' then
-- create set with unique values
set = {}
for i, el in pairs(value) do
set[el] = true
end
-- reset value var with set contents
value = {}
for item_value, present in pairs(set) do
table.insert(value, item_value)
end
end
cursor:set(config, name, key, value)
end

-- convert list of blocks in a table with a structure like:
-- {
-- network = {
Expand Down
51 changes: 51 additions & 0 deletions openwisp-config/tests/test_name_anonymous_uci.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require('os')
require('io')
-- manually add lib dir to lua package path
package.path = package.path .. ';../files/lib/?.lua'
local luaunit = require('luaunit')
local name_anonymous_uci = assert(loadfile("../files/name_anonymous_uci.lua"))
local string = string
local prefix = './anonymous/'
assertNotNil = luaunit.assertNotNil
assertNil = luaunit.assertNil
assertEquals = luaunit.assertEquals

local function _setup()
os.execute('mkdir -p ' .. prefix)
os.execute('cp ./config/system '..prefix..'system')
os.execute('cp ./config/network '..prefix..'network')
end

local function _clean()
os.remove(prefix .. 'network')
os.remove(prefix .. 'system')
os.remove(prefix)
end

TestStoreUnmanaged = {}

TestStoreUnmanaged.setUp = _setup
TestStoreUnmanaged.tearDown = _clean

function TestStoreUnmanaged.test_default_behaviour()
name_anonymous_uci('--test=1')
-- check network
local file = io.open(prefix .. 'network')
assertNotNil(file)
local contents = file:read('*all')
assertNotNil(string.find(contents, "config switch 'cfg"))
assertNotNil(string.find(contents, "config switch_vlan 'cfg"))
-- ensure rest of config options are present
assertNotNil(string.find(contents, "config interface 'loopback'"))
assertNotNil(string.find(contents, "config interface 'lan'"))
-- check system
local file = io.open(prefix .. 'system')
assertNotNil(file)
local contents = file:read('*all')
assertNotNil(string.find(contents, "config system 'system'"))
-- ensure rest of config options are present
assertNotNil(string.find(contents, "config timeserver 'ntp'"))
assertNotNil(string.find(contents, "config led 'led_usb1'"))
end

os.exit(luaunit.LuaUnit.run())
2 changes: 2 additions & 0 deletions openwisp-config/tests/test_store_unmanaged.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require('os')
require('io')
-- manually add lib dir to lua package path
package.path = package.path .. ';../files/lib/?.lua'
local luaunit = require('luaunit')
local store_unmanaged = assert(loadfile("../files/store_unmanaged.lua"))
local default_blocks = "system.ntp " ..
Expand Down

0 comments on commit 603fc50

Please sign in to comment.