From 8e50e0759df13a341f40a2221953506e1e0ceacb Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 23 Sep 2014 14:49:00 +0100 Subject: [PATCH] Make attach and detach idempotent --- weaver/weave | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/weaver/weave b/weaver/weave index d7a617ed..de0e72e7 100755 --- a/weaver/weave +++ b/weaver/weave @@ -113,7 +113,6 @@ container_netns_preamble() { echo "Container $1 unknown to Docker." >&2 exit 1 } - NSPID=$DOCKERPID [ ! -d /var/run/netns ] && mkdir -p /var/run/netns @@ -133,20 +132,21 @@ container_netns_cleanup() { configure_container_networking() { container_netns_preamble $1 - EXITCODE=1 - # If veth name already exists then assume we set it up already; - # just add the IP address + # just add the IP address, if it's not already there if ip link show $LOCAL_IFNAME >/dev/null 2>&1 ; then - if ip netns exec $NSPID ip addr add $2 dev $CONTAINER_IFNAME ; then - EXITCODE=0 - else - echo "Failed to attach address $2 to container $1" >&2 + if ! ip netns exec $NSPID ip addr list dev $CONTAINER_IFNAME | grep -qF $2 ; then + if ! ip netns exec $NSPID ip addr add $2 dev $CONTAINER_IFNAME ; then + echo "Failed to attach address $2 to container $1" >&2 + container_netns_cleanup + return 1 + fi fi container_netns_cleanup - return $EXITCODE + return 0 fi + EXITCODE=1 IP_TMPFILE=/tmp/weave_ip_output_$$ rm -f $IP_TMPFILE if ip link add name $LOCAL_IFNAME mtu $MTU type veth peer name $GUEST_IFNAME mtu $MTU >$IP_TMPFILE 2>&1 ; then @@ -185,19 +185,21 @@ detach() { container_netns_preamble $1 EXITCODE=1 - if ip netns exec $NSPID ip addr del $2 dev $CONTAINER_IFNAME ; then - # if no inet addresses remain then delete the veth - if [ -z "$(ip netns exec $NSPID ip -f inet addr list dev ethwe)" ] ; then - if ip link del $LOCAL_IFNAME type veth ; then - EXITCODE=0 + if ip netns exec $NSPID ip addr list dev $CONTAINER_IFNAME | grep -qF $2 ; then + if ip netns exec $NSPID ip addr del $2 dev $CONTAINER_IFNAME ; then + # if no inet addresses remain then delete the veth + if [ -z "$(ip netns exec $NSPID ip -f inet addr list dev ethwe)" ] ; then + if ip link del $LOCAL_IFNAME type veth ; then + EXITCODE=0 + else + echo "Failed to delete virtual ethernet device $LOCAL_IFNAME from container $1" >&2 + fi else - echo "Failed to delete virtual ethernet device $LOCAL_IFNAME from container $1" >&2 + EXITCODE=0 fi else - EXITCODE=0 + echo "Failed to detach address $2 from container $1" >&2 fi - else - echo "Failed to detach address $2 from container $1" >&2 fi container_netns_cleanup