#!/usr/bin/env bash # Run from a dedicated trusted clone on the Proxmox host. Always enter the fetched installer. set -euo pipefail set +x fail() { printf '%s\n' "FinalToken update: $*" >&2; exit 1; } usage() { printf '%s\n' 'Usage: bash /path/to/FinalToken/tools/proxmox-update.sh CTID [--dry-run]'; } if [[ ${1:-} == --help || ${1:-} == -h ]]; then usage exit 0 fi if [[ $# -lt 1 || $# -gt 2 || ! $1 =~ ^[1-9][0-9]{2,8}$ || ( $# == 2 && $2 != --dry-run ) ]]; then usage >&2 exit 2 fi ctid=$1 dry_run=${2:-} source_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P) for program in git bash; do command -v "$program" >/dev/null || fail "Missing host command: $program" done top=$(git -C "$source_dir" rev-parse --show-toplevel 2>/dev/null) || fail 'Use a dedicated Git clone of FinalToken.' [[ $(cd -- "$top" && pwd -P) == "$source_dir" ]] || fail 'Installer must be at the root of its own Git checkout.' git -C "$source_dir" symbolic-ref --quiet HEAD >/dev/null 2>&1 || fail 'Detached checkout; select the published branch first.' git -C "$source_dir" rev-parse --verify '@{upstream}' >/dev/null 2>&1 || fail 'The checked-out branch needs a tracking upstream.' clean_checkout() { local changes changes=$(GIT_OPTIONAL_LOCKS=0 git -C "$source_dir" status --porcelain=v1 --untracked-files=all) || fail 'Cannot inspect checkout status.' [[ -z $changes ]] || fail 'Use a clean dedicated checkout; local tracked, staged or untracked changes were retained.' } clean_checkout if [[ $dry_run == --dry-run ]]; then printf '%s\n' 'Plan: fast-forward the tracking branch, then deploy to the selected managed CT. No fetch or changes in dry-run.' exec bash "$source_dir/tools/proxmox.sh" update --ctid "$ctid" --source "$source_dir" --dry-run fi command -v flock >/dev/null || fail 'Missing host command: flock (util-linux).' git_dir=$(git -C "$source_dir" rev-parse --absolute-git-dir) # Keep this checkout locked through the installer, in addition to host.py's deployment lock. exec 9>"$git_dir/finaltoken-proxmox-update.lock" flock -n 9 || fail 'Another update is using this checkout; wait for it to finish.' clean_checkout printf '%s\n' 'Refreshing the published tracking branch (fast-forward only)...' # Git output can contain an operator-supplied credential URL. Never echo it or persist it. branch=$(git -C "$source_dir" symbolic-ref --quiet HEAD) remote=$(git -C "$source_dir" for-each-ref --format='%(upstream:remotename)' "$branch") [[ -n $remote && $remote != . ]] || fail 'Use a branch tracking a published remote, rather than another local branch.' if ! GIT_TERMINAL_PROMPT=0 GCM_INTERACTIVE=Never git -C "$source_dir" fetch --no-tags --no-recurse-submodules -- "$remote" >/dev/null 2>&1; then fail 'Source fetch failed; check Git access/network. No deployment was started; local work was not reset.' fi upstream=$(git -C "$source_dir" rev-parse '@{upstream}') git -C "$source_dir" merge-base --is-ancestor HEAD "$upstream" || fail 'Local-only or diverged commits were retained; use a checkout matching its published upstream. No deployment was started.' # Unlike plain git pull, this also refuses upstream paths that collide with ignored local data. if ! git -C "$source_dir" merge --ff-only --no-autostash --no-overwrite-ignore "$upstream" >/dev/null 2>&1; then fail 'Fast-forward failed; check for local or ignored files that conflict with upstream. Local work was not reset and no deployment was started.' fi head=$(git -C "$source_dir" rev-parse HEAD) [[ $head == "$upstream" ]] || fail 'Checkout does not match the fetched upstream; no deployment was started.' clean_checkout printf '%s\n' "Deploying source $head to CT $ctid; preserving existing configuration and data." exec bash "$source_dir/tools/proxmox.sh" update --ctid "$ctid" --source "$source_dir"