Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #800 from paulbellamy/proxy-status
Browse files Browse the repository at this point in the history
Add proxy info to 'weave status' output

Closes #756.
  • Loading branch information
rade committed May 29, 2015
2 parents 8c27d46 + 7f62ba6 commit 59fd53e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
44 changes: 39 additions & 5 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package proxy

import (
"bytes"
"fmt"
"net"
"net/http"
"net/url"
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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)
}
Expand All @@ -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()
}
6 changes: 4 additions & 2 deletions weave
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions weaveproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@ package main

import (
"fmt"
"net/http"
"os"

"code.google.com/p/getopt"
. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/proxy"
)

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
}
Expand All @@ -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)
}
}

0 comments on commit 59fd53e

Please sign in to comment.