Refactor common code in attach/detach
This commit is contained in:
1 parent
a21860689a
commit
dc611fa0af
1 file changed
+16
-29
+16
-29
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user