networks: Make setup script idempotent

Only add a connection for a device if a connection with that name is not already
associated with that device.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Signed-off-by: Joseph Nuthalpati <njoseph@thoughtworks.com>
This commit is contained in:
Sunil Mohan Adapa 2017-10-04 16:06:32 +05:30
parent 6802e20a1d
commit 5e6e02184e
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2

View File

@ -14,6 +14,21 @@ function get-interfaces {
NO_OF_WIRELESS_IFACES=$(echo $WIRELESS_IFACES | wc -w)
}
function add-connection {
local connection_name="$1"
shift
local interface="$1"
shift
local remaining_arguments="$@"
already_exists=$(nmcli --terse --fields name,device con show | grep "$connection_name:$interface" || true)
if [ -n "$already_exists" ]; then
echo "Connection '$connection_name' already exists for device '$interface', not adding."
else
nmcli con add con-name "$connection_name" ifname "$interface" $remaining_arguments
fi
}
function activate-connection {
connection_name="$1"
nohup nmcli con up "$connection_name" &>/dev/null &
@ -25,7 +40,7 @@ function configure-regular-interface {
local connection_name="FreedomBox WAN"
# Create n-m connection for a regular interface
nmcli con add con-name "$connection_name" ifname "$interface" type ethernet
add-connection "$connection_name" "$interface" type ethernet
nmcli con modify "$connection_name" connection.autoconnect TRUE
nmcli con modify "$connection_name" connection.zone "$zone"
activate-connection "$connection_name"
@ -38,7 +53,7 @@ function configure-shared-interface {
local connection_name="FreedomBox LAN $interface"
# Create n-m connection for eth1
nmcli con add con-name "$connection_name" ifname "$interface" type ethernet
add-connection "$connection_name" "$interface" type ethernet
nmcli con modify "$connection_name" connection.autoconnect TRUE
nmcli con modify "$connection_name" connection.zone internal
@ -61,7 +76,7 @@ function configure-wireless-interface {
local ssid="FreedomBox$interface"
local secret="freedombox123"
nmcli con add con-name "$connection_name" ifname "$interface" type wifi ssid "$ssid"
add-connection "$connection_name" "$interface" type wifi ssid "$ssid"
nmcli con modify "$connection_name" connection.autoconnect TRUE
nmcli con modify "$connection_name" connection.zone internal
nmcli con modify "$connection_name" ipv4.method shared