From e47f6e5803c532a0a8eb178678bb0736b3f4e986 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 28 Apr 2018 01:13:48 -0400 Subject: [PATCH 1/4] fix: use dokku_log_warn helper for warning messages in the ps plugin --- plugins/ps/functions | 10 +++++----- plugins/ps/subcommands/default | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/ps/functions b/plugins/ps/functions index 39401bc20..b65d4cba4 100755 --- a/plugins/ps/functions +++ b/plugins/ps/functions @@ -97,11 +97,11 @@ ps_start() { if ! (is_app_running "$APP"); then release_and_deploy "$APP" "$IMAGE_TAG" else - echo "App $APP already running" + dokku_log_warn "App $APP already running" fi plugn trigger proxy-build-config "$APP" else - echo "App $APP has not been deployed" + dokku_log_warn "App $APP has not been deployed" fi } @@ -109,9 +109,9 @@ ps_stop() { declare desc="stops app" local APP="$1"; verify_app_name "$APP" - ! (is_deployed "$APP") && echo "App $APP has not been deployed" && exit 0 + ! (is_deployed "$APP") && dokku_log_warn "App $APP has not been deployed" && exit 0 - echo "Stopping $APP ..." + dokku_log_quiet "Stopping $APP ..." local DOKKU_SCHEDULER=$(config_get "$APP" DOKKU_SCHEDULER || echo "docker-local") plugn trigger scheduler-stop "$DOKKU_SCHEDULER" "$APP" plugn trigger post-stop "$APP" @@ -132,7 +132,7 @@ ps_restart() { if (is_deployed "$APP"); then release_and_deploy "$APP" "$IMAGE_TAG" else - echo "App $APP has not been deployed" + dokku_log_warn "App $APP has not been deployed" fi } diff --git a/plugins/ps/subcommands/default b/plugins/ps/subcommands/default index 07f044d57..cc2ecfbe2 100755 --- a/plugins/ps/subcommands/default +++ b/plugins/ps/subcommands/default @@ -10,7 +10,7 @@ ps_main_cmd() { [[ -z $2 ]] && dokku_log_fail "Please specify an app to run the command on" local APP="$2"; verify_app_name "$APP" - ! (is_deployed "$APP") && echo "App $APP has not been deployed" && exit 0 + ! (is_deployed "$APP") && dokku_log_warn "App $APP has not been deployed" && exit 0 local DOKKU_APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids "$APP") From 8d07ffe775c4c76ce59f90d061cab564c54879d0 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 28 Apr 2018 01:19:23 -0400 Subject: [PATCH 2/4] refactor: deprecate is_app_running in favor of ps#fn-ps-is-app-running This will allow us to _actually_ introspect on the running state of an application. In certain cases, the detected state can be "mixed", meaning that we may need to take action, depending on what we want to do in mixed mode. --- plugins/common/functions | 1 + plugins/ps/functions | 31 +++++++++++++++++++++++++++++++ plugins/ps/internal-functions | 23 ++++------------------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index 85ca7e5ae..1ab124c6a 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -433,6 +433,7 @@ is_container_status() { is_app_running() { declare desc="return 0 if given app has a running container" + dokku_log_warn "Deprecated: ps#fn-is-app-running" local APP="$1" verify_app_name "$APP" diff --git a/plugins/ps/functions b/plugins/ps/functions index b65d4cba4..9da0b8ab7 100755 --- a/plugins/ps/functions +++ b/plugins/ps/functions @@ -171,3 +171,34 @@ get_restart_policies() { local -r phase_file_path=$1 get_raw_restart_policies "$phase_file_path" | sed -e 's/^--restart=//g' } + +fn-ps-app-status() { + declare APP="$1" + local PROCS=0 RUNNING="" + local APP_CIDS=$(get_app_container_ids "$APP"); + + for CID in $APP_CIDS; do + if (is_container_status "$CID" "Running"); then + RUNNING+="0" + else + RUNNING+="1" + fi + PROCS=$((PROCS + 1)) + done + + if [[ "${#RUNNING}" -eq 0 ]] || [[ "${#RUNNING}" -ne 0 ]] && [[ "$RUNNING" != *"0"* ]]; then + RUNNING="false" + elif [[ "$RUNNING" != *"1"* ]] && [[ "${#RUNNING}" -ne 0 ]]; then + RUNNING="true" + else + RUNNING="mixed" + fi + + echo "$PROCS $RUNNING" +} + +fn-ps-is-app-running() { + declare APP="$1" + APP_STATUS=$(fn-ps-app-status "$APP") + echo "$APP_STATUS" | cut -d ' ' -f 2 +} \ No newline at end of file diff --git a/plugins/ps/internal-functions b/plugins/ps/internal-functions index 763e2f1b2..dfa8639a0 100755 --- a/plugins/ps/internal-functions +++ b/plugins/ps/internal-functions @@ -33,27 +33,12 @@ cmd-ps-report-single() { local APP_DIR="$DOKKU_ROOT/$APP" [[ "$INFO_FLAG" == "true" ]] && INFO_FLAG="" - local passed_phases="deploy" - local APP_CIDS=$(get_app_container_ids "$APP"); local PROCS=0; local RUNNING="" - - for CID in $APP_CIDS; do - if (is_container_status "$CID" "Running"); then - RUNNING+="0" - else - RUNNING+="1" - fi - PROCS=$((PROCS + 1)) - done - - if [[ "${#RUNNING}" -eq 0 ]] || [[ "${#RUNNING}" -ne 0 ]] && [[ "$RUNNING" != *"0"* ]]; then - RUNNING="false" - elif [[ "$RUNNING" != *"1"* ]] && [[ "${#RUNNING}" -ne 0 ]]; then - RUNNING="true"; - else - RUNNING="mixed" - fi + local APP_RUNNING="$(fn-ps-app-status "$APP")" + local PROCS=$(echo "$APP_RUNNING" | cut -d ' ' -f 1) + local RUNNING=$(echo "$APP_RUNNING" | cut -d ' ' -f 2) if (is_deployed "$APP"); then DEPLOYED="true"; else DEPLOYED="false"; fi + local passed_phases="deploy" RESTARTPOLICY=$(get_restart_policies "$(get_phase_file_path "$passed_phases")" || true) local CONTAINER_FILES="$(find "$DOKKU_ROOT/$APP" -maxdepth 1 -name "CONTAINER.*" -printf "%f\n" 2>/dev/null | sort -t . -k 2 -n | xargs)" From 9b41f7a74e0e193efe58bfb1c4a162a8ede3674a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 28 Apr 2018 01:21:26 -0400 Subject: [PATCH 3/4] fix: use updated fn-ps-is-app-running function to determine an application's status In some cases, a single allocation for a service may not be running, but the others will, and `is_app_running` will return the wrong thing. Closes #2772 --- plugins/ps/functions | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/ps/functions b/plugins/ps/functions index 9da0b8ab7..60604924f 100755 --- a/plugins/ps/functions +++ b/plugins/ps/functions @@ -91,10 +91,17 @@ ps_start() { declare desc="starts app" local APP="$1"; verify_app_name "$APP" local IMAGE_TAG=$(get_running_image_tag "$APP"); + local RUNNING if (is_deployed "$APP"); then plugn trigger pre-start "$APP" - if ! (is_app_running "$APP"); then + RUNNING="$(fn-ps-is-app-running "$APP")" + + if [[ "$RUNNING" == "mixed" ]]; then + dokku_log_warn "App is running in mixed mode, releasing" + fi + + if [[ "$RUNNING" != "true" ]]; then release_and_deploy "$APP" "$IMAGE_TAG" else dokku_log_warn "App $APP already running" From 855090c9e7b09f6c71c4885a3abdbc4856d619d5 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 28 Apr 2018 01:27:34 -0400 Subject: [PATCH 4/4] fix: minor deprecated code cleanup --- plugins/common/functions | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index 1ab124c6a..2c89b5ab1 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -408,14 +408,17 @@ is_deployed() { is_container_running() { declare desc="return 0 if given docker container id is in running state" declare deprecated=true - local CID=$1 - local CONTAINER_STATUS=$(docker inspect -f '{{.State.Running}}' "$CID" || true) + declare CID="$1" + local CONTAINER_STATUS + + dokku_log_warn "Deprecated: common#is_container_status" + CONTAINER_STATUS=$(docker inspect -f '{{.State.Running}}' "$CID" || true) if [[ "$CONTAINER_STATUS" == "true" ]]; then return 0 - else - return 1 fi + + return 1 } is_container_status() { @@ -426,15 +429,17 @@ is_container_status() { if [[ "$CONTAINER_STATUS" == "true" ]]; then return 0 - else - return 1 fi + + return 1 } is_app_running() { declare desc="return 0 if given app has a running container" - dokku_log_warn "Deprecated: ps#fn-is-app-running" + declare deprecated=true local APP="$1" + + dokku_log_warn "Deprecated: ps#fn-is-app-running" verify_app_name "$APP" local APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids "$APP")