#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=scripts/common.sh source "$SCRIPT_DIR/scripts/common.sh" REUSE_ENV_FROM="" UPDATE_PACKAGE_IMAGE=1 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. USAGE } while [[ $# -gt 0 ]]; do case "$1" in --reuse-env-from) REUSE_ENV_FROM="$2" shift 2 ;; --keep-reused-image) UPDATE_PACKAGE_IMAGE=0 shift ;; -h|--help) usage exit 0 ;; *) die "unknown option: $1" ;; 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 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 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" else printf '%s=%s\n' "$key" "$value" >> "$ENV_FILE" 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" fi load_env create_data_dirs install_docker_if_missing load_installer_images ensure_binfmt_if_needed verify_runtime_image verify_monitor_images compose_cmd --profile core --profile sidecar --profile monitor up -d --force-recreate "$SCRIPT_DIR/status.sh" --brief || true