Merge pull request #1967 from weaveworks/issues/1859-start-existing
Start existing container, if its image and arguments match
This commit is contained in:
commit
e65a08471f
2 files changed
+90
-45
No files matched your search
Executable
+30
@@ -0,0 +1,30 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
. ./config.sh
|
||||||
|
|
||||||
|
start_suite "Check restart re-uses same container when possible"
|
||||||
|
|
||||||
|
ID=$(weave_on $HOST1 launch-router)
|
||||||
|
weave_on $HOST1 stop-router
|
||||||
|
assert "weave_on $HOST1 launch-router" "$ID"
|
||||||
|
# Can't start if already running with different args
|
||||||
|
assert_raises "weave_on $HOST1 launch-router --log-level=debug" 1
|
||||||
|
# Stop then start with different arg
|
||||||
|
weave_on $HOST1 stop-router
|
||||||
|
ID2=$(weave_on $HOST1 launch-router --log-level=debug)
|
||||||
|
assert "[ $ID != $ID2 ]"
|
||||||
|
|
||||||
|
# Stop then start with different password
|
||||||
|
weave_on $HOST1 stop-router
|
||||||
|
ID3=$(weave_on $HOST1 launch-router --password=xyzzy)
|
||||||
|
assert "[ $ID2 != $ID3 ]"
|
||||||
|
# Stop then start with same password
|
||||||
|
weave_on $HOST1 stop-router
|
||||||
|
assert "weave_on $HOST1 launch-router --password=xyzzy" "$ID3"
|
||||||
|
|
||||||
|
# Fails on bad argument
|
||||||
|
assert_raises "weave_on $HOST1 launch-plugin --foo" 1
|
||||||
|
# Fails again on bad argument, although there is a dead container
|
||||||
|
assert_raises "weave_on $HOST1 launch-plugin --foo" 1
|
||||||
|
|
||||||
|
end_suite
|
||||||
@@ -936,28 +936,58 @@ detach() {
|
|||||||
# functions for interacting with containers
|
# functions for interacting with containers
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
# Check that a container for component $1 named $2 with image $3 is not running
|
args_match() {
|
||||||
check_not_running() {
|
prev_args="$(docker inspect -f '{{.Args}} {{.Config.Env}}' $1)"
|
||||||
RUN_STATUS=$(docker inspect --format='{{.State.Running}} {{.State.Status}} {{.Config.Image}}' $2 2>/dev/null) || true
|
shift
|
||||||
|
dummy_container=$(docker create "$@" 2>/dev/null) || return 1
|
||||||
|
dummy_args="$(docker inspect -f '{{.Args}} {{.Config.Env}}' $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
|
||||||
|
image_base=${image%:*}
|
||||||
|
IMAGE_ID=$(docker inspect --format='{{.Id}}' $image)
|
||||||
|
RUN_STATUS=$(docker inspect --format='{{.State.Running}} {{.Image}} {{.Config.Image}} {{.Id}}' $name 2>/dev/null) || true
|
||||||
|
CONTAINER_ID=${RUN_STATUS##* }
|
||||||
case ${RUN_STATUS%:*} in
|
case ${RUN_STATUS%:*} in
|
||||||
"true restarting $3")
|
"true $IMAGE_ID "*)
|
||||||
echo "$2 is restarting; you can stop it with 'weave stop-$1'." >&2
|
if args_match $CONTAINER_ID "$@" ; then
|
||||||
return 3
|
ALREADY_RUNNING_CONTAINER=$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
|
||||||
;;
|
;;
|
||||||
"true "*" $3")
|
"false $IMAGE_ID "*)
|
||||||
echo "$2 is already running; you can stop it with 'weave stop-$1'." >&2
|
if args_match $CONTAINER_ID "$@" ; then
|
||||||
return 1
|
docker start $CONTAINER_ID
|
||||||
|
else
|
||||||
|
docker rm $name >/dev/null
|
||||||
|
docker run -d --name=$name "$@"
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
"false "*" $3")
|
true*$image_base)
|
||||||
docker rm $2 >/dev/null
|
echo "Found another version of $name running. Aborting." >&2
|
||||||
|
return 2
|
||||||
;;
|
;;
|
||||||
true*)
|
true*)
|
||||||
echo "Found another running container named '$2'. Aborting." >&2
|
echo "Found another running container named '$name'. Aborting." >&2
|
||||||
return 2
|
return 2
|
||||||
;;
|
;;
|
||||||
false*)
|
false*)
|
||||||
echo "Found another container named '$2'. Aborting." >&2
|
docker rm $name >/dev/null
|
||||||
return 2
|
docker run -d --name=$name "$@"
|
||||||
|
;;
|
||||||
|
"")
|
||||||
|
docker run -d --name=$name "$@"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
@@ -1177,6 +1207,7 @@ check_running() {
|
|||||||
echo "$1 container is not present. Have you launched it?" >&2
|
echo "$1 container is not present. Have you launched it?" >&2
|
||||||
return 1
|
return 1
|
||||||
elif [ "$STATUS" = "true true" ] ; then
|
elif [ "$STATUS" = "true true" ] ; then
|
||||||
|
[ -n "$ALREADY_RUNNING_CONTAINER" ] && return 0
|
||||||
echo "$1 container is restarting." >&2
|
echo "$1 container is restarting." >&2
|
||||||
return 2
|
return 2
|
||||||
elif [ "$STATUS" != "true false" ] ; then
|
elif [ "$STATUS" != "true false" ] ; then
|
||||||
@@ -1590,8 +1621,8 @@ launch_router() {
|
|||||||
# Set WEAVE_DOCKER_ARGS in the environment in order to supply
|
# Set WEAVE_DOCKER_ARGS in the environment in order to supply
|
||||||
# additional parameters, such as resource limits, to docker
|
# additional parameters, such as resource limits, to docker
|
||||||
# when launching the weave container.
|
# when launching the weave container.
|
||||||
ROUTER_CONTAINER=$(docker run --name=$CONTAINER_NAME \
|
start_if_necessary router $CONTAINER_NAME $IMAGE \
|
||||||
-d --privileged --net=host $(docker_sock_options) \
|
--privileged --net=host $(docker_sock_options) \
|
||||||
-e WEAVE_PASSWORD \
|
-e WEAVE_PASSWORD \
|
||||||
-e CHECKPOINT_DISABLE \
|
-e CHECKPOINT_DISABLE \
|
||||||
$WEAVE_DOCKER_ARGS $IMAGE $COVERAGE_ARGS \
|
$WEAVE_DOCKER_ARGS $IMAGE $COVERAGE_ARGS \
|
||||||
@@ -1601,9 +1632,10 @@ launch_router() {
|
|||||||
--dns-effective-listen-address $DOCKER_BRIDGE_IP \
|
--dns-effective-listen-address $DOCKER_BRIDGE_IP \
|
||||||
$DNS_ROUTER_OPTS $NO_DNS_OPT \
|
$DNS_ROUTER_OPTS $NO_DNS_OPT \
|
||||||
--http-addr $HTTP_ADDR \
|
--http-addr $HTTP_ADDR \
|
||||||
"$@")
|
"$@"
|
||||||
setup_router_iface_$BRIDGE_TYPE
|
setup_router_iface_$BRIDGE_TYPE
|
||||||
attach_router
|
wait_for_status $CONTAINER_NAME http_call $HTTP_ADDR
|
||||||
|
[ -n "$ALREADY_RUNNING_CONTAINER" ] || attach_router
|
||||||
}
|
}
|
||||||
|
|
||||||
# Recreate the parameter values that are set when the router is first launched
|
# Recreate the parameter values that are set when the router is first launched
|
||||||
@@ -1614,7 +1646,6 @@ fetch_router_args() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
attach_router() {
|
attach_router() {
|
||||||
wait_for_status $CONTAINER_NAME http_call $HTTP_ADDR
|
|
||||||
if [ -n "$IPRANGE" ] ; then
|
if [ -n "$IPRANGE" ] ; then
|
||||||
# Tell the newly-started weave IP allocator about existing weave IPs
|
# Tell the newly-started weave IP allocator about existing weave IPs
|
||||||
with_container_addresses ipam_reclaim weave:expose $(docker ps -q --no-trunc)
|
with_container_addresses ipam_reclaim weave:expose $(docker ps -q --no-trunc)
|
||||||
@@ -1646,8 +1677,8 @@ launch_proxy() {
|
|||||||
docker create -v /w -v /w-noop -v /w-nomcast --name=$VOLUMES_CONTAINER_NAME \
|
docker create -v /w -v /w-noop -v /w-nomcast --name=$VOLUMES_CONTAINER_NAME \
|
||||||
--label=weavevolumes --entrypoint=/bin/false $EXEC_IMAGE >/dev/null
|
--label=weavevolumes --entrypoint=/bin/false $EXEC_IMAGE >/dev/null
|
||||||
fi
|
fi
|
||||||
PROXY_CONTAINER=$(docker run --name=$PROXY_CONTAINER_NAME \
|
start_if_necessary proxy $PROXY_CONTAINER_NAME $EXEC_IMAGE \
|
||||||
-d --privileged --net=host $(docker_sock_options) \
|
--privileged --net=host $(docker_sock_options) \
|
||||||
$PROXY_VOLUMES \
|
$PROXY_VOLUMES \
|
||||||
--volumes-from $VOLUMES_CONTAINER_NAME \
|
--volumes-from $VOLUMES_CONTAINER_NAME \
|
||||||
-v /var/run/weave:/var/run/weave \
|
-v /var/run/weave:/var/run/weave \
|
||||||
@@ -1659,7 +1690,7 @@ launch_proxy() {
|
|||||||
-e WEAVE_HTTP_ADDR \
|
-e WEAVE_HTTP_ADDR \
|
||||||
-e EXEC_IMAGE=$EXEC_IMAGE \
|
-e EXEC_IMAGE=$EXEC_IMAGE \
|
||||||
--entrypoint=/home/weave/weaveproxy \
|
--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
|
wait_for_status $PROXY_CONTAINER_NAME http_call_unix $PROXY_CONTAINER_NAME status.sock
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1669,21 +1700,12 @@ stop_proxy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
launch_plugin_if_not_running() {
|
launch_plugin_if_not_running() {
|
||||||
retval=0
|
start_if_necessary plugin $PLUGIN_CONTAINER_NAME $PLUGIN_IMAGE \
|
||||||
check_not_running plugin $PLUGIN_CONTAINER_NAME $BASE_PLUGIN_IMAGE || retval=$?
|
--privileged --net=host $(docker_sock_options) \
|
||||||
# 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) \
|
|
||||||
--restart=always \
|
--restart=always \
|
||||||
-v /run/docker/plugins:/run/docker/plugins \
|
-v /run/docker/plugins:/run/docker/plugins \
|
||||||
-e WEAVE_HTTP_ADDR \
|
-e WEAVE_HTTP_ADDR \
|
||||||
$PLUGIN_IMAGE "$@") ; then
|
$PLUGIN_IMAGE "$@"
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
wait_for_status $PLUGIN_CONTAINER_NAME http_call_unix $PLUGIN_CONTAINER_NAME status.sock
|
wait_for_status $PLUGIN_CONTAINER_NAME http_call_unix $PLUGIN_CONTAINER_NAME status.sock
|
||||||
util_op create-plugin-network weave weavemesh
|
util_op create-plugin-network weave weavemesh
|
||||||
}
|
}
|
||||||
@@ -1848,19 +1870,14 @@ EOF
|
|||||||
;;
|
;;
|
||||||
launch)
|
launch)
|
||||||
deprecation_warnings "$@"
|
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 "$@")
|
COMMON_ARGS=$(common_launch_args "$@")
|
||||||
launch_router "$@"
|
launch_router "$@" >/dev/null
|
||||||
launch_proxy $COMMON_ARGS
|
launch_proxy $COMMON_ARGS >/dev/null
|
||||||
plugin_disabled || launch_plugin_if_not_running $COMMON_ARGS
|
plugin_disabled || launch_plugin_if_not_running $COMMON_ARGS >/dev/null
|
||||||
;;
|
;;
|
||||||
launch-router)
|
launch-router)
|
||||||
deprecation_warnings "$@"
|
deprecation_warnings "$@"
|
||||||
check_not_running router $CONTAINER_NAME $BASE_IMAGE
|
|
||||||
launch_router "$@"
|
launch_router "$@"
|
||||||
echo $ROUTER_CONTAINER
|
|
||||||
;;
|
;;
|
||||||
attach-router)
|
attach-router)
|
||||||
check_running $CONTAINER_NAME
|
check_running $CONTAINER_NAME
|
||||||
@@ -1868,17 +1885,15 @@ EOF
|
|||||||
create_bridge
|
create_bridge
|
||||||
fetch_router_args
|
fetch_router_args
|
||||||
with_container_netns $CONTAINER_NAME setup_router_iface_$BRIDGE_TYPE
|
with_container_netns $CONTAINER_NAME setup_router_iface_$BRIDGE_TYPE
|
||||||
|
wait_for_status $CONTAINER_NAME http_call $HTTP_ADDR
|
||||||
attach_router
|
attach_router
|
||||||
;;
|
;;
|
||||||
launch-proxy)
|
launch-proxy)
|
||||||
deprecation_warnings "$@"
|
deprecation_warnings "$@"
|
||||||
check_not_running proxy $PROXY_CONTAINER_NAME $BASE_EXEC_IMAGE
|
|
||||||
launch_proxy "$@"
|
launch_proxy "$@"
|
||||||
echo $PROXY_CONTAINER
|
|
||||||
;;
|
;;
|
||||||
launch-plugin)
|
launch-plugin)
|
||||||
launch_plugin_if_not_running "$@"
|
launch_plugin_if_not_running "$@"
|
||||||
echo $PLUGIN_CONTAINER
|
|
||||||
;;
|
;;
|
||||||
env|proxy-env)
|
env|proxy-env)
|
||||||
[ "$COMMAND" = "env" ] || deprecation_warning "$COMMAND" "'weave env'"
|
[ "$COMMAND" = "env" ] || deprecation_warning "$COMMAND" "'weave env'"
|
||||||
|
|||||||
Reference in new issue
Block a user