From acbabc4f9e4e01ece31421b203522523896a905c Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 27 Oct 2014 08:22:22 +0000 Subject: [PATCH 1/3] s/StatusPort/HttpPort since we are adding functionality other than status --- router/consts.go | 2 +- weaver/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/router/consts.go b/router/consts.go index 2d851dfb..02a93edf 100644 --- a/router/consts.go +++ b/router/consts.go @@ -11,7 +11,7 @@ const ( EthernetOverhead = 14 UDPOverhead = 28 // 20 bytes for IPv4, 8 bytes for UDP Port = 6783 - StatusPort = Port + 1 + HttpPort = Port + 1 DefaultPMTU = 65535 MaxUDPPacketSize = 65536 ChannelSize = 16 diff --git a/weaver/main.go b/weaver/main.go index 7bb0c416..523a5429 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -141,7 +141,7 @@ func handleHttp(router *weave.Router) { http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, router.Status()) }) - address := fmt.Sprintf(":%d", weave.StatusPort) + address := fmt.Sprintf(":%d", weave.HttpPort) err := http.ListenAndServe(address, nil) if err != nil { log.Fatal("Unable to create http listener: ", err) From 6393e545aea7ae9218d54019be27d56b9b9043c4 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 27 Oct 2014 09:15:14 +0000 Subject: [PATCH 2/3] introduce 'weave connect' to connect a running router to other peers Getting this to work with 'wget' is just too cumbersome especially when it comes to getting the error message out. So 'curl' only. --- weave | 45 ++++++++++++++++++++++++++++++--------------- weaver/main.go | 8 ++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/weave b/weave index de850089..a2bfceda 100755 --- a/weave +++ b/weave @@ -3,13 +3,14 @@ set -e usage() { echo "Usage:" - echo "weave launch [-password ] ..." - echo "weave run ..." - echo "weave start " - echo "weave attach " - echo "weave detach " - echo "weave expose " - echo "weave hide " + echo "weave launch [-password ] ..." + echo "weave run ..." + echo "weave start " + echo "weave attach " + echo "weave detach " + echo "weave expose " + echo "weave hide " + echo "weave connect " echo "weave status" echo "weave version" echo "weave stop" @@ -126,6 +127,17 @@ stop_masquerading() { run_iptables -t nat -X WEAVE >/dev/null 2>&1 || true } +docker_ip() { + if ! DOCKERIP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' $CONTAINER_NAME 2>/dev/null) ; then + echo "Weave is not running." >&2 + exit 1 + fi + if [ -z "$DOCKERIP" ] ; then + echo "Weave is not running." >&2 + exit 1 + fi +} + # the following borrows from https://github.com/jpetazzo/pipework with_container_netns () { CONTAINER="$1" @@ -269,16 +281,19 @@ case "$COMMAND" in with_container_netns $CONTAINER attach $CIDR eth0 echo $CONTAINER ;; + connect) + [ $# -eq 1 ] || usage + docker_ip + if command_exists curl ; then + curl -d peer=$1 $DOCKERIP:$HTTP_PORT/connect + else + echo "Cannot find curl. Install it for 'weave connect' to function." >&2 + exit 1 + fi + ;; status) [ $# -eq 0 ] || usage - if ! DOCKERIP=$(docker inspect --format='{{ .NetworkSettings.IPAddress }}' $CONTAINER_NAME 2>/dev/null) ; then - echo "Weave is not running." >&2 - exit 1 - fi - if [ -z "$DOCKERIP" ] ; then - echo "Weave is not running." >&2 - exit 1 - fi + docker_ip if command_exists wget ; then wget -O - -q $DOCKERIP:$HTTP_PORT/status elif command_exists curl ; then diff --git a/weaver/main.go b/weaver/main.go index 523a5429..76f1d82f 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -141,6 +141,14 @@ func handleHttp(router *weave.Router) { http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, router.Status()) }) + http.HandleFunc("/connect", func (w http.ResponseWriter, r *http.Request) { + peer := r.FormValue("peer") + if addr, err := net.ResolveTCPAddr("tcp4", weave.NormalisePeerAddr(peer)); err == nil { + router.ConnectionMaker.InitiateConnection(addr.String()) + } else { + http.Error(w, fmt.Sprint("invalid peer address: ", err), http.StatusBadRequest) + } + }) address := fmt.Sprintf(":%d", weave.HttpPort) err := http.ListenAndServe(address, nil) if err != nil { From 1b9dbad15fb88310b663d45bbc840317d767903f Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Mon, 27 Oct 2014 14:34:59 +0000 Subject: [PATCH 3/3] cosmetic --- weave | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/weave b/weave index 669cc575..ae034cf4 100755 --- a/weave +++ b/weave @@ -3,7 +3,7 @@ set -e usage() { echo "Usage:" - echo "weave launch [-password ] ..." + echo "weave launch [-password ] ..." echo "weave connect " echo "weave run ..." echo "weave start " @@ -16,8 +16,8 @@ usage() { echo "weave stop" echo "weave reset" echo - echo "where is of the form /, and" - echo " is of the form [:]" + echo "where is of the form [:], and" + echo " is of the form /" exit 1 }