initial pass at container-level scaling
This commit is contained in:
1 parent
4d7671dee3
commit
33f872183e
8 files changed
+180
-61
No files matched your search
@@ -66,53 +66,78 @@ case "$1" in
|
||||
APP="$2"; IMAGE="dokku/$APP"
|
||||
pluginhook pre-deploy $APP
|
||||
|
||||
if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then
|
||||
oldid=$(< "$DOKKU_ROOT/$APP/CONTAINER")
|
||||
fi
|
||||
|
||||
# start the app
|
||||
DOCKER_ARGS=$(: | pluginhook docker-args $APP deploy)
|
||||
DOCKER_ARGS+=$(: | pluginhook docker-args-deploy $APP)
|
||||
BIND_EXTERNAL=$(pluginhook bind-external-ip $APP)
|
||||
|
||||
is_image_buildstep_based "$IMAGE" && DOKKU_BUILDSTEP=true
|
||||
[[ -n "$DOKKU_BUILDSTEP" ]] && START_CMD="/start web"
|
||||
[[ -z "$DOKKU_BUILDSTEP" ]] && DOKKU_DOCKERFILE_PORT=$(dokku config:get $APP DOKKU_DOCKERFILE_PORT || true)
|
||||
DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
|
||||
|
||||
if [[ "$BIND_EXTERNAL" = "false" ]];then
|
||||
port=${DOKKU_DOCKERFILE_PORT:=5000}
|
||||
id=$(docker run -d -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD)
|
||||
ipaddr=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $id)
|
||||
else
|
||||
id=$(docker run -d -p 5000 -e PORT=5000 $DOCKER_ARGS $IMAGE $START_CMD)
|
||||
port=$(docker port $id 5000 | sed 's/[0-9.]*://')
|
||||
ipaddr=127.0.0.1
|
||||
fi
|
||||
while read line || [ -n "$line" ]
|
||||
do
|
||||
PROC_NAME=${line%%=*}
|
||||
PROC_COUNT=${line#*=}
|
||||
CONTAINER_NUM=1
|
||||
|
||||
# if we can't post-deploy successfully, kill new container
|
||||
kill_new() {
|
||||
docker inspect $id &> /dev/null && docker kill $id > /dev/null
|
||||
trap - INT TERM EXIT
|
||||
kill -9 $$
|
||||
}
|
||||
while [[ $CONTAINER_NUM -le $PROC_COUNT ]];do
|
||||
DOKKU_CONTAINER_ID_FILE="$DOKKU_ROOT/$APP/CONTAINER.$PROC_NAME.$CONTAINER_NUM"
|
||||
DOKKU_IP_FILE="$DOKKU_ROOT/$APP/IP.$PROC_NAME.$CONTAINER_NUM"
|
||||
DOKKU_PORT_FILE="$DOKKU_ROOT/$APP/PORT.$PROC_NAME.$CONTAINER_NUM"
|
||||
|
||||
# run checks first, then post-deploy hooks, which switches Nginx traffic
|
||||
trap kill_new INT TERM EXIT
|
||||
dokku_log_info1 "Running pre-flight checks"
|
||||
pluginhook check-deploy $APP $port $ipaddr $id
|
||||
trap - INT TERM EXIT
|
||||
# try old form even in case we're migrating dokku versions
|
||||
if [[ -f "$DOKKU_CONTAINER_ID_FILE" ]]; then
|
||||
oldids+=" "
|
||||
oldids+=$(< "$DOKKU_CONTAINER_ID_FILE")
|
||||
oldids+=" "
|
||||
elif [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then
|
||||
oldids+=$(< "$DOKKU_ROOT/$APP/CONTAINER")
|
||||
fi
|
||||
|
||||
# now using the new container
|
||||
echo $id > "$DOKKU_ROOT/$APP/CONTAINER"
|
||||
echo $ipaddr > "$DOKKU_ROOT/$APP/IP"
|
||||
echo $port > "$DOKKU_ROOT/$APP/PORT"
|
||||
echo "http://$(< "$DOKKU_ROOT/HOSTNAME"):$port" > "$DOKKU_ROOT/$APP/URL"
|
||||
# start the app
|
||||
DOCKER_ARGS=$(: | pluginhook docker-args $APP deploy)
|
||||
DOCKER_ARGS+=$(: | pluginhook docker-args-deploy $APP)
|
||||
BIND_EXTERNAL=$(pluginhook bind-external-ip $APP)
|
||||
|
||||
[[ -n "$DOKKU_BUILDSTEP" ]] && START_CMD="/start $PROC_NAME"
|
||||
[[ -z "$DOKKU_BUILDSTEP" ]] && DOKKU_DOCKERFILE_PORT=$(dokku config:get $APP DOKKU_DOCKERFILE_PORT || true)
|
||||
|
||||
|
||||
if [[ "$BIND_EXTERNAL" = "false" ]];then
|
||||
port=${DOKKU_DOCKERFILE_PORT:=5000}
|
||||
id=$(docker run -d -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD)
|
||||
ipaddr=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $id)
|
||||
else
|
||||
id=$(docker run -d -p 5000 -e PORT=5000 $DOCKER_ARGS $IMAGE $START_CMD)
|
||||
port=$(docker port $id 5000 | sed 's/[0-9.]*://')
|
||||
ipaddr=127.0.0.1
|
||||
fi
|
||||
|
||||
# if we can't post-deploy successfully, kill new container
|
||||
kill_new() {
|
||||
docker inspect $id &> /dev/null && docker kill $id > /dev/null
|
||||
trap - INT TERM EXIT
|
||||
kill -9 $$
|
||||
}
|
||||
|
||||
# run checks first, then post-deploy hooks, which switches Nginx traffic
|
||||
trap kill_new INT TERM EXIT
|
||||
dokku_log_info1 "Running pre-flight checks"
|
||||
pluginhook check-deploy $APP $port $ipaddr $id
|
||||
trap - INT TERM EXIT
|
||||
|
||||
# now using the new container
|
||||
echo $id > "$DOKKU_CONTAINER_ID_FILE"
|
||||
echo $ipaddr > "$DOKKU_IP_FILE"
|
||||
echo $port > "$DOKKU_PORT_FILE"
|
||||
echo "http://$(< "$DOKKU_ROOT/HOSTNAME"):$port" > "$DOKKU_ROOT/$APP/URL"
|
||||
|
||||
# cleanup pre-migration files
|
||||
rm -f $DOKKU_ROOT/$APP/CONTAINER $DOKKU_ROOT/$APP/IP $DOKKU_ROOT/$APP/PORT
|
||||
CONTAINER_NUM=$(( CONTAINER_NUM + 1 ))
|
||||
done
|
||||
done < "$DOKKU_SCALE_FILE"
|
||||
|
||||
dokku_log_info1 "Running post-deploy"
|
||||
pluginhook post-deploy $APP $port $ipaddr
|
||||
pluginhook post-deploy $APP $port $ipaddr
|
||||
|
||||
# kill the old container
|
||||
if [[ -n "$oldid" ]]; then
|
||||
if [[ -n "$oldids" ]]; then
|
||||
# Let the old container finish processing requests, before terminating it
|
||||
WAIT="${DOKKU_WAIT_TO_RETIRE:-60}"
|
||||
dokku_log_info1 "Shutting down old container in $WAIT seconds"
|
||||
@@ -120,13 +145,14 @@ case "$1" in
|
||||
exec >/dev/null 2>/dev/null </dev/null
|
||||
trap '' INT HUP
|
||||
sleep $WAIT
|
||||
docker kill $oldid
|
||||
for oldid in $oldids; do
|
||||
docker kill $oldid
|
||||
done
|
||||
) & disown -a
|
||||
# Use trap since disown/nohup don't seem to keep child alive
|
||||
# Give child process just enough time to set the traps
|
||||
sleep 0.1
|
||||
fi
|
||||
|
||||
;;
|
||||
|
||||
cleanup)
|
||||
|
||||
Reference in new issue
Block a user