diff --git a/dokku b/dokku index 21a78925c..962c1f95e 100755 --- a/dokku +++ b/dokku @@ -74,7 +74,7 @@ case "$1" in is_image_buildstep_based "$IMAGE" && DOKKU_BUILDSTEP=true DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE" - oldids=$(get_container_ids $APP) + oldids=$(get_app_container_ids $APP) while read line || [ -n "$line" ] do diff --git a/plugins/00_dokku-standard/commands b/plugins/00_dokku-standard/commands index 7613eeb7a..0e12adfb2 100755 --- a/plugins/00_dokku-standard/commands +++ b/plugins/00_dokku-standard/commands @@ -117,7 +117,7 @@ case "$1" in for dokku_app in $dokku_apps; do APP=$(basename $dokku_app) - DOKKU_APP_CIDS=$(get_container_ids $APP) + DOKKU_APP_CIDS=$(get_app_container_ids $APP) DOCKER_RUNNING_CONTAINERS=$(docker ps -q --no-trunc) if [[ -n $DOKKU_APP_CIDS ]]; then for DOKKU_APP_CID in $DOKKU_APP_CIDS; do @@ -138,7 +138,7 @@ case "$1" in APP="$2"; if (is_deployed $APP); then - CONTAINER_IDS=( $(get_container_ids $APP) ) + CONTAINER_IDS=( $(get_app_container_ids $APP) ) LAST_CONTAINER_ID=${CONTAINER_IDS[${#CONTAINER_IDS[@]} - 1]} if [[ $3 == "-t" ]]; then diff --git a/plugins/apps/commands b/plugins/apps/commands index 1a50ab119..415956341 100755 --- a/plugins/apps/commands +++ b/plugins/apps/commands @@ -40,7 +40,7 @@ case "$1" in echo "Destroying $APP (including all add-ons)" pluginhook pre-delete $APP - DOKKU_APP_CIDS=$(get_container_ids $APP) + DOKKU_APP_CIDS=$(get_app_container_ids $APP) if [[ -n $DOKKU_APP_CIDS ]]; then for ID in $DOKKU_APP_CIDS;do docker stop $ID > /dev/null || true diff --git a/plugins/checks/check-deploy b/plugins/checks/check-deploy index 7ebc73139..9a12db51a 100755 --- a/plugins/checks/check-deploy +++ b/plugins/checks/check-deploy @@ -45,7 +45,7 @@ if [[ -z "$DOKKU_APP_LISTEN_IP" ]] && [[ -f "$DOKKU_ROOT/$APP/IP" ]]; then DOKKU_APP_LISTEN_IP=$(< "$DOKKU_ROOT/$APP/IP") fi if [[ -z "$DOKKU_APP_CONTAINER_ID" ]]; then - DOKKU_APP_CIDS=( $(get_container_ids $APP) ) + DOKKU_APP_CIDS=( $(get_app_container_ids $APP) ) DOKKU_APP_CONTAINER_ID=${DOKKU_APP_CIDS[0]} fi @@ -95,7 +95,7 @@ if [[ ! -s "${TMPDIR}/CHECKS" ]] || [[ "$DOKKU_APP_CONTAINER_TYPE" != "web" ]]; dokku_log_info1 "Waiting for $DOKKU_DEFAULT_WAIT seconds ..." sleep $DOKKU_DEFAULT_WAIT - docker ps -q --no-trunc | grep -q "$DOKKU_APP_CONTAINER_ID" || dokku_log_fail "App container failed to start!!" + ! (is_container_running $DOKKU_APP_CONTAINER_ID) && dokku_log_fail "App container failed to start!!" dokku_log_info1 "Default container check successful!" && exit 0 fi diff --git a/plugins/common/functions b/plugins/common/functions index c5d900fc2..be01f811e 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -178,16 +178,7 @@ copy_from_image() { fi } -is_deployed() { - APP="$1" - if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]] || [[ $(ls $DOKKU_ROOT/$APP/CONTAINER.* &> /dev/null; echo $?) -eq 0 ]];then - return 0 - else - return 1 - fi -} - -get_container_ids() { +get_app_container_ids() { APP="$1" verify_app_name $APP [[ -f $DOKKU_ROOT/$APP/CONTAINER ]] && DOKKU_CIDS+=$(< $DOKKU_ROOT/$APP/CONTAINER) @@ -204,9 +195,53 @@ get_container_ids() { get_dockerfile_exposed_port() { local DOCKERFILE_PORT=$(grep "^EXPOSE \+[[:digit:]]\+\(\/tcp\)\? *$" $1 | head -1 | sed 's/EXPOSE \+\([[:digit:]]\+\)\(\/tcp\)\?.*/\1/' || true) - if [[ -n "$DOCKERFILE_PORT" ]]; then - echo "$DOCKERFILE_PORT" + echo "$DOCKERFILE_PORT" +} + +get_app_running_container_ids() { + local APP=$1 + verify_app_name $APP + + ! (is_deployed $APP) && dokku_log_fail "App $APP has not been deployed" + local CIDS=$(get_app_container_ids $APP) + + for CID in $CIDS;do + local APP_CONTAINER_STATUS=$(docker inspect -f '{{.State.Running}}' "$CID" || true) + [[ "$APP_CONTAINER_STATUS" == "true" ]] && local APP_RUNNING_CONTAINER_IDS+="$CID " + done + + echo "$APP_RUNNING_CONTAINER_IDS" +} + +is_deployed() { + APP="$1" + if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]] || [[ $(ls $DOKKU_ROOT/$APP/CONTAINER.* &> /dev/null; echo $?) -eq 0 ]];then + return 0 else - echo "" + return 1 + fi +} + +is_container_running () { + local CID=$1 + local CONTAINER_STATUS=$(docker inspect -f '{{.State.Running}}' "$CID" || true) + + if [[ "$CONTAINER_STATUS" == "true" ]]; then + return 0 + else + return 1 + fi +} + +is_app_running() { + local APP="$1" + verify_app_name $APP + + local APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids $APP) + + if [[ -n "$APP_RUNNING_CONTAINER_IDS" ]]; then + return 0 + else + return 1 fi } diff --git a/plugins/nginx-vhosts/commands b/plugins/nginx-vhosts/commands index e7e602a2b..98d23a10a 100755 --- a/plugins/nginx-vhosts/commands +++ b/plugins/nginx-vhosts/commands @@ -49,7 +49,7 @@ case "$1" in shopt -u nullglob fi - DOKKU_APP_CIDS=($(get_container_ids $APP)) + DOKKU_APP_CIDS=($(get_app_container_ids $APP)) docker cp "${DOKKU_APP_CID[0]}:/app/nginx.conf.template" "$APP_NGINX_TEMPLATE" 2> /dev/null || true [[ -f "$DOKKU_ROOT/ENV" ]] && source $DOKKU_ROOT/ENV diff --git a/plugins/ps/commands b/plugins/ps/commands index f5b824643..d19f0ec37 100755 --- a/plugins/ps/commands +++ b/plugins/ps/commands @@ -8,7 +8,7 @@ case "$1" in [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 verify_app_name "$2" - APP="$2"; CONTAINER_IDS=$(get_container_ids $APP) + APP="$2"; CONTAINER_IDS=$(get_app_container_ids $APP) ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0 for CID in $CONTAINER_IDS; do @@ -18,12 +18,10 @@ case "$1" in ps:start) [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 - verify_app_name "$2" - - APP="$2"; CONTAINER_IDS=( $(get_container_ids $APP) ) + verify_app_name "$2"; APP="$2" ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0 - if [[ "$(docker ps -q --no-trunc| grep -q ${CONTAINER_IDS[0]}; echo $?)" != "0" ]]; then + if ! (is_app_running $APP); then release_and_deploy $APP else echo "App $APP already running" @@ -34,15 +32,13 @@ case "$1" in [[ -z $2 ]] && echo "Please specify an app to run the command on" && exit 1 verify_app_name "$2" - APP="$2"; CONTAINER_IDS=$(get_container_ids $APP) + APP="$2"; CONTAINER_IDS=$(get_app_container_ids $APP) ! (is_deployed $APP) && echo "App $APP has not been deployed" && exit 0 - CONTAINER_IDS_EGREP_PATTERN=$(echo $CONTAINER_IDS | xargs | sed -e "s: :|:g") - if [[ "$(docker ps -q --no-trunc| egrep -q $CONTAINER_IDS_EGREP_PATTERN; echo $?)" = "0" ]]; then + DOKKU_APP_RUNNING_CONTAINER_IDS=$(get_app_running_container_ids $APP) + if [[ -n "$DOKKU_APP_RUNNING_CONTAINER_IDS" ]]; then echo "Stopping $APP ..." - for CID in $CONTAINER_IDS;do - docker stop $CID > /dev/null - done + docker stop $DOKKU_APP_RUNNING_CONTAINER_IDS > /dev/null || true else echo "App $APP already stopped" fi