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
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -19,14 +21,17 @@ var (
|
|||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
Dial func() (net.Conn, error)
|
Dial func() (net.Conn, error)
|
||||||
|
version string
|
||||||
client *docker.Client
|
client *docker.Client
|
||||||
|
dockerAddr string
|
||||||
|
listenAddr string
|
||||||
withDNS bool
|
withDNS bool
|
||||||
dockerBridgeIP string
|
dockerBridgeIP string
|
||||||
withIPAM bool
|
withIPAM bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxy(targetURL string, withDNS, withIPAM bool) (*Proxy, error) {
|
func NewProxy(version, dockerAddr, listenAddr string, withDNS, withIPAM bool) (*Proxy, error) {
|
||||||
u, err := url.Parse(targetURL)
|
u, err := url.Parse(dockerAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -56,7 +61,10 @@ func NewProxy(targetURL string, withDNS, withIPAM bool) (*Proxy, error) {
|
|||||||
Dial: func() (net.Conn, error) {
|
Dial: func() (net.Conn, error) {
|
||||||
return net.Dial(u.Scheme, targetAddr)
|
return net.Dial(u.Scheme, targetAddr)
|
||||||
},
|
},
|
||||||
|
version: version,
|
||||||
client: client,
|
client: client,
|
||||||
|
dockerAddr: dockerAddr,
|
||||||
|
listenAddr: listenAddr,
|
||||||
withDNS: withDNS,
|
withDNS: withDNS,
|
||||||
dockerBridgeIP: string(dockerBridgeIP),
|
dockerBridgeIP: string(dockerBridgeIP),
|
||||||
withIPAM: withIPAM,
|
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)
|
proxy.serveWithInterceptor(&startContainerInterceptor{proxy.client, proxy.withDNS, proxy.withIPAM}, w, r)
|
||||||
case execCreateRegexp.MatchString(path):
|
case execCreateRegexp.MatchString(path):
|
||||||
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.withIPAM}, w, r)
|
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.withIPAM}, w, r)
|
||||||
case strings.HasPrefix(path, "/weave"):
|
case strings.HasPrefix(path, "/status"):
|
||||||
w.WriteHeader(http.StatusOK)
|
fmt.Fprintln(w, "weave proxy", proxy.version)
|
||||||
|
fmt.Fprintln(w, proxy.Status())
|
||||||
default:
|
default:
|
||||||
proxy.serveWithInterceptor(&nullInterceptor{}, w, r)
|
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) {
|
func (proxy *Proxy) serveWithInterceptor(i interceptor, w http.ResponseWriter, r *http.Request) {
|
||||||
newClient(proxy.Dial, i).ServeHTTP(w, r)
|
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
|
WAIT_TIME=0
|
||||||
while [ $WAIT_TIME -lt 3 ]; do
|
while [ $WAIT_TIME -lt 3 ]; do
|
||||||
sleep $WAIT_TIME
|
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))
|
WAIT_TIME=$((WAIT_TIME+1))
|
||||||
done
|
done
|
||||||
echo "Timed out waiting for container to start." >&2
|
echo "Timed out waiting for container to start." >&2
|
||||||
@@ -866,7 +866,7 @@ case "$COMMAND" in
|
|||||||
$WEAVEPROXY_DOCKER_ARGS \
|
$WEAVEPROXY_DOCKER_ARGS \
|
||||||
$EXEC_IMAGE "$@")
|
$EXEC_IMAGE "$@")
|
||||||
|
|
||||||
wait_for_status_ip $PROXY_HOST $PROXY_PORT /weave
|
wait_for_status_ip $PROXY_HOST $PROXY_PORT
|
||||||
echo $PROXY_CONTAINER
|
echo $PROXY_CONTAINER
|
||||||
;;
|
;;
|
||||||
connect)
|
connect)
|
||||||
@@ -886,6 +886,8 @@ case "$COMMAND" in
|
|||||||
http_call $CONTAINER_NAME $HTTP_PORT GET /status || res=1
|
http_call $CONTAINER_NAME $HTTP_PORT GET /status || res=1
|
||||||
echo
|
echo
|
||||||
http_call $DNS_CONTAINER_NAME $DNS_HTTP_PORT GET /status 2>/dev/null || true
|
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 ]
|
[ $res -eq 0 ]
|
||||||
;;
|
;;
|
||||||
ps)
|
ps)
|
||||||
|
|||||||
+11
-8
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"os"
|
||||||
|
|
||||||
"code.google.com/p/getopt"
|
"code.google.com/p/getopt"
|
||||||
. "github.com/weaveworks/weave/common"
|
. "github.com/weaveworks/weave/common"
|
||||||
@@ -10,21 +10,28 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
version = "(unreleased version)"
|
||||||
defaultTarget = "unix:///var/run/docker.sock"
|
defaultTarget = "unix:///var/run/docker.sock"
|
||||||
defaultListen = ":12375"
|
defaultListen = ":12375"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var target, listen string
|
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(&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(&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.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(&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.BoolVarLong(&withIPAM, "with-ipam", 'i', "automatically allocate addresses for containers without a WEAVE_CIDR")
|
||||||
getopt.Parse()
|
getopt.Parse()
|
||||||
|
|
||||||
|
if justVersion {
|
||||||
|
fmt.Printf("weave proxy %s\n", version)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
if target == "" {
|
if target == "" {
|
||||||
target = defaultTarget
|
target = defaultTarget
|
||||||
}
|
}
|
||||||
@@ -37,19 +44,15 @@ func main() {
|
|||||||
InitDefaultLogging(true)
|
InitDefaultLogging(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := proxy.NewProxy(target, withDNS, withIPAM)
|
p, err := proxy.NewProxy(version, target, listen, withDNS, withIPAM)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Fatalf("Could not start proxy: %s", err)
|
Error.Fatalf("Could not start proxy: %s", err)
|
||||||
}
|
}
|
||||||
s := &http.Server{
|
|
||||||
Addr: listen,
|
|
||||||
Handler: p,
|
|
||||||
}
|
|
||||||
|
|
||||||
Info.Printf("Listening on %s", listen)
|
Info.Printf("Listening on %s", listen)
|
||||||
Info.Printf("Proxying %s", target)
|
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)
|
Error.Fatalf("Could not listen on %s: %s", listen, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user