From bb4c0f5247bed173c8ff5eae45f78ebadf1cee79 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 9 Feb 2016 09:41:42 +0000 Subject: [PATCH] Start existing container, if its image and arguments match --- weave | 96 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/weave b/weave index b16131db..8d83aaf3 100755 --- a/weave +++ b/weave @@ -936,28 +936,56 @@ detach() { # functions for interacting with containers ###################################################################### -# Check that a container for component $1 named $2 with image $3 is not running -check_not_running() { - RUN_STATUS=$(docker inspect --format='{{.State.Running}} {{.State.Status}} {{.Config.Image}}' $2 2>/dev/null) || true +args_match() { + prev_args="$(docker inspect -f '{{.Args}}' $1)" + shift + dummy_container=$(docker create "$@" 2>/dev/null) || return 1 + dummy_args="$(docker inspect -f '{{.Args}}' $dummy_container)" + docker rm $dummy_container >/dev/null + [ "$prev_args" = "$dummy_args" ] +} + +# Check if a container for component $1 named $2 with image $3 and args $4... is running, +# and start it if necessary +start_if_necessary() { + component=$1 + name=$2 + image=$3 + shift 3 + RUN_STATUS=$(docker inspect --format='{{.State.Running}} {{.State.Status}} {{.Config.Image}} {{.Id}}' $name 2>/dev/null) || true + CONTAINER_ID=${RUN_STATUS##* } case ${RUN_STATUS%:*} in - "true restarting $3") - echo "$2 is restarting; you can stop it with 'weave stop-$1'." >&2 + "true restarting $image") + echo "$name is restarting; you can stop it with 'weave stop-$component'." >&2 return 3 ;; - "true "*" $3") - echo "$2 is already running; you can stop it with 'weave stop-$1'." >&2 - return 1 + "true "*" $image") + if args_match $CONTAINER_ID "$@" ; then + ALREADY_RUNNING_ID=$CONTAINER_ID + echo $CONTAINER_ID + else + echo "$name is already running with different arguments; you can stop it with 'weave stop-$component'." >&2 + return 1 + fi ;; - "false "*" $3") - docker rm $2 >/dev/null + "false "*" $image") + if args_match $CONTAINER_ID "$@" ; then + docker start $CONTAINER_ID + else + docker rm $name >/dev/null + docker run -d --name=$name "$@" + fi ;; true*) - echo "Found another running container named '$2'. Aborting." >&2 + echo "Found another running container named '$name'. Aborting." >&2 return 2 ;; false*) - echo "Found another container named '$2'. Aborting." >&2 - return 2 + docker rm $name >/dev/null + docker run -d --name=$name "$@" + ;; + "") + docker run -d --name=$name "$@" ;; esac } @@ -1590,8 +1618,8 @@ launch_router() { # Set WEAVE_DOCKER_ARGS in the environment in order to supply # additional parameters, such as resource limits, to docker # when launching the weave container. - ROUTER_CONTAINER=$(docker run --name=$CONTAINER_NAME \ - -d --privileged --net=host $(docker_sock_options) \ + start_if_necessary router $CONTAINER_NAME $BASE_IMAGE \ + --privileged --net=host $(docker_sock_options) \ -e WEAVE_PASSWORD \ -e CHECKPOINT_DISABLE \ $WEAVE_DOCKER_ARGS $IMAGE $COVERAGE_ARGS \ @@ -1601,7 +1629,8 @@ launch_router() { --dns-effective-listen-address $DOCKER_BRIDGE_IP \ $DNS_ROUTER_OPTS $NO_DNS_OPT \ --http-addr $HTTP_ADDR \ - "$@") + "$@" + [ -n "$ALREADY_RUNNING_ID" ] && return 0 setup_router_iface_$BRIDGE_TYPE attach_router } @@ -1646,8 +1675,8 @@ launch_proxy() { docker create -v /w -v /w-noop -v /w-nomcast --name=$VOLUMES_CONTAINER_NAME \ --label=weavevolumes --entrypoint=/bin/false $EXEC_IMAGE >/dev/null fi - PROXY_CONTAINER=$(docker run --name=$PROXY_CONTAINER_NAME \ - -d --privileged --net=host $(docker_sock_options) \ + start_if_necessary proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE \ + --privileged --net=host $(docker_sock_options) \ $PROXY_VOLUMES \ --volumes-from $VOLUMES_CONTAINER_NAME \ -v /var/run/weave:/var/run/weave \ @@ -1659,7 +1688,7 @@ launch_proxy() { -e WEAVE_HTTP_ADDR \ -e EXEC_IMAGE=$EXEC_IMAGE \ --entrypoint=/home/weave/weaveproxy \ - $WEAVEPROXY_DOCKER_ARGS $EXEC_IMAGE $COVERAGE_ARGS $PROXY_ARGS) + $WEAVEPROXY_DOCKER_ARGS $EXEC_IMAGE $COVERAGE_ARGS $PROXY_ARGS wait_for_status $PROXY_CONTAINER_NAME http_call_unix $PROXY_CONTAINER_NAME status.sock } @@ -1669,21 +1698,12 @@ stop_proxy() { } launch_plugin_if_not_running() { - retval=0 - check_not_running plugin $PLUGIN_CONTAINER_NAME $BASE_PLUGIN_IMAGE || retval=$? - # If an existing plugin is running (we start it with restart=always), return its ID - [ $retval = 1 ] && PLUGIN_CONTAINER=$(container_id $PLUGIN_CONTAINER_NAME) && return 0 - # Any other kind of error code from check_not_running is a failure. - [ $retval -gt 0 ] && return $retval - - if ! PLUGIN_CONTAINER=$(docker run --name=$PLUGIN_CONTAINER_NAME \ - -d --privileged --net=host $(docker_sock_options) \ + start_if_necessary plugin $PLUGIN_CONTAINER_NAME $BASE_PLUGIN_IMAGE \ + --privileged --net=host $(docker_sock_options) \ --restart=always \ -v /run/docker/plugins:/run/docker/plugins \ -e WEAVE_HTTP_ADDR \ - $PLUGIN_IMAGE "$@") ; then - return 1 - fi + $PLUGIN_IMAGE "$@" wait_for_status $PLUGIN_CONTAINER_NAME http_call_unix $PLUGIN_CONTAINER_NAME status.sock util_op create-plugin-network weave weavemesh } @@ -1848,19 +1868,14 @@ EOF ;; launch) deprecation_warnings "$@" - check_not_running router $CONTAINER_NAME $BASE_IMAGE - check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE - # Don't check the plugin here, since we start it with `--restart=always` COMMON_ARGS=$(common_launch_args "$@") - launch_router "$@" - launch_proxy $COMMON_ARGS - plugin_disabled || launch_plugin_if_not_running $COMMON_ARGS + launch_router "$@" >/dev/null + launch_proxy $COMMON_ARGS >/dev/null + plugin_disabled || launch_plugin_if_not_running $COMMON_ARGS >/dev/null ;; launch-router) deprecation_warnings "$@" - check_not_running router $CONTAINER_NAME $BASE_IMAGE launch_router "$@" - echo $ROUTER_CONTAINER ;; attach-router) check_running $CONTAINER_NAME @@ -1872,13 +1887,10 @@ EOF ;; launch-proxy) deprecation_warnings "$@" - check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE launch_proxy "$@" - echo $PROXY_CONTAINER ;; launch-plugin) launch_plugin_if_not_running "$@" - echo $PLUGIN_CONTAINER ;; env|proxy-env) [ "$COMMAND" = "env" ] || deprecation_warning "$COMMAND" "'weave env'"