-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9634b80
commit f53d66d
Showing
7 changed files
with
168 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters