Files
dokku/plugins/openresty-vhosts/core-post-extract
T
Jose Diaz-Gonzalez 0690a9b655 fix: clean up stale extracted files on deploy
A failed deploy could leave a non-empty extraction directory behind, such as the scheduler-k3s `kustomization` directory, which caused every following deploy to fail. Leftovers from previous deploys are now fully removed, removing a directory such as `config/kustomize` from the repository now removes the stored copy, and openresty includes now persist across restarts.
2026-10-23 14:37:49 -04:00

101 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
fn-openresty-vhosts-copy-includes-from-source() {
declare desc="copy regular include files from the app repo; abort on unsafe filenames"
declare SRC_DIR="$1" DEST_DIR="$2"
local file filename
[[ -d "$SRC_DIR" ]] || return 0
for file in "$SRC_DIR"/*; do
[[ -e "$file" ]] || continue
filename="$(basename "$file")"
if [[ "$filename" =~ [^a-zA-Z0-9_.-] ]]; then
dokku_log_fail "Aborting deploy: include file has unsafe filename: $filename"
fi
[[ -L "$file" ]] && continue
[[ -f "$file" ]] || continue
cp -f "$file" "$DEST_DIR/"
done
}
fn-openresty-vhosts-sanitize-extracted-includes() {
declare desc="post-extract: abort on unsafe filenames; remove non-regular files"
declare TARGET_DIR="$1"
local file filename
[[ -d "$TARGET_DIR" ]] || return 0
for file in "$TARGET_DIR"/*; do
[[ -e "$file" ]] || continue
filename="$(basename "$file")"
if [[ "$filename" =~ [^a-zA-Z0-9_.-] ]]; then
dokku_log_fail "Aborting deploy: include file has unsafe filename: $filename"
fi
if [[ -L "$file" ]] || [[ ! -f "$file" ]]; then
rm -rf "$file"
fi
done
}
fn-openresty-vhosts-copy-from-image() {
declare APP="$1" IMAGE_NAME="$2"
local CONF_PATH="openresty/http-includes"
local LOCATION_CONF_PATH="openresty/http-location-includes"
copy_dir_from_image "$IMAGE_NAME" "$CONF_PATH" "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID" 2>/dev/null || true
fn-openresty-vhosts-sanitize-extracted-includes "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID"
if [[ ! -d "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID" ]]; then
touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID.missing"
fi
copy_dir_from_image "$IMAGE_NAME" "$LOCATION_CONF_PATH" "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID" 2>/dev/null || true
fn-openresty-vhosts-sanitize-extracted-includes "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID"
if [[ ! -d "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID" ]]; then
touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID.missing"
fi
}
fn-openresty-vhosts-copy-from-directory() {
declare APP="$1" SOURCECODE_WORK_DIR="$2"
local CONF_PATH="openresty/http-includes"
local LOCATION_CONF_PATH="openresty/http-location-includes"
pushd "$SOURCECODE_WORK_DIR" >/dev/null
if [[ -d "$CONF_PATH" ]]; then
mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID/"
fn-openresty-vhosts-copy-includes-from-source "$CONF_PATH" "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID"
else
touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID.missing"
fi
if [[ -d "$LOCATION_CONF_PATH" ]]; then
mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID/"
fn-openresty-vhosts-copy-includes-from-source "$LOCATION_CONF_PATH" "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID"
else
touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID.missing"
fi
popd &>/dev/null || pushd "/tmp" >/dev/null
}
trigger-openresty-vhosts-core-post-extract() {
declare desc="openresty-vhosts post-extract plugin trigger"
declare trigger="post-extract"
declare APP="$1" SOURCECODE_WORK_DIR="$2"
local app_source_image
mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP"
find "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/" -maxdepth 1 \( -name 'openresty-http-includes.*' -o -name 'openresty-location-includes.*' \) -exec rm -rf {} +
app_source_image="$(plugn trigger git-get-property "$APP" "source-image")"
if [[ -n "$app_source_image" ]]; then
fn-openresty-vhosts-copy-from-image "$APP" "$app_source_image"
else
fn-openresty-vhosts-copy-from-directory "$APP" "$SOURCECODE_WORK_DIR"
fi
}
trigger-openresty-vhosts-core-post-extract "$@"