-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathctl.sh
More file actions
executable file
·373 lines (340 loc) · 9.58 KB
/
ctl.sh
File metadata and controls
executable file
·373 lines (340 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HERMES_HOME="${HERMES_HOME:-${HOME}/.hermes}"
PID_FILE="${HERMES_WEBUI_PID_FILE:-${HERMES_HOME}/webui.pid}"
LOG_FILE="${HERMES_WEBUI_LOG_FILE:-${HERMES_HOME}/webui.log}"
STATE_FILE="${HERMES_WEBUI_CTL_STATE_FILE:-${HERMES_HOME}/webui.ctl.env}"
DEFAULT_STATE_DIR="${HERMES_WEBUI_STATE_DIR:-${HERMES_HOME}/webui}"
usage() {
cat <<'EOF'
Usage: ./ctl.sh <command> [args]
Commands:
start [bootstrap args...] Start Hermes WebUI as a background daemon
stop Stop the daemon started by ctl.sh
restart [bootstrap args...] Stop, then start again
status Show daemon, host/port, log, and health status
logs [--lines N] [--follow|--no-follow]
Show the daemon log (defaults to tail -n 100 -f)
EOF
}
ensure_home() {
mkdir -p "${HERMES_HOME}" "${DEFAULT_STATE_DIR}"
}
_load_repo_dotenv_preserving_env() {
local env_file="${REPO_ROOT}/.env"
[[ -f "${env_file}" ]] || return 0
local -a preserved=()
local line key value
while IFS= read -r line || [[ -n "${line}" ]]; do
line="${line#${line%%[![:space:]]*}}"
[[ -z "${line}" || "${line}" == \#* || "${line}" != *=* ]] && continue
key="${line%%=*}"
key="${key#export }"
key="${key//[[:space:]]/}"
[[ "${key}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue
if [[ -n "${!key+x}" ]]; then
value="${!key}"
preserved+=("${key}=${value}")
fi
done < "${env_file}"
set -a
# shellcheck source=/dev/null
source "${env_file}"
set +a
local assignment
if [[ ${#preserved[@]} -gt 0 ]]; then
for assignment in "${preserved[@]}"; do
export "${assignment}"
done
fi
}
_find_python() {
if [[ -n "${HERMES_WEBUI_PYTHON:-}" ]]; then
printf '%s\n' "${HERMES_WEBUI_PYTHON}"
elif command -v python3 >/dev/null 2>&1; then
command -v python3
elif command -v python >/dev/null 2>&1; then
command -v python
else
echo "[ctl] Python 3 is required to run bootstrap.py" >&2
return 1
fi
}
_parse_launch_binding() {
CTL_HOST="${HERMES_WEBUI_HOST:-127.0.0.1}"
CTL_PORT="${HERMES_WEBUI_PORT:-8787}"
local arg next_is_host=0 saw_port=0
for arg in "$@"; do
if (( next_is_host )); then
CTL_HOST="${arg}"
next_is_host=0
continue
fi
case "${arg}" in
--host)
next_is_host=1
;;
--host=*)
CTL_HOST="${arg#--host=}"
;;
--*)
;;
*)
if (( ! saw_port )) && [[ "${arg}" =~ ^[0-9]+$ ]]; then
CTL_PORT="${arg}"
saw_port=1
fi
;;
esac
done
}
_build_bootstrap_args() {
CTL_BOOTSTRAP_ARGS=()
local arg next_is_host=0 saw_port=0
for arg in "$@"; do
if (( next_is_host )); then
next_is_host=0
continue
fi
case "${arg}" in
--host)
next_is_host=1
;;
--host=*)
;;
--*)
CTL_BOOTSTRAP_ARGS+=("${arg}")
;;
*)
if (( ! saw_port )) && [[ "${arg}" =~ ^[0-9]+$ ]]; then
saw_port=1
else
CTL_BOOTSTRAP_ARGS+=("${arg}")
fi
;;
esac
done
}
_write_state() {
local pid="$1" host="$2" port="$3" python_exe="${4:-}"
local state_dir="${HERMES_WEBUI_STATE_DIR:-${DEFAULT_STATE_DIR}}"
{
printf 'PID=%q\n' "${pid}"
printf 'REPO_ROOT=%q\n' "${REPO_ROOT}"
printf 'PYTHON_EXE=%q\n' "${python_exe}"
printf 'HOST=%q\n' "${host}"
printf 'PORT=%q\n' "${port}"
printf 'LOG_FILE=%q\n' "${LOG_FILE}"
printf 'STATE_DIR=%q\n' "${state_dir}"
printf 'STARTED_AT=%q\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
} > "${STATE_FILE}"
}
_load_state_if_present() {
if [[ -f "${STATE_FILE}" ]]; then
# shellcheck source=/dev/null
source "${STATE_FILE}"
fi
}
_pid_from_file() {
[[ -f "${PID_FILE}" ]] || return 1
local pid
pid="$(tr -d '[:space:]' < "${PID_FILE}")"
[[ "${pid}" =~ ^[0-9]+$ ]] || return 1
printf '%s\n' "${pid}"
}
_is_alive() {
local pid="$1"
kill -0 "${pid}" >/dev/null 2>&1
}
_proc_args() {
local pid="$1"
ps -p "${pid}" -o args= 2>/dev/null || true
}
_is_owned_webui_pid() {
local pid="$1" args state_repo="" state_python=""
[[ -f "${STATE_FILE}" ]] || return 1
_load_state_if_present
state_repo="${REPO_ROOT:-}"
state_python="${PYTHON_EXE:-}"
[[ "${state_repo}" == "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ]] || return 1
args="$(_proc_args "${pid}")"
[[ -n "${args}" ]] || return 1
[[ "${args}" == *"${state_repo}/bootstrap.py"* || "${args}" == *"${state_repo}/server.py"* || "${args}" == *"${state_repo}/start.sh"* || ( -n "${state_python}" && "${args}" == *"${state_python}"* ) ]]
}
_current_pid() {
local pid
pid="$(_pid_from_file)" || return 1
if _is_alive "${pid}" && _is_owned_webui_pid "${pid}"; then
printf '%s\n' "${pid}"
return 0
fi
return 1
}
_clear_stale_pid() {
if [[ -f "${PID_FILE}" ]]; then
rm -f "${PID_FILE}" "${STATE_FILE}"
echo "[ctl] Removed stale PID file: ${PID_FILE}"
fi
}
start_cmd() {
ensure_home
_load_repo_dotenv_preserving_env
export HERMES_WEBUI_STATE_DIR="${HERMES_WEBUI_STATE_DIR:-${DEFAULT_STATE_DIR}}"
mkdir -p "${HERMES_WEBUI_STATE_DIR}"
_parse_launch_binding "$@"
_build_bootstrap_args "$@"
export HERMES_WEBUI_HOST="${CTL_HOST}"
export HERMES_WEBUI_PORT="${CTL_PORT}"
local existing_pid
if existing_pid="$(_current_pid 2>/dev/null)"; then
echo "[ctl] Hermes WebUI is already running (PID ${existing_pid})"
return 0
fi
_clear_stale_pid >/dev/null 2>&1 || true
local python_exe pid
python_exe="$(_find_python)"
: >> "${LOG_FILE}"
(
cd "${REPO_ROOT}"
trap '' HUP
export HERMES_WEBUI_PRESERVE_ENV=1
exec nohup "${python_exe}" "${REPO_ROOT}/bootstrap.py" --no-browser --foreground --host "${CTL_HOST}" "${CTL_PORT}" ${CTL_BOOTSTRAP_ARGS[@]+"${CTL_BOOTSTRAP_ARGS[@]}"}
) >> "${LOG_FILE}" 2>&1 &
pid=$!
printf '%s\n' "${pid}" > "${PID_FILE}"
_write_state "${pid}" "${CTL_HOST}" "${CTL_PORT}" "${python_exe}"
sleep 0.15
if ! _is_alive "${pid}"; then
echo "[ctl] Hermes WebUI failed to stay running. Log: ${LOG_FILE}" >&2
rm -f "${PID_FILE}" "${STATE_FILE}"
return 1
fi
echo "[ctl] Started Hermes WebUI (PID ${pid})"
echo "[ctl] Bound: ${CTL_HOST}:${CTL_PORT}"
echo "[ctl] Log: ${LOG_FILE}"
}
stop_cmd() {
ensure_home
local pid
if ! pid="$(_pid_from_file 2>/dev/null)"; then
echo "[ctl] Hermes WebUI is stopped"
rm -f "${PID_FILE}" "${STATE_FILE}"
return 0
fi
if ! _is_alive "${pid}" || ! _is_owned_webui_pid "${pid}"; then
_clear_stale_pid
return 0
fi
echo "[ctl] Stopping Hermes WebUI (PID ${pid})"
kill "${pid}" >/dev/null 2>&1 || true
local i
for i in {1..50}; do
if ! _is_alive "${pid}"; then
rm -f "${PID_FILE}" "${STATE_FILE}"
echo "[ctl] Stopped"
return 0
fi
sleep 0.1
done
echo "[ctl] Process did not exit after SIGTERM; sending SIGKILL" >&2
kill -KILL "${pid}" >/dev/null 2>&1 || true
rm -f "${PID_FILE}" "${STATE_FILE}"
}
_health_line() {
local host="$1" port="$2" url result
url="http://${host}:${port}/health"
if command -v curl >/dev/null 2>&1; then
if result="$(curl -fsS --max-time 2 "${url}" 2>/dev/null)"; then
if command -v python3 >/dev/null 2>&1; then
printf '%s' "${result}" | python3 -c 'import json,sys
try:
data=json.load(sys.stdin)
sessions=data.get("sessions", data.get("session_count", "?"))
active=data.get("active_streams", "?")
status=data.get("status", "ok")
print(f"ok ({sessions} sessions, {active} active streams)" if status == "ok" else status)
except Exception:
print("ok")'
else
echo "ok"
fi
else
echo "unreachable (${url})"
fi
else
echo "unknown (curl not found; ${url})"
fi
}
status_cmd() {
ensure_home
_load_state_if_present
local host="${HOST:-${HERMES_WEBUI_HOST:-127.0.0.1}}"
local port="${PORT:-${HERMES_WEBUI_PORT:-8787}}"
local log_path="${LOG_FILE}"
local pid uptime health
if pid="$(_current_pid 2>/dev/null)"; then
uptime="$(ps -p "${pid}" -o etime= 2>/dev/null | sed 's/^ *//' || true)"
health="$(_health_line "${host}" "${port}")"
echo "● hermes-webui — running"
echo " PID: ${pid}"
echo " Uptime: ${uptime:-unknown}"
echo " Bound: ${host}:${port}"
echo " Log: ${log_path}"
echo " Health: ${health}"
else
[[ -f "${PID_FILE}" ]] && _clear_stale_pid >/dev/null 2>&1 || true
echo "● hermes-webui — stopped"
echo " PID: -"
echo " Bound: ${host}:${port}"
echo " Log: ${log_path}"
echo " Health: not checked"
fi
}
logs_cmd() {
ensure_home
local lines=100 follow=1
while [[ $# -gt 0 ]]; do
case "$1" in
--lines)
shift
lines="${1:-}"
[[ "${lines}" =~ ^[0-9]+$ ]] || { echo "[ctl] --lines requires a number" >&2; return 2; }
;;
--lines=*)
lines="${1#--lines=}"
[[ "${lines}" =~ ^[0-9]+$ ]] || { echo "[ctl] --lines requires a number" >&2; return 2; }
;;
--follow|-f)
follow=1
;;
--no-follow)
follow=0
;;
*)
echo "[ctl] Unknown logs option: $1" >&2
return 2
;;
esac
shift
done
touch "${LOG_FILE}"
if (( follow )); then
tail -n "${lines}" -f "${LOG_FILE}"
else
tail -n "${lines}" "${LOG_FILE}"
fi
}
cmd="${1:-}"
if [[ $# -gt 0 ]]; then
shift
fi
case "${cmd}" in
start) start_cmd "$@" ;;
stop) stop_cmd ;;
restart) stop_cmd; start_cmd "$@" ;;
status) status_cmd ;;
logs) logs_cmd "$@" ;;
-h|--help|help|"") usage ;;
*) echo "[ctl] Unknown command: ${cmd}" >&2; usage >&2; exit 2 ;;
esac