diff --git a/proxy/proxy.go b/proxy/proxy.go index f781385b..492a2cdb 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -1,6 +1,8 @@ package proxy import ( + "bytes" + "fmt" "net" "net/http" "net/url" @@ -19,14 +21,17 @@ var ( type Proxy struct { Dial func() (net.Conn, error) + version string client *docker.Client + dockerAddr string + listenAddr string withDNS bool dockerBridgeIP string withIPAM bool } -func NewProxy(targetURL string, withDNS, withIPAM bool) (*Proxy, error) { - u, err := url.Parse(targetURL) +func NewProxy(version, dockerAddr, listenAddr string, withDNS, withIPAM bool) (*Proxy, error) { + u, err := url.Parse(dockerAddr) if err != nil { return nil, err } @@ -39,7 +44,7 @@ func NewProxy(targetURL string, withDNS, withIPAM bool) (*Proxy, error) { } } - client, err := docker.NewClient(targetURL) + client, err := docker.NewClient(dockerAddr) if err != nil { return nil, err } @@ -56,7 +61,10 @@ func NewProxy(targetURL string, withDNS, withIPAM bool) (*Proxy, error) { Dial: func() (net.Conn, error) { return net.Dial(u.Scheme, targetAddr) }, + version: version, client: client, + dockerAddr: dockerAddr, + listenAddr: listenAddr, withDNS: withDNS, dockerBridgeIP: string(dockerBridgeIP), withIPAM: withIPAM, @@ -73,8 +81,9 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.withDNS, proxy.withIPAM}, w, r) case execCreateRegexp.MatchString(path): proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.withIPAM}, w, r) - case strings.HasPrefix(path, "/weave"): - w.WriteHeader(http.StatusOK) + case strings.HasPrefix(path, "/status"): + fmt.Fprintln(w, "weave proxy", proxy.version) + fmt.Fprintln(w, proxy.Status()) default: proxy.serveWithInterceptor(&nullInterceptor{}, w, r) } @@ -83,3 +92,28 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (proxy *Proxy) serveWithInterceptor(i interceptor, w http.ResponseWriter, r *http.Request) { newClient(proxy.Dial, i).ServeHTTP(w, r) } + +// Return status string +func (proxy *Proxy) Status() string { + var buf bytes.Buffer + fmt.Fprintln(&buf, "Listen address is", proxy.listenAddr) + fmt.Fprintln(&buf, "Docker address is", proxy.dockerAddr) + if proxy.withDNS { + fmt.Fprintln(&buf, "DNS on") + } else { + fmt.Fprintln(&buf, "DNS off") + } + if proxy.withIPAM { + fmt.Fprintln(&buf, "IPAM on") + } else { + fmt.Fprintln(&buf, "IPAM off") + } + return buf.String() +} + +func (proxy *Proxy) ListenAndServe() error { + return (&http.Server{ + Addr: proxy.listenAddr, + Handler: proxy, + }).ListenAndServe() +} diff --git a/weave b/weave index fca32b33..141b3408 100755 --- a/weave +++ b/weave @@ -559,7 +559,7 @@ wait_for_status_ip() { WAIT_TIME=0 while [ $WAIT_TIME -lt 3 ]; do sleep $WAIT_TIME - http_call_ip $1 $2 GET ${3:-/status} >/dev/null && return 0 + http_call_ip $1 $2 GET /status >/dev/null && return 0 WAIT_TIME=$((WAIT_TIME+1)) done echo "Timed out waiting for container to start." >&2 @@ -866,7 +866,7 @@ case "$COMMAND" in $WEAVEPROXY_DOCKER_ARGS \ $EXEC_IMAGE "$@") - wait_for_status_ip $PROXY_HOST $PROXY_PORT /weave + wait_for_status_ip $PROXY_HOST $PROXY_PORT echo $PROXY_CONTAINER ;; connect) @@ -886,6 +886,8 @@ case "$COMMAND" in http_call $CONTAINER_NAME $HTTP_PORT GET /status || res=1 echo http_call $DNS_CONTAINER_NAME $DNS_HTTP_PORT GET /status 2>/dev/null || true + echo + http_call_ip $PROXY_HOST $PROXY_PORT GET /status 2>/dev/null || true [ $res -eq 0 ] ;; ps) diff --git a/weaveproxy/main.go b/weaveproxy/main.go index 2d0baf93..8b43ee6f 100644 --- a/weaveproxy/main.go +++ b/weaveproxy/main.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "net/http" + "os" "code.google.com/p/getopt" . "github.com/weaveworks/weave/common" @@ -10,21 +10,28 @@ import ( ) var ( + version = "(unreleased version)" defaultTarget = "unix:///var/run/docker.sock" defaultListen = ":12375" ) func main() { var target, listen string - var withDNS, withIPAM, debug bool + var withDNS, withIPAM, debug, justVersion bool getopt.BoolVarLong(&debug, "debug", 'd', "log debugging information") + getopt.BoolVarLong(&justVersion, "version", 0, "print version and exit") getopt.StringVar(&target, 'H', fmt.Sprintf("docker daemon URL to proxy (default %s)", defaultTarget)) getopt.StringVar(&listen, 'L', fmt.Sprintf("address on which to listen (default %s)", defaultListen)) getopt.BoolVarLong(&withDNS, "with-dns", 'w', "instruct created containers to use weaveDNS as their nameserver") getopt.BoolVarLong(&withIPAM, "with-ipam", 'i', "automatically allocate addresses for containers without a WEAVE_CIDR") getopt.Parse() + if justVersion { + fmt.Printf("weave proxy %s\n", version) + os.Exit(0) + } + if target == "" { target = defaultTarget } @@ -37,19 +44,15 @@ func main() { InitDefaultLogging(true) } - p, err := proxy.NewProxy(target, withDNS, withIPAM) + p, err := proxy.NewProxy(version, target, listen, withDNS, withIPAM) if err != nil { Error.Fatalf("Could not start proxy: %s", err) } - s := &http.Server{ - Addr: listen, - Handler: p, - } Info.Printf("Listening on %s", listen) Info.Printf("Proxying %s", target) - if err := s.ListenAndServe(); err != nil { + if err := p.ListenAndServe(); err != nil { Error.Fatalf("Could not listen on %s: %s", listen, err) } }