| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- #!/usr/bin/env bash
- set -euo pipefail
- ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- 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
- PREVIOUS_RELEASE_VERSION=v0.1.1
- PREVIOUS_RELEASE_COMMIT=7128a205a5cf70256bf6bb3cc87068b1d03e7fd4
- BACKEND=mock
- BACKEND_EXPLICIT=0
- SERIAL_PATH=/dev/hth/controller0
- NO_START=0
- UPGRADE=0
- usage() {
- cat >&2 <<'EOF'
- Usage: sudo ./install.sh [--upgrade] [--backend mock|serial]
- [--serial-path PATH] [--no-start]
- With no options this performs an idempotent 6400 Production Mock install.
- --upgrade migrates the previous formal release (v0.1.1) automatically.
- Serial mode is opt-in and requires an existing stable device path.
- EOF
- }
- while [[ $# -gt 0 ]]; do
- case "$1" in
- --upgrade) UPGRADE=1; shift ;;
- --backend) BACKEND="${2:-}"; BACKEND_EXPLICIT=1; shift 2 ;;
- --serial-path) SERIAL_PATH="${2:-}"; shift 2 ;;
- --no-start) NO_START=1; shift ;;
- -h|--help) usage; exit 0 ;;
- *) echo "install: unknown argument: $1" >&2; usage; exit 2 ;;
- esac
- done
- fail() { echo "install: $*" >&2; exit 1; }
- json_string() {
- local key="$1" file="$2"
- sed -n "s/^[[:space:]]*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$file" | head -n 1
- }
- [[ "$(uname -m)" == x86_64 ]] || fail "this release targets x86_64 Linux (found $(uname -m))"
- [[ "$(id -u)" -eq 0 ]] || fail "run as root, for example: sudo ./install.sh"
- [[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
- VERSION="$(json_string version "$ROOT/RELEASE-METADATA.json")"
- SOURCE_COMMIT="$(json_string source_commit "$ROOT/RELEASE-METADATA.json")"
- [[ -n "$VERSION" && -n "$SOURCE_COMMIT" ]] || fail "invalid release metadata"
- case "$BACKEND" in mock|serial) ;; *) fail "--backend must be mock or serial" ;; esac
- [[ "$SERIAL_PATH" != *$'\n'* && "$SERIAL_PATH" != *'|'* ]] || fail "unsafe serial path"
- if [[ "$BACKEND" == serial && ! -e "$SERIAL_PATH" ]]; then
- fail "serial device does not exist: $SERIAL_PATH"
- fi
- for command_name in realpath readlink sha256sum systemctl systemd-sysusers systemd-tmpfiles base64 find; do
- command -v "$command_name" >/dev/null 2>&1 || fail "required command is missing: $command_name"
- done
- for required in \
- "$ROOT/bin/hthd" "$ROOT/bin/hthctl" "$ROOT/bin/hth-capture" \
- "$ROOT/config/hthd.6400-mock.yaml" "$ROOT/config/hthd.6400-serial.yaml" \
- "$ROOT/service/hthd.service" "$ROOT/service/hth.conf" \
- "$ROOT/service/hthd-tmpfiles.conf" "$ROOT/profiles/6400-control/profile.json" \
- "$ROOT/sdk"/*.whl "$ROOT/verify-install.sh"; do
- [[ -f "$required" ]] || fail "package is incomplete: $required"
- done
- if [[ -f "$ROOT/SHA256SUMS" ]]; then
- (cd "$ROOT" && sha256sum -c SHA256SUMS >/dev/null) || fail "package checksum verification failed"
- fi
- for absolute_dir in "$BINDIR" "$SHAREDIR" "$ETCDIR" "$UNITDIR" "$STATEDIR" "$RUNDIR"; do
- [[ "$absolute_dir" == /* && "$(realpath -m -s -- "$absolute_dir")" == "$absolute_dir" ]] ||
- fail "unsafe install path: $absolute_dir"
- done
- INSTALLED_META="$ETCDIR/release-metadata.json"
- INSTALLED_VERSION=""
- INSTALLED_COMMIT=""
- if [[ -f "$INSTALLED_META" ]]; then
- INSTALLED_VERSION="$(json_string version "$INSTALLED_META")"
- INSTALLED_COMMIT="$(json_string source_commit "$INSTALLED_META")"
- fi
- FOOTPRINT=0
- for path in "$BINDIR/hthd" "$UNITDIR/hthd.service" "$ETCDIR/hthd.yaml"; do
- [[ -e "$path" || -L "$path" ]] && FOOTPRINT=1
- done
- ACTION=install
- FROM_VERSION=none
- PRESERVE_CONFIG=0
- if [[ "$FOOTPRINT" -eq 1 ]]; then
- if [[ "$INSTALLED_VERSION" == "$VERSION" && "$INSTALLED_COMMIT" == "$SOURCE_COMMIT" && "$UPGRADE" -eq 0 ]]; then
- ACTION=reinstall
- FROM_VERSION="$INSTALLED_VERSION"
- PRESERVE_CONFIG=1
- elif [[ "$UPGRADE" -eq 1 ]]; then
- ACTION=upgrade
- FROM_VERSION="${INSTALLED_VERSION:-$PREVIOUS_RELEASE_VERSION}"
- else
- fail "an existing installation was found; use --upgrade for a different/legacy version"
- fi
- elif [[ "$UPGRADE" -eq 1 ]]; then
- fail "--upgrade requested but no existing HTH installation was found"
- fi
- if [[ "$ACTION" == reinstall && "$BACKEND_EXPLICIT" -eq 1 ]]; then
- PRESERVE_CONFIG=0
- fi
- if [[ -f "$UNITDIR/hthd.service" ]] && ! grep -Fq '/usr/local/bin/hthd --config /etc/hth/hthd.yaml' "$UNITDIR/hthd.service"; then
- fail "existing hthd.service is not the recognized HTH unit; refusing to overwrite it"
- fi
- systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
- if systemctl is-active --quiet "$SERVICE"; then
- fail "$SERVICE is still active after stop"
- fi
- if [[ "$ACTION" == upgrade ]]; then
- rm -f -- \
- "$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"
- rm -f -- \
- "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
- "$ETCDIR/install-manifest.txt" \
- "$ETCDIR/profiles/prod2u-door-temp/profile.json" \
- "$ETCDIR/profiles/6400-control/profile.json"
- rmdir "$ETCDIR/profiles/prod2u-door-temp" 2>/dev/null || true
- if [[ -d "$RUNDIR" ]]; then
- resolved="$(realpath -m -s -- "$RUNDIR")"
- [[ "$resolved" == /run/hth ]] || fail "unsafe runtime path during upgrade: $resolved"
- rm -rf --one-file-system -- "$resolved"
- fi
- systemctl daemon-reload
- fi
- install -d -m 0755 "$BINDIR" "$SHAREDIR/sdk" "$UNITDIR" "$LIBDIR/sysusers.d" "$LIBDIR/tmpfiles.d"
- install -d -m 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
- install -d -m 0750 "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"
- install -m 0755 "$ROOT/bin/hthd" "$BINDIR/hthd"
- install -m 0755 "$ROOT/bin/hthctl" "$BINDIR/hthctl"
- install -m 0755 "$ROOT/bin/hth-capture" "$BINDIR/hth-capture"
- install -m 0644 "$ROOT/service/hthd.service" "$UNITDIR/hthd.service"
- install -m 0644 "$ROOT/service/hth.conf" "$LIBDIR/sysusers.d/hth.conf"
- install -m 0644 "$ROOT/service/hthd-tmpfiles.conf" "$LIBDIR/tmpfiles.d/hth.conf"
- install -m 0644 "$ROOT/RELEASE-METADATA.json" "$ETCDIR/release-metadata.json"
- install -m 0644 "$ROOT/profiles/6400-control/profile.json" "$ETCDIR/profiles/6400-control/profile.json"
- rm -f -- "$SHAREDIR/sdk"/*.whl
- install -m 0644 "$ROOT"/sdk/*.whl "$SHAREDIR/sdk/"
- if [[ "$PRESERVE_CONFIG" -eq 0 || ! -f "$ETCDIR/hthd.yaml" ]]; then
- template="$ROOT/config/hthd.6400-${BACKEND}.yaml"
- temp_config="$(mktemp -t hth-install-config.XXXXXX)"
- trap 'rm -f "${temp_config:-}"' EXIT
- if [[ "$BACKEND" == serial ]]; then
- sed "s|@SERIAL_PATH@|$SERIAL_PATH|g" "$template" >"$temp_config"
- else
- cp "$template" "$temp_config"
- fi
- install -m 0640 "$temp_config" "$ETCDIR/hthd.yaml"
- fi
- cat >"$ETCDIR/install-manifest.txt" <<EOF
- # HTH owned paths; generated by ${VERSION} (${SOURCE_COMMIT})
- file $BINDIR/hthd
- file $BINDIR/hthctl
- file $BINDIR/hth-capture
- file $UNITDIR/hthd.service
- symlink $UNITDIR/multi-user.target.wants/hthd.service
- file $LIBDIR/sysusers.d/hth.conf
- file $LIBDIR/tmpfiles.d/hth.conf
- file $SHAREDIR/sdk/$(basename "$ROOT"/sdk/*.whl)
- config $ETCDIR/hthd.yaml
- config $ETCDIR/release-metadata.json
- config $ETCDIR/install-manifest.txt
- config $ETCDIR/profiles/6400-control/profile.json
- config $ETCDIR/profiles/prod2u-door-temp/profile.json
- managed_tree $RUNDIR
- managed_tree $STATEDIR/captures
- managed_tree $STATEDIR/replay
- managed_tree $STATEDIR/receipts
- account hth
- account hth-observe
- account hth-control
- group hth
- group hth-observe
- group hth-control
- EOF
- chmod 0644 "$ETCDIR/install-manifest.txt"
- getent group dialout >/dev/null 2>&1 || groupadd --system dialout
- systemd-sysusers "$LIBDIR/sysusers.d/hth.conf"
- systemd-tmpfiles --create "$LIBDIR/tmpfiles.d/hth.conf"
- chown root:hth "$ETCDIR" "$ETCDIR/hthd.yaml" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control" 2>/dev/null || true
- chown hth:hth "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts" 2>/dev/null || true
- chmod 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
- chmod 0640 "$ETCDIR/hthd.yaml"
- "$BINDIR/hthd" --check-config --config "$ETCDIR/hthd.yaml"
- systemctl daemon-reload
- if [[ "$NO_START" -eq 0 ]]; then
- systemctl enable --now "$SERVICE"
- for _ in $(seq 1 50); do
- systemctl is-active --quiet "$SERVICE" && break
- sleep 0.1
- done
- systemctl is-active --quiet "$SERVICE" || {
- journalctl -u "$SERVICE" -n 80 --no-pager >&2 || true
- fail "$SERVICE did not become active"
- }
- fi
- receipt="$STATEDIR/receipts/install-latest.txt"
- EFFECTIVE_BACKEND="$(sed -n 's/^[[:space:]]*backend:[[:space:]]*//p' "$ETCDIR/hthd.yaml" | head -n 1)"
- {
- echo "schema=hth-install-receipt-v1"
- echo "status=PASS"
- echo "action=$ACTION"
- echo "from_version=$FROM_VERSION"
- echo "version=$VERSION"
- echo "source_commit=$SOURCE_COMMIT"
- echo "backend=$EFFECTIVE_BACKEND"
- echo "service=$SERVICE"
- echo "config=$ETCDIR/hthd.yaml"
- echo "generated_at_utc=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
- } >"$receipt"
- chown hth:hth "$receipt" 2>/dev/null || true
- chmod 0644 "$receipt"
- if [[ "$NO_START" -eq 0 ]]; then
- "$ROOT/verify-install.sh" --no-escalate
- fi
- echo "HTH_INSTALL_PASS action=$ACTION from=$FROM_VERSION version=$VERSION backend=$EFFECTIVE_BACKEND"
|