From a91357978d7abb836dc7b4f4307dd9b9da4e4379 Mon Sep 17 00:00:00 2001 From: adieu Date: Fri, 12 Sep 2014 00:36:21 +0800 Subject: [PATCH 1/8] Add attach command to weave. --- weaver/weave | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/weaver/weave b/weaver/weave index 5dab5d9b..559680e6 100755 --- a/weaver/weave +++ b/weaver/weave @@ -5,6 +5,7 @@ usage() { echo "Usage:" echo "weave launch / [-passwd ] ..." echo "weave run / ..." + echo "weave attach / " echo "weave expose /" echo "weave hide /" echo "weave status" @@ -131,6 +132,13 @@ case "$COMMAND" in echo $CONTAINER IFACE=$CONTAINER_IFNAME ;; + attach) + [ $# -gt 1 ] || usage + IPADDR=$1 + create_bridge + CONTAINER=$2 + IFACE=$CONTAINER_IFNAME + ;; expose) [ $# -gt 0 ] || usage IPADDR=$1 From 5490826666921dc2d538739251c68ea8b1a989f1 Mon Sep 17 00:00:00 2001 From: Kai Davenport Date: Mon, 15 Sep 2014 10:25:59 -0700 Subject: [PATCH 2/8] attach command that accepts and existing container id and runs configure_container_networking using it --- weaver/weave | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/weaver/weave b/weaver/weave index 77a28173..3faf7c19 100755 --- a/weaver/weave +++ b/weaver/weave @@ -5,6 +5,7 @@ usage() { echo "Usage:" echo "weave launch / [-passwd ] ..." echo "weave run / ..." + echo "weave attach / ..." echo "weave expose /" echo "weave hide /" echo "weave status" @@ -205,6 +206,14 @@ case "$COMMAND" in configure_container_networking $CONTAINER $IPADDR $CONTAINER_IFNAME echo $CONTAINER ;; + attach) + [ $# -gt 1 ] || usage + IPADDR=$1 + shift 1 + CONTAINER=$1 + shift 1 + configure_container_networking $CONTAINER $IPADDR $CONTAINER_IFNAME + ;; expose) [ $# -gt 0 ] || usage IPADDR=$1 From 4868585d6e30c5a9d9d6d15ba22eb4edbe0d1393 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 18 Sep 2014 17:18:54 +0100 Subject: [PATCH 3/8] Add detach command --- weaver/weave | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/weaver/weave b/weaver/weave index 371f3cec..b9b2d69c 100755 --- a/weaver/weave +++ b/weaver/weave @@ -6,6 +6,7 @@ usage() { echo "weave launch / [-passwd ] ..." echo "weave run / ..." echo "weave attach / " + echo "weave detach " echo "weave expose /" echo "weave hide /" echo "weave status" @@ -102,7 +103,7 @@ stop_masquerading() { } # the following borrows from https://github.com/jpetazzo/pipework -configure_container_networking() { +find_container_pid() { DOCKERPID=$(docker inspect --format='{{ .State.Pid }}' $1) [ "$DOCKERPID" = 0 ] && { echo "Container $1 not running." >&2 @@ -112,6 +113,11 @@ configure_container_networking() { echo "Container $1 unknown to Docker." >&2 exit 1 } + return 0 +} + +configure_container_networking() { + find_container_pid $1 NSPID=$DOCKERPID [ ! -d /var/run/netns ] && mkdir -p /var/run/netns @@ -154,6 +160,15 @@ configure_container_networking() { return $EXITCODE } +takedown_container_networking() { + find_container_pid $1 + NSPID=$DOCKERPID + + LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}" + + ip link del $LOCAL_IFNAME type veth || true +} + case "$COMMAND" in launch) [ $# -gt 0 ] || usage @@ -236,6 +251,12 @@ case "$COMMAND" in shift 1 configure_container_networking $CONTAINER $IPADDR $CONTAINER_IFNAME ;; + detach) + [ $# -gt 0 ] || usage + CONTAINER=$1 + shift 1 + takedown_container_networking $CONTAINER + ;; expose) [ $# -gt 0 ] || usage IPADDR=$1 From 0947fc204f9eb73e3950ffa9777a8b9f2c0e1d0f Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 18 Sep 2014 17:20:08 +0100 Subject: [PATCH 4/8] Untabify --- weaver/weave | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/weaver/weave b/weaver/weave index b9b2d69c..7340dc3f 100755 --- a/weaver/weave +++ b/weaver/weave @@ -113,11 +113,11 @@ find_container_pid() { echo "Container $1 unknown to Docker." >&2 exit 1 } - return 0 + return 0 } configure_container_networking() { - find_container_pid $1 + find_container_pid $1 NSPID=$DOCKERPID [ ! -d /var/run/netns ] && mkdir -p /var/run/netns @@ -161,7 +161,7 @@ configure_container_networking() { } takedown_container_networking() { - find_container_pid $1 + find_container_pid $1 NSPID=$DOCKERPID LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}" From 47310fbfc57876c705dd9eaa1719da6f08928591 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 18 Sep 2014 18:00:58 +0100 Subject: [PATCH 5/8] Described attach and detach commands --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 475ec6b2..ff0e2028 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,30 @@ will work. And, more interestingly, will work too, which is talking to a container that resides on $HOST1. +### Attach to already-running containers + +If you don't want to start containers via the `weave` script, e.g. because +you are controlling them as part of some larger orchestration scheme, you can +attach the weave network to an already-running container. To illustrate, we +can achieve the same effect as the first example like this: + + host1# C=$(docker run -d -t -i ubuntu) + host1# weave attach 10.0.1.1/24 $C + +There is a matching `weave detach` command: + + host1# weave detach $C + +You can detach a container from one subnet and re-attach it to another: + + host1# weave detach $C + host1# weave attach 10.0.2.1/24 $C + +or attach a container to multiple subnets: + + host1# weave attach 10.0.1.1/24 $C + host1# weave attach 10.0.2.1/24 $C + ### Service export Services running in containers on a weave network can be made From d1896e86d03e9da712aa816c091cc30e6038fd46 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 23 Sep 2014 13:01:21 +0100 Subject: [PATCH 6/8] Redesigned attach/detach --- weaver/weave | 73 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/weaver/weave b/weaver/weave index 7340dc3f..589a2710 100755 --- a/weaver/weave +++ b/weaver/weave @@ -103,7 +103,7 @@ stop_masquerading() { } # the following borrows from https://github.com/jpetazzo/pipework -find_container_pid() { +container_netns_preamble() { DOCKERPID=$(docker inspect --format='{{ .State.Pid }}' $1) [ "$DOCKERPID" = 0 ] && { echo "Container $1 not running." >&2 @@ -113,20 +113,40 @@ find_container_pid() { echo "Container $1 unknown to Docker." >&2 exit 1 } - return 0 -} -configure_container_networking() { - find_container_pid $1 NSPID=$DOCKERPID [ ! -d /var/run/netns ] && mkdir -p /var/run/netns rm -f /var/run/netns/$NSPID ln -s /proc/$NSPID/ns/net /var/run/netns/$NSPID - - EXITCODE=1 LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}" GUEST_IFNAME="v${CONTAINER_IFNAME}pg${NSPID}" + + return 0 +} + +container_netns_cleanup() { + rm -f /var/run/netns/$NSPID +} + +# Parameters are container ID, CIDR address +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 + 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 + fi + container_netns_cleanup + return $EXITCODE + fi + 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 @@ -155,18 +175,33 @@ configure_container_networking() { fi rm -f $IP_TMPFILE - rm -f /var/run/netns/$NSPID + container_netns_cleanup return $EXITCODE } -takedown_container_networking() { - find_container_pid $1 - NSPID=$DOCKERPID +# Parameters are container ID, CIDR address, interface-name +detach() { + container_netns_preamble $1 - LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${NSPID}" + 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 [ $(ip netns exec $NSPID ip -f inet addr show dev ethwe | wc -c) -eq 0 ] ; 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 + EXITCODE=0 + fi + else + echo "Failed to detach address $2 from container $1" >&2 + fi - ip link del $LOCAL_IFNAME type veth || true + container_netns_cleanup + return $EXITCODE } case "$COMMAND" in @@ -244,18 +279,12 @@ case "$COMMAND" in ;; attach) [ $# -gt 1 ] || usage - IPADDR=$1 - shift 1 create_bridge - CONTAINER=$1 - shift 1 - configure_container_networking $CONTAINER $IPADDR $CONTAINER_IFNAME + configure_container_networking $2 $1 $CONTAINER_IFNAME ;; detach) - [ $# -gt 0 ] || usage - CONTAINER=$1 - shift 1 - takedown_container_networking $CONTAINER + [ $# -gt 1 ] || usage + detach $2 $1 ;; expose) [ $# -gt 0 ] || usage From a6b83a1234689c8412a74e6c26d514be97e74c41 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 23 Sep 2014 13:52:41 +0100 Subject: [PATCH 7/8] Updated instructions for attach and detach --- README.md | 4 ++-- weaver/weave | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ff0e2028..53ac9e33 100644 --- a/README.md +++ b/README.md @@ -233,11 +233,11 @@ can achieve the same effect as the first example like this: There is a matching `weave detach` command: - host1# weave detach $C + host1# weave detach 10.0.1.1/24 $C You can detach a container from one subnet and re-attach it to another: - host1# weave detach $C + host1# weave detach 10.0.1.1/24 $C host1# weave attach 10.0.2.1/24 $C or attach a container to multiple subnets: diff --git a/weaver/weave b/weaver/weave index 589a2710..80d220c1 100755 --- a/weaver/weave +++ b/weaver/weave @@ -6,7 +6,7 @@ usage() { echo "weave launch / [-passwd ] ..." echo "weave run / ..." echo "weave attach / " - echo "weave detach " + echo "weave detach / " echo "weave expose /" echo "weave hide /" echo "weave status" From 2d8318b1584b84a7148367f46c59ee6dc55d79a5 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 23 Sep 2014 13:53:20 +0100 Subject: [PATCH 8/8] Improved test for blank string --- weaver/weave | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weaver/weave b/weaver/weave index 80d220c1..d7a617ed 100755 --- a/weaver/weave +++ b/weaver/weave @@ -187,7 +187,7 @@ detach() { 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 [ $(ip netns exec $NSPID ip -f inet addr show dev ethwe | wc -c) -eq 0 ] ; then + 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