From dc611fa0af793002e3f7057f18238754689d5d9e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 24 Sep 2014 09:39:44 +0100 Subject: [PATCH] Refactor common code in attach/detach --- weaver/weave | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/weaver/weave b/weaver/weave index 2870314e..f9853fc0 100755 --- a/weaver/weave +++ b/weaver/weave @@ -120,20 +120,30 @@ container_netns_preamble() { ln -s /proc/$NSPID/ns/net /var/run/netns/$NSPID LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}" GUEST_IFNAME="v${CONTAINER_IFNAME}pg${NSPID}" + EXITCODE=1 + IP_TMPFILE=/tmp/weave_ip_output_$$ + rm -f $IP_TMPFILE return 0 } -container_netns_cleanup() { +container_netns_postamble() { + if [ "$EXITCODE" -ne 0 ] ; then + if ! ps -p $DOCKERPID >/dev/null 2>&1 ; then + echo "Container $1 died" >&2 + else + echo "Failure during network configuration for container $1:" >&2 + cat $IP_TMPFILE >&2 + fi + fi + + rm -f $IP_TMPFILE rm -f /var/run/netns/$NSPID } # Parameters are container ID, CIDR address configure_container_networking() { container_netns_preamble $1 - EXITCODE=1 - IP_TMPFILE=/tmp/weave_ip_output_$$ - rm -f $IP_TMPFILE # If veth name already exists then assume we set it up already; # just add the IP address, if it's not already there @@ -161,17 +171,7 @@ configure_container_networking() { fi fi - if [ "$EXITCODE" -ne 0 ] ; then - if ! ps -p $DOCKERPID >/dev/null 2>&1 ; then - echo "Container $1 died" >&2 - else - echo "Failure during network configuration for container $1:" >&2 - cat $IP_TMPFILE >&2 - fi - fi - - rm -f $IP_TMPFILE - container_netns_cleanup + container_netns_postamble $1 return $EXITCODE } @@ -180,9 +180,6 @@ configure_container_networking() { detach() { container_netns_preamble $1 - EXITCODE=1 - IP_TMPFILE=/tmp/weave_ip_output_$$ - rm -f $IP_TMPFILE if (ip netns exec $NSPID ip addr list dev $CONTAINER_IFNAME | grep -qF $2) 2>$IP_TMPFILE ; then if ip netns exec $NSPID ip addr del $2 dev $CONTAINER_IFNAME 2>$IP_TMPFILE ; then # if no inet addresses remain then delete the veth @@ -198,17 +195,7 @@ detach() { EXITCODE=0 fi - if [ "$EXITCODE" -ne 0 ] ; then - if ! ps -p $DOCKERPID >/dev/null 2>&1 ; then - echo "Container $1 died" >&2 - else - echo "Failure while detaching address $2 from container $1" >&2 - cat $IP_TMPFILE >&2 - fi - fi - - rm -f $IP_TMPFILE - container_netns_cleanup + container_netns_postamble $1 return $EXITCODE }