From 06adae8fb5dacc96b95a21c15ef5ab945c7b792d Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 23 Oct 2014 13:06:02 +0100 Subject: [PATCH] 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