taler-monitoring/host-agent/update-suite.sh

153 lines
6.3 KiB
Bash
Executable file

#!/usr/bin/env bash
# update-suite.sh — pull latest taler-monitoring from Forgejo (standalone repo root).
#
# Sourced by run-host-report.sh / wrappers. Every host-agent run must land on
# current origin/$SUITE_GIT_REF (default main). Stale clones are not acceptable.
#
# Env:
# SUITE_GIT_URL default https://git.hacktivism.ch/hernani/taler-monitoring.git
# SUITE_GIT_REF default main
# SUITE_DIR default ~/src/taler-monitoring
# SUITE_UPDATE_MODE reset (default) | pull | keep
# keep = no fetch/reset (local deploy / unpushed fixes)
# SUITE_UPDATE_STRICT 1 (default) = fetch/reset failure aborts the agent run
# 0 = warn and continue on old tree (emergency only)
#
set -uo pipefail
CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/taler-monitoring"
ENV_FILE="${TALER_MONITORING_ENV:-$CFG_DIR/env}"
if [ -f "$ENV_FILE" ] && [ "${_TALER_MON_ENV_LOADED:-0}" != "1" ]; then
set -a; source "$ENV_FILE"; set +a
fi
SUITE_GIT_URL="${SUITE_GIT_URL:-https://git.hacktivism.ch/hernani/taler-monitoring.git}"
SUITE_GIT_REF="${SUITE_GIT_REF:-main}"
SUITE_UPDATE_MODE="${SUITE_UPDATE_MODE:-reset}"
SUITE_UPDATE_STRICT="${SUITE_UPDATE_STRICT:-1}"
if [ -z "${SUITE_DIR:-}" ]; then
if [ -d "$HOME/src/taler-monitoring/.git" ]; then SUITE_DIR="$HOME/src/taler-monitoring"
elif [ -d "$HOME/taler-monitoring/.git" ]; then SUITE_DIR="$HOME/taler-monitoring"
else SUITE_DIR="$HOME/src/taler-monitoring"; fi
fi
export SUITE_DIR
_upd_die() {
echo "ERROR suite-update: $*" >&2
if [ "${SUITE_UPDATE_STRICT}" = "1" ]; then
return 1 2>/dev/null || exit 1
fi
echo "WARN suite-update: continuing on existing tree (SUITE_UPDATE_STRICT=0)" >&2
return 0
}
mkdir -p "$(dirname "$SUITE_DIR")" "$CFG_DIR"
if [ ! -d "$SUITE_DIR/.git" ]; then
echo "clone $SUITE_GIT_URL$SUITE_DIR"
if ! git clone --branch "$SUITE_GIT_REF" "$SUITE_GIT_URL" "$SUITE_DIR" \
&& ! git clone "$SUITE_GIT_URL" "$SUITE_DIR"; then
_upd_die "git clone failed"
return 1 2>/dev/null || exit 1
fi
fi
cd "$SUITE_DIR" || { _upd_die "cd $SUITE_DIR failed"; return 1 2>/dev/null || exit 1; }
git remote set-url origin "$SUITE_GIT_URL" 2>/dev/null || true
_before=$(git rev-parse --short=12 HEAD 2>/dev/null || echo none)
_before_ver=$(git describe --tags --exact-match 2>/dev/null \
|| git describe --tags --abbrev=0 2>/dev/null \
|| echo unknown)
case "$SUITE_UPDATE_MODE" in
keep|skip|off|none)
echo "suite update skipped (SUITE_UPDATE_MODE=$SUITE_UPDATE_MODE) — keep local tree ${_before} (${_before_ver})"
;;
*)
echo "fetch origin ($SUITE_GIT_REF) + tags …"
if ! git fetch --tags --force --prune origin 2>&1; then
_upd_die "git fetch failed — suite would stay at ${_before} (${_before_ver})"
# fall through only if STRICT=0
else
case "$SUITE_UPDATE_MODE" in
reset)
if ! git checkout -B "$SUITE_GIT_REF" "origin/$SUITE_GIT_REF" 2>/dev/null \
&& ! git checkout -B "$SUITE_GIT_REF" FETCH_HEAD; then
_upd_die "git checkout $SUITE_GIT_REF failed"
fi
if ! git reset --hard "origin/$SUITE_GIT_REF" 2>/dev/null \
&& ! git reset --hard FETCH_HEAD; then
_upd_die "git reset --hard origin/$SUITE_GIT_REF failed"
fi
;;
pull)
if ! git pull --ff-only origin "$SUITE_GIT_REF" 2>&1; then
_upd_die "git pull --ff-only failed"
fi
;;
*)
if ! git pull --ff-only origin "$SUITE_GIT_REF" 2>&1; then
_upd_die "git pull --ff-only failed (mode=$SUITE_UPDATE_MODE)"
fi
;;
esac
fi
;;
esac
COMMIT=$(git rev-parse HEAD)
COMMIT_SHORT=$(git rev-parse --short=12 HEAD)
SOURCE_REPO_WEB="${SOURCE_REPO_WEB:-https://git.hacktivism.ch/hernani/taler-monitoring}"
COMMIT_URL="${SOURCE_REPO_WEB}/src/commit/${COMMIT}"
export COMMIT COMMIT_SHORT COMMIT_URL SOURCE_REPO_WEB
# Release tag for sticky bar (exact tag if HEAD is tagged, else nearest)
SUITE_VERSION=""
if command -v git >/dev/null 2>&1; then
SUITE_VERSION=$(git describe --tags --exact-match 2>/dev/null || true)
if [ -z "$SUITE_VERSION" ]; then
SUITE_VERSION=$(git describe --tags --abbrev=0 2>/dev/null || true)
fi
fi
if [ -z "$SUITE_VERSION" ] && [ -f "$SUITE_DIR/VERSION" ]; then
SUITE_VERSION="v$(tr -d '[:space:]' <"$SUITE_DIR/VERSION" | sed 's/^v//')"
fi
[ -z "$SUITE_VERSION" ] && SUITE_VERSION="unknown"
case "$SUITE_VERSION" in
unknown|v*) ;;
*) SUITE_VERSION="v${SUITE_VERSION}" ;;
esac
# Sticky version badge: prefer commit URL (exact tree of this run).
# Tag URLs (/src/tag/vX.Y.Z) 404 when the tag is not yet pushed to Forgejo;
# commit URLs work as soon as main (or the tree) is on the remote.
SUITE_VERSION_URL=""
if [ -n "$SOURCE_REPO_WEB" ] && [ -n "${COMMIT:-}" ] && [ "$COMMIT" != "unknown" ]; then
SUITE_VERSION_URL="${SOURCE_REPO_WEB}/src/commit/${COMMIT}"
elif [ -n "$SOURCE_REPO_WEB" ] && [ "$SUITE_VERSION" != "unknown" ]; then
SUITE_VERSION_URL="${SOURCE_REPO_WEB}/src/tag/${SUITE_VERSION}"
fi
export SUITE_VERSION SUITE_VERSION_URL
if [ "$_before" != "$COMMIT_SHORT" ]; then
echo " upgraded ${_before} (${_before_ver}) → ${COMMIT_SHORT} (${SUITE_VERSION})"
else
echo " already at ${COMMIT_SHORT} (${SUITE_VERSION})"
fi
echo " version=$SUITE_VERSION ${SUITE_VERSION_URL:$SUITE_VERSION_URL}"
echo "suite @ $COMMIT_SHORT $COMMIT_URL"
echo " dir=$SUITE_DIR"
# Sanity: must have suite entrypoint after update
if [ ! -x "$SUITE_DIR/taler-monitoring.sh" ] && [ ! -f "$SUITE_DIR/taler-monitoring.sh" ]; then
_upd_die "taler-monitoring.sh missing after update"
return 1 2>/dev/null || exit 1
fi
ln -sfn "$SUITE_DIR" "$HOME/taler-monitoring" 2>/dev/null || true
chmod +x taler-monitoring.sh check_*.sh host-agent/*.sh site-gen/*.sh site-gen/*.py 2>/dev/null || true
# Drop bytecode so a stale site-gen never wins over the tree we just reset to.
rm -rf "$SUITE_DIR/site-gen/__pycache__" 2>/dev/null || true
# Refuse known-bad pre-1.15.8 generator (box-drawing JS regex → SyntaxWarning noise)
if [ -f "$SUITE_DIR/site-gen/console_to_html.py" ] \
&& grep -qE 'if \(/\^\[[╔╗╚╝║═]' "$SUITE_DIR/site-gen/console_to_html.py" 2>/dev/null; then
_upd_die "site-gen/console_to_html.py still has pre-1.15.8 box-drawing JS regex — refuse stale generator"
return 1 2>/dev/null || exit 1
fi
return 0 2>/dev/null || true