2015-02-05 11:32:46 -08:00
#!/usr/bin/env bash
2019-01-07 01:04:17 -05:00
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
2015-02-05 11:32:46 -08:00
2015-02-07 10:57:30 -08:00
has_tty() {
2016-03-08 15:30:34 -05:00
declare desc="return 0 if we have a tty"
2024-02-11 00:57:44 +02:00
if [[ "$DOKKU_FORCE_TTY" == "true" ]]; then
return 0
fi
2021-07-27 13:16:33 -04:00
if [[ "$DOKKU_DISABLE_TTY" == "true" ]]; then
2021-07-27 02:26:15 -04:00
return 1
fi
2019-09-16 04:02:54 -04:00
if [[ "$(LC_ALL=C /usr/bin/tty || true)" == "not a tty" ]]; then
2015-02-05 11:32:46 -08:00
return 1
else
return 0
fi
}
2015-11-13 16:40:22 -08:00
dokku_apps() {
2016-03-08 15:30:34 -05:00
declare desc="prints list of all local apps"
2022-05-15 15:33:19 -04:00
declare FILTER="$1"
local INSTALLED_APPS="$(plugn trigger app-list "$FILTER")"
2021-11-12 15:54:08 -05:00
if [[ -z "$INSTALLED_APPS" ]]; then
dokku_log_fail "You haven't deployed any applications yet"
fi
echo "$INSTALLED_APPS"
2015-11-13 16:40:22 -08:00
}
2021-01-23 12:32:31 -05:00
dokku_version() {
if [[ -f "$DOKKU_LIB_ROOT/STABLE_VERSION" ]]; then
DOKKU_VERSION=$(cat "${DOKKU_LIB_ROOT}/STABLE_VERSION")
elif [[ -f "$DOKKU_LIB_ROOT/VERSION" ]]; then
DOKKU_VERSION=$(cat "${DOKKU_LIB_ROOT}/VERSION")
else
dokku_log_fail "Unable to determine dokku's version"
fi
echo "dokku version ${DOKKU_VERSION}"
}
2018-04-27 03:55:00 -04:00
dokku_log_quiet() {
declare desc="log quiet formatter"
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
echo "$*"
fi
}
2015-02-07 10:57:30 -08:00
dokku_log_info1() {
2016-03-08 15:30:34 -05:00
declare desc="log info1 formatter"
2015-11-02 17:54:02 -06:00
echo "-----> $*"
2015-02-05 11:32:46 -08:00
}
2015-02-07 10:57:30 -08:00
dokku_log_info2() {
2016-03-08 15:30:34 -05:00
declare desc="log info2 formatter"
2015-11-02 17:54:02 -06:00
echo "=====> $*"
2015-02-05 11:32:46 -08:00
}
2015-02-07 10:57:30 -08:00
dokku_log_info1_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="log info1 formatter (with quiet option)"
2015-09-14 21:20:35 -07:00
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2015-11-02 17:54:02 -06:00
echo "-----> $*"
2015-02-05 12:38:59 -08:00
fi
}
2015-02-07 10:57:30 -08:00
dokku_log_info2_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="log info2 formatter (with quiet option)"
2015-09-14 21:20:35 -07:00
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2015-11-02 17:54:02 -06:00
echo "=====> $*"
2015-02-05 12:38:59 -08:00
fi
}
2015-04-24 10:49:18 -07:00
dokku_col_log_info1() {
2016-03-08 15:30:34 -05:00
declare desc="columnar log info1 formatter"
2015-11-02 17:54:02 -06:00
printf "%-6s %-18s %-25s %-25s %-25s\n" "----->" "$@"
2015-02-07 14:09:39 -08:00
}
2015-04-24 10:49:18 -07:00
dokku_col_log_info1_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="columnar log info1 formatter (with quiet option)"
2015-09-14 21:20:35 -07:00
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2015-11-02 17:54:02 -06:00
printf "%-6s %-18s %-25s %-25s %-25s\n" "----->" "$@"
2015-02-07 14:09:39 -08:00
fi
}
2015-04-24 10:49:18 -07:00
dokku_col_log_info2() {
2016-03-08 15:30:34 -05:00
declare desc="columnar log info2 formatter"
2015-11-02 17:54:02 -06:00
printf "%-6s %-18s %-25s %-25s %-25s\n" "=====>" "$@"
2015-02-07 14:09:39 -08:00
}
2015-04-24 10:49:18 -07:00
dokku_col_log_info2_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="columnar log info2 formatter (with quiet option)"
2015-09-14 21:20:35 -07:00
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2015-11-02 17:54:02 -06:00
printf "%-6s %-18s %-25s %-25s %-25s\n" "=====>" "$@"
2015-02-07 14:09:39 -08:00
fi
}
2015-04-24 10:49:18 -07:00
dokku_col_log_msg() {
2016-03-08 15:30:34 -05:00
declare desc="columnar log formatter"
2015-04-24 10:49:18 -07:00
printf "%-25s %-25s %-25s %-25s\n" "$@"
2015-02-07 14:09:39 -08:00
}
2015-04-24 10:49:18 -07:00
dokku_col_log_msg_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="columnar log formatter (with quiet option)"
2015-09-14 21:20:35 -07:00
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2015-04-24 10:49:18 -07:00
printf "%-25s %-25s %-25s %-25s\n" "$@"
2015-02-07 14:09:39 -08:00
fi
}
2015-03-20 08:29:35 -07:00
dokku_log_verbose_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="log verbose formatter (with quiet option)"
2015-09-14 21:20:35 -07:00
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2015-11-02 17:54:02 -06:00
echo " $*"
2015-03-20 08:29:35 -07:00
fi
}
2015-02-07 10:57:30 -08:00
dokku_log_verbose() {
2016-03-08 15:30:34 -05:00
declare desc="log verbose formatter"
2015-11-02 17:54:02 -06:00
echo " $*"
2015-02-05 11:32:46 -08:00
}
2019-03-12 12:34:45 -04:00
dokku_log_exclaim_quiet() {
declare desc="log exclaim formatter"
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
echo " ! $*"
fi
}
dokku_log_exclaim() {
declare desc="log exclaim formatter"
echo " ! $*"
}
2018-11-05 01:20:34 -05:00
dokku_log_warn_quiet() {
declare desc="log warning formatter"
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
echo " ! $*" 1>&2
fi
}
2015-02-07 10:57:30 -08:00
dokku_log_warn() {
2016-03-08 15:30:34 -05:00
declare desc="log warning formatter"
2017-03-13 00:16:32 -06:00
echo " ! $*" 1>&2
2015-02-05 11:32:46 -08:00
}
2019-01-11 11:21:56 -05:00
dokku_log_exit_quiet() {
declare desc="log exit formatter"
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
echo "$@" 1>&2
fi
exit 0
}
dokku_log_exit() {
declare desc="log exit formatter"
2020-02-08 15:57:01 -05:00
echo "$@" 1>&2
2019-01-11 11:21:56 -05:00
exit 0
}
2018-11-05 01:20:34 -05:00
dokku_log_fail_quiet() {
declare desc="log fail formatter"
if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then
2021-02-13 02:31:01 -05:00
echo " ! $*" 1>&2
2018-11-05 01:20:34 -05:00
fi
2022-04-16 02:44:51 -04:00
exit "${DOKKU_FAIL_EXIT_CODE:=1}"
2018-11-05 01:20:34 -05:00
}
2015-02-07 10:57:30 -08:00
dokku_log_fail() {
2016-03-08 15:30:34 -05:00
declare desc="log fail formatter"
2021-02-13 02:31:01 -05:00
echo " ! $*" 1>&2
2022-03-12 06:23:32 -05:00
exit "${DOKKU_FAIL_EXIT_CODE:=1}"
2015-02-05 11:32:46 -08:00
}
2020-02-08 15:56:45 -05:00
dokku_log_stderr() {
declare desc="log stderr formatter"
echo "$@" 1>&2
}
2015-06-26 13:14:04 +01:00
dokku_log_event() {
2016-03-08 15:30:34 -05:00
declare desc="log dokku events"
2019-07-17 17:59:38 -04:00
logger -t dokku-event -i -- "$@"
2015-06-26 13:14:04 +01:00
}
2015-09-07 03:55:37 -04:00
dokku_log_plugn_trigger_call() {
2016-03-08 15:30:34 -05:00
declare desc="log plugn trigger calls"
2015-06-26 13:14:04 +01:00
2019-01-07 01:04:17 -05:00
local l_hook="$1"
shift
2021-01-02 05:23:12 -05:00
dokku_log_event "INVOKED: ${l_hook}( $* ) NAME=$NAME FINGERPRINT=$FINGERPRINT DOKKU_PID=$DOKKU_PID"
2015-06-26 13:14:04 +01:00
}
2015-03-20 08:29:35 -07:00
dokku_container_log_verbose_quiet() {
2016-03-08 15:30:34 -05:00
declare desc="log verbose container output (with quiet option)"
2019-01-07 01:04:17 -05:00
local CID=$1
2015-03-20 08:29:35 -07:00
shift
OIFS=$IFS
IFS=$'\n'
2016-03-02 10:50:09 -08:00
local line
2019-05-29 04:05:24 -04:00
for line in $("$DOCKER_BIN" container logs "$CID" 2>&1); do
2015-03-20 08:29:35 -07:00
dokku_log_verbose_quiet "$line"
done
IFS=$OIFS
}
2024-03-06 19:00:59 -05:00
fn-force-arm64-image() {
declare IMAGE="$1"
local image_architecture="$("$DOCKER_BIN" image inspect --format '{{.Architecture}}' "$IMAGE")"
if [[ "$image_architecture" == "amd64" ]] && [[ "$(dpkg --print-architecture 2>/dev/null || true)" != "amd64" ]]; then
return 0
fi
return 1
}
2021-01-09 07:40:45 -05:00
fn-is-valid-app-name() {
2020-12-21 18:35:09 -05:00
declare desc="verify that the app name matches naming restrictions"
2015-09-03 16:07:23 -07:00
local APP="$1"
2020-12-21 18:35:09 -05:00
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
2019-01-07 01:04:17 -05:00
if [[ "$APP" =~ ^[a-z].* ]] || [[ "$APP" =~ ^[0-9].* ]]; then
2020-09-09 20:06:53 -04:00
if [[ ! $APP =~ [A-Z] ]] && [[ ! $APP =~ [:] ]] && [[ ! $APP =~ [_] ]]; then
2018-02-13 03:30:27 -05:00
return 0
fi
2015-12-22 12:02:18 -08:00
fi
2018-02-13 03:30:27 -05:00
2021-01-09 07:40:45 -05:00
return 1
2017-08-19 19:16:16 -04:00
}
2021-01-09 07:40:45 -05:00
fn-is-valid-app-name-old() {
2020-12-21 18:35:09 -05:00
declare desc="verify that the app name matches the old naming restrictions"
local APP="$1"
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
if [[ "$APP" =~ ^[a-z].* ]] || [[ "$APP" =~ ^[0-9].* ]]; then
if [[ ! $APP =~ [A-Z] ]] && [[ ! $APP =~ [:] ]]; then
return 0
fi
fi
2021-01-09 07:40:45 -05:00
return 1
}
2023-08-19 12:07:06 -04:00
fn-plugn-trigger-exists() {
declare desc="checks if a plugn trigger exists"
declare TRIGGER_NAME="$1"
local exists
exists="$("$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet plugn-trigger-exists "$TRIGGER_NAME" || true)"
if [[ "$exists" == "true" ]]; then
return
fi
return 1
}
2021-01-09 07:40:45 -05:00
is_valid_app_name() {
declare desc="verify that the app name matches naming restrictions"
local APP="$1"
if fn-is-valid-app-name "$APP"; then
return
fi
dokku_log_fail "App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, colons, or underscores"
}
is_valid_app_name_old() {
declare desc="verify that the app name matches the old naming restrictions"
local APP="$1"
if fn-is-valid-app-name-old "$APP"; then
return
fi
2020-12-21 18:35:09 -05:00
dokku_log_fail "App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, or colons"
}
2017-08-19 19:16:16 -04:00
verify_app_name() {
declare desc="verify app name format and app existence"
2020-12-21 18:35:09 -05:00
declare APP="$1"
2022-05-16 00:56:18 -04:00
2022-05-29 11:28:34 -04:00
if "$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet verify-app-name "$APP"; then
2022-05-16 00:56:18 -04:00
return 0
fi
DOKKU_FAIL_EXIT_CODE=20 dokku_log_fail "App $APP does not exist"
2015-02-05 11:32:46 -08:00
}
2015-04-21 20:25:05 -07:00
verify_image() {
2016-03-08 15:30:34 -05:00
declare desc="verify image existence"
2015-04-21 20:25:05 -07:00
local IMAGE="$1"
2019-05-29 04:05:24 -04:00
if "$DOCKER_BIN" image inspect "$IMAGE" &>/dev/null; then
2015-04-18 15:39:08 -07:00
return 0
else
return 1
fi
}
2015-09-03 17:05:49 -07:00
get_app_image_repo() {
2016-03-08 15:30:34 -05:00
declare desc="central definition of image repo pattern"
2019-01-07 01:04:17 -05:00
local APP="$1"
local IMAGE_REPO="dokku/$APP"
2016-02-22 10:16:57 -08:00
echo "$IMAGE_REPO"
2015-09-03 17:05:49 -07:00
}
2016-04-14 19:10:03 -04:00
get_deploying_app_image_name() {
2016-07-29 04:51:36 -04:00
declare desc="return deploying image identifier for a given app, tag tuple. validate if tag is presented"
2019-01-07 01:04:17 -05:00
local APP="$1"
local IMAGE_TAG="$2"
2021-09-05 00:02:46 -04:00
local IMAGE_REPO="$3"
2016-04-14 19:10:03 -04:00
2021-09-05 00:02:46 -04:00
IMAGE_TAG="$(get_running_image_tag "$APP" "$IMAGE_TAG")"
2016-04-14 19:10:03 -04:00
local IMAGE_REMOTE_REPOSITORY=$(plugn trigger deployed-app-repository "$APP")
2016-07-05 02:34:13 -04:00
local NEW_IMAGE_REPO=$(plugn trigger deployed-app-image-repo "$APP")
2016-04-14 19:10:03 -04:00
2016-07-29 04:51:36 -04:00
[[ -n "$NEW_IMAGE_REPO" ]] && IMAGE_REPO="$NEW_IMAGE_REPO"
[[ -z "$IMAGE_REPO" ]] && IMAGE_REPO=$(get_app_image_repo "$APP")
2016-07-05 02:34:13 -04:00
2016-07-29 04:51:36 -04:00
local IMAGE="${IMAGE_REMOTE_REPOSITORY}${IMAGE_REPO}:${IMAGE_TAG}"
2018-04-03 01:21:50 -04:00
verify_image "$IMAGE" || dokku_log_fail "App image ($IMAGE) not found"
2016-04-14 19:10:03 -04:00
echo "$IMAGE"
}
2015-09-03 17:05:49 -07:00
get_app_image_name() {
2016-03-08 15:30:34 -05:00
declare desc="return image identifier for a given app, tag tuple. validate if tag is presented"
2019-01-07 01:04:17 -05:00
local APP="$1"
local IMAGE_TAG="$2"
IMAGE_REPO="$3"
2015-09-03 16:07:23 -07:00
2016-07-05 02:34:13 -04:00
if [[ -z "$IMAGE_REPO" ]]; then
IMAGE_REPO=$(get_app_image_repo "$APP")
fi
2015-09-03 16:07:23 -07:00
if [[ -n "$IMAGE_TAG" ]]; then
2016-03-02 10:50:09 -08:00
local IMAGE="$IMAGE_REPO:$IMAGE_TAG"
2018-04-03 01:21:50 -04:00
verify_image "$IMAGE" || dokku_log_fail "App image ($IMAGE) not found"
2015-09-03 16:07:23 -07:00
else
2016-03-02 10:50:09 -08:00
local IMAGE="$IMAGE_REPO:latest"
2015-09-03 16:07:23 -07:00
fi
2016-02-22 10:16:57 -08:00
echo "$IMAGE"
2015-09-03 16:07:23 -07:00
}
2018-09-30 21:36:53 -04:00
get_app_scheduler() {
declare desc="fetch the scheduler for a given application"
declare APP="$1"
2021-10-09 17:44:35 -04:00
"$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet scheduler-detect "$APP"
2018-09-30 21:36:53 -04:00
}
2015-06-10 14:21:47 -07:00
get_running_image_tag() {
2021-09-05 00:02:46 -04:00
declare desc="retrieves current deployed image tag for a given app"
local APP="$1" IMAGE_TAG="$2"
2015-06-10 14:21:47 -07:00
2021-09-05 00:02:46 -04:00
local NEW_IMAGE_TAG=$(plugn trigger deployed-app-image-tag "$APP")
[[ -n "$NEW_IMAGE_TAG" ]] && IMAGE_TAG="$NEW_IMAGE_TAG"
[[ -z "$IMAGE_TAG" ]] && IMAGE_TAG="latest"
echo "$IMAGE_TAG"
2015-06-10 14:21:47 -07:00
}
2022-08-09 02:11:32 -04:00
get_image_builder_type() {
declare desc="returns the image builder_type"
declare IMAGE="$1" APP="$2"
if [[ -z "$IMAGE" ]]; then
echo "dockerfile"
return
fi
BUILDER_TYPE="$("$DOCKER_BIN" image inspect --format '{{ index .Config.Labels "com.dokku.builder-type" }}' "$IMAGE")"
if [[ -n "$BUILDER_TYPE" ]]; then
echo "$BUILDER_TYPE"
return
fi
if is_image_herokuish_based "$IMAGE" "$APP"; then
echo "herokuish"
return
fi
echo "dockerfile"
}
2020-12-01 14:49:39 -05:00
is_image_cnb_based() {
declare desc="returns true if app image is based on cnb"
declare IMAGE="$1"
2022-12-02 15:54:48 -05:00
if [[ "$("$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet image-is-cnb-based "$IMAGE")" != "true" ]]; then
return 1
2020-12-01 14:49:39 -05:00
fi
}
2015-04-20 16:32:18 -07:00
is_image_herokuish_based() {
2020-12-01 14:49:39 -05:00
declare desc="returns true if app image is based on herokuish or a buildpack-like env"
2020-02-10 01:40:30 -05:00
declare IMAGE="$1" APP="$2"
fix: Only override the `WORKDIR` in copy_from_image if the image is `gliderlabs/herokuish` based
Rather than always assuming a missing `WORKDIR` means herokuish, we instead inspect the image to verify that it is. If it is, then and _only_ then do we set WORKDIR. Otherwise, we respect the decision of `docker cp` to execute from within the last known `WORKDIR` context, which is inherited at the image level, not image metadata level.
Additionally, speed up `is_image_herokuish_based` by inspecting the environment variables on the image. When there is a "USER=herokuishuser", we can more or less safely assume it is an image that Dokku built, and is therefore a herokuish image. While possible, it would be very strange if a non-herokuish image had this environment variable set, so it is a relatively safe change.
2019-03-05 23:28:59 -05:00
2022-12-02 15:54:48 -05:00
if [[ "$("$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet image-is-herokuish-based "$IMAGE" "$APP")" != "true" ]]; then
2019-05-12 07:37:21 -04:00
return 1
fi
2015-02-07 10:47:04 -08:00
}
2016-09-22 09:33:15 -07:00
get_docker_version() {
2019-05-29 04:05:24 -04:00
CLIENT_VERSION_STRING="$("$DOCKER_BIN" version --format "{{ .Client.Version }}")"
2016-09-22 09:33:15 -07:00
echo "$CLIENT_VERSION_STRING"
}
2022-09-07 10:53:49 -04:00
fn-is-compose-installed() {
declare desc="check if the compose docker plugin is installed"
local COMPOSE_INSTALLED
2022-11-07 10:07:48 -05:00
COMPOSE_INSTALLED="$(docker info -f '{{range .ClientInfo.Plugins}}{{if eq .Name "compose"}}true{{end}}{{end}}')"
2022-09-07 10:53:49 -04:00
if [[ "$COMPOSE_INSTALLED" == "true" ]]; then
return 0
fi
return 1
}
2015-04-18 15:39:08 -07:00
is_number() {
2016-03-08 15:30:34 -05:00
declare desc="returns 0 if input is a number"
2019-01-07 01:04:17 -05:00
local NUMBER=$1
local NUM_RE='^[0-9]+$'
2015-09-14 21:20:35 -07:00
if [[ $NUMBER =~ $NUM_RE ]]; then
2015-04-18 15:39:08 -07:00
return 0
else
return 1
fi
}
2016-02-14 15:37:24 -05:00
is_abs_path() {
2016-03-08 15:30:34 -05:00
declare desc="returns 0 if input path is absolute"
2016-02-14 15:37:24 -05:00
local TEST_PATH=$1
2016-03-02 10:50:09 -08:00
if [[ "$TEST_PATH" == /* ]]; then
2019-01-07 01:04:17 -05:00
return 0
else
return 1
2016-02-14 15:37:24 -05:00
fi
}
2015-02-07 10:57:30 -08:00
parse_args() {
2016-03-08 15:30:34 -05:00
declare desc="top-level cli arg parser"
2019-01-07 01:04:17 -05:00
local next_index=1
local skip=false
local args=("$@")
2019-09-16 23:07:01 -04:00
local flags
2015-02-05 11:32:46 -08:00
for arg in "$@"; do
2017-01-09 02:13:52 -07:00
if [[ "$skip" == "true" ]]; then
2019-01-07 01:04:17 -05:00
next_index=$((next_index + 1))
2017-01-09 02:13:52 -07:00
skip=false
continue
fi
2015-02-05 11:32:46 -08:00
case "$arg" in
2015-02-09 16:59:07 -08:00
--quiet)
2015-02-05 11:32:46 -08:00
export DOKKU_QUIET_OUTPUT=1
;;
2015-02-09 16:59:07 -08:00
--trace)
2015-02-05 11:32:46 -08:00
export DOKKU_TRACE=1
;;
2015-02-09 16:59:07 -08:00
--force)
2015-02-05 11:32:46 -08:00
export DOKKU_APPS_FORCE_DELETE=1
;;
2016-01-10 14:05:33 -05:00
--app)
2019-01-07 01:04:17 -05:00
export DOKKU_APP_NAME=${args[$next_index]}
skip=true
2016-01-10 14:05:33 -05:00
;;
2015-02-05 11:32:46 -08:00
esac
2019-09-16 23:07:01 -04:00
if [[ "$skip" == "true" ]]; then
flags="${flags} ${arg}"
elif [[ "$arg" == "--app" ]]; then
flags="${flags} ${arg}=${args[$next_index]}"
elif [[ "$arg" =~ ^--.* ]]; then
flags="${flags} ${arg}"
fi
2019-01-07 01:04:17 -05:00
next_index=$((next_index + 1))
2015-02-05 11:32:46 -08:00
done
2019-09-16 23:07:01 -04:00
if [[ -z "$DOKKU_GLOBAL_FLAGS" ]]; then
export DOKKU_GLOBAL_FLAGS="$(echo -e "${flags}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
fi
2015-02-05 11:32:46 -08:00
return 0
}
2015-04-17 15:23:59 -07:00
copy_from_image() {
2016-03-08 15:30:34 -05:00
declare desc="copy file from named image to destination"
2019-08-01 19:33:52 -04:00
declare IMAGE="$1" SRC_FILE="$2" DST_FILE="$3"
2015-04-17 15:23:59 -07:00
2024-12-16 01:47:00 -05:00
if ! "$PLUGIN_CORE_AVAILABLE_PATH/common/common" copy-from-image "$APP" "$IMAGE" "$SRC_FILE" "$DST_FILE"; then
2015-04-18 10:37:17 -07:00
return 1
fi
2015-04-17 15:23:59 -07:00
}
2015-04-17 18:57:37 -07:00
2023-08-01 02:16:11 -04:00
copy_dir_from_image() {
declare desc="copy a directory from named image to destination"
declare IMAGE="$1" SRC_DIR="$2" DST_DIR="$3"
2025-07-23 17:55:45 -04:00
if ! "$PLUGIN_CORE_AVAILABLE_PATH/common/common" copy-dir-from-image "$APP" "$IMAGE" "$SRC_DIR" "$DST_DIR"; then
2023-08-01 02:16:11 -04:00
return 1
fi
}
2015-07-13 08:24:46 -07:00
get_app_container_ids() {
2016-06-28 20:32:04 -07:00
declare desc="returns list of docker container ids for given app and optional container_type"
2019-01-07 01:04:17 -05:00
local APP="$1"
local CONTAINER_TYPE="$2"
[[ -f $DOKKU_ROOT/$APP/CONTAINER ]] && DOKKU_CIDS+=$(<"$DOKKU_ROOT/$APP/CONTAINER")
2015-04-17 18:57:37 -07:00
2015-08-25 23:00:49 -04:00
if [[ -n "$CONTAINER_TYPE" ]]; then
2016-03-02 10:50:09 -08:00
local CONTAINER_PATTERN="$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE.*"
2015-08-25 23:00:49 -04:00
if [[ $CONTAINER_TYPE == *.* ]]; then
2016-03-02 10:50:09 -08:00
local CONTAINER_PATTERN="$DOKKU_ROOT/$APP/CONTAINER.$CONTAINER_TYPE"
2015-08-25 23:00:49 -04:00
[[ ! -f $CONTAINER_PATTERN ]] && echo "" && return 0
fi
else
2016-03-02 10:50:09 -08:00
local CONTAINER_PATTERN="$DOKKU_ROOT/$APP/CONTAINER.*"
2015-08-25 23:00:49 -04:00
fi
2015-04-17 18:57:37 -07:00
shopt -s nullglob
2016-03-02 10:50:09 -08:00
local DOKKU_CID_FILE
2015-08-25 23:00:49 -04:00
for DOKKU_CID_FILE in $CONTAINER_PATTERN; do
2016-03-02 10:50:09 -08:00
local DOKKU_CIDS+=" "
2019-01-07 01:04:17 -05:00
local DOKKU_CIDS+=$(<"$DOKKU_CID_FILE")
2016-03-02 10:50:09 -08:00
local DOKKU_CIDS+=" "
2015-04-17 18:57:37 -07:00
done
shopt -u nullglob
2016-02-22 10:16:57 -08:00
echo "$DOKKU_CIDS"
2015-04-17 18:57:37 -07:00
}
2015-07-12 15:56:52 -07:00
2015-07-13 08:24:46 -07:00
get_app_running_container_ids() {
2016-06-28 20:32:04 -07:00
declare desc="return list of running docker container ids for given app and optional container_type"
local APP="$1" CONTAINER_TYPE="$2"
2017-03-26 05:03:16 -06:00
local CIDS
2015-07-13 08:24:46 -07:00
2016-02-22 10:16:57 -08:00
! (is_deployed "$APP") && dokku_log_fail "App $APP has not been deployed"
2017-03-26 05:03:16 -06:00
CIDS=$(get_app_container_ids "$APP" "$CONTAINER_TYPE")
2015-07-13 08:24:46 -07:00
2015-09-14 21:20:35 -07:00
for CID in $CIDS; do
2018-02-12 02:21:34 -05:00
(is_container_status "$CID" "Running") && local APP_RUNNING_CONTAINER_IDS+="$CID "
2015-07-13 08:24:46 -07:00
done
echo "$APP_RUNNING_CONTAINER_IDS"
}
2016-05-11 11:52:29 -07:00
get_app_running_container_types() {
declare desc="return list of running container types for given app"
local APP=$1 CONTAINER_TYPES
! (is_deployed "$APP") && dokku_log_fail "App $APP has not been deployed"
2016-08-26 13:46:05 -07:00
CONTAINER_TYPES="$(find "$DOKKU_ROOT/$APP" -maxdepth 1 -name "CONTAINER.*" -print0 2>/dev/null | xargs -0)"
2016-05-11 11:52:29 -07:00
if [[ -n "$CONTAINER_TYPES" ]]; then
CONTAINER_TYPES="${CONTAINER_TYPES//$DOKKU_ROOT\/$APP\//}"
2019-01-07 01:04:17 -05:00
CONTAINER_TYPES="$(tr " " $'\n' <<<"$CONTAINER_TYPES" | awk -F. '{ print $2 }' | sort | uniq | xargs)"
2016-05-11 11:52:29 -07:00
fi
echo "$CONTAINER_TYPES"
}
2015-07-13 08:24:46 -07:00
is_deployed() {
2016-03-08 15:30:34 -05:00
declare desc="return 0 if given app has a running container"
2015-09-03 18:55:52 -07:00
local APP="$1"
2019-05-10 17:02:26 -04:00
2021-02-13 00:46:35 -05:00
"$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet is-deployed "$APP"
2015-07-13 08:24:46 -07:00
}
2016-07-29 04:51:36 -04:00
is_container_status() {
2016-03-08 15:30:34 -05:00
declare desc="return 0 if given docker container id is in given state"
2015-08-29 02:31:03 -04:00
local CID=$1
local TEMPLATE="{{.State.$2}}"
2019-05-29 04:05:24 -04:00
local CONTAINER_STATUS=$("$DOCKER_BIN" container inspect --format "$TEMPLATE" "$CID" 2>/dev/null || true)
2015-08-29 02:31:03 -04:00
if [[ "$CONTAINER_STATUS" == "true" ]]; then
return 0
fi
2018-04-28 01:27:34 -04:00
return 1
2015-08-29 02:31:03 -04:00
}
2016-06-17 16:40:35 -07:00
dokku_build() {
declare desc="build phase"
2019-07-30 14:29:48 -04:00
declare APP="$1" IMAGE_SOURCE_TYPE="$2" SOURCECODE_WORK_DIR="$3"
2016-06-17 16:40:35 -07:00
2022-03-12 06:25:12 -05:00
local IMAGE=$(get_app_image_name "$APP")
local RELEASED_IMAGE_ID="$(docker image ls --filter "label=com.dokku.image-stage=release" --filter "label=com.dokku.app-name=$APP" --format "{{.ID}}")"
if plugn trigger builder-build "$IMAGE_SOURCE_TYPE" "$APP" "$SOURCECODE_WORK_DIR"; then
return
fi
if [[ -n "$RELEASED_IMAGE_ID" ]]; then
dokku_log_warn "Retagging old image $RELEASED_IMAGE_ID as $IMAGE"
2022-03-12 19:22:17 -05:00
"$DOCKER_BIN" image tag "$RELEASED_IMAGE_ID" "$IMAGE" 2>/dev/null || true
2022-03-12 06:25:12 -05:00
else
dokku_log_warn "Removing invalid image tag $IMAGE"
"$DOCKER_BIN" image remove --force --no-prune "$IMAGE" &>/dev/null || true
fi
dokku_log_fail "App build failed"
2016-06-17 16:40:35 -07:00
}
dokku_release() {
declare desc="release phase"
2019-06-29 15:33:24 -04:00
declare APP="$1" IMAGE_SOURCE_TYPE="$2" IMAGE_TAG="$3"
2020-12-01 14:49:39 -05:00
local IMAGE=$(get_app_image_name "$APP" "$IMAGE_TAG")
if is_image_cnb_based "$IMAGE"; then
2021-02-28 16:02:22 -05:00
IMAGE_SOURCE_TYPE="pack"
2020-12-01 14:49:39 -05:00
fi
2019-07-30 14:29:48 -04:00
plugn trigger builder-release "$IMAGE_SOURCE_TYPE" "$APP" "$IMAGE_TAG"
2016-06-17 16:40:35 -07:00
}
2020-02-09 22:41:39 -05:00
cmd-deploy() {
2016-06-17 16:40:35 -07:00
declare desc="deploy phase"
2021-09-05 00:02:46 -04:00
declare APP="$1" IMAGE_TAG="$2" PROCESS_TYPE="$3"
2016-06-17 16:40:35 -07:00
2020-12-25 01:45:35 -05:00
verify_app_name "$APP"
2018-09-30 21:36:53 -04:00
local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
2021-09-05 00:02:46 -04:00
plugn trigger scheduler-deploy "$DOKKU_SCHEDULER" "$APP" "$IMAGE_TAG" "$PROCESS_TYPE"
2016-06-17 16:40:35 -07:00
}
2015-06-10 14:21:47 -07:00
release_and_deploy() {
2016-03-08 15:30:34 -05:00
declare desc="main function for releasing and deploying an app"
2016-03-02 10:50:09 -08:00
source "$PLUGIN_AVAILABLE_PATH/config/functions"
2019-01-07 01:04:17 -05:00
local APP="$1"
2021-08-05 04:16:49 -04:00
local IMAGE_TAG="${2:-latest}"
2019-01-07 01:04:17 -05:00
local IMAGE=$(get_app_image_name "$APP" "$IMAGE_TAG")
2017-03-26 05:03:16 -06:00
2015-06-10 14:21:47 -07:00
if verify_image "$IMAGE"; then
2022-08-09 02:11:32 -04:00
IMAGE_SOURCE_TYPE="$(get_image_builder_type "$IMAGE" "$APP")"
2016-03-02 10:50:09 -08:00
local DOKKU_APP_SKIP_DEPLOY="$(config_get "$APP" DOKKU_SKIP_DEPLOY || true)"
local DOKKU_GLOBAL_SKIP_DEPLOY="$(config_get --global DOKKU_SKIP_DEPLOY || true)"
2015-12-17 21:07:58 +01:00
2016-03-01 13:26:35 -08:00
local DOKKU_SKIP_DEPLOY=${DOKKU_APP_SKIP_DEPLOY:="$DOKKU_GLOBAL_SKIP_DEPLOY"}
2015-12-17 21:07:58 +01:00
2020-05-05 23:39:38 -04:00
dokku_log_info1 "Releasing $APP..."
2016-06-17 16:40:35 -07:00
dokku_release "$APP" "$IMAGE_SOURCE_TYPE" "$IMAGE_TAG"
2015-12-17 21:07:58 +01:00
if [[ "$DOKKU_SKIP_DEPLOY" != "true" ]]; then
2021-10-09 17:44:35 -04:00
local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
dokku_log_info1 "Deploying $APP via the $DOKKU_SCHEDULER scheduler..."
2020-02-09 22:41:39 -05:00
cmd-deploy "$APP" "$IMAGE_TAG"
2015-12-17 21:07:58 +01:00
dokku_log_info2 "Application deployed:"
2022-12-02 02:19:59 -05:00
plugn trigger domains-urls "$APP" urls | sed "s/^/ /"
2015-12-17 21:07:58 +01:00
else
dokku_log_info1 "Skipping deployment"
fi
2015-06-10 14:21:47 -07:00
echo
fi
}
2015-09-06 19:21:12 -04:00
2016-06-17 16:40:35 -07:00
dokku_receive() {
declare desc="receives an app kicks off deploy process"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
2019-01-07 01:04:17 -05:00
local APP="$1"
local IMAGE=$(get_app_image_name "$APP")
local IMAGE_SOURCE_TYPE="$2"
local TMP_WORK_DIR="$3"
2020-12-01 14:49:39 -05:00
2017-07-02 16:54:26 -06:00
docker_cleanup "$APP"
2020-12-01 14:49:39 -05:00
2025-08-21 23:57:58 -04:00
plugn trigger builder-set-property "$APP" "detected" "$IMAGE_SOURCE_TYPE"
2022-04-16 02:44:51 -04:00
if ! dokku_build "$APP" "$IMAGE_SOURCE_TYPE" "$TMP_WORK_DIR"; then
return 1
fi
2020-10-10 17:32:21 -04:00
plugn trigger release-and-deploy "$APP"
2016-06-17 16:40:35 -07:00
}
2015-09-06 19:21:12 -04:00
docker_cleanup() {
2016-03-08 15:30:34 -05:00
declare desc="cleans up all exited/dead containers and removes all dangling images"
2017-07-02 16:54:26 -06:00
declare APP="$1" FORCE_CLEANUP="$2"
local DOKKU_APP_SKIP_CLEANUP
2019-07-18 04:28:45 -04:00
2021-08-08 01:56:43 -04:00
"$PLUGIN_CORE_AVAILABLE_PATH/common/common" --quiet docker-cleanup "$APP"
2015-09-06 19:21:12 -04:00
}
2015-09-17 19:09:29 -07:00
2015-11-08 15:36:04 -05:00
dokku_auth() {
2016-03-08 15:30:34 -05:00
declare desc="calls user-auth plugin trigger"
2015-11-08 15:36:04 -05:00
export SSH_USER=${SSH_USER:=$USER}
export SSH_NAME=${NAME:="default"}
2022-07-17 17:58:37 -04:00
export DOKKU_COMMAND="$1"
2019-08-01 20:18:14 -04:00
2021-08-07 17:55:56 -04:00
local user_auth_count=$(find "$PLUGIN_PATH"/enabled/*/user-auth 2>/dev/null | wc -l)
# no plugin trigger exists
if [[ $user_auth_count == 0 ]]; then
return 0
fi
2019-08-01 20:18:14 -04:00
# this plugin trigger exists in the core `20_events` plugin
2021-08-07 17:55:56 -04:00
if [[ "$user_auth_count" == 1 ]] && [[ -f "$PLUGIN_PATH"/enabled/20_events/user-auth ]]; then
2019-08-01 20:18:14 -04:00
return 0
fi
2019-01-07 01:04:17 -05:00
if ! plugn trigger user-auth "$SSH_USER" "$SSH_NAME" "$@"; then
2015-11-08 15:36:04 -05:00
return 1
fi
return 0
}
2015-11-19 09:10:04 -05:00
_ipv4_regex() {
2016-03-08 15:30:34 -05:00
declare desc="ipv4 regex"
2015-11-19 09:10:04 -05:00
echo "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
}
_ipv6_regex() {
2016-03-08 15:30:34 -05:00
declare desc="ipv6 regex"
2015-11-19 09:10:04 -05:00
local RE_IPV4="$(_ipv4_regex)"
2019-01-07 01:04:17 -05:00
local RE_IPV6="([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|" # TEST: 1:2:3:4:5:6:7:8
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,7}:|" # TEST: 1:: 1:2:3:4:5:6:7::
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|" # TEST: 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|" # TEST: 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|" # TEST: 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|" # TEST: 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|" # TEST: 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
local RE_IPV6="${RE_IPV6}[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|" # TEST: 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
local RE_IPV6="${RE_IPV6}:((:[0-9a-fA-F]{1,4}){1,7}|:)|" # TEST: ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
local RE_IPV6="${RE_IPV6}fe08:(:[0-9a-fA-F]{1,4}){2,2}%[0-9a-zA-Z]{1,}|" # TEST: fe08::7:8%eth0 fe08::7:8%1 (link-local IPv6 addresses with zone index)
local RE_IPV6="${RE_IPV6}::(ffff(:0{1,4}){0,1}:){0,1}${RE_IPV4}|" # TEST: ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255 (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
local RE_IPV6="${RE_IPV6}([0-9a-fA-F]{1,4}:){1,4}:${RE_IPV4}" # TEST: 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33
2015-11-19 09:10:04 -05:00
echo "$RE_IPV6"
}
get_ipv4_regex() {
2016-03-08 15:30:34 -05:00
declare desc="returns ipv4 regex"
2015-11-19 09:10:04 -05:00
local RE_IPV4="$(_ipv4_regex)"
# Ensure the ip address continues to the end of the line
2021-08-05 23:29:43 -04:00
# Fixes using a wildcard dns service such as sslip.io which allows for *.<ip address>.sslip.io
2015-11-19 09:10:04 -05:00
echo "${RE_IPV4}\$"
}
get_ipv6_regex() {
2016-03-08 15:30:34 -05:00
declare desc="returns ipv6 regex"
2016-01-14 23:30:10 +09:00
local RE_IPV6="$(_ipv6_regex)"
2015-11-19 09:10:04 -05:00
# Ensure the ip address continues to the end of the line
2021-08-05 23:29:43 -04:00
# Fixes using a wildcard dns service such as sslip.io which allows for *.<ip address>.sslip.io
2015-11-19 09:10:04 -05:00
echo "${RE_IPV6}\$"
}
2016-02-14 18:43:40 -08:00
2016-05-06 11:30:22 -07:00
get_entrypoint_from_image() {
declare desc="return .Config.Entrypoint from passed image name"
2019-01-07 01:04:17 -05:00
local IMAGE="$1"
verify_image "$IMAGE"
2019-05-29 04:05:24 -04:00
local DOCKER_IMAGE_ENTRYPOINT="$("$DOCKER_BIN" image inspect --format '{{range .Config.Entrypoint}}{{.}} {{end}}' "$IMAGE")"
2016-05-06 11:30:22 -07:00
echo "ENTRYPOINT $DOCKER_IMAGE_ENTRYPOINT"
}
get_cmd_from_image() {
declare desc="return .Config.Cmd from passed image name"
2019-01-07 01:04:17 -05:00
local IMAGE="$1"
verify_image "$IMAGE"
2019-05-29 04:05:24 -04:00
local DOCKER_IMAGE_CMD="$("$DOCKER_BIN" image inspect --format '{{range .Config.Cmd}}{{.}} {{end}}' "$IMAGE")"
2016-05-06 11:30:22 -07:00
DOCKER_IMAGE_CMD="${DOCKER_IMAGE_CMD/\/bin\/sh -c/}"
echo "CMD $DOCKER_IMAGE_CMD"
}
2016-04-01 13:47:22 -07:00
extract_directive_from_dockerfile() {
declare desc="return requested directive from passed file path"
2019-01-07 01:04:17 -05:00
local FILE_PATH="$1"
local SEARCH_STRING="$2"
2020-02-17 05:59:52 -05:00
local FOUND_LINE=$(grep -E "^${SEARCH_STRING} " "$FILE_PATH" | tail -n1) || true
2016-04-01 13:47:22 -07:00
echo "$FOUND_LINE"
}
2016-02-14 18:43:40 -08:00
get_container_ports() {
2016-03-08 15:30:34 -05:00
declare desc="returns published ports from app containers"
2019-01-07 01:04:17 -05:00
local APP="$1"
2016-02-22 10:16:57 -08:00
local APP_CIDS="$(get_app_container_ids "$APP")"
2016-02-14 18:43:40 -08:00
local cid
for cid in $APP_CIDS; do
2019-05-29 04:05:24 -04:00
local container_ports="$("$DOCKER_BIN" container port "$cid" | awk '{ print $3 "->" $1}' | awk -F ":" '{ print $2 }')"
2016-02-14 18:43:40 -08:00
done
2016-02-22 10:16:57 -08:00
echo "$container_ports"
2016-02-14 18:43:40 -08:00
}
2016-01-06 18:18:57 -08:00
get_json_value() {
2016-03-08 15:30:34 -05:00
declare desc="return value of provided json key from a json stream on stdin"
2016-01-06 18:18:57 -08:00
# JSON_NODE should be expresses as either a top-level object that has no children
2019-09-17 03:06:00 -04:00
# or in the format of .json.node.path
2016-01-06 18:18:57 -08:00
local JSON_NODE="$1"
2019-09-17 03:06:00 -04:00
cat | jq -r "${JSON_NODE} | select (.!=null)" 2>/dev/null
2016-01-06 18:18:57 -08:00
}
2016-03-29 16:14:08 -07:00
strip_inline_comments() {
declare desc="removes bash-style comment from input line"
local line="$1"
local stripped_line="${line%[[:space:]]#*}"
echo "$stripped_line"
}
2016-06-09 16:17:45 -07:00
is_val_in_list() {
declare desc="return true if value ($1) is in list ($2) separated by delimiter ($3); delimiter defaults to comma"
local value="$1" list="$2" delimiter="${3:-,}"
local IFS="$delimiter" val_in_list=false
for val in $list; do
if [[ "$val" == "$value" ]]; then
val_in_list=true
fi
done
echo "$val_in_list"
}
2016-05-09 16:03:07 -07:00
2019-12-19 02:15:18 -05:00
fn-in-array() {
declare desc="return true if value ($1) is in list (all other arguments)"
local e
for e in "${@:2}"; do
[[ "$e" == "$1" ]] && return 0
done
return 1
}
2016-05-09 16:03:07 -07:00
remove_val_from_list() {
declare desc="remove value ($1) from list ($2) separated by delimiter ($3) (delimiter defaults to comma) and return list"
local value="$1" list="$2" delimiter="${3:-,}"
list="${list//$value/}"
list="${list//$delimiter$delimiter/$delimiter}"
list="${list/#$delimiter/}"
list="${list/%$delimiter/}"
echo "$list"
}
add_val_to_list() {
declare desc="add value ($1) to list ($2) separated by delimiter ($3) (delimiter defaults to comma) and return list"
local value="$1" list="$2" delimiter="${3:-,}"
list+="${delimiter}$value"
echo "$list"
}
2016-06-14 17:36:26 -07:00
merge_dedupe_list() {
declare desc="combine lists ($1) separated by delimiter ($2) (delimiter defaults to comma), dedupe and return list"
local input_lists="$1" delimiter="${2:-,}"
2019-01-07 01:04:17 -05:00
local merged_list="$(tr "$delimiter" $'\n' <<<"$input_lists" | sort | uniq | xargs)"
2016-06-14 17:36:26 -07:00
echo "$merged_list"
}
2016-06-17 15:25:46 -07:00
acquire_app_deploy_lock() {
2021-10-09 18:43:53 -04:00
declare desc="acquire advisory lock for use in deploys"
2019-01-07 01:04:17 -05:00
local APP="$1"
2016-06-17 16:10:51 -07:00
local LOCK_TYPE="${2:-waiting}"
2024-02-09 14:38:43 -05:00
local APP_DEPLOY_LOCK_FILE="$DOKKU_LIB_ROOT/data/apps/$APP/.deploy.lock"
2018-03-04 01:04:21 -05:00
local LOCK_WAITING_MSG="$APP currently has a deploy lock in place. Waiting..."
local LOCK_FAILED_MSG="$APP currently has a deploy lock in place. Exiting..."
2018-07-20 02:53:17 -04:00
acquire_advisory_lock "$APP_DEPLOY_LOCK_FILE" "$LOCK_TYPE" "$LOCK_WAITING_MSG" "$LOCK_FAILED_MSG"
}
release_app_deploy_lock() {
2021-10-09 18:43:53 -04:00
declare desc="release advisory lock used in deploys"
2019-01-07 01:04:17 -05:00
local APP="$1"
2024-02-09 14:38:43 -05:00
local APP_DEPLOY_LOCK_FILE="$DOKKU_LIB_ROOT/data/apps/$APP/.deploy.lock"
2018-07-20 02:53:17 -04:00
release_advisory_lock "$APP_DEPLOY_LOCK_FILE"
}
acquire_advisory_lock() {
declare desc="acquire advisory lock"
local LOCK_FILE="$1" LOCK_TYPE="$2" LOCK_WAITING_MSG="$3" LOCK_FAILED_MSG="$4"
local LOCK_FD="200"
2016-06-17 15:25:46 -07:00
local SHOW_MSG=true
2018-07-26 10:09:02 -04:00
eval "exec $LOCK_FD>$LOCK_FILE"
2016-06-17 16:10:51 -07:00
if [[ "$LOCK_TYPE" == "waiting" ]]; then
2019-01-07 01:04:17 -05:00
while [[ $(
flock -n "$LOCK_FD" &>/dev/null
echo $?
) -ne 0 ]]; do
2016-06-17 16:10:51 -07:00
if [[ "$SHOW_MSG" == "true" ]]; then
echo "$LOCK_WAITING_MSG"
SHOW_MSG=false
fi
sleep 1
done
else
2021-07-11 21:33:32 -04:00
if ! flock -n "$LOCK_FD" &>/dev/null; then
dokku_log_warn "$LOCK_FAILED_MSG"
dokku_log_fail "Run 'apps:unlock' to release the existing deploy lock"
fi
2016-06-17 16:10:51 -07:00
fi
2024-03-14 05:54:37 -04:00
echo "$DOKKU_PID" >"$LOCK_FILE"
2016-06-17 15:25:46 -07:00
}
2018-07-20 02:53:17 -04:00
release_advisory_lock() {
declare desc="release advisory lock"
local LOCK_FILE="$1"
local LOCK_FD="200"
2016-06-17 15:25:46 -07:00
2019-01-07 01:04:17 -05:00
flock -u "$LOCK_FD" && rm -f "$LOCK_FILE" &>/dev/null
2016-06-17 15:25:46 -07:00
}
2018-03-05 23:47:10 -05:00
suppress_output() {
declare desc="suppress all output from a given command unless there is an error"
local TMP_COMMAND_OUTPUT
2019-08-12 18:16:16 -04:00
TMP_COMMAND_OUTPUT=$(mktemp "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
2018-03-05 23:47:10 -05:00
2019-01-07 01:04:17 -05:00
"$@" >"$TMP_COMMAND_OUTPUT" 2>&1 || {
2018-03-05 23:47:10 -05:00
local exit_code="$?"
cat "$TMP_COMMAND_OUTPUT"
2024-03-13 06:22:29 -04:00
rm -rf "$TMP_COMMAND_OUTPUT" >/dev/null
2018-03-05 23:47:10 -05:00
return "$exit_code"
}
2024-03-13 06:22:29 -04:00
rm -rf "$TMP_COMMAND_OUTPUT" >/dev/null
2018-03-05 23:47:10 -05:00
return 0
2018-04-10 14:12:10 -04:00
}
2022-11-15 23:42:36 -05:00
fn-migrate-config-to-property() {
declare desc="migrates deprecated config variables to property counterpart"
declare PLUGIN="$1" KEY="$2" CONFIG_VAR="$3" GLOBAL_CONFIG_VAR="$4"
# todo: refactor to remove config_unset usage
if ! declare -f -F config_unset >/dev/null; then
source "$PLUGIN_AVAILABLE_PATH/config/functions"
fi
if ! declare -f -F fn-plugin-property-write >/dev/null; then
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
fi
if [[ -n "$GLOBAL_CONFIG_VAR" ]]; then
local value=$(plugn trigger config-get-global "$GLOBAL_CONFIG_VAR" || true)
if [[ -n "$value" ]]; then
dokku_log_info1 "Migrating deprecated global $GLOBAL_CONFIG_VAR to $PLUGIN $KEY property."
fn-plugin-property-write "$PLUGIN" --global "$KEY" "$value"
DOKKU_QUIET_OUTPUT=1 config_unset --global "$GLOBAL_CONFIG_VAR" || true
fi
fi
for app in $(dokku_apps "false"); do
local value=$(plugn trigger config-get "$app" "$CONFIG_VAR" || true)
if [[ -n "$value" ]]; then
dokku_log_info1 "Migrating deprecated $CONFIG_VAR to $PLUGIN $KEY property for $app."
fn-plugin-property-write "$PLUGIN" "$app" "$KEY" "$value"
DOKKU_QUIET_OUTPUT=1 config_unset --no-restart "$app" "$CONFIG_VAR" || true
fi
done
2022-11-22 20:34:23 -05:00
}