install.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. BINDIR=/usr/local/bin
  5. SHAREDIR=/usr/local/share/hth
  6. ETCDIR=/etc/hth
  7. UNITDIR=/etc/systemd/system
  8. LIBDIR=/usr/lib
  9. STATEDIR=/var/lib/hth
  10. RUNDIR=/run/hth
  11. SERVICE=hthd.service
  12. PREVIOUS_RELEASE_VERSION=v0.1.1
  13. PREVIOUS_RELEASE_COMMIT=7128a205a5cf70256bf6bb3cc87068b1d03e7fd4
  14. BACKEND=mock
  15. BACKEND_EXPLICIT=0
  16. SERIAL_PATH=/dev/hth/controller0
  17. NO_START=0
  18. UPGRADE=0
  19. usage() {
  20. cat >&2 <<'EOF'
  21. Usage: sudo ./install.sh [--upgrade] [--backend mock|serial]
  22. [--serial-path PATH] [--no-start]
  23. With no options this performs an idempotent 6400 Production Mock install.
  24. --upgrade migrates the previous formal release (v0.1.1) automatically.
  25. Serial mode is opt-in and requires an existing stable device path.
  26. EOF
  27. }
  28. while [[ $# -gt 0 ]]; do
  29. case "$1" in
  30. --upgrade) UPGRADE=1; shift ;;
  31. --backend) BACKEND="${2:-}"; BACKEND_EXPLICIT=1; shift 2 ;;
  32. --serial-path) SERIAL_PATH="${2:-}"; shift 2 ;;
  33. --no-start) NO_START=1; shift ;;
  34. -h|--help) usage; exit 0 ;;
  35. *) echo "install: unknown argument: $1" >&2; usage; exit 2 ;;
  36. esac
  37. done
  38. fail() { echo "install: $*" >&2; exit 1; }
  39. json_string() {
  40. local key="$1" file="$2"
  41. sed -n "s/^[[:space:]]*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$file" | head -n 1
  42. }
  43. [[ "$(uname -m)" == x86_64 ]] || fail "this release targets x86_64 Linux (found $(uname -m))"
  44. [[ "$(id -u)" -eq 0 ]] || fail "run as root, for example: sudo ./install.sh"
  45. [[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
  46. VERSION="$(json_string version "$ROOT/RELEASE-METADATA.json")"
  47. SOURCE_COMMIT="$(json_string source_commit "$ROOT/RELEASE-METADATA.json")"
  48. [[ -n "$VERSION" && -n "$SOURCE_COMMIT" ]] || fail "invalid release metadata"
  49. case "$BACKEND" in mock|serial) ;; *) fail "--backend must be mock or serial" ;; esac
  50. [[ "$SERIAL_PATH" != *$'\n'* && "$SERIAL_PATH" != *'|'* ]] || fail "unsafe serial path"
  51. if [[ "$BACKEND" == serial && ! -e "$SERIAL_PATH" ]]; then
  52. fail "serial device does not exist: $SERIAL_PATH"
  53. fi
  54. for command_name in realpath readlink sha256sum systemctl systemd-sysusers systemd-tmpfiles base64 find; do
  55. command -v "$command_name" >/dev/null 2>&1 || fail "required command is missing: $command_name"
  56. done
  57. for required in \
  58. "$ROOT/bin/hthd" "$ROOT/bin/hthctl" "$ROOT/bin/hth-capture" \
  59. "$ROOT/config/hthd.6400-mock.yaml" "$ROOT/config/hthd.6400-serial.yaml" \
  60. "$ROOT/service/hthd.service" "$ROOT/service/hth.conf" \
  61. "$ROOT/service/hthd-tmpfiles.conf" "$ROOT/profiles/6400-control/profile.json" \
  62. "$ROOT/sdk"/*.whl "$ROOT/verify-install.sh"; do
  63. [[ -f "$required" ]] || fail "package is incomplete: $required"
  64. done
  65. if [[ -f "$ROOT/SHA256SUMS" ]]; then
  66. (cd "$ROOT" && sha256sum -c SHA256SUMS >/dev/null) || fail "package checksum verification failed"
  67. fi
  68. for absolute_dir in "$BINDIR" "$SHAREDIR" "$ETCDIR" "$UNITDIR" "$STATEDIR" "$RUNDIR"; do
  69. [[ "$absolute_dir" == /* && "$(realpath -m -s -- "$absolute_dir")" == "$absolute_dir" ]] ||
  70. fail "unsafe install path: $absolute_dir"
  71. done
  72. INSTALLED_META="$ETCDIR/release-metadata.json"
  73. INSTALLED_VERSION=""
  74. INSTALLED_COMMIT=""
  75. if [[ -f "$INSTALLED_META" ]]; then
  76. INSTALLED_VERSION="$(json_string version "$INSTALLED_META")"
  77. INSTALLED_COMMIT="$(json_string source_commit "$INSTALLED_META")"
  78. fi
  79. FOOTPRINT=0
  80. for path in "$BINDIR/hthd" "$UNITDIR/hthd.service" "$ETCDIR/hthd.yaml"; do
  81. [[ -e "$path" || -L "$path" ]] && FOOTPRINT=1
  82. done
  83. ACTION=install
  84. FROM_VERSION=none
  85. PRESERVE_CONFIG=0
  86. if [[ "$FOOTPRINT" -eq 1 ]]; then
  87. if [[ "$INSTALLED_VERSION" == "$VERSION" && "$INSTALLED_COMMIT" == "$SOURCE_COMMIT" && "$UPGRADE" -eq 0 ]]; then
  88. ACTION=reinstall
  89. FROM_VERSION="$INSTALLED_VERSION"
  90. PRESERVE_CONFIG=1
  91. elif [[ "$UPGRADE" -eq 1 ]]; then
  92. ACTION=upgrade
  93. FROM_VERSION="${INSTALLED_VERSION:-$PREVIOUS_RELEASE_VERSION}"
  94. else
  95. fail "an existing installation was found; use --upgrade for a different/legacy version"
  96. fi
  97. elif [[ "$UPGRADE" -eq 1 ]]; then
  98. fail "--upgrade requested but no existing HTH installation was found"
  99. fi
  100. if [[ "$ACTION" == reinstall && "$BACKEND_EXPLICIT" -eq 1 ]]; then
  101. PRESERVE_CONFIG=0
  102. fi
  103. if [[ -f "$UNITDIR/hthd.service" ]] && ! grep -Fq '/usr/local/bin/hthd --config /etc/hth/hthd.yaml' "$UNITDIR/hthd.service"; then
  104. fail "existing hthd.service is not the recognized HTH unit; refusing to overwrite it"
  105. fi
  106. systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
  107. if systemctl is-active --quiet "$SERVICE"; then
  108. fail "$SERVICE is still active after stop"
  109. fi
  110. if [[ "$ACTION" == upgrade ]]; then
  111. rm -f -- \
  112. "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
  113. "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
  114. "$UNITDIR/multi-user.target.wants/hthd.service" \
  115. "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"
  116. rm -f -- \
  117. "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
  118. "$ETCDIR/install-manifest.txt" \
  119. "$ETCDIR/profiles/prod2u-door-temp/profile.json" \
  120. "$ETCDIR/profiles/6400-control/profile.json"
  121. rmdir "$ETCDIR/profiles/prod2u-door-temp" 2>/dev/null || true
  122. if [[ -d "$RUNDIR" ]]; then
  123. resolved="$(realpath -m -s -- "$RUNDIR")"
  124. [[ "$resolved" == /run/hth ]] || fail "unsafe runtime path during upgrade: $resolved"
  125. rm -rf --one-file-system -- "$resolved"
  126. fi
  127. systemctl daemon-reload
  128. fi
  129. install -d -m 0755 "$BINDIR" "$SHAREDIR/sdk" "$UNITDIR" "$LIBDIR/sysusers.d" "$LIBDIR/tmpfiles.d"
  130. install -d -m 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
  131. install -d -m 0750 "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"
  132. install -m 0755 "$ROOT/bin/hthd" "$BINDIR/hthd"
  133. install -m 0755 "$ROOT/bin/hthctl" "$BINDIR/hthctl"
  134. install -m 0755 "$ROOT/bin/hth-capture" "$BINDIR/hth-capture"
  135. install -m 0644 "$ROOT/service/hthd.service" "$UNITDIR/hthd.service"
  136. install -m 0644 "$ROOT/service/hth.conf" "$LIBDIR/sysusers.d/hth.conf"
  137. install -m 0644 "$ROOT/service/hthd-tmpfiles.conf" "$LIBDIR/tmpfiles.d/hth.conf"
  138. install -m 0644 "$ROOT/RELEASE-METADATA.json" "$ETCDIR/release-metadata.json"
  139. install -m 0644 "$ROOT/profiles/6400-control/profile.json" "$ETCDIR/profiles/6400-control/profile.json"
  140. rm -f -- "$SHAREDIR/sdk"/*.whl
  141. install -m 0644 "$ROOT"/sdk/*.whl "$SHAREDIR/sdk/"
  142. if [[ "$PRESERVE_CONFIG" -eq 0 || ! -f "$ETCDIR/hthd.yaml" ]]; then
  143. template="$ROOT/config/hthd.6400-${BACKEND}.yaml"
  144. temp_config="$(mktemp -t hth-install-config.XXXXXX)"
  145. trap 'rm -f "${temp_config:-}"' EXIT
  146. if [[ "$BACKEND" == serial ]]; then
  147. sed "s|@SERIAL_PATH@|$SERIAL_PATH|g" "$template" >"$temp_config"
  148. else
  149. cp "$template" "$temp_config"
  150. fi
  151. install -m 0640 "$temp_config" "$ETCDIR/hthd.yaml"
  152. fi
  153. cat >"$ETCDIR/install-manifest.txt" <<EOF
  154. # HTH owned paths; generated by ${VERSION} (${SOURCE_COMMIT})
  155. file $BINDIR/hthd
  156. file $BINDIR/hthctl
  157. file $BINDIR/hth-capture
  158. file $UNITDIR/hthd.service
  159. symlink $UNITDIR/multi-user.target.wants/hthd.service
  160. file $LIBDIR/sysusers.d/hth.conf
  161. file $LIBDIR/tmpfiles.d/hth.conf
  162. file $SHAREDIR/sdk/$(basename "$ROOT"/sdk/*.whl)
  163. config $ETCDIR/hthd.yaml
  164. config $ETCDIR/release-metadata.json
  165. config $ETCDIR/install-manifest.txt
  166. config $ETCDIR/profiles/6400-control/profile.json
  167. config $ETCDIR/profiles/prod2u-door-temp/profile.json
  168. managed_tree $RUNDIR
  169. managed_tree $STATEDIR/captures
  170. managed_tree $STATEDIR/replay
  171. managed_tree $STATEDIR/receipts
  172. account hth
  173. account hth-observe
  174. account hth-control
  175. group hth
  176. group hth-observe
  177. group hth-control
  178. EOF
  179. chmod 0644 "$ETCDIR/install-manifest.txt"
  180. getent group dialout >/dev/null 2>&1 || groupadd --system dialout
  181. systemd-sysusers "$LIBDIR/sysusers.d/hth.conf"
  182. systemd-tmpfiles --create "$LIBDIR/tmpfiles.d/hth.conf"
  183. chown root:hth "$ETCDIR" "$ETCDIR/hthd.yaml" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control" 2>/dev/null || true
  184. chown hth:hth "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts" 2>/dev/null || true
  185. chmod 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
  186. chmod 0640 "$ETCDIR/hthd.yaml"
  187. "$BINDIR/hthd" --check-config --config "$ETCDIR/hthd.yaml"
  188. systemctl daemon-reload
  189. if [[ "$NO_START" -eq 0 ]]; then
  190. systemctl enable --now "$SERVICE"
  191. for _ in $(seq 1 50); do
  192. systemctl is-active --quiet "$SERVICE" && break
  193. sleep 0.1
  194. done
  195. systemctl is-active --quiet "$SERVICE" || {
  196. journalctl -u "$SERVICE" -n 80 --no-pager >&2 || true
  197. fail "$SERVICE did not become active"
  198. }
  199. fi
  200. receipt="$STATEDIR/receipts/install-latest.txt"
  201. EFFECTIVE_BACKEND="$(sed -n 's/^[[:space:]]*backend:[[:space:]]*//p' "$ETCDIR/hthd.yaml" | head -n 1)"
  202. {
  203. echo "schema=hth-install-receipt-v1"
  204. echo "status=PASS"
  205. echo "action=$ACTION"
  206. echo "from_version=$FROM_VERSION"
  207. echo "version=$VERSION"
  208. echo "source_commit=$SOURCE_COMMIT"
  209. echo "backend=$EFFECTIVE_BACKEND"
  210. echo "service=$SERVICE"
  211. echo "config=$ETCDIR/hthd.yaml"
  212. echo "generated_at_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  213. } >"$receipt"
  214. chown hth:hth "$receipt" 2>/dev/null || true
  215. chmod 0644 "$receipt"
  216. if [[ "$NO_START" -eq 0 ]]; then
  217. "$ROOT/verify-install.sh" --no-escalate
  218. fi
  219. echo "HTH_INSTALL_PASS action=$ACTION from=$FROM_VERSION version=$VERSION backend=$EFFECTIVE_BACKEND"