From 2cedc19b2a6bdf6c4e1ba7a6040f5e056a6805a4 Mon Sep 17 00:00:00 2001 From: Michael Hobbs Date: Thu, 9 Jun 2016 16:17:45 -0700 Subject: [PATCH] cleanup container state files when proc type is removed from app. closes #2230 --- .../00_dokku-standard/exec-app-json-scripts | 22 ------------ plugins/00_dokku-standard/post-deploy | 35 ++++++++++++++++++- plugins/00_dokku-standard/pre-deploy | 16 ++++++++- plugins/common/functions | 14 ++++++++ plugins/named-containers/post-deploy | 12 ++++--- 5 files changed, 71 insertions(+), 28 deletions(-) mode change 120000 => 100755 plugins/00_dokku-standard/post-deploy mode change 120000 => 100755 plugins/00_dokku-standard/pre-deploy diff --git a/plugins/00_dokku-standard/exec-app-json-scripts b/plugins/00_dokku-standard/exec-app-json-scripts index 6da8c9203..3f7700fa7 100755 --- a/plugins/00_dokku-standard/exec-app-json-scripts +++ b/plugins/00_dokku-standard/exec-app-json-scripts @@ -78,25 +78,3 @@ execute_script() { fi fi } - -exec_app_json_scripts() { - declare desc="core app.json scripts execution" - local trigger="$0 app_json_scripts" - local APP="$1" - - case "$0" in - *pre-deploy) - local IMAGE_TAG="$2" - local PHASE_SCRIPT_KEY="predeploy" - ;; - *post-deploy) - local IMAGE_TAG="$4" - local PHASE_SCRIPT_KEY="postdeploy" - ;; - esac - - dokku_log_info1 "Attempting to run scripts.dokku.$PHASE_SCRIPT_KEY from app.json (if defined)" - execute_script "$APP" "$IMAGE_TAG" "$PHASE_SCRIPT_KEY" -} - -exec_app_json_scripts "$@" diff --git a/plugins/00_dokku-standard/post-deploy b/plugins/00_dokku-standard/post-deploy deleted file mode 120000 index b24231790..000000000 --- a/plugins/00_dokku-standard/post-deploy +++ /dev/null @@ -1 +0,0 @@ -exec-app-json-scripts \ No newline at end of file diff --git a/plugins/00_dokku-standard/post-deploy b/plugins/00_dokku-standard/post-deploy new file mode 100755 index 000000000..e4d4ab9fb --- /dev/null +++ b/plugins/00_dokku-standard/post-deploy @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/00_dokku-standard/exec-app-json-scripts" + +exec_app_json_scripts() { + declare desc="core app.json scripts execution" + local trigger="pre-deploy app_json_scripts" + local APP="$1"; local IMAGE_TAG="$4"; local PHASE_SCRIPT_KEY="postdeploy" + + dokku_log_info1 "Attempting to run scripts.dokku.$PHASE_SCRIPT_KEY from app.json (if defined)" + execute_script "$APP" "$IMAGE_TAG" "$PHASE_SCRIPT_KEY" +} + +cleanup_container_state() { + declare desc="core post-deploy state cleanup" + local trigger="post-deploy cleanup_container_state" + local APP="$1"; local PROCTYPES="$(egrep -v "^#" "$DOKKU_ROOT/$APP/DOKKU_SCALE" | awk -F '=' '{ print $1 }' | xargs)" + local CONTAINER_FILES="$(find "$DOKKU_ROOT/$APP" -maxdepth 1 -name "CONTAINER.*" -printf "%f\n" | sort -t . -k 3 -n | xargs)" + + local CONTAINER_FILE + for CONTAINER_FILE in $CONTAINER_FILES; do + local CONTAINER_TYPE="$(awk -F '.' '{ print $2 }' <<< "$CONTAINER_FILE")" + if [[ "$(is_val_in_list "$CONTAINER_TYPE" "$PROCTYPES" " ")" == "false" ]]; then + dokku_log_info1 "Container type ($CONTAINER_TYPE) is no longer defined. Removing state" + local IP_FILE="${CONTAINER_FILE//CONTAINER/IP}" + local PORT_FILE="${CONTAINER_FILE//CONTAINER/PORT}" + rm -f "$DOKKU_ROOT/$APP/$CONTAINER_FILE" "$DOKKU_ROOT/$APP/$IP_FILE" "$DOKKU_ROOT/$APP/$PORT_FILE" + fi + done +} + +exec_app_json_scripts "$@" +cleanup_container_state "$@" diff --git a/plugins/00_dokku-standard/pre-deploy b/plugins/00_dokku-standard/pre-deploy deleted file mode 120000 index b24231790..000000000 --- a/plugins/00_dokku-standard/pre-deploy +++ /dev/null @@ -1 +0,0 @@ -exec-app-json-scripts \ No newline at end of file diff --git a/plugins/00_dokku-standard/pre-deploy b/plugins/00_dokku-standard/pre-deploy new file mode 100755 index 000000000..4570350ae --- /dev/null +++ b/plugins/00_dokku-standard/pre-deploy @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_CORE_AVAILABLE_PATH/00_dokku-standard/exec-app-json-scripts" + +exec_app_json_scripts() { + declare desc="core app.json scripts execution" + local trigger="pre-deploy app_json_scripts" + local APP="$1"; local IMAGE_TAG="$2"; local PHASE_SCRIPT_KEY="predeploy" + + dokku_log_info1 "Attempting to run scripts.dokku.$PHASE_SCRIPT_KEY from app.json (if defined)" + execute_script "$APP" "$IMAGE_TAG" "$PHASE_SCRIPT_KEY" +} + +exec_app_json_scripts "$@" diff --git a/plugins/common/functions b/plugins/common/functions index 5e919f620..d3367c2ef 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -752,3 +752,17 @@ strip_inline_comments() { echo "$stripped_line" } + +is_val_in_list() { + declare desc="return true if value ($1) is in list ($2) separated by delimiter ($3); delimiter defaults to comma" + local value="$1" list="$2" delimiter="${3:-,}" + local IFS="$delimiter" val_in_list=false + + for val in $list; do + if [[ "$val" == "$value" ]]; then + val_in_list=true + fi + done + + echo "$val_in_list" +} diff --git a/plugins/named-containers/post-deploy b/plugins/named-containers/post-deploy index 4fbc60c7c..2489b71ce 100755 --- a/plugins/named-containers/post-deploy +++ b/plugins/named-containers/post-deploy @@ -6,9 +6,10 @@ named_containers_post_deploy() { declare desc="names deployed app container is consistent manner" local trigger="named_containers_post_deploy" local APP="$1"; local APP_ROOT="$DOKKU_ROOT/$APP" - [[ -z $(stat -t "$APP_ROOT"/CONTAINER.* 2>/dev/null) ]] && exit 0 local container - for container in "$APP_ROOT"/CONTAINER.*; do + + shopt -s nullglob + for container in $APP_ROOT/CONTAINER.*; do local DYNO=$(echo "$container" | sed -r 's/.*CONTAINER\.(.*)/\1/') || true local NAME="$APP.$DYNO" local CURRENT_CONTAINER_ID="$(< "$container")" @@ -29,9 +30,12 @@ named_containers_post_deploy() { fi local ID=$(cat "$container") local CURRENT_NAME=$(docker inspect -f '{{.Name}}' "$ID" | tr -d /) - dokku_log_info2_quiet "renaming container (${ID:0:12}) $CURRENT_NAME to $NAME" - docker rename "$CURRENT_NAME" "$NAME" > /dev/null + if [[ -n "$CURRENT_NAME" ]]; then + dokku_log_info2_quiet "renaming container (${ID:0:12}) $CURRENT_NAME to $NAME" + docker rename "$CURRENT_NAME" "$NAME" > /dev/null + fi done + shopt -u nullglob } named_containers_post_deploy "$@"