2018-08-01 02:00:32 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
|
|
|
|
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
|
2018-12-04 16:13:47 -05:00
|
|
|
source "$PLUGIN_AVAILABLE_PATH/config/functions"
|
2019-01-07 01:04:17 -05:00
|
|
|
set -eo pipefail
|
|
|
|
|
[[ $DOKKU_TRACE ]] && set -x
|
2018-08-01 02:00:32 -04:00
|
|
|
|
2026-05-11 22:29:28 -04:00
|
|
|
fn-scheduler-docker-local-init-process() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
fn-plugin-property-get-default "scheduler-docker-local" "$APP" "init-process" ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-computed-init-process() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
local value
|
|
|
|
|
value="$(fn-scheduler-docker-local-init-process "$APP")"
|
|
|
|
|
if [[ -z "$value" ]]; then
|
|
|
|
|
value="$(fn-scheduler-docker-local-global-init-process "$APP")"
|
|
|
|
|
fi
|
2026-05-13 16:39:38 -04:00
|
|
|
if [[ -z "$value" ]]; then
|
|
|
|
|
value="true"
|
|
|
|
|
fi
|
2026-05-11 22:29:28 -04:00
|
|
|
echo "$value"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-global-init-process() {
|
|
|
|
|
declare APP="$1"
|
2026-05-13 16:39:38 -04:00
|
|
|
fn-plugin-property-get-default "scheduler-docker-local" "--global" "init-process" ""
|
2026-05-11 22:29:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-parallel-schedule-count() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
fn-plugin-property-get-default "scheduler-docker-local" "$APP" "parallel-schedule-count" ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-computed-parallel-schedule-count() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
local value
|
|
|
|
|
value="$(fn-scheduler-docker-local-parallel-schedule-count "$APP")"
|
|
|
|
|
if [[ -z "$value" ]]; then
|
|
|
|
|
value="$(fn-scheduler-docker-local-global-parallel-schedule-count "$APP")"
|
|
|
|
|
fi
|
2026-05-13 16:39:38 -04:00
|
|
|
if [[ -z "$value" ]]; then
|
|
|
|
|
value="1"
|
|
|
|
|
fi
|
2026-05-11 22:29:28 -04:00
|
|
|
echo "$value"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-global-parallel-schedule-count() {
|
|
|
|
|
declare APP="$1"
|
2026-05-13 16:39:38 -04:00
|
|
|
fn-plugin-property-get-default "scheduler-docker-local" "--global" "parallel-schedule-count" ""
|
2026-05-11 22:29:28 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-02 15:34:24 -04:00
|
|
|
fn-scheduler-docker-local-get-checks-file-path() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
|
|
|
|
|
echo "${DOKKU_LIB_ROOT}/data/scheduler-docker-local/$APP/CHECKS"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-get-process-specific-checks-file-path() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
|
|
|
|
|
checks_path="$(fn-scheduler-docker-local-get-checks-file-path "$APP")"
|
|
|
|
|
process_specific_checks_path="$checks_path.$DOKKU_PID"
|
|
|
|
|
if [[ -f "$process_specific_checks_path" ]]; then
|
|
|
|
|
echo "$process_specific_checks_path"
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "$checks_path"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-has-checks-file() {
|
|
|
|
|
declare APP="$1"
|
|
|
|
|
|
|
|
|
|
checks_path="$(fn-scheduler-docker-local-get-checks-file-path "$APP")"
|
|
|
|
|
if [[ -f "$checks_path.$DOKKU_PID.missing" ]]; then
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ -f "$checks_path.$DOKKU_PID" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ -f "$checks_path" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-20 10:37:58 -04:00
|
|
|
fn-scheduler-docker-local-retire-container() {
|
2021-08-08 03:04:25 -04:00
|
|
|
declare APP="$1" CID="$2"
|
2019-01-11 11:08:59 -05:00
|
|
|
local STATE
|
2018-07-20 10:37:58 -04:00
|
|
|
|
2021-08-08 03:04:25 -04:00
|
|
|
dokku_log_verbose_quiet "Attempting to retire $APP container $CID"
|
2019-05-29 04:05:24 -04:00
|
|
|
STATE="$("$DOCKER_BIN" container inspect --format "{{ .State.Status }}" "$CID" 2>/dev/null || true)"
|
2019-01-11 11:08:59 -05:00
|
|
|
if [[ -z "$STATE" ]]; then
|
2018-07-20 10:37:58 -04:00
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
2025-03-09 05:09:49 -04:00
|
|
|
DOKKU_DOCKER_STOP_TIMEOUT="$(plugn trigger ps-get-property "$APP" stop-timeout-seconds || true)"
|
2018-07-20 10:37:58 -04:00
|
|
|
[[ $DOKKU_DOCKER_STOP_TIMEOUT ]] && DOCKER_STOP_TIME_ARG="--time=${DOKKU_DOCKER_STOP_TIMEOUT}"
|
|
|
|
|
|
2019-01-11 11:08:59 -05:00
|
|
|
if [[ "$STATE" == "restarting" ]]; then
|
2023-07-01 03:48:45 -04:00
|
|
|
"$DOCKER_BIN" container update --restart=no "$CID" &>/dev/null
|
2019-01-11 11:08:59 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ "$STATE" != "dead" ]] && [[ "$STATE" != "exited" ]]; then
|
|
|
|
|
# Attempt to stop, if that fails, then force a kill as docker seems
|
|
|
|
|
# to not send SIGKILL as the docs would indicate. If that fails, move
|
|
|
|
|
# on to the next.
|
2019-05-29 04:05:24 -04:00
|
|
|
"$DOCKER_BIN" container stop $DOCKER_STOP_TIME_ARG "$CID" \
|
|
|
|
|
|| "$DOCKER_BIN" container kill "$CID" \
|
2019-01-11 11:08:59 -05:00
|
|
|
|| dokku_log_warn "Unable to kill container ${CID}"
|
|
|
|
|
fi
|
|
|
|
|
|
2019-05-29 04:05:24 -04:00
|
|
|
STATE="$("$DOCKER_BIN" container inspect --format "{{ .State.Status }}" "$CID" 2>/dev/null || true)"
|
2019-01-11 11:08:59 -05:00
|
|
|
if [[ -z "$STATE" ]]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
2018-07-20 10:37:58 -04:00
|
|
|
|
2019-01-11 11:08:59 -05:00
|
|
|
if [[ "$STATE" != "dead" ]] && [[ "$STATE" != "exited" ]]; then
|
2019-05-29 04:05:24 -04:00
|
|
|
if ! "$DOCKER_BIN" container kill "$CID"; then
|
2019-01-11 11:08:59 -05:00
|
|
|
dokku_log_warn "Unable to kill container ${CID}"
|
|
|
|
|
fi
|
2018-07-20 10:37:58 -04:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 03:21:45 -05:00
|
|
|
fn-scheduler-docker-local-retire-containers() {
|
2018-07-20 14:29:00 -04:00
|
|
|
local DEAD_CONTAINER_FILE="${DOKKU_LIB_ROOT}/data/scheduler-docker-local/dead-containers"
|
2020-11-26 03:21:45 -05:00
|
|
|
local APP CID CURRENT_TIME DEAD_TIME STATE
|
2021-08-08 03:04:25 -04:00
|
|
|
declare SCHEDULER="$1" APP="$2"
|
2020-11-26 03:21:45 -05:00
|
|
|
|
|
|
|
|
if [[ ! -f "$DEAD_CONTAINER_FILE" ]]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
DEAD_CONTAINERS=()
|
|
|
|
|
while read line; do
|
|
|
|
|
[[ -z "$line" ]] && continue
|
|
|
|
|
CURRENT_TIME="$(date +%s)"
|
2021-08-08 03:04:25 -04:00
|
|
|
RETIRE_APP="$(echo "$line" | cut -d ' ' -f1)"
|
2020-11-26 03:21:45 -05:00
|
|
|
CID="$(echo "$line" | cut -d ' ' -f2)"
|
|
|
|
|
DEAD_TIME="$(echo "$line" | cut -d ' ' -f3)"
|
|
|
|
|
|
2021-08-08 03:04:25 -04:00
|
|
|
if [[ -n "$APP" ]] && [[ "$APP" != "$RETIRE_APP" ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2020-11-26 03:21:45 -05:00
|
|
|
if [[ "$CURRENT_TIME" -le "$DEAD_TIME" ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2021-08-08 03:04:25 -04:00
|
|
|
fn-scheduler-docker-local-retire-container "$RETIRE_APP" "$CID"
|
2020-11-26 03:21:45 -05:00
|
|
|
STATE="$("$DOCKER_BIN" container inspect --format "{{ .State.Status }}" "$CID" 2>/dev/null || true)"
|
|
|
|
|
if [[ -z "$STATE" ]]; then
|
|
|
|
|
DEAD_CONTAINERS+=("$CID")
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ "$STATE" == "running" ]]; then
|
|
|
|
|
dokku_log_warn "Container ${CID} still running"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2023-07-01 03:48:45 -04:00
|
|
|
"$DOCKER_BIN" container rm --force "$CID" &>/dev/null || true
|
|
|
|
|
if "$DOCKER_BIN" container inspect "${CID}" &>/dev/null; then
|
2020-11-26 03:21:45 -05:00
|
|
|
dokku_log_warn "Container ${CID} still running"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
DEAD_CONTAINERS+=("$CID")
|
|
|
|
|
done <"$DEAD_CONTAINER_FILE"
|
|
|
|
|
|
|
|
|
|
for CID in "${DEAD_CONTAINERS[@]}"; do
|
|
|
|
|
sed -i "/${CID}/d" "$DEAD_CONTAINER_FILE"
|
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-retire-images() {
|
|
|
|
|
local DEAD_IMAGE_FILE="${DOKKU_LIB_ROOT}/data/scheduler-docker-local/dead-images"
|
|
|
|
|
local APP IMAGE_ID CURRENT_TIME DEAD_TIME STATE RM_OUTPUT
|
2021-08-08 03:04:25 -04:00
|
|
|
declare SCHEDULER="$1" APP="$2"
|
2020-11-26 03:21:45 -05:00
|
|
|
|
|
|
|
|
if [[ ! -f "$DEAD_IMAGE_FILE" ]]; then
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
DEAD_IMAGES=()
|
|
|
|
|
while read line; do
|
|
|
|
|
[[ -z "$line" ]] && continue
|
|
|
|
|
CURRENT_TIME="$(date +%s)"
|
2021-08-08 03:04:25 -04:00
|
|
|
RETIRE_APP="$(echo "$line" | cut -d ' ' -f1)"
|
2020-11-26 03:21:45 -05:00
|
|
|
IMAGE_ID="$(echo "$line" | cut -d ' ' -f2)"
|
|
|
|
|
DEAD_TIME="$(echo "$line" | cut -d ' ' -f3)"
|
|
|
|
|
|
2021-08-08 03:04:25 -04:00
|
|
|
if [[ -n "$APP" ]] && [[ "$APP" != "$RETIRE_APP" ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2020-11-26 03:21:45 -05:00
|
|
|
if [[ "$CURRENT_TIME" -le "$DEAD_TIME" ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
STATE="$("$DOCKER_BIN" image inspect --format "{{ .Id }}" "$IMAGE_ID" 2>/dev/null || true)"
|
|
|
|
|
if [[ -z "$STATE" ]]; then
|
|
|
|
|
DEAD_IMAGES+=("$IMAGE_ID")
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
2021-08-08 03:04:25 -04:00
|
|
|
dokku_log_verbose_quiet "Attempting to retire $RETIRE_APP image $IMAGE_ID"
|
2022-03-12 06:24:42 -05:00
|
|
|
if RM_OUTPUT="$("$DOCKER_BIN" image remove "$IMAGE_ID" 2>&1)"; then
|
2020-11-26 03:21:45 -05:00
|
|
|
DEAD_IMAGES+=("$IMAGE_ID")
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if echo "$RM_OUTPUT" | grep -q "image has dependent child images"; then
|
2021-08-05 17:48:09 -04:00
|
|
|
TAG_COUNT="$(docker inspect "$IMAGE_ID" --format '{{ json .RepoTags }}' | jq '. | length')"
|
|
|
|
|
if [[ "$TAG_COUNT" -eq 0 ]]; then
|
|
|
|
|
dokku_log_warn "Image ${IMAGE_ID} has children images and is untagged, skipping rm and marking dead"
|
|
|
|
|
DEAD_IMAGES+=("$IMAGE_ID")
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
dokku_log_warn "Image ${IMAGE_ID} has children images and has $TAG_COUNT tags, skipping rm"
|
2020-11-26 03:21:45 -05:00
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if echo "$RM_OUTPUT" | grep -q "image is being used by running container"; then
|
2026-04-29 05:36:04 -04:00
|
|
|
if fn-scheduler-docker-local-image-in-use-by-app "$RETIRE_APP" "$IMAGE_ID" ""; then
|
|
|
|
|
dokku_log_warn "Image ${IMAGE_ID} is still in use by ${RETIRE_APP}, removing from retire list"
|
|
|
|
|
DEAD_IMAGES+=("$IMAGE_ID")
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2020-11-26 03:21:45 -05:00
|
|
|
dokku_log_warn "Image ${IMAGE_ID} has running containers, skipping rm"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
dokku_log_warn "Image ${IMAGE_ID} still running"
|
|
|
|
|
done <"$DEAD_IMAGE_FILE"
|
|
|
|
|
|
|
|
|
|
for IMAGE_ID in "${DEAD_IMAGES[@]}"; do
|
|
|
|
|
sed -i "/${IMAGE_ID}/d" "$DEAD_IMAGE_FILE"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
sort -o "$DEAD_IMAGE_FILE" -r "$DEAD_IMAGE_FILE"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn-scheduler-docker-local-register-retired() {
|
|
|
|
|
declare TYPE="$1" APP="$2" DOCKER_ID="$3" WAIT="$4"
|
|
|
|
|
local DEAD_FILE="${DOKKU_LIB_ROOT}/data/scheduler-docker-local/dead-containers"
|
|
|
|
|
if [[ "$TYPE" == "image" ]]; then
|
|
|
|
|
local DEAD_FILE="${DOKKU_LIB_ROOT}/data/scheduler-docker-local/dead-images"
|
|
|
|
|
fi
|
2018-07-20 10:37:58 -04:00
|
|
|
local CURRENT_TIME DEAD_TIME
|
|
|
|
|
|
|
|
|
|
CURRENT_TIME="$(date +%s)"
|
2018-07-20 13:08:15 -04:00
|
|
|
DEAD_TIME=$((CURRENT_TIME + WAIT))
|
2020-12-13 02:05:23 -05:00
|
|
|
touch "$DEAD_FILE"
|
2020-11-26 03:21:45 -05:00
|
|
|
if ! grep -q "$DOCKER_ID" "$DEAD_FILE"; then
|
|
|
|
|
echo "${APP} ${DOCKER_ID} ${DEAD_TIME}" >>"${DEAD_FILE}"
|
|
|
|
|
fi
|
2018-07-20 10:37:58 -04:00
|
|
|
}
|
2021-08-12 21:11:11 -04:00
|
|
|
|
2026-04-29 05:36:04 -04:00
|
|
|
fn-scheduler-docker-local-image-in-use-by-app() {
|
|
|
|
|
declare desc="returns 0 if IMAGE_ID is used by an app container that is not already pending retirement"
|
|
|
|
|
declare APP="$1" IMAGE_ID="$2" EXCLUDE_CONTAINER_ID="$3"
|
|
|
|
|
local DEAD_CONTAINER_FILE="${DOKKU_LIB_ROOT}/data/scheduler-docker-local/dead-containers"
|
|
|
|
|
local cid exclude_full_id container_image short_cid
|
|
|
|
|
|
|
|
|
|
if [[ -z "$IMAGE_ID" ]]; then
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ -n "$EXCLUDE_CONTAINER_ID" ]]; then
|
|
|
|
|
exclude_full_id="$("$DOCKER_BIN" container inspect "$EXCLUDE_CONTAINER_ID" --format '{{.Id}}' 2>/dev/null || true)"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
for cid in $("$DOCKER_BIN" container ls -q --no-trunc -f label=com.dokku.app-name="$APP" 2>/dev/null); do
|
|
|
|
|
[[ -z "$cid" ]] && continue
|
|
|
|
|
if [[ -n "$exclude_full_id" ]] && [[ "$cid" == "$exclude_full_id" ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
short_cid="${cid:0:12}"
|
|
|
|
|
if [[ -f "$DEAD_CONTAINER_FILE" ]] && grep -q "$short_cid" "$DEAD_CONTAINER_FILE"; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
container_image="$("$DOCKER_BIN" container inspect "$cid" --format '{{.Image}}' 2>/dev/null | cut -d: -f2 || true)"
|
|
|
|
|
if [[ -n "$container_image" ]] && [[ "$container_image" == "$IMAGE_ID" ]]; then
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 21:11:11 -04:00
|
|
|
fn-scheduler-docker-local-start-app-container() {
|
|
|
|
|
declare desc="starts a single app container"
|
2021-09-14 04:03:40 -04:00
|
|
|
declare APP="$1"
|
|
|
|
|
shift
|
2021-10-03 20:09:16 -04:00
|
|
|
|
2022-07-30 00:48:38 -04:00
|
|
|
declare -a DOCKER_ARGS
|
2021-10-03 20:09:16 -04:00
|
|
|
for i in "$@"; do
|
2022-07-30 00:48:38 -04:00
|
|
|
DOCKER_ARGS+=("$i")
|
2021-10-03 20:09:16 -04:00
|
|
|
done
|
2022-07-30 00:48:38 -04:00
|
|
|
set -- "${DOCKER_ARGS[@]}"
|
2021-08-12 21:11:11 -04:00
|
|
|
|
|
|
|
|
eval "$(config_export app "$APP" --merged)"
|
2021-10-03 19:47:54 -04:00
|
|
|
# shellcheck disable=SC2124
|
2021-10-03 20:09:16 -04:00
|
|
|
"$DOCKER_BIN" container create "$@"
|
2021-08-12 21:11:11 -04:00
|
|
|
}
|