uninstall.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. BINDIR=/usr/local/bin
  4. SHAREDIR=/usr/local/share/hth
  5. ETCDIR=/etc/hth
  6. UNITDIR=/etc/systemd/system
  7. LIBDIR=/usr/lib
  8. STATEDIR=/var/lib/hth
  9. RUNDIR=/run/hth
  10. SERVICE=hthd.service
  11. MANIFEST="$ETCDIR/install-manifest.txt"
  12. PURGE=0
  13. YES=0
  14. usage() { echo "Usage: sudo ./uninstall.sh [--purge --yes]" >&2; }
  15. while [[ $# -gt 0 ]]; do
  16. case "$1" in
  17. --purge) PURGE=1; shift ;;
  18. --yes) YES=1; shift ;;
  19. -h|--help) usage; exit 0 ;;
  20. *) echo "uninstall: unknown argument: $1" >&2; usage; exit 2 ;;
  21. esac
  22. done
  23. [[ "$(id -u)" -eq 0 ]] || { echo "uninstall: run as root" >&2; exit 2; }
  24. if [[ "$PURGE" -eq 1 && "$YES" -ne 1 ]]; then
  25. echo "uninstall: --purge requires --yes because product config and state will be removed" >&2
  26. exit 2
  27. fi
  28. if [[ "$YES" -eq 1 && "$PURGE" -ne 1 ]]; then
  29. echo "uninstall: --yes is only valid with --purge" >&2
  30. exit 2
  31. fi
  32. safe_path() {
  33. local kind="$1" path="$2" resolved
  34. [[ "$path" == /* ]] || { echo "uninstall: non-absolute manifest path: $path" >&2; return 1; }
  35. resolved="$(realpath -m -s -- "$path")"
  36. [[ "$resolved" == "$path" ]] || { echo "uninstall: unresolved path rejected: $path -> $resolved" >&2; return 1; }
  37. case "$kind:$resolved" in
  38. file:/usr/local/bin/hthd|file:/usr/local/bin/hthctl|file:/usr/local/bin/hth-capture|\
  39. file:/etc/systemd/system/hthd.service|file:/usr/lib/sysusers.d/hth.conf|\
  40. file:/usr/lib/tmpfiles.d/hth.conf|file:/usr/local/share/hth/sdk/hth_sdk-*.whl|\
  41. config:/etc/hth/hthd.yaml|config:/etc/hth/release-metadata.json|\
  42. config:/etc/hth/install-manifest.txt|config:/etc/hth/profiles/6400-control/profile.json|\
  43. config:/etc/hth/profiles/prod2u-door-temp/profile.json|\
  44. symlink:/etc/systemd/system/multi-user.target.wants/hthd.service|\
  45. managed_tree:/run/hth|managed_tree:/var/lib/hth/captures|\
  46. managed_tree:/var/lib/hth/replay|managed_tree:/var/lib/hth/receipts) return 0 ;;
  47. *) echo "uninstall: manifest path outside product allowlist: $kind $resolved" >&2; return 1 ;;
  48. esac
  49. }
  50. declare -a FILES=(
  51. "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture"
  52. "$UNITDIR/hthd.service" "$UNITDIR/multi-user.target.wants/hthd.service"
  53. "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"
  54. )
  55. declare -a CONFIGS=(
  56. "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt"
  57. "$ETCDIR/profiles/6400-control/profile.json" "$ETCDIR/profiles/prod2u-door-temp/profile.json"
  58. )
  59. declare -a TREES=("$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts")
  60. if [[ -f "$MANIFEST" ]]; then
  61. while read -r kind path extra; do
  62. [[ -z "$kind" || "$kind" == \#* ]] && continue
  63. [[ -n "$path" && -z "${extra:-}" ]] || { echo "uninstall: malformed manifest line: $kind $path ${extra:-}" >&2; exit 1; }
  64. case "$kind" in
  65. file|config|symlink|managed_tree) safe_path "$kind" "$path" || exit 1 ;;
  66. account|group) [[ "$path" == hth || "$path" == hth-observe || "$path" == hth-control ]] || exit 1; continue ;;
  67. *) echo "uninstall: unknown manifest kind: $kind" >&2; exit 1 ;;
  68. esac
  69. case "$kind" in
  70. file|symlink) FILES+=("$path") ;;
  71. config) CONFIGS+=("$path") ;;
  72. managed_tree) TREES+=("$path") ;;
  73. esac
  74. done <"$MANIFEST"
  75. fi
  76. systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
  77. systemctl disable --now 'hthd@*.service' >/dev/null 2>&1 || true
  78. if systemctl is-active --quiet "$SERVICE"; then
  79. echo "uninstall: $SERVICE is still active" >&2
  80. exit 1
  81. fi
  82. for path in "${FILES[@]}"; do
  83. case "$path" in
  84. "$BINDIR"/*) safe_path file "$path" ;;
  85. "$UNITDIR"/multi-user.target.wants/*) safe_path symlink "$path" ;;
  86. *) safe_path file "$path" ;;
  87. esac
  88. rm -f -- "$path"
  89. done
  90. rm -f -- "$UNITDIR/hthd@.service"
  91. systemctl daemon-reload >/dev/null 2>&1 || true
  92. if [[ "$PURGE" -eq 1 ]]; then
  93. for path in "${CONFIGS[@]}"; do safe_path config "$path"; rm -f -- "$path"; done
  94. for path in "${TREES[@]}"; do
  95. safe_path managed_tree "$path"
  96. if [[ -d "$path" ]]; then rm -rf --one-file-system -- "$path"; fi
  97. done
  98. rmdir "$ETCDIR/profiles/6400-control" "$ETCDIR/profiles/prod2u-door-temp" \
  99. "$ETCDIR/profiles" "$ETCDIR" 2>/dev/null || true
  100. rmdir "$STATEDIR" 2>/dev/null || true
  101. for account_name in hth hth-control hth-observe; do
  102. if getent passwd "$account_name" >/dev/null 2>&1; then userdel "$account_name"; fi
  103. done
  104. for group_name in hth-control hth-observe hth; do
  105. if getent group "$group_name" >/dev/null 2>&1; then groupdel "$group_name"; fi
  106. done
  107. fi
  108. rmdir "$SHAREDIR/sdk" "$SHAREDIR" 2>/dev/null || true
  109. failures=0
  110. for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
  111. "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
  112. "$UNITDIR/multi-user.target.wants/hthd.service" \
  113. "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"; do
  114. if [[ -e "$path" || -L "$path" ]]; then echo "uninstall residual: $path" >&2; failures=1; fi
  115. done
  116. for proc_dir in /proc/[0-9]*; do
  117. [[ -r "$proc_dir/comm" ]] || continue
  118. if [[ "$(<"$proc_dir/comm")" == hthd ]]; then
  119. echo "uninstall residual: hthd process ${proc_dir##*/}" >&2
  120. failures=1
  121. fi
  122. done
  123. if [[ "$PURGE" -eq 1 ]]; then
  124. for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt" \
  125. "$ETCDIR/profiles/6400-control/profile.json" \
  126. "$ETCDIR/profiles/prod2u-door-temp/profile.json" "$RUNDIR" \
  127. "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
  128. if [[ -e "$path" || -L "$path" ]]; then echo "purge residual: $path" >&2; failures=1; fi
  129. done
  130. fi
  131. [[ "$failures" -eq 0 ]] || exit 1
  132. echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0"