Add proxy info to './weave status' output

This commit is contained in:
Paul Bellamy committed 2015-05-29 17:05:25 +01:00
1 parent 2be69634df
commit 7f62ba6ebe
3 files changed
+51 -12

No files matched your search

+38 -4
View File
@@ -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,
@@ -74,7 +82,8 @@ func (proxy *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case execCreateRegexp.MatchString(path):
proxy.serveWithInterceptor(&createExecInterceptor{proxy.client, proxy.withIPAM}, w, r)
case strings.HasPrefix(path, "/status"):
w.WriteHeader(http.StatusOK)
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()
}