From 6c4271f15d689ad0ce01f5e7e6379f6a7990bff2 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 5 May 2015 15:17:11 +0100 Subject: [PATCH] Add launch-proxy command to cli Use curl /weave to wait for the proxy to boot before returning. --- weave | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/weave b/weave index 5cf1da6e..39426d9f 100755 --- a/weave +++ b/weave @@ -27,6 +27,7 @@ usage() { echo "weave setup" echo "weave launch [-password ] [-nickname ] [-iprange ] ..." echo "weave launch-dns " + echo "weave launch-proxy [-H ]" echo "weave connect " echo "weave forget " echo "weave run [--with-dns] [ ...] ..." @@ -679,6 +680,63 @@ get_ipam_cidr() { fi } +###################################################################### +# weave proxy helpers +###################################################################### + +PROXY_PORT=12375 +PROXY_CONTAINER_NAME=weaveproxy + +proxy_host() { + case $DOCKER_HOST in + tcp://*) + PROXY_HOST=$(echo $DOCKER_HOST | sed -e "s|tcp://\([^:]*\):[0-9]*|\1|") + ;; + *) + PROXY_HOST="localhost" + ;; + esac +} + +wait_for_proxy_to_boot() { + proxy_host + for n in `seq 1 10`; do + if curl --max-time 1 --connect-timeout 1 -s -X GET http://$PROXY_HOST:$PROXY_PORT/weave ; then + return 0 + fi + sleep 1 + done + echo "Proxy did not start within 10 seconds" >&2 + return 1 +} + +ensure_proxy_running() { + case $(docker inspect --format '{{.State.Running}} {{.Config.Image}}' $PROXY_CONTAINER_NAME 2>/dev/null) in + "true $EXEC_IMAGE") + return 0 + ;; + true*) + echo "Proxy already running with unknown image" >&2 + exit 1 + ;; + false*) + docker rm -f $PROXY_CONTAINER_NAME >/dev/null + ;; + esac + # Set WEAVEPROXY_DOCKER_ARGS in the environment in order to supply + # additional parameters, such as resource limits, to docker + # when launching the weave proxy container. + PROXY_CONTAINER=$(docker run --privileged --net=host -d \ + --name=$PROXY_CONTAINER_NAME \ + -p $PROXY_PORT:$PROXY_PORT/tcp -v /var/run/docker.sock:/var/run/docker.sock \ + -v /proc:/hostproc -e PROCFS=/hostproc \ + --entrypoint=/home/weave/proxy \ + $WEAVE_DOCKER_ARGS $WEAVEPROXY_DOCKER_ARGS \ + $EXEC_IMAGE "$@") + + wait_for_proxy_to_boot +} + ########################################################################################## [ $(id -u) = 0 ] || { @@ -807,6 +865,10 @@ case "$COMMAND" in populate_dns echo $DNS_CONTAINER ;; + launch-proxy) + ensure_proxy_running "$@" + echo $PROXY_CONTAINER + ;; connect) [ $# -eq 1 ] || usage http_call $CONTAINER_NAME $HTTP_PORT POST /connect -d "peer=$1"