| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #!/usr/bin/env bash
- set -euo pipefail
- BINDIR=/usr/local/bin
- SHAREDIR=/usr/local/share/hth
- ETCDIR=/etc/hth
- UNITDIR=/etc/systemd/system
- LIBDIR=/usr/lib
- STATEDIR=/var/lib/hth
- RUNDIR=/run/hth
- SERVICE=hthd.service
- MANIFEST="$ETCDIR/install-manifest.txt"
- PURGE=0
- YES=0
- usage() { echo "Usage: sudo ./uninstall.sh [--purge --yes]" >&2; }
- while [[ $# -gt 0 ]]; do
- case "$1" in
- --purge) PURGE=1; shift ;;
- --yes) YES=1; shift ;;
- -h|--help) usage; exit 0 ;;
- *) echo "uninstall: unknown argument: $1" >&2; usage; exit 2 ;;
- esac
- done
- [[ "$(id -u)" -eq 0 ]] || { echo "uninstall: run as root" >&2; exit 2; }
- if [[ "$PURGE" -eq 1 && "$YES" -ne 1 ]]; then
- echo "uninstall: --purge requires --yes because product config and state will be removed" >&2
- exit 2
- fi
- if [[ "$YES" -eq 1 && "$PURGE" -ne 1 ]]; then
- echo "uninstall: --yes is only valid with --purge" >&2
- exit 2
- fi
- safe_path() {
- local kind="$1" path="$2" resolved
- [[ "$path" == /* ]] || { echo "uninstall: non-absolute manifest path: $path" >&2; return 1; }
- resolved="$(realpath -m -s -- "$path")"
- [[ "$resolved" == "$path" ]] || { echo "uninstall: unresolved path rejected: $path -> $resolved" >&2; return 1; }
- case "$kind:$resolved" in
- file:/usr/local/bin/hthd|file:/usr/local/bin/hthctl|file:/usr/local/bin/hth-capture|\
- file:/etc/systemd/system/hthd.service|file:/usr/lib/sysusers.d/hth.conf|\
- file:/usr/lib/tmpfiles.d/hth.conf|file:/usr/local/share/hth/sdk/hth_sdk-*.whl|\
- config:/etc/hth/hthd.yaml|config:/etc/hth/release-metadata.json|\
- config:/etc/hth/install-manifest.txt|config:/etc/hth/profiles/6400-control/profile.json|\
- config:/etc/hth/profiles/prod2u-door-temp/profile.json|\
- symlink:/etc/systemd/system/multi-user.target.wants/hthd.service|\
- managed_tree:/run/hth|managed_tree:/var/lib/hth/captures|\
- managed_tree:/var/lib/hth/replay|managed_tree:/var/lib/hth/receipts) return 0 ;;
- *) echo "uninstall: manifest path outside product allowlist: $kind $resolved" >&2; return 1 ;;
- esac
- }
- declare -a FILES=(
- "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture"
- "$UNITDIR/hthd.service" "$UNITDIR/multi-user.target.wants/hthd.service"
- "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"
- )
- declare -a CONFIGS=(
- "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt"
- "$ETCDIR/profiles/6400-control/profile.json" "$ETCDIR/profiles/prod2u-door-temp/profile.json"
- )
- declare -a TREES=("$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts")
- if [[ -f "$MANIFEST" ]]; then
- while read -r kind path extra; do
- [[ -z "$kind" || "$kind" == \#* ]] && continue
- [[ -n "$path" && -z "${extra:-}" ]] || { echo "uninstall: malformed manifest line: $kind $path ${extra:-}" >&2; exit 1; }
- case "$kind" in
- file|config|symlink|managed_tree) safe_path "$kind" "$path" || exit 1 ;;
- account|group) [[ "$path" == hth || "$path" == hth-observe || "$path" == hth-control ]] || exit 1; continue ;;
- *) echo "uninstall: unknown manifest kind: $kind" >&2; exit 1 ;;
- esac
- case "$kind" in
- file|symlink) FILES+=("$path") ;;
- config) CONFIGS+=("$path") ;;
- managed_tree) TREES+=("$path") ;;
- esac
- done <"$MANIFEST"
- fi
- systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
- systemctl disable --now 'hthd@*.service' >/dev/null 2>&1 || true
- if systemctl is-active --quiet "$SERVICE"; then
- echo "uninstall: $SERVICE is still active" >&2
- exit 1
- fi
- for path in "${FILES[@]}"; do
- case "$path" in
- "$BINDIR"/*) safe_path file "$path" ;;
- "$UNITDIR"/multi-user.target.wants/*) safe_path symlink "$path" ;;
- *) safe_path file "$path" ;;
- esac
- rm -f -- "$path"
- done
- rm -f -- "$UNITDIR/hthd@.service"
- systemctl daemon-reload >/dev/null 2>&1 || true
- if [[ "$PURGE" -eq 1 ]]; then
- for path in "${CONFIGS[@]}"; do safe_path config "$path"; rm -f -- "$path"; done
- for path in "${TREES[@]}"; do
- safe_path managed_tree "$path"
- if [[ -d "$path" ]]; then rm -rf --one-file-system -- "$path"; fi
- done
- rmdir "$ETCDIR/profiles/6400-control" "$ETCDIR/profiles/prod2u-door-temp" \
- "$ETCDIR/profiles" "$ETCDIR" 2>/dev/null || true
- rmdir "$STATEDIR" 2>/dev/null || true
- for account_name in hth hth-control hth-observe; do
- if getent passwd "$account_name" >/dev/null 2>&1; then userdel "$account_name"; fi
- done
- for group_name in hth-control hth-observe hth; do
- if getent group "$group_name" >/dev/null 2>&1; then groupdel "$group_name"; fi
- done
- fi
- rmdir "$SHAREDIR/sdk" "$SHAREDIR" 2>/dev/null || true
- failures=0
- for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
- "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
- "$UNITDIR/multi-user.target.wants/hthd.service" \
- "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"; do
- if [[ -e "$path" || -L "$path" ]]; then echo "uninstall residual: $path" >&2; failures=1; fi
- done
- for proc_dir in /proc/[0-9]*; do
- [[ -r "$proc_dir/comm" ]] || continue
- if [[ "$(<"$proc_dir/comm")" == hthd ]]; then
- echo "uninstall residual: hthd process ${proc_dir##*/}" >&2
- failures=1
- fi
- done
- if [[ "$PURGE" -eq 1 ]]; then
- for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt" \
- "$ETCDIR/profiles/6400-control/profile.json" \
- "$ETCDIR/profiles/prod2u-door-temp/profile.json" "$RUNDIR" \
- "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
- if [[ -e "$path" || -L "$path" ]]; then echo "purge residual: $path" >&2; failures=1; fi
- done
- fi
- [[ "$failures" -eq 0 ]] || exit 1
- echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0"
|