fix periodic snapshot upgrade env merge

This commit is contained in:
yuyr 2026-06-29 16:59:00 +08:00
parent 4ca08a12a4
commit 8d15188ea8
2 changed files with 49 additions and 30 deletions

View File

@ -11,10 +11,11 @@ usage() {
cat <<'USAGE' cat <<'USAGE'
Usage: ./upgrade.sh [--reuse-env-from /path/to/.env] [--keep-reused-image] Usage: ./upgrade.sh [--reuse-env-from /path/to/.env] [--keep-reused-image]
By default, --reuse-env-from preserves user/runtime settings but refreshes image By default, --reuse-env-from creates the new .env from this package's
tags from this package's .env.example so the upgraded service actually runs the .env.example first, then overlays existing user settings from the old .env.
new packaged runtime and monitor images. Use --keep-reused-image only when you Image tags are intentionally kept from the new package so the upgraded service
intentionally want to keep the previous image tags. actually runs the new packaged runtime and monitor images. Use
--keep-reused-image only when you intentionally want to keep previous image tags.
USAGE USAGE
} }
@ -38,42 +39,59 @@ while [[ $# -gt 0 ]]; do
esac esac
done done
if [[ -n "$REUSE_ENV_FROM" ]]; then env_get_key() {
[[ -f "$REUSE_ENV_FROM" ]] || die "missing reuse env file: $REUSE_ENV_FROM" local env_path="$1"
if [[ ! -f "$ENV_FILE" ]]; then local key="$2"
cp "$REUSE_ENV_FROM" "$ENV_FILE" awk -F= -v key="$key" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_path"
log "copied existing env into new package: $REUSE_ENV_FROM -> $ENV_FILE" }
else
log "keeping existing env at $ENV_FILE; reuse source ignored: $REUSE_ENV_FROM"
fi
fi
refresh_env_key_from_example() { env_set_key() {
local key="$1" local env_path="$1"
local value local key="$2"
[[ -f "$ENV_EXAMPLE" ]] || die "missing $ENV_EXAMPLE" local value="$3"
value="$(awk -F= -v key="$key" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$ENV_EXAMPLE")" local tmp_env
[[ -n "$value" ]] || return 0 if grep -q "^${key}=" "$env_path"; then
if grep -q "^${key}=" "$ENV_FILE"; then
tmp_env="$(mktemp)" tmp_env="$(mktemp)"
awk -v key="$key" -v value="$value" ' awk -v key="$key" -v value="$value" '
BEGIN { done=0 } BEGIN { done=0 }
$0 ~ "^" key "=" { print key "=" value; done=1; next } $0 ~ "^" key "=" { print key "=" value; done=1; next }
{ print } { print }
END { if (!done) print key "=" value } END { if (!done) print key "=" value }
' "$ENV_FILE" > "$tmp_env" ' "$env_path" > "$tmp_env"
mv "$tmp_env" "$ENV_FILE" mv "$tmp_env" "$env_path"
else else
printf '%s=%s\n' "$key" "$value" >> "$ENV_FILE" printf '%s=%s\n' "$key" "$value" >> "$env_path"
fi fi
log "refreshed $key from packaged .env.example"
} }
if [[ "$UPDATE_PACKAGE_IMAGE" == "1" ]]; then overlay_reused_env() {
load_env local source_env="$1"
refresh_env_key_from_example "RPKI_IMAGE" local target_env="$2"
refresh_env_key_from_example "PROMETHEUS_IMAGE" local key
refresh_env_key_from_example "GRAFANA_IMAGE" local value
while IFS='=' read -r key _; do
[[ -n "$key" ]] || continue
[[ "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue
case "$key" in
RPKI_IMAGE|PROMETHEUS_IMAGE|GRAFANA_IMAGE)
[[ "$UPDATE_PACKAGE_IMAGE" == "0" ]] || continue
;;
esac
value="$(env_get_key "$source_env" "$key")"
env_set_key "$target_env" "$key" "$value"
done < <(grep -E '^[A-Za-z_][A-Za-z0-9_]*=' "$source_env" || true)
}
if [[ -n "$REUSE_ENV_FROM" ]]; then
[[ -f "$REUSE_ENV_FROM" ]] || die "missing reuse env file: $REUSE_ENV_FROM"
if [[ ! -f "$ENV_FILE" ]]; then
[[ -f "$ENV_EXAMPLE" ]] || die "missing $ENV_EXAMPLE"
cp "$ENV_EXAMPLE" "$ENV_FILE"
overlay_reused_env "$REUSE_ENV_FROM" "$ENV_FILE"
log "created new package env from .env.example and overlaid user settings from $REUSE_ENV_FROM"
else
log "keeping existing env at $ENV_FILE; reuse source ignored: $REUSE_ENV_FROM"
fi
fi fi
load_env load_env

View File

@ -416,10 +416,11 @@ max_existing_run_index() {
local run_name local run_name
local numeric_part local numeric_part
shopt -s nullglob shopt -s nullglob
for run_dir in "$RUNS_ROOT"/run_[0-9][0-9][0-9][0-9]; do for run_dir in "$RUNS_ROOT"/run_*; do
[[ -d "$run_dir" ]] || continue [[ -d "$run_dir" ]] || continue
run_name="$(basename "$run_dir")" run_name="$(basename "$run_dir")"
numeric_part="${run_name#run_}" numeric_part="${run_name#run_}"
[[ "$numeric_part" =~ ^[0-9]+$ ]] || continue
if (( 10#$numeric_part > max_index )); then if (( 10#$numeric_part > max_index )); then
max_index=$((10#$numeric_part)) max_index=$((10#$numeric_part))
fi fi