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

Commit

Permalink
First pass at passing DOCKER_HOST=unix://* through to the weave and p…
Browse files Browse the repository at this point in the history
…roxy
  • Loading branch information
paulbellamy committed Nov 16, 2015
1 parent 3d4b691 commit 1401100
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
8 changes: 7 additions & 1 deletion prog/weaveproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

var (
version = "(unreleased version)"
version = "(unreleased version)"
defaultDockerHost = "unix:///var/run/docker.sock"
)

func main() {
Expand Down Expand Up @@ -56,6 +57,11 @@ func main() {
Log.Infoln("weave proxy", version)
Log.Infoln("Command line arguments:", strings.Join(os.Args[1:], " "))

c.DockerHost = defaultDockerHost
if dockerHost := os.Getenv("DOCKER_HOST"); dockerHost != "" {
c.DockerHost = dockerHost
}

p, err := proxy.NewProxy(c)
if err != nil {
Log.Fatalf("Could not start proxy: %s", err)
Expand Down
1 change: 1 addition & 0 deletions proxy/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func callWeave(args ...string) ([]byte, []byte, error) {
}

propagateEnv("DOCKER_BRIDGE")
propagateEnv("DOCKER_HOST")

// Propogage WEAVE_DEBUG, to make debugging easier.
propagateEnv("WEAVE_DEBUG")
Expand Down
22 changes: 16 additions & 6 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const (
defaultCaFile = "ca.pem"
defaultKeyFile = "key.pem"
defaultCertFile = "cert.pem"
dockerSock = "/var/run/docker.sock"
dockerSockUnix = "unix://" + dockerSock
)

var (
Expand Down Expand Up @@ -54,6 +52,7 @@ type Config struct {
Version string
WithDNS bool
WithoutDNS bool
DockerHost string
}

type wait struct {
Expand Down Expand Up @@ -86,7 +85,7 @@ func NewProxy(c Config) (*Proxy, error) {
// to insulate ourselves from breaking changes to the API, as
// happened in 1.20 (Docker 1.8.0) when the presentation of
// volumes changed in `inspect`.
client, err := weavedocker.NewVersionedClient(dockerSockUnix, "1.15")
client, err := weavedocker.NewVersionedClient(c.DockerHost, "1.15")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,7 +131,16 @@ func (proxy *Proxy) AttachExistingContainers() {
}

func (proxy *Proxy) Dial() (net.Conn, error) {
return net.Dial("unix", dockerSock)
proto := "tcp"
addr := proxy.Config.DockerHost
switch {
case strings.HasPrefix(addr, "unix://"):
proto = "unix"
addr = strings.TrimPrefix(addr, "unix://")
case strings.HasPrefix(addr, "tcp://"):
addr = strings.TrimPrefix(addr, "tcp://")
}
return net.Dial(proto, addr)
}

func (proxy *Proxy) findWeaveWaitVolumes() error {
Expand Down Expand Up @@ -291,8 +299,10 @@ func (proxy *Proxy) listen(protoAndAddr string) (net.Listener, string, error) {
if err != nil {
return nil, "", err
}
if err = copyOwnerAndPermissions(dockerSock, addr); err != nil {
return nil, "", err
if strings.HasPrefix(proxy.Config.DockerHost, "unix://") {
if err = copyOwnerAndPermissions(strings.TrimPrefix(proxy.Config.DockerHost, "unix://"), addr); err != nil {
return nil, "", err
}
}

default:
Expand Down
22 changes: 17 additions & 5 deletions weave
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,21 @@ usage() {
exit 1
}

docker_sock() {
if echo "$DOCKER_HOST" | grep -q "^unix://" >/dev/null; then
echo "${DOCKER_HOST#unix://}"
else
echo "/var/run/docker.sock"
fi
}

docker_sock_volume_mount() {
echo "-v $(docker_sock):$(docker_sock) -e DOCKER_HOST=unix://$(docker_sock)"
}

exec_remote() {
docker $DOCKER_CLIENT_ARGS run --rm --privileged --net=host \
-v /var/run/docker.sock:/var/run/docker.sock \
$(docker_sock_volume_mount) \
-v /proc:/hostproc \
-e PROCFS=/hostproc \
-e DOCKERHUB_USER="$DOCKERHUB_USER" \
Expand Down Expand Up @@ -1478,7 +1490,7 @@ launch_router() {
# additional parameters, such as resource limits, to docker
# when launching the weave container.
ROUTER_CONTAINER=$(docker run --privileged -d --name=$CONTAINER_NAME \
-v /var/run/docker.sock:/var/run/docker.sock \
$(docker_sock_volume_mount) \
-p $PORT:$CONTAINER_PORT/tcp -p $PORT:$CONTAINER_PORT/udp \
${NETHOST_OPT:-$DNS_PORT_MAPPING} \
-e WEAVE_PASSWORD \
Expand All @@ -1490,7 +1502,7 @@ launch_router() {
--dns-effective-listen-address $DOCKER_BRIDGE_IP \
${NETHOST_OPT:+$DNS_ROUTER_OPTS} $NO_DNS_OPT \
--http-addr $HTTP_IP:$HTTP_PORT \
--docker-api "unix:///var/run/docker.sock" "$@")
--docker-api "unix://$(docker_sock)" "$@")
with_container_netns_or_die $CONTAINER_NAME setup_router_iface_$BRIDGE_TYPE
attach_router
}
Expand Down Expand Up @@ -1537,7 +1549,7 @@ launch_proxy() {
proxy_args "$@"
PROXY_CONTAINER=$(docker run --privileged -d --name=$PROXY_CONTAINER_NAME --net=host \
$PROXY_VOLUMES \
-v /var/run/docker.sock:/var/run/docker.sock \
$(docker_sock_volume_mount) \
-v /var/run/weave:/var/run/weave \
-v /proc:/hostproc \
-e PROCFS=/hostproc \
Expand All @@ -1559,7 +1571,7 @@ launch_plugin() {
PLUGIN_CONTAINER=$(docker run --privileged -d --name=$PLUGIN_CONTAINER_NAME \
--restart=always \
--net=host \
-v /var/run/docker.sock:/var/run/docker.sock \
$(docker_sock_volume_mount) \
-v /run/docker/plugins:/run/docker/plugins \
$PLUGIN_IMAGE "$@")
}
Expand Down

0 comments on commit 1401100

Please sign in to comment.