From a113da0e34b2cd9620354fbe2702930f6f1a618e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 29 Aug 2014 14:44:50 +0100 Subject: [PATCH] Added a new command 'weave status', implemented via an http listener #11 --- consts.go | 1 + http_responder.go | 34 ++++++++++++++++++++++++++++++++++ weaver/main.go | 6 ++++-- weaver/weave | 24 +++++++++++++++++++++--- 4 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 http_responder.go diff --git a/consts.go b/consts.go index 106e1c8e..6d4074a2 100644 --- a/consts.go +++ b/consts.go @@ -10,6 +10,7 @@ const ( EthernetOverhead = 14 UDPOverhead = 28 // 20 bytes for IPv4, 8 bytes for UDP Port = 6783 + StatusPort = Port+1 DefaultPMTU = 65535 MaxUDPPacketSize = 65536 ChannelSize = 16 diff --git a/http_responder.go b/http_responder.go new file mode 100644 index 00000000..bf2ff05c --- /dev/null +++ b/http_responder.go @@ -0,0 +1,34 @@ +package weave + +import ( + "fmt" + "net/http" +) + +/* A simple http responder that will show the status of the router when asked, + e.g. via http://172.17.0.2/status +*/ +type HttpStatusResponder struct { + router *Router +} + +func NewHttpStatusResponder(router *Router) *HttpStatusResponder { + resp := &HttpStatusResponder{router} + return resp +} + +func (resp *HttpStatusResponder) SetRouter(newRouter *Router) { + resp.router = newRouter +} + +func (resp *HttpStatusResponder) ListenAndServe() { + http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { + if resp.router != nil { + fmt.Fprint(w, resp.router.Status()) + } else { + fmt.Fprintf(w, "Not initialized") + } + }) + address := fmt.Sprintf(":%d", StatusPort) + http.ListenAndServe(address, nil) +} diff --git a/weaver/main.go b/weaver/main.go index 174f9f93..27e89912 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/zettio/weave" + "github.com/bboreham/weave" "code.google.com/p/gopacket/layers" "crypto/sha256" "flag" @@ -70,7 +70,7 @@ func main() { flag.IntVar(&bufSz, "bufsz", 8, "capture buffer size in MB (defaults to 8MB)") flag.Parse() peers = flag.Args() - + if ifaceName == "" { fmt.Println("Missing required parameter 'iface'") os.Exit(1) @@ -123,6 +123,8 @@ func main() { } }() } + httpResponder := weave.NewHttpStatusResponder(router) + go httpResponder.ListenAndServe() handleSignals(router) } diff --git a/weaver/weave b/weaver/weave index e6bd3143..a7dee039 100755 --- a/weaver/weave +++ b/weaver/weave @@ -13,10 +13,14 @@ usage() { echo "Usage:" echo "weave / launch [-passwd ] ..." echo "weave / run ..." + echo "weave status" exit 1 } -shift 2 || usage +if [ "$#" -lt 1 ]; then + usage +exit 1 +fi [ `id -u` = 0 ] || { echo "weave must be run as 'root'" @@ -46,10 +50,25 @@ MACADDR=`cat /sys/class/net/$BRIDGE/address` case "$COMMAND" in launch) + shift 2 CONTAINER=$(docker run --privileged -d --name=weave -p 6783:6783/tcp -p 6783:6783/udp $WEAVE_DOCKER_ARGS zettio/weave -name $MACADDR "$@") IFACE=eth0 ;; + status) + DOCKERIP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' weave) + if command -v wget >/dev/null 2>&1 ; then + wget -O - -q $DOCKERIP:6784/status + elif command -v curl >/dev/null 2>&1 ; then + curl $DOCKERIP:6784/status + else + echo "weave status needs either curl or wget to function" >&2 + exit 1 + fi + # return the status from the wget or curl command, so if they fail then we fail. + exit $? + ;; run) + shift 2 CONTAINER=$(docker run -d "$@") IFACE=$CONTAINER_IFNAME ;; @@ -87,8 +106,7 @@ ip netns exec $NSPID ip link set $GUEST_IFNAME name $CONTAINER_IFNAME ip netns exec $NSPID ip addr add $IPADDR dev $CONTAINER_IFNAME ip netns exec $NSPID ip link set $CONTAINER_IFNAME up -if [ command -v ethtool >/dev/null 2>&1 ] -then +if command -v ethtool >/dev/null 2>&1 ; then ip netns exec $NSPID ethtool -K $IFACE tx off > /dev/null else echo "Cannot find ethtool; continuing without it" >&2