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

View File

@ -416,10 +416,11 @@ max_existing_run_index() {
local run_name
local numeric_part
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
run_name="$(basename "$run_dir")"
numeric_part="${run_name#run_}"
[[ "$numeric_part" =~ ^[0-9]+$ ]] || continue
if (( 10#$numeric_part > max_index )); then
max_index=$((10#$numeric_part))
fi