fix: correct entering running containers

This functionality was broken due to a refactor of argument handling in a previous release.

In addition to the fix, all the functionality was moved to scheduler-enter, which allows scheduler plugins to implement ways of entering containers in the relevant scheduler.

Closes #3972
This commit is contained in:
Jose Diaz-Gonzalez
2020-05-12 23:06:10 -04:00
parent 883bcd897f
commit 2dfb0ef3e6
5 changed files with 105 additions and 55 deletions

View File

@@ -1683,6 +1683,25 @@ DOKKU_SCHEDULER="$1"; APP="$2"; FORCE_CLEANUP="$3";
# TODO
```
### `scheduler-enter`
> Warning: The scheduler plugin trigger apis are under development and may change
> between minor releases until the 1.0 release.
- Description: Allows you to enter a running container for a given app
- Invoked by: `dokku enter`
- Arguments: `$DOKKU_SCHEDULER $APP $@`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
DOKKU_SCHEDULER="$1"; APP="$2"; ARGS="$@"
# TODO
```
### `scheduler-inspect`
> Warning: The scheduler plugin trigger apis are under development and may change
@@ -1720,6 +1739,7 @@ DOKKU_SCHEDULER="$1"; APP="$2";
# TODO
```
### `scheduler-logs`
> Warning: The scheduler plugin trigger apis are under development and may change

View File

@@ -0,0 +1 @@
hook

View File

@@ -472,7 +472,6 @@ get_app_running_container_ids() {
declare desc="return list of running docker container ids for given app and optional container_type"
local APP="$1" CONTAINER_TYPE="$2"
local CIDS
verify_app_name "$APP"
! (is_deployed "$APP") && dokku_log_fail "App $APP has not been deployed"
CIDS=$(get_app_container_ids "$APP" "$CONTAINER_TYPE")
@@ -487,7 +486,6 @@ get_app_running_container_ids() {
get_app_running_container_types() {
declare desc="return list of running container types for given app"
local APP=$1 CONTAINER_TYPES
verify_app_name "$APP"
! (is_deployed "$APP") && dokku_log_fail "App $APP has not been deployed"

View File

@@ -1,67 +1,18 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
cmd-enter-default() {
declare desc="enters running app container of specified proc type"
declare cmd="enter"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1" CONTAINER_TYPE="$2" CONTAINER_ID="$3"
declare APP="$1"
local IMAGE_TAG=$(get_running_image_tag "$APP")
local IMAGE=$(get_deploying_app_image_name "$APP" "$IMAGE_TAG")
verify_app_name "$APP"
local AVAILABLE_CONTAINER_TYPES=($(get_app_running_container_types "$APP"))
if [[ -z "$CONTAINER_TYPE" ]]; then
if [[ ${#AVAILABLE_CONTAINER_TYPES[@]} -gt 1 ]]; then
dokku_log_warn "No container type specified."
dokku_log_fail "Available types for app ($APP): ${AVAILABLE_CONTAINER_TYPES[*]}"
else
CONTAINER_TYPE="${AVAILABLE_CONTAINER_TYPES[0]}"
fi
fi
if [[ "$CONTAINER_TYPE" == "--container-id" ]]; then
local DOKKU_APP_CIDS=($(get_app_container_ids "$APP"))
if [[ -z "$CONTAINER_ID" ]]; then
dokku_log_warn "No container id specified."
dokku_log_fail "Available ids for app ($APP): ${DOKKU_APP_CIDS[*]}"
fi
if ! (printf -- '%s\n' "${DOKKU_APP_CIDS[@]}" | grep -q -e "^$4"); then
dokku_log_warn "Invalid container id for app"
dokku_log_fail "Available ids for app ($APP): ${DOKKU_APP_CIDS[*]}"
fi
local ID=$(printf -- '%s\n' "${DOKKU_APP_CIDS[@]}" | grep -e "^$4")
shift 3
else
local DOKKU_APP_CIDS=($(get_app_container_ids "$APP" "$CONTAINER_TYPE"))
local ID=${DOKKU_APP_CIDS[0]}
if [[ -z $ID ]]; then
dokku_log_warn "No containers found for type '$CONTAINER_TYPE'"
dokku_log_fail "Available types for app ($APP): ${AVAILABLE_CONTAINER_TYPES[*]}"
fi
if [[ "$CONTAINER_TYPE" ]]; then
shift 2
else
shift 1
fi
fi
(is_container_status "$ID" "Running") || dokku_log_fail "Container is not running"
local DOKKU_APP_SHELL="/bin/bash"
DOKKU_APP_SHELL="$(config_get --global DOKKU_APP_SHELL || echo "$DOKKU_APP_SHELL")"
DOKKU_APP_SHELL="$(config_get "$APP" DOKKU_APP_SHELL || echo "$DOKKU_APP_SHELL")"
[[ -z "$DOKKU_APP_SHELL" ]] && DOKKU_APP_SHELL="/bin/bash"
local EXEC_CMD=""
has_tty && local DOKKU_RUN_OPTS+=" -i -t"
is_image_herokuish_based "$IMAGE" "$APP" && local EXEC_CMD="/exec"
# shellcheck disable=SC2086
"$DOCKER_BIN" container exec $DOKKU_RUN_OPTS $ID $EXEC_CMD "${@:-$DOKKU_APP_SHELL}"
local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
plugn trigger scheduler-enter "$DOKKU_SCHEDULER" "$@"
}
cmd-enter-default "$@"

View File

@@ -0,0 +1,80 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-scheduler-docker-local-scheduler-enter() {
declare desc="enter a running container"
declare trigger="scheduler-enter"
declare DOKKU_SCHEDULER="$1" APP="$2"
shift 2
if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
return
fi
declare CONTAINER_TYPE="$1"
local AVAILABLE_CONTAINER_TYPES CONTAINER_ID
local CONTAINER_TYPE_SPECIFIED=false
if [[ -z "$CONTAINER_TYPE" ]]; then
AVAILABLE_CONTAINER_TYPES=($(get_app_running_container_types "$APP"))
if [[ ${#AVAILABLE_CONTAINER_TYPES[@]} -gt 1 ]]; then
dokku_log_warn "No container type specified."
dokku_log_fail "Available types for app ($APP): ${AVAILABLE_CONTAINER_TYPES[*]}"
else
CONTAINER_TYPE="${AVAILABLE_CONTAINER_TYPES[0]}"
fi
else
CONTAINER_TYPE_SPECIFIED=true
fi
if [[ "$CONTAINER_TYPE" == "--container-id" ]]; then
CONTAINER_ID="$2"
local DOKKU_APP_CIDS=($(get_app_container_ids "$APP"))
if [[ -z "$CONTAINER_ID" ]]; then
dokku_log_warn "No container id specified."
dokku_log_fail "Available ids for app ($APP): ${DOKKU_APP_CIDS[*]}"
fi
if ! (printf -- '%s\n' "${DOKKU_APP_CIDS[@]}" | grep -q -e "^$CONTAINER_ID"); then
dokku_log_warn "Invalid container id for app"
dokku_log_fail "Available ids for app ($APP): ${DOKKU_APP_CIDS[*]}"
fi
local CONTAINER_ID=$(printf -- '%s\n' "${DOKKU_APP_CIDS[@]}" | grep -e "^$CONTAINER_ID")
shift 2
else
local DOKKU_APP_CIDS=($(get_app_container_ids "$APP" "$CONTAINER_TYPE"))
local CONTAINER_ID=${DOKKU_APP_CIDS[0]}
if [[ -z "$CONTAINER_ID" ]]; then
if [[ ${#AVAILABLE_CONTAINER_TYPES[@]} -eq 0 ]]; then
AVAILABLE_CONTAINER_TYPES=($(get_app_running_container_types "$APP"))
fi
dokku_log_warn "No containers found for type '$CONTAINER_TYPE'"
dokku_log_fail "Available types for app ($APP): ${AVAILABLE_CONTAINER_TYPES[*]}"
fi
[[ "$CONTAINER_TYPE_SPECIFIED" == "true" ]] && shift 1
fi
if ! is_container_status "$CONTAINER_ID" "Running"; then
dokku_log_fail "Container is not running"
fi
local DOKKU_APP_SHELL="/bin/bash"
DOKKU_APP_SHELL="$(config_get --global DOKKU_APP_SHELL || echo "$DOKKU_APP_SHELL")"
DOKKU_APP_SHELL="$(config_get "$APP" DOKKU_APP_SHELL || echo "$DOKKU_APP_SHELL")"
[[ -z "$DOKKU_APP_SHELL" ]] && DOKKU_APP_SHELL="/bin/bash"
local EXEC_CMD=""
has_tty && local DOKKU_RUN_OPTS+=" -i -t"
local IMAGE="$("$DOCKER_BIN" container inspect --format '{{ .Config.Image }}' "$CONTAINER_ID")"
is_image_herokuish_based "$IMAGE" "$APP" && local EXEC_CMD="/exec"
# shellcheck disable=SC2086
"$DOCKER_BIN" container exec $DOKKU_RUN_OPTS "$CONTAINER_ID" $EXEC_CMD "${@:-$DOKKU_APP_SHELL}"
}
trigger-scheduler-docker-local-scheduler-enter "$@"