Skip to content

Commit

Permalink
Added openwisp-remove-default-wifi script #19
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jun 20, 2016
1 parent dff12ee commit f8037e6
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
4 changes: 4 additions & 0 deletions openwisp-config/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ define Package/openwisp-config/install
files/sbin/openwisp-restore-unmanaged.lua \
$(1)/usr/sbin/openwisp-restore-unmanaged

$(INSTALL_BIN) \
files/sbin/openwisp-remove-default-wifi.lua \
$(1)/usr/sbin/openwisp-remove-default-wifi

$(INSTALL_BIN) \
files/sbin/openwisp-uci-autoname.lua \
$(1)/usr/sbin/openwisp-uci-autoname
Expand Down
10 changes: 7 additions & 3 deletions openwisp-config/files/openwisp.agent
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ call_store_unmanaged() {
/usr/sbin/openwisp-store-unmanaged -o="$UNMANAGED"
}

# ensures there are no anonymous UCI blocks
call_uci_autoname() {
# 1. removes default wifi-ifaces directive (LEDE or OpenWrt SSID)
# 2. ensures there are no anonymous UCI blocks
fix_uci_config() {
/usr/sbin/openwisp-remove-default-wifi
output=$(/usr/sbin/openwisp-uci-autoname)
if [ -n "$output" ]; then
logger "The following uci configs have been renamed: $output" \
Expand All @@ -323,7 +325,9 @@ update_configuration() {
return 3
fi

call_uci_autoname
# makes fixes to the default uci config so
# it's more suited for remote control
fix_uci_config

# control file to avoid reloading the agent while
# configuration is still being applied
Expand Down
28 changes: 28 additions & 0 deletions openwisp-config/files/sbin/openwisp-remove-default-wifi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env lua

require('uci')

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

local standard_prefix = test and '../tests/' or '/etc/'
local standard_path = standard_prefix .. 'config/'
-- standard cursor
local cursor = uci.cursor(standard_path)
local changed = false

cursor:foreach('wireless', 'wifi-iface', function(section)
if section['.anonymous'] and
section.encryption == 'none' and
section.mode == 'ap' and
(section.ssid == 'LEDE' or section.ssid == 'OpenWrt')
then
cursor:delete('wireless', section['.name'])
changed = true
end
end)
if changed then cursor:commit('wireless') end
21 changes: 21 additions & 0 deletions openwisp-config/tests/config/wireless
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

config wifi-device 'radio0'
option path 'platform/ar934x_wmac'
option disabled '1'
option country 'IT'
option phy 'phy0'
option hwmode '11g'
option channel '9'
option type 'mac80211'
option htmode 'HT20'

config wifi-device 'radio1'
option path 'pci0000:00/0000:00:00.0'
option disabled '1'
option country 'IT'
option phy 'phy1'
option hwmode '11a'
option channel '48'
option type 'mac80211'
option htmode 'HT20'

26 changes: 26 additions & 0 deletions openwisp-config/tests/test_remove_default_wifi.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- manually add lib dir to lua package path
package.path = package.path .. ';../files/lib/?.lua'
require('os')
require('io')
luaunit = require('luaunit')
remove_default_wifi = assert(loadfile("../files/sbin/openwisp-remove-default-wifi.lua"))

TestRemoveDefaultWifi = {
setUp = function()
os.execute('cp ./wifi/wireless ./config')
end,
tearDown = function()
-- os.execute('rm ./config/wireless')
end
}

function TestRemoveDefaultWifi.test_default()
remove_default_wifi('--test=1')
local file = io.open('./config/wireless')
luaunit.assertNotNil(file)
local contents = file:read('*all')
luaunit.assertNil(string.find(contents, "option ssid 'LEDE'"))
luaunit.assertNil(string.find(contents, "option ssid 'OpenWrt'"))
end

os.exit(luaunit.LuaUnit.run())
47 changes: 47 additions & 0 deletions openwisp-config/tests/wifi/wireless
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
config wifi-device 'radio0'
option path 'platform/ar934x_wmac'
option disabled '1'
option country 'IT'
option phy 'phy0'
option hwmode '11g'
option channel '9'
option type 'mac80211'
option htmode 'HT20'

config wifi-device 'radio1'
option path 'pci0000:00/0000:00:00.0'
option disabled '1'
option country 'IT'
option phy 'phy1'
option hwmode '11a'
option channel '48'
option type 'mac80211'
option htmode 'HT20'

config wifi-iface
option ssid 'LEDE'
option encryption 'none'
option device 'radio0'
option mode 'ap'
option network 'lan'

config wifi-iface
option ssid 'LEDE'
option encryption 'none'
option device 'radio1'
option mode 'ap'
option network 'lan'

config wifi-iface
option ssid 'OpenWrt'
option encryption 'none'
option device 'radio0'
option mode 'ap'
option network 'lan'

config wifi-iface
option ssid 'OpenWrt'
option encryption 'none'
option device 'radio1'
option mode 'ap'
option network 'lan'
1 change: 1 addition & 0 deletions runtests
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lua test_store_unmanaged.lua -v
lua test_restore_unmanaged.lua -v
lua test_uci_autoname.lua -v
lua test_utils.lua -v
lua test_remove_deafult_wifi.lua -v

0 comments on commit f8037e6

Please sign in to comment.