|
|
@@ -2,107 +2,238 @@
|
|
|
set -euo pipefail
|
|
|
|
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
-PREFIX="${HTH_PREFIX:-/usr/local}"
|
|
|
-ETCDIR="${HTH_ETC_DIR:-/etc/hth}"
|
|
|
-UNITDIR="${HTH_UNIT_DIR:-/etc/systemd/system}"
|
|
|
-LIBDIR="${HTH_LIB_DIR:-/usr/lib}"
|
|
|
-BACKEND="mock"
|
|
|
-SERIAL_PATH="/dev/hth/controller0"
|
|
|
+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
|
|
|
-FORCE=0
|
|
|
+UPGRADE=0
|
|
|
|
|
|
usage() {
|
|
|
cat >&2 <<'EOF'
|
|
|
-Usage: sudo ./install.sh [--backend mock|serial] [--serial-path PATH]
|
|
|
- [--no-start] [--force]
|
|
|
+Usage: sudo ./install.sh [--upgrade] [--backend mock|serial]
|
|
|
+ [--serial-path PATH] [--no-start]
|
|
|
|
|
|
-The default backend is mock. Use --backend serial explicitly for a real
|
|
|
-controller. The serial path must exist when installing the serial backend.
|
|
|
+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
|
|
|
- --backend) BACKEND="${2:-}"; shift 2 ;;
|
|
|
+ --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 ;;
|
|
|
- --force) FORCE=1; shift ;;
|
|
|
-h|--help) usage; exit 0 ;;
|
|
|
*) echo "install: unknown argument: $1" >&2; usage; exit 2 ;;
|
|
|
esac
|
|
|
done
|
|
|
|
|
|
-if [[ "$(uname -m)" != "x86_64" ]]; then
|
|
|
- echo "install: this release targets x86_64 Linux (found $(uname -m))" >&2
|
|
|
- exit 2
|
|
|
-fi
|
|
|
-if [[ "$(id -u)" -ne 0 ]]; then
|
|
|
- echo "install: run as root, for example: sudo ./install.sh --backend mock" >&2
|
|
|
- exit 2
|
|
|
-fi
|
|
|
-case "$BACKEND" in
|
|
|
- mock|serial) ;;
|
|
|
- *) echo "install: --backend must be mock or serial" >&2; exit 2 ;;
|
|
|
-esac
|
|
|
+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
|
|
|
- echo "install: serial device does not exist: $SERIAL_PATH" >&2
|
|
|
- exit 2
|
|
|
+ fail "serial device does not exist: $SERIAL_PATH"
|
|
|
fi
|
|
|
-for required in "$ROOT/bin/hthd" "$ROOT/bin/hthctl" "$ROOT/bin/hth-capture" \
|
|
|
- "$ROOT/profile.json" "$ROOT/config/hthd.mock.yaml" \
|
|
|
- "$ROOT/config/hthd.serial.yaml" "$ROOT/service/hthd.service" \
|
|
|
- "$ROOT/service/hth.conf" "$ROOT/service/hthd-tmpfiles.conf"; do
|
|
|
- [[ -f "$required" ]] || { echo "install: package is incomplete: $required" >&2; exit 1; }
|
|
|
+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)
|
|
|
+ (cd "$ROOT" && sha256sum -c SHA256SUMS >/dev/null) || fail "package checksum verification failed"
|
|
|
fi
|
|
|
|
|
|
-if [[ -e "$ETCDIR/hthd.yaml" && "$FORCE" -ne 1 ]]; then
|
|
|
- echo "install: refusing to overwrite $ETCDIR/hthd.yaml; use --force after reviewing it" >&2
|
|
|
- exit 3
|
|
|
+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
|
|
|
-for command_name in systemctl systemd-sysusers systemd-tmpfiles; do
|
|
|
- if ! command -v "$command_name" >/dev/null 2>&1; then
|
|
|
- echo "install: required systemd command is missing: $command_name" >&2
|
|
|
- exit 1
|
|
|
- fi
|
|
|
+FOOTPRINT=0
|
|
|
+for path in "$BINDIR/hthd" "$UNITDIR/hthd.service" "$ETCDIR/hthd.yaml"; do
|
|
|
+ [[ -e "$path" || -L "$path" ]] && FOOTPRINT=1
|
|
|
done
|
|
|
|
|
|
-install -d -m 0755 "$PREFIX/bin"
|
|
|
-install -d -m 0755 "$UNITDIR" "$LIBDIR/sysusers.d" "$LIBDIR/tmpfiles.d"
|
|
|
-install -m 0755 "$ROOT/bin/hthd" "$PREFIX/bin/hthd"
|
|
|
-install -m 0755 "$ROOT/bin/hthctl" "$PREFIX/bin/hthctl"
|
|
|
-install -m 0755 "$ROOT/bin/hth-capture" "$PREFIX/bin/hth-capture"
|
|
|
-install -d -m 0750 "$ETCDIR/profiles/prod2u-door-temp"
|
|
|
-install -m 0644 "$ROOT/profile.json" "$ETCDIR/profiles/prod2u-door-temp/profile.json"
|
|
|
+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 -d -m 0750 "$ETCDIR"
|
|
|
-
|
|
|
-TEMPLATE="$ROOT/config/hthd.${BACKEND}.yaml"
|
|
|
-TMP_CONFIG="$(mktemp)"
|
|
|
-trap 'rm -f "$TMP_CONFIG"' EXIT
|
|
|
-if [[ "$BACKEND" == serial ]]; then
|
|
|
- sed "s|@SERIAL_PATH@|${SERIAL_PATH//|/\\|}|g" "$TEMPLATE" > "$TMP_CONFIG"
|
|
|
-else
|
|
|
- cp "$TEMPLATE" "$TMP_CONFIG"
|
|
|
+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
|
|
|
-sed -i "s|/etc/hth/profiles/prod2u-door-temp/profile.json|$ETCDIR/profiles/prod2u-door-temp/profile.json|g" "$TMP_CONFIG"
|
|
|
-install -m 0640 "$TMP_CONFIG" "$ETCDIR/hthd.yaml"
|
|
|
|
|
|
+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/prod2u-door-temp" 2>/dev/null || true
|
|
|
-chmod 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/prod2u-door-temp"
|
|
|
-chmod 0640 "$ETCDIR/hthd.yaml" "$ETCDIR/profiles/prod2u-door-temp/profile.json"
|
|
|
+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"
|
|
|
|
|
|
-"$PREFIX/bin/hthd" --check-config --config "$ETCDIR/hthd.yaml"
|
|
|
+"$BINDIR/hthd" --check-config --config "$ETCDIR/hthd.yaml"
|
|
|
systemctl daemon-reload
|
|
|
if [[ "$NO_START" -eq 0 ]]; then
|
|
|
- systemctl enable --now hthd.service
|
|
|
- systemctl --no-pager --full status hthd.service | sed -n '1,12p'
|
|
|
+ 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 amd64 release installed: backend=$BACKEND config=$ETCDIR/hthd.yaml"
|
|
|
-echo "uninstall: sudo $ROOT/uninstall.sh (add --purge to remove config and data)"
|
|
|
+echo "HTH_INSTALL_PASS action=$ACTION from=$FROM_VERSION version=$VERSION backend=$EFFECTIVE_BACKEND"
|