initial pass at container-level scaling

This commit is contained in:
Michael Hobbs committed 2015-04-21 14:27:23 -07:00
1 parent 4d7671dee3
commit 33f872183e
8 files changed
+180 -61

No files matched your search

+66 -40
View File
@@ -66,53 +66,78 @@ case "$1" in
APP="$2"; IMAGE="dokku/$APP" APP="$2"; IMAGE="dokku/$APP"
pluginhook pre-deploy $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 is_image_buildstep_based "$IMAGE" && DOKKU_BUILDSTEP=true
[[ -n "$DOKKU_BUILDSTEP" ]] && START_CMD="/start web" DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
[[ -z "$DOKKU_BUILDSTEP" ]] && DOKKU_DOCKERFILE_PORT=$(dokku config:get $APP DOKKU_DOCKERFILE_PORT || true)
if [[ "$BIND_EXTERNAL" = "false" ]];then while read line || [ -n "$line" ]
port=${DOKKU_DOCKERFILE_PORT:=5000} do
id=$(docker run -d -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD) PROC_NAME=${line%%=*}
ipaddr=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $id) PROC_COUNT=${line#*=}
else CONTAINER_NUM=1
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 while [[ $CONTAINER_NUM -le $PROC_COUNT ]];do
kill_new() { DOKKU_CONTAINER_ID_FILE="$DOKKU_ROOT/$APP/CONTAINER.$PROC_NAME.$CONTAINER_NUM"
docker inspect $id &> /dev/null && docker kill $id > /dev/null DOKKU_IP_FILE="$DOKKU_ROOT/$APP/IP.$PROC_NAME.$CONTAINER_NUM"
trap - INT TERM EXIT DOKKU_PORT_FILE="$DOKKU_ROOT/$APP/PORT.$PROC_NAME.$CONTAINER_NUM"
kill -9 $$
}
# run checks first, then post-deploy hooks, which switches Nginx traffic # try old form even in case we're migrating dokku versions
trap kill_new INT TERM EXIT if [[ -f "$DOKKU_CONTAINER_ID_FILE" ]]; then
dokku_log_info1 "Running pre-flight checks" oldids+=" "
pluginhook check-deploy $APP $port $ipaddr $id oldids+=$(< "$DOKKU_CONTAINER_ID_FILE")
trap - INT TERM EXIT oldids+=" "
elif [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then
oldids+=$(< "$DOKKU_ROOT/$APP/CONTAINER")
fi
# now using the new container # start the app
echo $id > "$DOKKU_ROOT/$APP/CONTAINER" DOCKER_ARGS=$(: | pluginhook docker-args $APP deploy)
echo $ipaddr > "$DOKKU_ROOT/$APP/IP" DOCKER_ARGS+=$(: | pluginhook docker-args-deploy $APP)
echo $port > "$DOKKU_ROOT/$APP/PORT" BIND_EXTERNAL=$(pluginhook bind-external-ip $APP)
echo "http://$(< "$DOKKU_ROOT/HOSTNAME"):$port" > "$DOKKU_ROOT/$APP/URL"
[[ -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" dokku_log_info1 "Running post-deploy"
pluginhook post-deploy $APP $port $ipaddr pluginhook post-deploy $APP $port $ipaddr
# kill the old container # kill the old container
if [[ -n "$oldid" ]]; then if [[ -n "$oldids" ]]; then
# Let the old container finish processing requests, before terminating it # Let the old container finish processing requests, before terminating it
WAIT="${DOKKU_WAIT_TO_RETIRE:-60}" WAIT="${DOKKU_WAIT_TO_RETIRE:-60}"
dokku_log_info1 "Shutting down old container in $WAIT seconds" 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 exec >/dev/null 2>/dev/null </dev/null
trap '' INT HUP trap '' INT HUP
sleep $WAIT sleep $WAIT
docker kill $oldid for oldid in $oldids; do
docker kill $oldid
done
) & disown -a ) & disown -a
# Use trap since disown/nohup don't seem to keep child alive # Use trap since disown/nohup don't seem to keep child alive
# Give child process just enough time to set the traps # Give child process just enough time to set the traps
sleep 0.1 sleep 0.1
fi fi
;; ;;
cleanup) cleanup)
+13 -13
View File
@@ -24,7 +24,7 @@
# The default behavior is to wait for 5 seconds before running the first check, # The default behavior is to wait for 5 seconds before running the first check,
# and timeout each check to 30 minutes. # and timeout each check to 30 minutes.
# #
# By default, checks will be retried 5 times. # By default, checks will be retried 5 times.
# You can change these by setting WAIT, TIMEOUT and ATTEMPTS to different values, for # You can change these by setting WAIT, TIMEOUT and ATTEMPTS to different values, for
# example: # example:
@@ -86,7 +86,7 @@ if [[ ! -s "${TMPDIR}/CHECKS" ]] ; then
# simple default check to see if the container stuck around # simple default check to see if the container stuck around
# for more thorough checks, create a CHECKS file # for more thorough checks, create a CHECKS file
DOKKU_DEFAULT_WAIT=$((WAIT+TIMEOUT)) DOKKU_DEFAULT_WAIT=10
dokku_log_info1 "Waiting for $DOKKU_DEFAULT_WAIT seconds ..." dokku_log_info1 "Waiting for $DOKKU_DEFAULT_WAIT seconds ..."
sleep $DOKKU_DEFAULT_WAIT sleep $DOKKU_DEFAULT_WAIT
@@ -116,15 +116,15 @@ do
ATTEMPT=$(( ATTEMPT + 1 )) ATTEMPT=$(( ATTEMPT + 1 ))
dokku_log_info1 "Attempt $ATTEMPT/$ATTEMPTS Waiting for $WAIT seconds ..." dokku_log_info1 "Attempt $ATTEMPT/$ATTEMPTS Waiting for $WAIT seconds ..."
sleep $WAIT sleep $WAIT
# -q do not use .curlrc (must come first) # -q do not use .curlrc (must come first)
# --compressed Test compression handled correctly # --compressed Test compression handled correctly
# --fail Fail on server errors (4xx, 5xx) # --fail Fail on server errors (4xx, 5xx)
# --location Follow redirects # --location Follow redirects
CURL_OPTIONS="-q --compressed --fail --location --max-time $TIMEOUT" CURL_OPTIONS="-q --compressed --fail --location --max-time $TIMEOUT"
exec < "$FILENAME" exec < "$FILENAME"
while read CHECK_URL EXPECTED ; do while read CHECK_URL EXPECTED ; do
# Ignore empty lines and lines starting with # # Ignore empty lines and lines starting with #
@@ -132,14 +132,14 @@ do
[[ -z "$CHECK_URL" || "$CHECK_URL" =~ ^\# ]] && continue [[ -z "$CHECK_URL" || "$CHECK_URL" =~ ^\# ]] && continue
# Ignore variables # Ignore variables
[[ "$CHECK_URL" =~ ^.+= ]] && continue [[ "$CHECK_URL" =~ ^.+= ]] && continue
if [[ "$CHECK_URL" =~ ^https?: ]] ; then if [[ "$CHECK_URL" =~ ^https?: ]] ; then
URL_PROTOCOL=${CHECK_URL%:*} URL_PROTOCOL=${CHECK_URL%:*}
CHECK_URL=${CHECK_URL#*:} CHECK_URL=${CHECK_URL#*:}
else else
URL_PROTOCOL="http" URL_PROTOCOL="http"
fi fi
if [[ "$CHECK_URL" =~ ^//.+ ]] ; then if [[ "$CHECK_URL" =~ ^//.+ ]] ; then
# To test a URL with specific host name, we still make request to localhost, # To test a URL with specific host name, we still make request to localhost,
# but we set Host header to $SEND_HOST. # but we set Host header to $SEND_HOST.
@@ -149,22 +149,22 @@ do
UNPREFIXED=${CHECK_URL#//} UNPREFIXED=${CHECK_URL#//}
URL_HOSTNAME=${UNPREFIXED%%/*} URL_HOSTNAME=${UNPREFIXED%%/*}
URL_PATHNAME=${UNPREFIXED#$URL_HOSTNAME} URL_PATHNAME=${UNPREFIXED#$URL_HOSTNAME}
HEADERS="-H Host:$URL_HOSTNAME" HEADERS="-H Host:$URL_HOSTNAME"
else else
URL_HOSTNAME=localhost URL_HOSTNAME=localhost
URL_PATHNAME=$CHECK_URL URL_PATHNAME=$CHECK_URL
fi fi
# This URL will show up in the messages # This URL will show up in the messages
LOG_URL="$URL_PROTOCOL://$URL_HOSTNAME$URL_PATHNAME" LOG_URL="$URL_PROTOCOL://$URL_HOSTNAME$URL_PATHNAME"
# And how we formulate the CURL request # And how we formulate the CURL request
CURL_ARGS="$CURL_OPTIONS $URL_PROTOCOL://$DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT$URL_PATHNAME $HEADERS" CURL_ARGS="$CURL_OPTIONS $URL_PROTOCOL://$DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT$URL_PATHNAME $HEADERS"
dokku_log_verbose "CHECKS expected result:" dokku_log_verbose "CHECKS expected result:"
dokku_log_verbose "$LOG_URL => \"$EXPECTED\"" dokku_log_verbose "$LOG_URL => \"$EXPECTED\""
[[ $DOKKU_TRACE ]] && dokku_log_verbose "$ curl $CURL_ARGS" [[ $DOKKU_TRACE ]] && dokku_log_verbose "$ curl $CURL_ARGS"
# Capture HTTP response or CURL error message # Capture HTTP response or CURL error message
if OUTPUT=$(curl -# $CURL_ARGS 2>&1) ; then if OUTPUT=$(curl -# $CURL_ARGS 2>&1) ; then
# OUTPUT contains the HTTP response # OUTPUT contains the HTTP response
@@ -178,7 +178,7 @@ do
FAILEDCHECKS=$(( FAILEDCHECKS + 1 )) FAILEDCHECKS=$(( FAILEDCHECKS + 1 ))
fi fi
done done
if [ $FAILEDCHECKS -gt 0 ]; then if [ $FAILEDCHECKS -gt 0 ]; then
dokku_log_warn "Check attempt $ATTEMPT/$ATTEMPTS failed." dokku_log_warn "Check attempt $ATTEMPT/$ATTEMPTS failed."
SUCCESS=0 SUCCESS=0
+8
View File
@@ -135,3 +135,11 @@ parse_args() {
done done
return 0 return 0
} }
copy_from_image() {
local APP="$1"; local IMAGE="dokku/$APP"; local SRC_FILE="$2"; local DST_DIR="$3"; verify_app_name $APP
CID=$(docker run -d $IMAGE bash)
docker cp "$CID:$SRC_FILE" "$DST_DIR"
docker rm -f "$CID"
}
+18 -5
View File
@@ -34,11 +34,18 @@ case "$1" in
WILDCARD_SSL="$DOKKU_ROOT/tls" WILDCARD_SSL="$DOKKU_ROOT/tls"
SSL="$DOKKU_ROOT/$APP/tls" SSL="$DOKKU_ROOT/$APP/tls"
if [[ -z "$DOKKU_APP_LISTEN_PORT" ]] && [[ -f "$DOKKU_ROOT/$APP/PORT" ]]; then if [[ -z "$DOKKU_APP_LISTEN_PORT" ]] && [[ -z "$DOKKU_APP_LISTEN_IP" ]]; then
DOKKU_APP_LISTEN_PORT=$(< "$DOKKU_ROOT/$APP/PORT") shopt -s nullglob
fi for DOKKU_APP_IP_FILE in $DOKKU_ROOT/$APP/IP.*;do
if [[ -z "$DOKKU_APP_LISTEN_IP" ]] && [[ -f "$DOKKU_ROOT/$APP/IP" ]]; then DOKKU_APP_PORT_FILE=$(echo $DOKKU_APP_IP_FILE | sed -e "s:IP:PORT:g")
DOKKU_APP_LISTEN_IP=$(< "$DOKKU_ROOT/$APP/IP") DOKKU_APP_LISTENER_IP=$(< $DOKKU_APP_IP_FILE)
DOKKU_APP_LISTENER_PORT=$(< $DOKKU_APP_PORT_FILE)
DOKKU_APP_LISTENERS+=" "
DOKKU_APP_LISTENERS+="$DOKKU_APP_LISTENER_IP:$DOKKU_APP_LISTENER_PORT"
DOKKU_APP_LISTENERS+=" "
done
shopt -u nullglob
fi fi
[[ -f "$DOKKU_ROOT/ENV" ]] && source $DOKKU_ROOT/ENV [[ -f "$DOKKU_ROOT/ENV" ]] && source $DOKKU_ROOT/ENV
@@ -100,6 +107,12 @@ EOF
if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then if [[ -n "$DOKKU_APP_LISTEN_PORT" ]] && [[ -n "$DOKKU_APP_LISTEN_IP" ]]; then
echo "upstream $APP { server $DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT; }" >> $NGINX_CONF echo "upstream $APP { server $DOKKU_APP_LISTEN_IP:$DOKKU_APP_LISTEN_PORT; }" >> $NGINX_CONF
elif [[ -n "$DOKKU_APP_LISTENERS" ]]; then
echo "upstream $APP { " >> $NGINX_CONF
for listener in $DOKKU_APP_LISTENERS;do
echo " server $listener;" >> $NGINX_CONF
done
echo "}" >> $NGINX_CONF
fi fi
dokku_log_info1 "Creating $SCHEME nginx.conf" dokku_log_info1 "Creating $SCHEME nginx.conf"
+2 -2
View File
@@ -2,7 +2,7 @@
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(dirname $0)/../common/functions" source "$(dirname $0)/../common/functions"
APP="$1"; PORT="$2"; IP="$3" APP="$1"
NO_VHOST=$(dokku config:get $APP NO_VHOST || true) NO_VHOST=$(dokku config:get $APP NO_VHOST || true)
if [[ -n "$NO_VHOST" ]]; then if [[ -n "$NO_VHOST" ]]; then
@@ -11,4 +11,4 @@ elif [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
dokku domains:setup $APP dokku domains:setup $APP
fi fi
dokku nginx:build-config $APP $PORT $IP dokku nginx:build-config $APP
+20 -1
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(dirname $0)/../common/functions" source "$(dirname $0)/../common/functions"
source "$(dirname $0)/functions"
release_and_deploy() { release_and_deploy() {
source "$(dirname $0)/../common/functions" source "$(dirname $0)/../common/functions"
local APP="$1"; local IMAGE="dokku/$APP" local APP="$1"; local IMAGE="dokku/$APP"
if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then if ls $DOKKU_ROOT/$APP/CONTAINER.* &> /dev/null; then
if is_image_buildstep_based "$IMAGE"; then if is_image_buildstep_based "$IMAGE"; then
IMAGE_SOURCE_TYPE="buildstep" IMAGE_SOURCE_TYPE="buildstep"
else else
@@ -97,9 +98,27 @@ case "$1" in
done done
;; ;;
ps:scale)
[[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1
verify_app_name "$2"
APP="$2"; DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
shift 2
generate_scale_file "$APP"
if [[ -z "$@" ]];then
dokku_log_info1 "Scaling for $APP"
dokku_log_info2 "$(< $DOKKU_SCALE_FILE)"
else
set_scale "$APP" "$@"
fi
release_and_deploy "$APP"
;;
help | ps:help) help | ps:help)
cat && cat<<EOF cat && cat<<EOF
ps <app> List processes running in app container(s) ps <app> List processes running in app container(s)
ps:scale <app> <proc>=<count> [<proc>=<count>] Set how many processes of a given process to run
ps:start <app> Start app container(s) ps:start <app> Start app container(s)
ps:stop <app> Stop app container(s) ps:stop <app> Stop app container(s)
ps:rebuild <app> Rebuild an app ps:rebuild <app> Rebuild an app
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(dirname $0)/../common/functions"
generate_scale_file() {
local APP="$1"; local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
if [[ ! -f $DOKKU_SCALE_FILE ]]; then
copy_from_image "$APP" "/app/DOKKU_SCALE" "$DOKKU_ROOT/$APP" || dokku_log_info1_quiet "DOKKU_SCALE not found in app image"
if [[ ! -f $DOKKU_SCALE_FILE ]]; then
local TMP_WORK_DIR=$(mktemp -d DOKKU_SCALE.XXXXX)
trap 'rm -rf $TMP_WORK_DIR' EXIT
copy_from_image "$APP" /app/Procfile $TMP_WORK_DIR || true
if [[ -f $TMP_WORK_DIR/Procfile ]];then
local PROCFILE="$TMP_WORK_DIR/Procfile"
while read line || [ -n "$line" ]
do
local PROC_NAME=${line%%:*}
echo "$PROC_NAME=1" >> $DOKKU_SCALE_FILE
done < "$PROCFILE"
else
echo "default=1" >> $DOKKU_SCALE_FILE
fi
dokku_log_info1_quiet "New DOKKU_SCALE file generated"
dokku_log_info1_quiet "$(< $DOKKU_SCALE_FILE)"
fi
fi
}
set_scale() {
local APP="$1"; local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
shift 1
local SCALE_SETTINGS=("$@")
for procscale in "${SCALE_SETTINGS[@]}"; do
PROC_NAME=${procscale%%=*}
PROC_COUNT=${procscale#*=}
dokku_log_info1_quiet "Scaling $APP:$PROC_NAME to $PROC_COUNT"
if (egrep -q ^${PROC_NAME}= $DOKKU_SCALE_FILE > /dev/null 2>&1);then
sed --in-place "s:^${PROC_NAME}=.*:$PROC_NAME=$PROC_COUNT:g" $DOKKU_SCALE_FILE
else
echo "$PROC_NAME=$PROC_COUNT" >> $DOKKU_SCALE_FILE
fi
done
}
+8
View File
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$(dirname $0)/../common/functions"
source "$(dirname $0)/functions"
APP="$1"
generate_scale_file $APP