Adds the report subcommand to the ps plugin

This commit is contained in:
Mriyam Tamuli
2016-12-01 11:39:18 +05:30
parent d04b0f7cca
commit 45f8b8ccc8
2 changed files with 105 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ case "$1" in
ps:stop <app>, Stop app container(s)
ps:rebuild <app>, Rebuild an app from source
ps:rebuildall, Rebuild all apps from source
ps:report, Shows report of all processes in app container(s)
ps:restart <app>, Restart app container(s)
ps:restartall, Restart all deployed app containers
ps:restore, Start previously running apps e.g. after reboot

104
plugins/ps/subcommands/report Executable file
View File

@@ -0,0 +1,104 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/docker-options/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
ps_report_single_app() {
local APP="$1"; local APP_DIR="$DOKKU_ROOT/$APP"; local INFO_FLAG="$2"
local passed_phases="deploy"
local APP_CIDS=$(get_app_container_ids "$APP"); local PROCS=0; local RUNNING=""
for CID in $APP_CIDS; do
local APP_CONTAINER_STATUS=$(docker inspect -f '{{.State.Running}}' "$CID" 2>/dev/null || true)
if [[ "$APP_CONTAINER_STATUS" == "true" ]]; 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
if (is_deployed "$APP"); then DEPLOYED="true"; else DEPLOYED="false"; fi
RESTARTPOLICY=$(get_restart_policies "$(get_phase_file_path "$passed_phases")")
local CONTAINER_FILES="$(find "$DOKKU_ROOT/$APP" -maxdepth 1 -name "CONTAINER.*" -printf "%f\n" 2>/dev/null | sort -t . -k 2 -n | xargs)"
local CONTAINER_FILE
local DOKKU_APP_RESTORE=$(config_get "$APP" DOKKU_APP_RESTORE || true)
local STATUSES=()
if [[ "$DOKKU_APP_RESTORE" != 0 ]]; then RESTORE="true"; else RESTORE="false"; fi
for CONTAINER_FILE in $CONTAINER_FILES; do
CID=$(<$DOKKU_ROOT/$APP/$CONTAINER_FILE)
local APP_CONTAINER_STATUS=$(docker inspect -f '{{.State.Running}}' "$CID")
STATUSES+=("${CONTAINER_FILE#*.}:$APP_CONTAINER_STATUS#${CID:0:12}")
done
local flag_map=(
"--processes: $PROCS"
"--deployed: $DEPLOYED"
"--running: $RUNNING"
"--restore: $RESTORE"
"--restart-policy: $RESTARTPOLICY"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2 "$APP"
for flag in "${flag_map[@]}"; do
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")"
done
for STATUS in "${STATUSES[@]}"; do
name=${STATUS%:*}
value=${STATUS#*:}
status=${value%#*}
cid=${value#*#}
dokku_log_verbose "$(printf "%-20s %-10s (CID: %s)" "Status $name" "$status" "$cid")"
done
else
local match=false; local value_exists=false
for STATUS in "${STATUSES[@]}"; do
name=${STATUS%:*}
value=${STATUS#*:}
status=${value%#*}
cid=${value#*#}
status_flag="--status-$name"
flag_map+=("$status_flag: $status")
done
for flag in "${flag_map[@]}"; do
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
value=${flag#*: }
size="${#value}"
if [[ "$size" -ne 0 ]]; then
echo "$value" && match=true && value_exists=true
else
match=true
fi
fi
done
if [[ "$match" == "true" ]]; then
[[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
else
dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
fi
fi
}
ps_report_cmd() {
declare desc="shows reports for an app"
local cmd="ps:report"
local INSTALLED_APPS=$(dokku_apps); local APP
if [[ -z $2 ]]; then
for APP in $INSTALLED_APPS; do
ps_report_single_app "$APP"
done
else
ps_report_single_app "$2" "$3"
fi
}
ps_report_cmd "$@"