Bash · 3439 bytes Raw Blame History
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 SOCKET_PATH="${GARCARD_SPRINT07_SOCKET:-${PWD}/target/garcard-sprint07.sock}"
5 BACKEND="${GARCARD_SPRINT07_BACKEND:-polkit}"
6 ACTION_ID="${GARCARD_SPRINT07_ACTION_ID:-com.mesonbuild.install.run}"
7 AUTH_CYCLES="${GARCARD_SPRINT07_AUTH_CYCLES:-3}"
8 LOG_FILE="${GARCARD_SPRINT07_LOG:-${PWD}/target/garcard-sprint07.log}"
9 RUN_PKCHECK="${GARCARD_SPRINT07_RUN_PKCHECK:-1}"
10 POLKIT_RESTART_CMD="${GARCARD_SPRINT07_POLKIT_RESTART_CMD:-}"
11
12 if command -v garcard >/dev/null 2>&1; then
13 DAEMON_CMD=(garcard daemon)
14 else
15 DAEMON_CMD=(cargo run -q -p garcard -- daemon)
16 fi
17
18 if command -v garcardctl >/dev/null 2>&1; then
19 CTL_CMD=(garcardctl)
20 else
21 CTL_CMD=(cargo run -q -p garcardctl --)
22 fi
23
24 DAEMON_PID=0
25
26 run_ctl() {
27 GARCARD_SOCKET="${SOCKET_PATH}" "${CTL_CMD[@]}" "$@"
28 }
29
30 wait_for_daemon() {
31 local tries=120
32 while (( tries > 0 )); do
33 if run_ctl ping >/dev/null 2>&1; then
34 return 0
35 fi
36 sleep 0.2
37 tries=$((tries - 1))
38 done
39 echo "daemon did not become ready in time"
40 return 1
41 }
42
43 start_daemon() {
44 GARCARD_SOCKET="${SOCKET_PATH}" \
45 GARCARD_AGENT_BACKEND="${BACKEND}" \
46 "${DAEMON_CMD[@]}" >>"${LOG_FILE}" 2>&1 &
47 DAEMON_PID=$!
48 wait_for_daemon
49 }
50
51 stop_daemon() {
52 if [[ "${DAEMON_PID}" -gt 0 ]] && kill -0 "${DAEMON_PID}" 2>/dev/null; then
53 run_ctl quit >/dev/null 2>&1 || true
54 wait "${DAEMON_PID}" 2>/dev/null || true
55 fi
56 DAEMON_PID=0
57 }
58
59 cleanup() {
60 stop_daemon
61 rm -f "${SOCKET_PATH}"
62 }
63 trap cleanup EXIT
64
65 mkdir -p "$(dirname "${SOCKET_PATH}")"
66 mkdir -p "$(dirname "${LOG_FILE}")"
67 rm -f "${SOCKET_PATH}" "${LOG_FILE}"
68
69 echo "Sprint 07 lifecycle validation"
70 echo " socket: ${SOCKET_PATH}"
71 echo " backend: ${BACKEND}"
72 echo " action: ${ACTION_ID}"
73 echo " log: ${LOG_FILE}"
74
75 start_daemon
76
77 echo "[1/5] Baseline lifecycle and diagnostics surface"
78 run_ctl status
79 run_ctl auth-summary
80 run_ctl diagnose
81 run_ctl temp-list || true
82
83 echo "[2/5] Repeated auth + revocation loop (${AUTH_CYCLES} iterations)"
84 if [[ "${RUN_PKCHECK}" == "1" ]] && command -v pkcheck >/dev/null 2>&1; then
85 pkcheck --revoke-temp || true
86 for i in $(seq 1 "${AUTH_CYCLES}"); do
87 echo " cycle ${i}: trigger auth"
88 set +e
89 pkcheck --allow-user-interaction --process "$$" --action-id "${ACTION_ID}"
90 rc=$?
91 set -e
92 echo " cycle ${i}: pkcheck rc=${rc}"
93 run_ctl auth-summary || true
94 run_ctl temp-list || true
95 run_ctl temp-revoke-all || true
96 run_ctl temp-list || true
97 done
98 else
99 echo " skipped (set GARCARD_SPRINT07_RUN_PKCHECK=1 and install pkcheck)"
100 fi
101
102 echo "[3/5] Daemon restart resilience"
103 run_ctl quit >/dev/null
104 wait "${DAEMON_PID}" 2>/dev/null || true
105 DAEMON_PID=0
106 start_daemon
107 run_ctl status
108 run_ctl diagnose
109
110 echo "[4/5] Optional polkit restart check"
111 if [[ -n "${POLKIT_RESTART_CMD}" ]]; then
112 echo " running: ${POLKIT_RESTART_CMD}"
113 set +e
114 bash -lc "${POLKIT_RESTART_CMD}"
115 restart_rc=$?
116 set -e
117 echo " restart command rc=${restart_rc}"
118 if [[ "${restart_rc}" -eq 0 ]]; then
119 run_ctl quit >/dev/null
120 wait "${DAEMON_PID}" 2>/dev/null || true
121 DAEMON_PID=0
122 start_daemon
123 run_ctl diagnose
124 run_ctl temp-list || true
125 fi
126 else
127 echo " skipped (set GARCARD_SPRINT07_POLKIT_RESTART_CMD, e.g. 'sudo systemctl restart polkit')"
128 fi
129
130 echo "[5/5] Final summary snapshot"
131 run_ctl temp-revoke-all || true
132 run_ctl status
133 run_ctl auth-summary
134 run_ctl diagnose
135
136 echo "Validation complete. Log output:"
137 echo " ${LOG_FILE}"