From 606352b6ed6bab0a24123d11b074b88dcbd7756a Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 25 Aug 2015 23:00:49 -0400 Subject: [PATCH 1/3] Add the ability to retrieve container ids by container type You can now use the following to narrow down the search for a container id: get_app_container_ids app web get_app_container_ids app worker You can also specify a specific type "id" like so: get_app_container_ids app web.1 get_app_container_ids app worker.3 Specifying an invalid type or id will result in an empty response --- plugins/common/functions | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index 66d787b05..0b186dff1 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -179,12 +179,22 @@ copy_from_image() { } get_app_container_ids() { - APP="$1" + APP="$1"; CONTAINER_TYPE="$2" verify_app_name $APP [[ -f $DOKKU_ROOT/$APP/CONTAINER ]] && DOKKU_CIDS+=$(< $DOKKU_ROOT/$APP/CONTAINER) + if [[ -n "$CONTAINER_TYPE" ]]; then + CONTAINER_PATTERN=$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE.* + if [[ $CONTAINER_TYPE == *.* ]]; then + CONTAINER_PATTERN=$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE + [[ ! -f $CONTAINER_PATTERN ]] && echo "" && return 0 + fi + else + CONTAINER_PATTERN=$DOKKU_ROOT/$APP/CONTAINER.* + fi + shopt -s nullglob - for DOKKU_CID_FILE in $DOKKU_ROOT/$APP/CONTAINER.*; do + for DOKKU_CID_FILE in $CONTAINER_PATTERN; do DOKKU_CIDS+=" " DOKKU_CIDS+=$(< $DOKKU_CID_FILE) DOKKU_CIDS+=" " From eccdc0426847907f287f236b936c449cefc1503d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 25 Aug 2015 23:23:26 -0400 Subject: [PATCH 2/3] Add `dokku enter` for connecting to an app container This command can be used to enter a running container. The following variations of the command exist: dokku enter api web dokku enter api web.1 dokku enter api --container-id ID By default, it runs a `/bin/bash`, but can also be used to run a custom command: # just echo hi dokku enter api web echo hi # run a long-running command, as one might for a cron task dokku enter api web python script/background-worker.py Closes #1332 --- plugins/enter/commands | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 plugins/enter/commands diff --git a/plugins/enter/commands b/plugins/enter/commands new file mode 100755 index 000000000..5f5f2d4a8 --- /dev/null +++ b/plugins/enter/commands @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x +source "$(dirname "$0")/../common/functions" + +case "$1" in + enter) + APP="$2"; IMAGE="dokku/$APP"; CONTAINER_TYPE="$3" + verify_app_name "$APP" + + [[ ! -n "$3" ]] && dokku_log_fail "No container id specified" + + if [[ "$3" == "--container-id" ]]; then + [[ ! -n "$4" ]] && dokku_log_fail "No container id specified" + + DOKKU_APP_CIDS=( $(get_app_container_ids "$APP") ) + printf -- '%s\n' "${DOKKU_APP_CIDS[@]}" | grep -q -e "^$4" || dokku_log_fail "Invalid container id for app" + ID=$(printf -- '%s\n' "${DOKKU_APP_CIDS[@]}" | grep -e "^$4") + shift 4 + else + DOKKU_APP_CIDS=( $(get_app_container_ids "$APP" "$CONTAINER_TYPE") ) + ID=${DOKKU_APP_CIDS[0]} + [[ ! -n $ID ]] && dokku_log_fail "No containers found for type '$CONTAINER_TYPE'" + shift 3 + fi + + docker ps -aq --no-trunc | grep -e "^$ID" > /dev/null || dokku_log_fail "Container does not exist" + docker ps -q --no-trunc | grep -e "^$ID" > /dev/null || dokku_log_fail "Container is not running" + + EXEC_CMD="" + is_image_herokuish_based "$IMAGE" && EXEC_CMD="/exec" + docker exec -it $ID $EXEC_CMD "${@:-/bin/bash}" + ;; + + help | enter:help) + cat && cat< [ || --container-id ], Connect to a specific app container +EOF + ;; + + *) + exit "$DOKKU_NOT_IMPLEMENTED_EXIT" + ;; + +esac From 771eaa33eef7e5e3d23b87245990308fc16d76ac Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 25 Aug 2015 23:31:53 -0400 Subject: [PATCH 3/3] Fix SC2125 violation --- plugins/common/functions | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/common/functions b/plugins/common/functions index 0b186dff1..0d790bf9d 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -184,13 +184,13 @@ get_app_container_ids() { [[ -f $DOKKU_ROOT/$APP/CONTAINER ]] && DOKKU_CIDS+=$(< $DOKKU_ROOT/$APP/CONTAINER) if [[ -n "$CONTAINER_TYPE" ]]; then - CONTAINER_PATTERN=$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE.* + CONTAINER_PATTERN="$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE.*" if [[ $CONTAINER_TYPE == *.* ]]; then - CONTAINER_PATTERN=$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE + CONTAINER_PATTERN="$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE" [[ ! -f $CONTAINER_PATTERN ]] && echo "" && return 0 fi else - CONTAINER_PATTERN=$DOKKU_ROOT/$APP/CONTAINER.* + CONTAINER_PATTERN="$DOKKU_ROOT/$APP/CONTAINER.*" fi shopt -s nullglob