From 06adae8fb5dacc96b95a21c15ef5ab945c7b792d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 23 Oct 2014 13:06:02 +0100 Subject: [PATCH 01/10] Added weave ps command --- weave | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/weave b/weave index ea6d9a9f..56efff72 100755 --- a/weave +++ b/weave @@ -10,6 +10,7 @@ usage() { echo "weave detach " echo "weave expose " echo "weave hide " + echo "weave ps" echo "weave status" echo "weave version" echo "weave stop" @@ -118,6 +119,9 @@ stop_masquerading() { } # the following borrows from https://github.com/jpetazzo/pipework + +# run all args as a command in the network namespace of container $2 +# if an error is caused by container dieing, swallow output from error with_container_netns () { CONTAINER="$2" CONTAINER_PID=$(docker inspect --format='{{ .State.Pid }}' $CONTAINER) @@ -139,6 +143,8 @@ with_container_netns () { LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${CONTAINER_PID}" GUEST_IFNAME="v${CONTAINER_IFNAME}pg${CONTAINER_PID}" + # if an error is caused by container dieing, swallow output from all commands and + # just say the container died. IP_TMPFILE=/tmp/weave_ip_output_$$ rm -f $IP_TMPFILE @@ -154,6 +160,7 @@ with_container_netns () { fi fi + cat $IP_TMPFILE # no error - send the output to stdout rm -f $IP_TMPFILE /var/run/netns/$NETNS return $STATUS } @@ -227,6 +234,23 @@ detach() { ip link del $LOCAL_IFNAME type veth } +# send to stdout any IPv4 addresses configured on the interface 'ethwe', one per line +container_weave_ips() { + (ip netns exec $NETNS ip addr show dev ethwe 2>&1 || true) | grep 'inet ' | while read line + do + echo $line | tr -s ' ' | cut -d ' ' -f 2 + done +} + +weave_ps() { + for CONTAINER_ID in $(docker ps -q) ; do + CONTAINER_IPS=$(with_container_netns container_weave_ips $CONTAINER_ID) + if [ -n "$CONTAINER_IPS" ] ; then + echo $(docker inspect --format '{{ .Name }} {{ .Config.Image }} ({{ .Config.Hostname }}.{{ .Config.Domainname }}):' $CONTAINER_ID) $CONTAINER_IPS + fi + done +} + case "$COMMAND" in launch) [ $# -gt 0 ] || usage @@ -284,6 +308,9 @@ case "$COMMAND" in exit 1 fi ;; + ps) + weave_ps + ;; version) # Try the running container first; if no luck try the image if ! DOCKERIMAGE=$(docker inspect --format='{{ .Image }}' weave 2>/dev/null) ; then From f7eeb1f8445e53ee7730a35e7a6782ac63955278 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 23 Oct 2014 15:35:02 +0100 Subject: [PATCH 02/10] Fix issues raised in code review --- weave | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/weave b/weave index 56efff72..cb1bda9a 100755 --- a/weave +++ b/weave @@ -121,7 +121,7 @@ stop_masquerading() { # the following borrows from https://github.com/jpetazzo/pipework # run all args as a command in the network namespace of container $2 -# if an error is caused by container dieing, swallow output from error +# if an error is caused by container dying, swallow output from error with_container_netns () { CONTAINER="$2" CONTAINER_PID=$(docker inspect --format='{{ .State.Pid }}' $CONTAINER) @@ -143,8 +143,6 @@ with_container_netns () { LOCAL_IFNAME="v${CONTAINER_IFNAME}pl${CONTAINER_PID}" GUEST_IFNAME="v${CONTAINER_IFNAME}pg${CONTAINER_PID}" - # if an error is caused by container dieing, swallow output from all commands and - # just say the container died. IP_TMPFILE=/tmp/weave_ip_output_$$ rm -f $IP_TMPFILE @@ -236,10 +234,7 @@ detach() { # send to stdout any IPv4 addresses configured on the interface 'ethwe', one per line container_weave_ips() { - (ip netns exec $NETNS ip addr show dev ethwe 2>&1 || true) | grep 'inet ' | while read line - do - echo $line | tr -s ' ' | cut -d ' ' -f 2 - done + ip netns exec $NETNS ip addr show dev ethwe 2>&1 | grep -o 'inet .*' | cut -d ' ' -f 2 || true } weave_ps() { @@ -287,7 +282,7 @@ case "$COMMAND" in # additional parameters, such as resource limits, to docker # when launching the weave container. CONTAINER=$(docker run --privileged -d --name=weave -p 6783:6783/tcp -p 6783:6783/udp $WEAVE_DOCKER_ARGS $IMAGE -name $MACADDR "$@" | tail -n 1) - with_container_netns attach $CONTAINER $IPADDR eth0 + with_container_netns attach $CONTAINER $IPADDR eth0 >/dev/null echo $CONTAINER ;; status) @@ -327,7 +322,7 @@ case "$COMMAND" in shift 1 create_bridge CONTAINER=$(docker run -d "$@" | tail -n 1) - with_container_netns attach $CONTAINER $IPADDR $CONTAINER_IFNAME + with_container_netns attach $CONTAINER $IPADDR $CONTAINER_IFNAME >/dev/null echo $CONTAINER ;; start) @@ -341,11 +336,11 @@ case "$COMMAND" in attach) [ $# -gt 1 ] || usage create_bridge - with_container_netns attach $2 $1 $CONTAINER_IFNAME + with_container_netns attach $2 $1 $CONTAINER_IFNAME >/dev/null ;; detach) [ $# -gt 1 ] || usage - with_container_netns detach $2 $1 + with_container_netns detach $2 $1 >/dev/null ;; expose) [ $# -gt 0 ] || usage From e4fe57a02c79e97309776d636394db2a83bc3d02 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 24 Oct 2014 10:03:49 +0100 Subject: [PATCH 03/10] Make extra container information optional --- weave | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/weave b/weave index cb1bda9a..efca257d 100755 --- a/weave +++ b/weave @@ -241,7 +241,8 @@ weave_ps() { for CONTAINER_ID in $(docker ps -q) ; do CONTAINER_IPS=$(with_container_netns container_weave_ips $CONTAINER_ID) if [ -n "$CONTAINER_IPS" ] ; then - echo $(docker inspect --format '{{ .Name }} {{ .Config.Image }} ({{ .Config.Hostname }}.{{ .Config.Domainname }}):' $CONTAINER_ID) $CONTAINER_IPS + [ $# -gt 0 ] && [ "$1" = "-v" ] && CONTAINER_INFO=$(docker inspect --format '{{ .Name }} {{ .Config.Image }}' $CONTAINER_ID) + echo $CONTAINER_ID $CONTAINER_INFO $CONTAINER_IPS fi done } @@ -304,7 +305,7 @@ case "$COMMAND" in fi ;; ps) - weave_ps + weave_ps "$@" ;; version) # Try the running container first; if no luck try the image From ced729914084322459ce156aa7f1e10bc7dfa744 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 24 Oct 2014 10:10:06 +0100 Subject: [PATCH 04/10] Removed weave ps -v option --- weave | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/weave b/weave index efca257d..cd566cf3 100755 --- a/weave +++ b/weave @@ -241,8 +241,7 @@ weave_ps() { for CONTAINER_ID in $(docker ps -q) ; do CONTAINER_IPS=$(with_container_netns container_weave_ips $CONTAINER_ID) if [ -n "$CONTAINER_IPS" ] ; then - [ $# -gt 0 ] && [ "$1" = "-v" ] && CONTAINER_INFO=$(docker inspect --format '{{ .Name }} {{ .Config.Image }}' $CONTAINER_ID) - echo $CONTAINER_ID $CONTAINER_INFO $CONTAINER_IPS + echo $CONTAINER_ID $CONTAINER_IPS fi done } @@ -305,7 +304,7 @@ case "$COMMAND" in fi ;; ps) - weave_ps "$@" + weave_ps ;; version) # Try the running container first; if no luck try the image From 52a4fe545e011f8ba6ccea8bcc1e1e21b4abcdb5 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Oct 2014 11:39:12 +0000 Subject: [PATCH 05/10] Add mac address to weave ps output --- weave | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/weave b/weave index a92dd811..99bfdc4b 100755 --- a/weave +++ b/weave @@ -262,16 +262,16 @@ detach() { ip link del $LOCAL_IFNAME type veth } -# send to stdout any IPv4 addresses configured on the interface 'ethwe', one per line -container_weave_ips() { - ip netns exec $NETNS ip addr show dev ethwe 2>&1 | grep -o 'inet .*' | cut -d ' ' -f 2 || true +container_weave_addrs() { + ip netns exec $NETNS ip addr show dev ethwe } weave_ps() { for CONTAINER_ID in $(docker ps -q) ; do - CONTAINER_IPS=$(with_container_netns $CONTAINER_ID container_weave_ips) - if [ -n "$CONTAINER_IPS" ] ; then - echo $CONTAINER_ID $CONTAINER_IPS + if CONTAINER_ADDRS=$(with_container_netns $CONTAINER_ID container_weave_addrs 2>&1) ; then + CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) + CONTAINER_MAC=$(echo "$CONTAINER_ADDRS" | grep -o 'link/ether .*' | cut -d ' ' -f 2) + echo $CONTAINER_ID $CONTAINER_MAC $CONTAINER_IPS fi done } From fe2d1a3699b92cc4155242921499677671838eaf Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Oct 2014 11:44:20 +0000 Subject: [PATCH 06/10] CONTAINER_IFNAME rather than ethwe --- weave | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weave b/weave index 99bfdc4b..5ddb30d7 100755 --- a/weave +++ b/weave @@ -263,7 +263,7 @@ detach() { } container_weave_addrs() { - ip netns exec $NETNS ip addr show dev ethwe + ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME } weave_ps() { From d56ac73732438d3c2405a4d6a75131cc92b7061c Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Oct 2014 11:47:23 +0000 Subject: [PATCH 07/10] Inline weave ps function --- weave | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/weave b/weave index 5ddb30d7..8f4caef8 100755 --- a/weave +++ b/weave @@ -266,16 +266,6 @@ container_weave_addrs() { ip netns exec $NETNS ip addr show dev $CONTAINER_IFNAME } -weave_ps() { - for CONTAINER_ID in $(docker ps -q) ; do - if CONTAINER_ADDRS=$(with_container_netns $CONTAINER_ID container_weave_addrs 2>&1) ; then - CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) - CONTAINER_MAC=$(echo "$CONTAINER_ADDRS" | grep -o 'link/ether .*' | cut -d ' ' -f 2) - echo $CONTAINER_ID $CONTAINER_MAC $CONTAINER_IPS - fi - done -} - case "$COMMAND" in launch) case $(docker inspect --format '{{ .State.Running }} {{ .Config.Image }}' $CONTAINER_NAME 2>/dev/null) in @@ -338,7 +328,14 @@ case "$COMMAND" in fi ;; ps) - weave_ps + [ $# -eq 0 ] || usage + for CONTAINER_ID in $(docker ps -q) ; do + if CONTAINER_ADDRS=$(with_container_netns $CONTAINER_ID container_weave_addrs 2>&1) ; then + CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) + CONTAINER_MAC=$(echo "$CONTAINER_ADDRS" | grep -o 'link/ether .*' | cut -d ' ' -f 2) + echo $CONTAINER_ID $CONTAINER_MAC $CONTAINER_IPS + fi + done ;; version) [ $# -eq 0 ] || usage From c749427de5c868104142226280746c1c58d6365a Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Oct 2014 11:48:23 +0000 Subject: [PATCH 08/10] cosmetic --- weave | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weave b/weave index 8f4caef8..3e83f705 100755 --- a/weave +++ b/weave @@ -331,8 +331,8 @@ case "$COMMAND" in [ $# -eq 0 ] || usage for CONTAINER_ID in $(docker ps -q) ; do if CONTAINER_ADDRS=$(with_container_netns $CONTAINER_ID container_weave_addrs 2>&1) ; then - CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) CONTAINER_MAC=$(echo "$CONTAINER_ADDRS" | grep -o 'link/ether .*' | cut -d ' ' -f 2) + CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) echo $CONTAINER_ID $CONTAINER_MAC $CONTAINER_IPS fi done From 72b62b4ffcc4daee49f960acc208e68c1e32ccb7 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Oct 2014 12:34:09 +0000 Subject: [PATCH 09/10] Add weave ps to usage --- weave | 1 + 1 file changed, 1 insertion(+) diff --git a/weave b/weave index 3e83f705..20928d82 100755 --- a/weave +++ b/weave @@ -11,6 +11,7 @@ usage() { echo "weave detach " echo "weave expose " echo "weave hide " + echo "weave ps" echo "weave status" echo "weave version" echo "weave stop" From 8ab9bad31d0743f9980201035fc1218420bd615a Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Wed, 29 Oct 2014 12:34:28 +0000 Subject: [PATCH 10/10] cosmetic --- weave | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weave b/weave index 20928d82..7a482bec 100755 --- a/weave +++ b/weave @@ -333,7 +333,7 @@ case "$COMMAND" in for CONTAINER_ID in $(docker ps -q) ; do if CONTAINER_ADDRS=$(with_container_netns $CONTAINER_ID container_weave_addrs 2>&1) ; then CONTAINER_MAC=$(echo "$CONTAINER_ADDRS" | grep -o 'link/ether .*' | cut -d ' ' -f 2) - CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) + CONTAINER_IPS=$(echo "$CONTAINER_ADDRS" | grep -o 'inet .*' | cut -d ' ' -f 2) echo $CONTAINER_ID $CONTAINER_MAC $CONTAINER_IPS fi done