From 608cdcf3c182ab348007805d162db7516e556105 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Sat, 26 Nov 2016 22:37:54 +0530 Subject: [PATCH 1/6] Adds the report subcommand to the apps plugin --- plugins/apps/subcommands/report | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 plugins/apps/subcommands/report diff --git a/plugins/apps/subcommands/report b/plugins/apps/subcommands/report new file mode 100755 index 000000000..febcb5e18 --- /dev/null +++ b/plugins/apps/subcommands/report @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions" +source "$PLUGIN_AVAILABLE_PATH/apps/functions" + +report_single_app() { + local APP="$1"; local APP_DIR="$DOKKU_ROOT/$APP"; local INFO_FLAG="$2" + verify_app_name "$APP" + local DOKKU_APP_CIDS=$(get_app_container_ids "$APP") + if [[ -n $DOKKU_APP_CIDS ]]; then + local cid + for CID in $DOKKU_APP_CIDS; do + local APP_CONTAINER_STATUS + local DOKKU_APP_CID + DOKKU_APP_CID=$CID + APP_CONTAINER_STATUS=$(docker inspect -f '{{.State.Status}}' "$CID") + done + fi + local flag_map=( + "--app-dir: $APP_DIR" + "--git-sha: $(GIT_DIR="$APP_DIR" git rev-parse --short HEAD 2> /dev/null || false)" + "--app-cid: ${DOKKU_APP_CID:0:12}" + "--status: $APP_CONTAINER_STATUS" + ) + + if [[ -z "$INFO_FLAG" ]]; then + dokku_log_info2 "$APP" + if (is_deployed "$APP"); then + for flag in "${flag_map[@]}"; do + key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')" + dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")" + done + else + echo "not deployed" + fi + else + local match=false; local value_exists=false + 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 +} + +apps_report_cmd() { + declare desc="shows reports for an app" + local cmd="apps:report" + local INSTALLED_APPS=$(dokku_apps) + local APP + + if [[ -z $2 ]]; then + for APP in $INSTALLED_APPS; do + report_single_app "$APP" + done + else + report_single_app "$2" "$3" + fi +} + +apps_report_cmd "$@" From 8d60d071df300e84e54e9f1d804891396aea48fe Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Sun, 27 Nov 2016 19:24:57 +0530 Subject: [PATCH 2/6] Docs and help text for apps:report subcommand --- docs/deployment/application-management.md | 49 +++++++++++++++++++++++ plugins/apps/commands | 1 + 2 files changed, 50 insertions(+) diff --git a/docs/deployment/application-management.md b/docs/deployment/application-management.md index 9b44c3ebc..38d8993d2 100644 --- a/docs/deployment/application-management.md +++ b/docs/deployment/application-management.md @@ -7,6 +7,7 @@ apps # List your apps apps:create # Create a new app apps:destroy # Permanently destroy an app apps:rename # Rename an app +apps:report [] [] # Display report about an app ``` ## Usage @@ -112,3 +113,51 @@ Renaming node-js-app to io-js-app... done ``` This will copy all of your app's contents into a new app directory with the name of your choice, delete your old app, then rebuild the new version of the app and deploy it. All of your config variables, including database urls, will be preserved. + +### Displaying reports about an app + +> New as of 0.7.2 + +You can get a report about the deployed apps using the `apps:report` command: + +```shell +dokku apps:report +``` + +``` +=====> node-js-sample + App dir: /home/dokku/node-js-sample + Git sha: dbddc3f + App cid: 7b18489c98be + Status: running +=====> python-sample +not deployed +=====> ruby-sample + App dir: /home/dokku/ruby-sample + Git sha: a2d477c + App cid: 78a44d71012a + Status: running +``` + +You can run the command for a specific app also. + +```shell +dokku apps:report node-js-sample +``` + +``` +=====> node-js-sample + App dir: /home/dokku/node-js-sample + Git sha: dbddc3f + App cid: 7b18489c98be + Status: running +``` + +You can pass flags which will output only the value of the specific information you want. For example: +```shell +dokku apps:report node-js-sample --git-sha +``` + +``` +dbddc3f +``` diff --git a/plugins/apps/commands b/plugins/apps/commands index c46d255b1..60f2053b1 100755 --- a/plugins/apps/commands +++ b/plugins/apps/commands @@ -11,6 +11,7 @@ case "$1" in apps:create , Create a new app apps:destroy , Permanently destroy an app apps:rename , Rename an app + apps:report [] [], Display report about an app help_content } From d04b0f7cca2561666e1e9b300452f3925c551d66 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Mon, 28 Nov 2016 08:46:30 +0530 Subject: [PATCH 3/6] Use dokku helpers to output to stderr --- plugins/apps/subcommands/report | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/apps/subcommands/report b/plugins/apps/subcommands/report index febcb5e18..9a3963f68 100755 --- a/plugins/apps/subcommands/report +++ b/plugins/apps/subcommands/report @@ -31,7 +31,7 @@ report_single_app() { dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")" done else - echo "not deployed" + dokku_log_fail "not deployed" fi else local match=false; local value_exists=false @@ -48,7 +48,7 @@ report_single_app() { fi done if [[ "$match" == "true" ]]; then - [[ "$value_exists" == "true" ]] || dokku_log_fail "not_deployed" + [[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed" else dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}" fi From 45f8b8ccc857234835e8254c736718f9fd9d0cb5 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Thu, 1 Dec 2016 11:39:18 +0530 Subject: [PATCH 4/6] Adds the report subcommand to the ps plugin --- plugins/ps/commands | 1 + plugins/ps/subcommands/report | 104 ++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100755 plugins/ps/subcommands/report diff --git a/plugins/ps/commands b/plugins/ps/commands index 6f705f448..c5ee28b84 100755 --- a/plugins/ps/commands +++ b/plugins/ps/commands @@ -13,6 +13,7 @@ case "$1" in ps:stop , Stop app container(s) ps:rebuild , 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 , Restart app container(s) ps:restartall, Restart all deployed app containers ps:restore, Start previously running apps e.g. after reboot diff --git a/plugins/ps/subcommands/report b/plugins/ps/subcommands/report new file mode 100755 index 000000000..e80f009ba --- /dev/null +++ b/plugins/ps/subcommands/report @@ -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 "$@" From a574a6db1c672af2965e0d249dcf14f600f722b1 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Fri, 2 Dec 2016 00:00:10 +0530 Subject: [PATCH 5/6] Quotes unquoted variable --- plugins/ps/subcommands/report | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/ps/subcommands/report b/plugins/ps/subcommands/report index e80f009ba..e637ceab2 100755 --- a/plugins/ps/subcommands/report +++ b/plugins/ps/subcommands/report @@ -32,7 +32,7 @@ ps_report_single_app() { 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) + 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 From 49b01143a083d3edbd2f36e3907e5ca6cf7f9e65 Mon Sep 17 00:00:00 2001 From: Mriyam Tamuli Date: Sun, 11 Dec 2016 13:26:07 +0530 Subject: [PATCH 6/6] Correct apps:report to not exit if one app is not deployed --- plugins/apps/subcommands/report | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/apps/subcommands/report b/plugins/apps/subcommands/report index 9a3963f68..6a915cc94 100755 --- a/plugins/apps/subcommands/report +++ b/plugins/apps/subcommands/report @@ -5,6 +5,11 @@ source "$PLUGIN_AVAILABLE_PATH/apps/functions" report_single_app() { local APP="$1"; local APP_DIR="$DOKKU_ROOT/$APP"; local INFO_FLAG="$2" + local use_echo; + if [[ "$INFO_FLAG" == "true" ]]; then + use_echo=true + INFO_FLAG="" + fi verify_app_name "$APP" local DOKKU_APP_CIDS=$(get_app_container_ids "$APP") if [[ -n $DOKKU_APP_CIDS ]]; then @@ -31,7 +36,11 @@ report_single_app() { dokku_log_verbose "$(printf "%-20s %-25s" "${key^}" "${flag#*: }")" done else - dokku_log_fail "not deployed" + if [[ "$use_echo" ]]; then + echo "not deployed" + else + dokku_log_fail "not deployed" + fi fi else local match=false; local value_exists=false @@ -63,7 +72,7 @@ apps_report_cmd() { if [[ -z $2 ]]; then for APP in $INSTALLED_APPS; do - report_single_app "$APP" + report_single_app "$APP" "true" done else report_single_app "$2" "$3"