install.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. no_symlink_parents() {
  44. local path="$1" parent
  45. parent="${path%/*}"
  46. [[ -n "$parent" ]] || parent=/
  47. while [[ "$parent" != / ]]; do
  48. if [[ -L "$parent" ]]; then
  49. echo "install: symlink parent rejected: $parent (target $path)" >&2
  50. return 1
  51. fi
  52. parent="${parent%/*}"
  53. [[ -n "$parent" ]] || parent=/
  54. done
  55. }
  56. safe_product_path() {
  57. local kind="$1" path="$2" resolved
  58. [[ "$path" == /* ]] || fail "non-absolute product path: $path"
  59. resolved="$(realpath -m -s -- "$path")"
  60. [[ "$resolved" == "$path" ]] || fail "unresolved product path rejected: $path -> $resolved"
  61. no_symlink_parents "$resolved" || fail "unsafe symlink parent for product path: $resolved"
  62. case "$kind:$resolved" in
  63. file:/usr/local/bin/hthd|file:/usr/local/bin/hthctl|file:/usr/local/bin/hth-capture|\
  64. file:/etc/systemd/system/hthd.service|\
  65. file:/usr/lib/sysusers.d/hth.conf|file:/usr/lib/tmpfiles.d/hth.conf|\
  66. config:/etc/hth/hthd.yaml|config:/etc/hth/release-metadata.json|\
  67. config:/etc/hth/install-manifest.txt|\
  68. config:/etc/hth/profiles/6400-control/profile.json|\
  69. config:/etc/hth/profiles/prod2u-door-temp/profile.json|\
  70. symlink:/etc/systemd/system/multi-user.target.wants/hthd.service|\
  71. managed_tree:/run/hth|managed_tree:/var/lib/hth/captures|\
  72. managed_tree:/var/lib/hth/replay|managed_tree:/var/lib/hth/receipts|\
  73. owned_dir:/usr/local/share/hth/sdk|owned_dir:/usr/local/share/hth|\
  74. owned_dir:/etc/hth/profiles/6400-control|\
  75. owned_dir:/etc/hth/profiles/prod2u-door-temp|owned_dir:/etc/hth/profiles|\
  76. owned_dir:/etc/hth|owned_dir:/var/lib/hth) return 0 ;;
  77. file:/usr/local/share/hth/sdk/hth_sdk-*.whl)
  78. [[ "${resolved#/usr/local/share/hth/sdk/}" != */* ]] ||
  79. fail "nested SDK wheel path rejected: $resolved"
  80. return 0
  81. ;;
  82. *) fail "product path outside upgrade allowlist: $kind $resolved" ;;
  83. esac
  84. }
  85. remove_product_path() {
  86. local kind="$1" path="$2"
  87. safe_product_path "$kind" "$path"
  88. case "$kind" in
  89. file|config|symlink) rm -f -- "$path" ;;
  90. managed_tree)
  91. if [[ -e "$path" || -L "$path" ]]; then
  92. rm -rf --one-file-system -- "$path"
  93. fi
  94. ;;
  95. owned_dir) rmdir -- "$path" 2>/dev/null || true ;;
  96. *) fail "unsupported product removal kind: $kind" ;;
  97. esac
  98. }
  99. identity_allowed() {
  100. case "$1:$2" in
  101. created_account:hth|created_account:hth-observe|created_account:hth-control|\
  102. created_group:hth|created_group:hth-observe|created_group:hth-control) return 0 ;;
  103. *) fail "identity outside product allowlist: $1 $2" ;;
  104. esac
  105. }
  106. verify_legacy_v011() {
  107. local kind path expected actual
  108. while read -r kind path expected; do
  109. safe_product_path "$kind" "$path"
  110. [[ -f "$path" ]] || fail "legacy v0.1.1 fingerprint missing file: $path"
  111. actual="$(sha256sum "$path" | awk '{print $1}')"
  112. [[ "$actual" == "$expected" ]] ||
  113. fail "legacy v0.1.1 fingerprint mismatch: $path"
  114. done <<'EOF'
  115. file /usr/local/bin/hthd 498ee4146b2d44490d58d0e13404978331772fd85bf546f5eea7cdded8c55ad8
  116. file /usr/local/bin/hthctl b57403ff3d3dfc0163aba4b35af33d26cb9aa64e54e4017e52f124aae0cf2421
  117. file /usr/local/bin/hth-capture 382e3e72f61732d26ce77b50323432c8ad534dffba2bea6b9ec029f89fcd1f43
  118. file /etc/systemd/system/hthd.service a6a83979d4d2195adf4704719dc750c0abdd48e803f6f8cc178234695d4afc7e
  119. file /usr/lib/sysusers.d/hth.conf 4894e25647825ea614f8ffaffd72c66b0dc31c6efda1c8481dddd2316a07ae96
  120. file /usr/lib/tmpfiles.d/hth.conf 3c783b5dfb7a17ed1f44eccb1956c5a8ec25e5daebab5d5287111c1387166364
  121. config /etc/hth/profiles/prod2u-door-temp/profile.json 348a07b6b23a9fd3e860b830c15e0bdea17d1da5f28a380bea4065f6fbe800ac
  122. EOF
  123. safe_product_path config "$ETCDIR/hthd.yaml"
  124. [[ -f "$ETCDIR/hthd.yaml" ]] || fail "legacy v0.1.1 config is missing"
  125. grep -Fqx 'api_version: hth.api.v1' "$ETCDIR/hthd.yaml" ||
  126. fail "legacy v0.1.1 config API marker mismatch"
  127. grep -Fqx 'profile: /etc/hth/profiles/prod2u-door-temp/profile.json' "$ETCDIR/hthd.yaml" ||
  128. fail "legacy v0.1.1 config profile marker mismatch"
  129. if [[ -e "$UNITDIR/hthd@.service" || -L "$UNITDIR/hthd@.service" ]]; then
  130. fail "unexpected hthd@.service beside legacy v0.1.1; refusing ambiguous upgrade"
  131. fi
  132. }
  133. [[ "$(uname -m)" == x86_64 ]] || fail "this release targets x86_64 Linux (found $(uname -m))"
  134. [[ "$(id -u)" -eq 0 ]] || fail "run as root, for example: sudo ./install.sh"
  135. [[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
  136. VERSION="$(json_string version "$ROOT/RELEASE-METADATA.json")"
  137. SOURCE_COMMIT="$(json_string source_commit "$ROOT/RELEASE-METADATA.json")"
  138. [[ -n "$VERSION" && -n "$SOURCE_COMMIT" ]] || fail "invalid release metadata"
  139. case "$BACKEND" in mock|serial) ;; *) fail "--backend must be mock or serial" ;; esac
  140. [[ "$SERIAL_PATH" != *$'\n'* && "$SERIAL_PATH" != *'|'* ]] || fail "unsafe serial path"
  141. if [[ "$BACKEND" == serial && ! -e "$SERIAL_PATH" ]]; then
  142. fail "serial device does not exist: $SERIAL_PATH"
  143. fi
  144. for command_name in realpath readlink sha256sum systemctl systemd-sysusers systemd-tmpfiles \
  145. base64 find getent groupadd awk basename grep; do
  146. command -v "$command_name" >/dev/null 2>&1 || fail "required command is missing: $command_name"
  147. done
  148. shopt -s nullglob
  149. PACKAGE_WHEELS=("$ROOT"/sdk/*.whl)
  150. shopt -u nullglob
  151. [[ "${#PACKAGE_WHEELS[@]}" -eq 1 ]] || fail "package must contain exactly one Python SDK wheel"
  152. PACKAGE_WHEEL="${PACKAGE_WHEELS[0]}"
  153. for required in \
  154. "$ROOT/bin/hthd" "$ROOT/bin/hthctl" "$ROOT/bin/hth-capture" \
  155. "$ROOT/config/hthd.6400-mock.yaml" "$ROOT/config/hthd.6400-serial.yaml" \
  156. "$ROOT/service/hthd.service" "$ROOT/service/hth.conf" \
  157. "$ROOT/service/hthd-tmpfiles.conf" "$ROOT/profiles/6400-control/profile.json" \
  158. "$PACKAGE_WHEEL" "$ROOT/verify-install.sh"; do
  159. [[ -f "$required" ]] || fail "package is incomplete: $required"
  160. done
  161. if [[ -f "$ROOT/SHA256SUMS" ]]; then
  162. (cd "$ROOT" && sha256sum -c SHA256SUMS >/dev/null) || fail "package checksum verification failed"
  163. fi
  164. for absolute_dir in \
  165. "$BINDIR" "$SHAREDIR" "$SHAREDIR/sdk" \
  166. "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control" \
  167. "$UNITDIR" "$UNITDIR/multi-user.target.wants" \
  168. "$LIBDIR/sysusers.d" "$LIBDIR/tmpfiles.d" \
  169. "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts" \
  170. "$RUNDIR" "$RUNDIR/observe" "$RUNDIR/control"; do
  171. [[ "$absolute_dir" == /* && "$(realpath -m -s -- "$absolute_dir")" == "$absolute_dir" ]] ||
  172. fail "unsafe install path: $absolute_dir"
  173. no_symlink_parents "$absolute_dir" || fail "unsafe symlink parent for install path: $absolute_dir"
  174. [[ ! -L "$absolute_dir" ]] || fail "install path is a symlink: $absolute_dir"
  175. done
  176. INSTALLED_META="$ETCDIR/release-metadata.json"
  177. OLD_MANIFEST="$ETCDIR/install-manifest.txt"
  178. INSTALLED_VERSION=""
  179. INSTALLED_COMMIT=""
  180. if [[ -f "$INSTALLED_META" ]]; then
  181. INSTALLED_VERSION="$(json_string version "$INSTALLED_META")"
  182. INSTALLED_COMMIT="$(json_string source_commit "$INSTALLED_META")"
  183. fi
  184. FOOTPRINT=0
  185. for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
  186. "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
  187. "$UNITDIR/multi-user.target.wants/hthd.service" \
  188. "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf" \
  189. "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
  190. "$ETCDIR/install-manifest.txt" \
  191. "$ETCDIR/profiles/6400-control/profile.json" \
  192. "$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" \
  193. "$STATEDIR/receipts" \
  194. "$SHAREDIR/sdk/$(basename "$PACKAGE_WHEEL")"; do
  195. [[ -e "$path" || -L "$path" ]] && FOOTPRINT=1
  196. done
  197. if [[ -d "$SHAREDIR/sdk" ]] &&
  198. find "$SHAREDIR/sdk" -maxdepth 1 -type f -name 'hth_sdk-*.whl' -print -quit | grep -q .; then
  199. FOOTPRINT=1
  200. fi
  201. declare -A OLD_MANIFEST_ENTRIES=()
  202. declare -a OWNED_WHEELS=()
  203. WANTS_MANIFEST_KEY="symlink $UNITDIR/multi-user.target.wants/hthd.service"
  204. if [[ -f "$OLD_MANIFEST" ]]; then
  205. safe_product_path config "$INSTALLED_META"
  206. safe_product_path config "$OLD_MANIFEST"
  207. [[ -f "$INSTALLED_META" ]] || fail "trusted install manifest requires release metadata"
  208. [[ "$INSTALLED_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
  209. fail "installed release metadata has an invalid version"
  210. [[ "$INSTALLED_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
  211. fail "installed release metadata has an invalid source_commit"
  212. OLD_WHEEL_COUNT=0
  213. while read -r old_kind old_value old_extra; do
  214. [[ -z "$old_kind" || "$old_kind" == \#* ]] && continue
  215. [[ -n "$old_value" && -z "${old_extra:-}" ]] ||
  216. fail "malformed old manifest line: $old_kind $old_value ${old_extra:-}"
  217. if [[ "$old_kind:$old_value" == "config:$ETCDIR/profiles/prod2u-door-temp/profile.json" ||
  218. "$old_kind:$old_value" == "owned_dir:$ETCDIR/profiles/prod2u-door-temp" ]]; then
  219. fail "old manifest claims an unowned legacy door-temperature path: $old_kind $old_value"
  220. fi
  221. case "$old_kind" in
  222. file|config|symlink|managed_tree|owned_dir)
  223. safe_product_path "$old_kind" "$old_value"
  224. ;;
  225. created_account)
  226. identity_allowed "$old_kind" "$old_value"
  227. ;;
  228. created_group)
  229. identity_allowed "$old_kind" "$old_value"
  230. ;;
  231. account|group)
  232. [[ "$old_value" == hth || "$old_value" == hth-observe || "$old_value" == hth-control ]] ||
  233. fail "legacy manifest identity outside product allowlist: $old_kind $old_value"
  234. ;;
  235. *) fail "unknown old manifest kind: $old_kind" ;;
  236. esac
  237. manifest_key="$old_kind $old_value"
  238. [[ -z "${OLD_MANIFEST_ENTRIES[$manifest_key]:-}" ]] ||
  239. fail "duplicate old manifest entry: $manifest_key"
  240. OLD_MANIFEST_ENTRIES["$manifest_key"]=1
  241. if [[ "$old_kind" == file && "$old_value" == "$SHAREDIR"/sdk/hth_sdk-*.whl ]]; then
  242. OWNED_WHEELS+=("$old_value")
  243. ((OLD_WHEEL_COUNT += 1))
  244. fi
  245. done <"$OLD_MANIFEST"
  246. while read -r required_kind required_path; do
  247. required_key="$required_kind $required_path"
  248. [[ "${OLD_MANIFEST_ENTRIES[$required_key]:-0}" -eq 1 ]] ||
  249. fail "old manifest is incomplete: $required_key"
  250. done <<EOF
  251. file $BINDIR/hthd
  252. file $BINDIR/hthctl
  253. file $BINDIR/hth-capture
  254. file $UNITDIR/hthd.service
  255. file $LIBDIR/sysusers.d/hth.conf
  256. file $LIBDIR/tmpfiles.d/hth.conf
  257. config $ETCDIR/hthd.yaml
  258. config $ETCDIR/release-metadata.json
  259. config $ETCDIR/install-manifest.txt
  260. config $ETCDIR/profiles/6400-control/profile.json
  261. managed_tree $RUNDIR
  262. managed_tree $STATEDIR/captures
  263. managed_tree $STATEDIR/replay
  264. managed_tree $STATEDIR/receipts
  265. owned_dir $SHAREDIR/sdk
  266. owned_dir $SHAREDIR
  267. owned_dir $ETCDIR/profiles/6400-control
  268. owned_dir $ETCDIR/profiles
  269. owned_dir $ETCDIR
  270. owned_dir $STATEDIR
  271. EOF
  272. [[ "$OLD_WHEEL_COUNT" -eq 1 ]] || fail "old manifest must contain exactly one owned SDK wheel"
  273. fi
  274. ACTION=install
  275. FROM_VERSION=none
  276. PRESERVE_CONFIG=0
  277. if [[ "$FOOTPRINT" -eq 1 ]]; then
  278. if [[ "$INSTALLED_VERSION" == "$VERSION" && "$INSTALLED_COMMIT" == "$SOURCE_COMMIT" && "$UPGRADE" -eq 0 ]]; then
  279. ACTION=reinstall
  280. FROM_VERSION="$INSTALLED_VERSION"
  281. PRESERVE_CONFIG=1
  282. elif [[ "$UPGRADE" -eq 1 ]]; then
  283. ACTION=upgrade
  284. FROM_VERSION="${INSTALLED_VERSION:-$PREVIOUS_RELEASE_VERSION}"
  285. else
  286. fail "an existing installation was found; use --upgrade for a different/legacy version"
  287. fi
  288. elif [[ "$UPGRADE" -eq 1 ]]; then
  289. fail "--upgrade requested but no existing HTH installation was found"
  290. fi
  291. if [[ "$ACTION" == reinstall && "$BACKEND_EXPLICIT" -eq 1 ]]; then
  292. PRESERVE_CONFIG=0
  293. fi
  294. if [[ -f "$UNITDIR/hthd.service" ]] && ! grep -Fq '/usr/local/bin/hthd --config /etc/hth/hthd.yaml' "$UNITDIR/hthd.service"; then
  295. fail "existing hthd.service is not the recognized HTH unit; refusing to overwrite it"
  296. fi
  297. if [[ -e "$UNITDIR/hthd@.service" || -L "$UNITDIR/hthd@.service" ]]; then
  298. fail "unexpected hthd@.service; refusing to overwrite an ambiguous installation"
  299. fi
  300. if [[ "$ACTION" == upgrade && ! -f "$OLD_MANIFEST" ]]; then
  301. verify_legacy_v011
  302. fi
  303. if [[ "$ACTION" == upgrade && -f "$OLD_MANIFEST" ]]; then
  304. for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
  305. "$UNITDIR/hthd.service" "$ETCDIR/hthd.yaml" "$INSTALLED_META" \
  306. "$ETCDIR/profiles/6400-control/profile.json" "${OWNED_WHEELS[@]}"; do
  307. [[ -f "$path" ]] || fail "trusted manifest footprint is incomplete at upgrade: $path"
  308. done
  309. fi
  310. systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
  311. if systemctl is-active --quiet "$SERVICE"; then
  312. fail "$SERVICE is still active after stop"
  313. fi
  314. if [[ "$ACTION" == upgrade ]]; then
  315. for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
  316. "$UNITDIR/hthd.service" \
  317. "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"; do
  318. remove_product_path file "$path"
  319. done
  320. if [[ ! -f "$OLD_MANIFEST" || "${OLD_MANIFEST_ENTRIES[$WANTS_MANIFEST_KEY]:-0}" -eq 1 ]]; then
  321. remove_product_path symlink "$UNITDIR/multi-user.target.wants/hthd.service"
  322. fi
  323. for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
  324. "$ETCDIR/install-manifest.txt" \
  325. "$ETCDIR/profiles/6400-control/profile.json"; do
  326. remove_product_path config "$path"
  327. done
  328. if [[ ! -f "$OLD_MANIFEST" ]]; then
  329. remove_product_path config "$ETCDIR/profiles/prod2u-door-temp/profile.json"
  330. remove_product_path owned_dir "$ETCDIR/profiles/prod2u-door-temp"
  331. fi
  332. remove_product_path managed_tree "$RUNDIR"
  333. systemctl daemon-reload
  334. fi
  335. install -d -m 0755 "$BINDIR" "$SHAREDIR/sdk" "$UNITDIR" "$LIBDIR/sysusers.d" "$LIBDIR/tmpfiles.d"
  336. install -d -m 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
  337. install -d -m 0750 "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"
  338. install -m 0755 "$ROOT/bin/hthd" "$BINDIR/hthd"
  339. install -m 0755 "$ROOT/bin/hthctl" "$BINDIR/hthctl"
  340. install -m 0755 "$ROOT/bin/hth-capture" "$BINDIR/hth-capture"
  341. install -m 0644 "$ROOT/service/hthd.service" "$UNITDIR/hthd.service"
  342. install -m 0644 "$ROOT/service/hth.conf" "$LIBDIR/sysusers.d/hth.conf"
  343. install -m 0644 "$ROOT/service/hthd-tmpfiles.conf" "$LIBDIR/tmpfiles.d/hth.conf"
  344. install -m 0644 "$ROOT/RELEASE-METADATA.json" "$ETCDIR/release-metadata.json"
  345. install -m 0644 "$ROOT/profiles/6400-control/profile.json" "$ETCDIR/profiles/6400-control/profile.json"
  346. for path in "${OWNED_WHEELS[@]}"; do remove_product_path file "$path"; done
  347. install -m 0644 "$PACKAGE_WHEEL" "$SHAREDIR/sdk/"
  348. if [[ "$PRESERVE_CONFIG" -eq 0 || ! -f "$ETCDIR/hthd.yaml" ]]; then
  349. template="$ROOT/config/hthd.6400-${BACKEND}.yaml"
  350. temp_config="$(mktemp -t hth-install-config.XXXXXX)"
  351. trap 'rm -f "${temp_config:-}"' EXIT
  352. if [[ "$BACKEND" == serial ]]; then
  353. sed "s|@SERIAL_PATH@|$SERIAL_PATH|g" "$template" >"$temp_config"
  354. else
  355. cp "$template" "$temp_config"
  356. fi
  357. install -m 0640 "$temp_config" "$ETCDIR/hthd.yaml"
  358. fi
  359. getent group dialout >/dev/null 2>&1 || groupadd --system dialout
  360. systemd-sysusers "$LIBDIR/sysusers.d/hth.conf"
  361. systemd-tmpfiles --create "$LIBDIR/tmpfiles.d/hth.conf"
  362. cat >"$ETCDIR/install-manifest.txt" <<EOF
  363. # HTH owned paths; generated by ${VERSION} (${SOURCE_COMMIT})
  364. file $BINDIR/hthd
  365. file $BINDIR/hthctl
  366. file $BINDIR/hth-capture
  367. file $UNITDIR/hthd.service
  368. file $LIBDIR/sysusers.d/hth.conf
  369. file $LIBDIR/tmpfiles.d/hth.conf
  370. file $SHAREDIR/sdk/$(basename "$PACKAGE_WHEEL")
  371. config $ETCDIR/hthd.yaml
  372. config $ETCDIR/release-metadata.json
  373. config $ETCDIR/install-manifest.txt
  374. config $ETCDIR/profiles/6400-control/profile.json
  375. managed_tree $RUNDIR
  376. managed_tree $STATEDIR/captures
  377. managed_tree $STATEDIR/replay
  378. managed_tree $STATEDIR/receipts
  379. owned_dir $SHAREDIR/sdk
  380. owned_dir $SHAREDIR
  381. owned_dir $ETCDIR/profiles/6400-control
  382. owned_dir $ETCDIR/profiles
  383. owned_dir $ETCDIR
  384. owned_dir $STATEDIR
  385. EOF
  386. chmod 0644 "$ETCDIR/install-manifest.txt"
  387. chown root:hth "$ETCDIR" "$ETCDIR/hthd.yaml" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control" 2>/dev/null || true
  388. chown hth:hth "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts" 2>/dev/null || true
  389. chmod 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
  390. chmod 0640 "$ETCDIR/hthd.yaml"
  391. "$BINDIR/hthd" --check-config --config "$ETCDIR/hthd.yaml"
  392. systemctl daemon-reload
  393. if [[ "$NO_START" -eq 0 ]]; then
  394. systemctl enable --now "$SERVICE"
  395. for _ in $(seq 1 50); do
  396. systemctl is-active --quiet "$SERVICE" && break
  397. sleep 0.1
  398. done
  399. systemctl is-active --quiet "$SERVICE" || {
  400. journalctl -u "$SERVICE" -n 80 --no-pager >&2 || true
  401. fail "$SERVICE did not become active"
  402. }
  403. fi
  404. if [[ -L "$UNITDIR/multi-user.target.wants/hthd.service" ]]; then
  405. safe_product_path symlink "$UNITDIR/multi-user.target.wants/hthd.service"
  406. echo "symlink $UNITDIR/multi-user.target.wants/hthd.service" >>"$ETCDIR/install-manifest.txt"
  407. fi
  408. receipt="$STATEDIR/receipts/install-latest.txt"
  409. EFFECTIVE_BACKEND="$(sed -n 's/^[[:space:]]*backend:[[:space:]]*//p' "$ETCDIR/hthd.yaml" | head -n 1)"
  410. {
  411. echo "schema=hth-install-receipt-v1"
  412. echo "status=PASS"
  413. echo "action=$ACTION"
  414. echo "from_version=$FROM_VERSION"
  415. echo "version=$VERSION"
  416. echo "source_commit=$SOURCE_COMMIT"
  417. echo "backend=$EFFECTIVE_BACKEND"
  418. echo "service=$SERVICE"
  419. echo "config=$ETCDIR/hthd.yaml"
  420. echo "generated_at_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
  421. } >"$receipt"
  422. chown hth:hth "$receipt" 2>/dev/null || true
  423. chmod 0644 "$receipt"
  424. if [[ "$NO_START" -eq 0 ]]; then
  425. "$ROOT/verify-install.sh" --no-escalate
  426. fi
  427. echo "HTH_INSTALL_PASS action=$ACTION from=$FROM_VERSION version=$VERSION backend=$EFFECTIVE_BACKEND"