Merge pull request #800 from paulbellamy/proxy-status
Add proxy info to 'weave status' output Closes #756.
This commit is contained in:
commit
59fd53ee99
3 files changed
+54
-15
No files matched your search
+39
-5
@@ -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()
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
+11
-8
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user