feat: implement run:stop command

Closes #5169
This commit is contained in:
Jose Diaz-Gonzalez
2022-08-22 22:45:45 -04:00
parent 78157c8072
commit aa810cda7d
5 changed files with 148 additions and 0 deletions

View File

@@ -4,6 +4,7 @@
run [-e|--env KEY=VALUE] [--no-tty] <app> <cmd> # Run a command in a new container using the current app image
run:detached [-e|-env KEY=VALUE] [--no-tty] <app> <cmd> # Run a command in a new detached container using the current app image
run:list [--format json|stdout] [<app>] # List all run containers for an app
run:stop <app|--container CONTAINER> # Stops all run containers for an app or a specified run container
```
Sometimes it is necessary to run a one-off command under an app. Dokku makes it easy to run a fresh container via the `run` command.
@@ -102,3 +103,33 @@ dokku run:list node-js-app --format json
}
]
```
### Stopping a one-off cotainer
> New as of 0.29.0
Run containers for an app can be stopped via the `run:stop` command. The output will be the container id.
```shell
# start a container
# the output will be something like: node-js-app.run.2313
dokku run node-js-app sleep 300
# stop the container
dokku run:stop --container node-js-app.run.2313
````
```
node-js-app.run.2313
```
All containers for a given app can be stopped by specifying the app name.
```shell
dokku run:stop node-js-app
```
```
node-js-app.run.2313
node-js-app.run.574
```

View File

@@ -30,5 +30,6 @@ fn-help-content() {
run [-e|--env KEY=VALUE] [--no-tty] <app> <cmd>, Run a command in a new container using the current app image
run:detached [-e|-env KEY=VALUE] [--no-tty] <app> <cmd>, Run a command in a new detached container using the current app image
run:list [--format json|stdout] <app>, List all run containers for an app
run:stop <app|--container CONTAINER>, Stops all run containers for an app or a specified run container
help_content
}

View File

@@ -109,3 +109,35 @@ cmd-run-list() {
local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
plugn trigger scheduler-run-list "$DOKKU_SCHEDULER" "$APP" "$FORMAT"
}
cmd-run-stop() {
declare desc="Stops all run containers for an app or a specified run container"
declare cmd="run:stop"
[[ "$1" == "$cmd" ]] && shift 1
declare APP CONTAINER_NAME
while [[ $# -gt 0 ]]; do
case $1 in
--container=*)
local arg=$(printf "%s" "$1" | sed -E 's/(^--container=)//g')
CONTAINER_NAME="$arg"
shift
;;
--container)
if [[ ! $2 ]]; then
dokku_log_warn "expected $1 to have an argument"
break
fi
CONTAINER_NAME="$2"
shift 2
;;
*)
APP="$1"
shift
;;
esac
done
local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
plugn trigger scheduler-run-stop "$DOKKU_SCHEDULER" "$APP" "$CONTAINER_NAME"
}

6
plugins/run/subcommands/stop Executable file
View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -eo pipefail
source "$PLUGIN_AVAILABLE_PATH/run/internal-functions"
[[ $DOKKU_TRACE ]] && set -x
cmd-run-stop "$@"

View File

@@ -0,0 +1,78 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
fn-scheduler-docker-local-stop-container() {
declare CONTAINER_ID_OR_NAME="$1" DOCKER_STOP_TIMEOUT="$2"
if [ ! "$("$DOCKER_BIN" container ls -q -f "name=$CONTAINER_ID_OR_NAME" 2>/dev/null)" ]; then
dokku_log_warn "Specified container does not exist"
return 0
fi
[[ $DOKKU_DOCKER_STOP_TIMEOUT ]] && DOCKER_STOP_TIME_ARG="--time=${DOKKU_DOCKER_STOP_TIMEOUT}"
# shellcheck disable=SC2086
if "$DOCKER_BIN" container stop $DOCKER_STOP_TIME_ARG "$CONTAINER_ID_OR_NAME"; then
return
fi
if "$DOCKER_BIN" container kill "$CONTAINER_ID_OR_NAME"; then
return
fi
dokku_log_warn "Unable to stop container ${CONTAINER_ID_OR_NAME}"
return 1
}
trigger-scheduler-docker-local-scheduler-run-stop() {
declare desc="stops an app run container"
declare trigger="scheduler-run-stop"
declare DOKKU_SCHEDULER="$1" APP="$2" CONTAINER_NAME="$3"
if [[ "$DOKKU_SCHEDULER" != "docker-local" ]]; then
return
fi
if [[ -z "$APP" ]]; then
if [[ -z "$CONTAINER_NAME" ]]; then
dokku_log_fail "No container or app specified"
fi
if [[ "$(echo "$CONTAINER_NAME" | grep -o '\.' | wc -l)" -ne 2 ]]; then
dokku_log_fail "Invalid container name specified: $CONTAINER_NAME"
fi
APP="$(echo "$CONTAINER_NAME" | cut -d'.' -f1)"
STATE="$(echo "$CONTAINER_NAME" | cut -d'.' -f2)"
if [[ "$STATE" != "run" ]]; then
dokku_log_fail "Specified container must be a run container"
fi
fi
verify_app_name "$APP"
DOKKU_DOCKER_STOP_TIMEOUT="$(config_get "$APP" DOKKU_DOCKER_STOP_TIMEOUT || true)"
if [[ -n "$CONTAINER_NAME" ]]; then
fn-scheduler-docker-local-stop-container "$CONTAINER_NAME" "$DOKKU_DOCKER_STOP_TIMEOUT"
return $?
fi
CONTAINERS="$("$DOCKER_BIN" container ls --all --filter "label=com.dokku.app-name=$APP" --filter "label=com.dokku.container-type=run" --format '{{ .Names }}')"
if [[ -z "$CONTAINERS" ]]; then
dokku_log_quiet "No run containers exist"
return
fi
exit_code=0
while IFS= read -r CONTAINER_NAME; do
if ! fn-scheduler-docker-local-stop-container "$CONTAINER_NAME" "$DOKKU_DOCKER_STOP_TIMEOUT"; then
exit_code=1
fi
done <<< "$CONTAINERS"
return "$exit_code"
}
trigger-scheduler-docker-local-scheduler-run-stop "$@"