#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" EXPECTED_VERSION="$(tr -d ' \n\r\t' < "$ROOT_DIR/VERSION")" REPORT_DIR="$ROOT_DIR/run/deploy-check" TS="$(date +%Y%m%d-%H%M%S)" REPORT="$REPORT_DIR/deploy-check-$TS.txt" mkdir -p "$REPORT_DIR" pass() { echo "[PASS] $*" | tee -a "$REPORT"; } fail() { echo "[FAIL] $*" | tee -a "$REPORT"; FAIL_COUNT=$((FAIL_COUNT+1)); } info() { echo "[INFO] $*" | tee -a "$REPORT"; } FAIL_COUNT=0 info "root=$ROOT_DIR" info "expected_version=$EXPECTED_VERSION" # 1) 二进制版本校验 for bin in "$ROOT_DIR/bin/inp2ps" "$ROOT_DIR/bin/inp2pc"; do if [[ ! -x "$bin" ]]; then fail "binary missing or not executable: $bin" continue fi ver_out="$($bin -version 2>/dev/null || true)" if [[ -z "$ver_out" ]]; then fail "cannot read version: $bin" continue fi got_ver="$(echo "$ver_out" | awk '/^inp2p[sc] version/{print $3; exit}')" if [[ "$got_ver" == "$EXPECTED_VERSION" ]]; then pass "$(basename "$bin") version ok: $got_ver" else fail "$(basename "$bin") version mismatch: got=$got_ver expected=$EXPECTED_VERSION" fi echo "$ver_out" | sed 's/^/[BIN] /' | tee -a "$REPORT" >/dev/null done # 2) systemd 进程与路径校验(仅 inp2ps.service) if systemctl show inp2ps.service -p Id --value >/dev/null 2>&1; then pass "inp2ps.service exists" active_state="$(systemctl is-active inp2ps.service || true)" if [[ "$active_state" == "active" ]]; then pass "inp2ps.service active" else fail "inp2ps.service not active: $active_state" fi main_pid="$(systemctl show inp2ps.service -p MainPID --value 2>/dev/null || echo 0)" if [[ -n "$main_pid" && "$main_pid" != "0" ]]; then pass "inp2ps.service main pid: $main_pid" exe_path="$(readlink -f "/proc/$main_pid/exe" 2>/dev/null || true)" if [[ "$exe_path" == "$ROOT_DIR/bin/inp2ps" ]]; then pass "inp2ps.service uses expected binary: $exe_path" else fail "inp2ps.service binary path mismatch: $exe_path" fi else fail "inp2ps.service MainPID is empty/0" fi else fail "inp2ps.service not found" fi # 3) 残留进程检查(inp2ps/inp2pc) mapfile -t pids < <(pgrep -f '(^|/)inp2p[sc](\s|$)' || true) if [[ ${#pids[@]} -eq 0 ]]; then pass "no inp2p processes running" else info "found inp2p-related pids: ${pids[*]}" allow_pid="$(systemctl show inp2ps.service -p MainPID --value 2>/dev/null || echo 0)" residual=0 for pid in "${pids[@]}"; do exe="$(readlink -f "/proc/$pid/exe" 2>/dev/null || true)" cmd="$(tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null || true)" echo "[PROC] pid=$pid exe=$exe cmd=$cmd" | tee -a "$REPORT" >/dev/null if [[ "$pid" == "$allow_pid" ]]; then continue fi # 允许合法 systemd-run 的 inp2pc 客户端由独立服务名管理:后续可加白名单 residual=$((residual+1)) done if [[ $residual -eq 0 ]]; then pass "no residual inp2p process (except inp2ps main pid=$allow_pid)" else fail "residual inp2p processes detected: $residual" fi fi # 4) 旧二进制残留(根目录/临时后缀) residual_bins=0 while IFS= read -r f; do [[ -z "$f" ]] && continue echo "[FILE] residual-bin: $f" | tee -a "$REPORT" >/dev/null residual_bins=$((residual_bins+1)) done < <(find "$ROOT_DIR" -maxdepth 2 -type f \( -name 'inp2ps.old*' -o -name 'inp2pc.old*' -o -name 'inp2ps.new*' -o -name 'inp2pc.new*' -o -path "$ROOT_DIR/inp2ps" -o -path "$ROOT_DIR/inp2pc" \) 2>/dev/null) if [[ $residual_bins -eq 0 ]]; then pass "no residual old/new binaries found" else fail "residual binary files found: $residual_bins" fi # 5) 输出总结 if [[ $FAIL_COUNT -eq 0 ]]; then pass "deploy check passed" info "report=$REPORT" exit 0 else fail "deploy check failed (count=$FAIL_COUNT)" info "report=$REPORT" exit 1 fi