|
| 1 | +#!/bin/bash |
| 2 | +# server.sh — Helpers for starting, stopping, and health-checking the torc server. |
| 3 | +# |
| 4 | +# Requires: TORC_BIN, TORC_SERVER_BIN, RUN_DIR to be set before sourcing. |
| 5 | + |
| 6 | +# start_server DB_PATH PORT HOST |
| 7 | +# Starts the torc server with a SQLite database at DB_PATH on PORT. |
| 8 | +# HOST is the hostname/IP the server binds to (must be reachable from compute nodes). |
| 9 | +# Sets SERVER_PID and TORC_API_URL. |
| 10 | +# Enables authentication with two users: "admin" and the current $USER. |
| 11 | +start_server() { |
| 12 | + local db_path="$1" |
| 13 | + local port="$2" |
| 14 | + local host="$3" |
| 15 | + local log_file="${RUN_DIR}/server.log" |
| 16 | + local htpasswd_file="${RUN_DIR}/htpasswd" |
| 17 | + |
| 18 | + # Generate a random password for test users |
| 19 | + if [ -f /usr/share/dict/words ] && command -v shuf &>/dev/null; then |
| 20 | + TORC_TEST_PASSWORD=$(shuf -n3 /usr/share/dict/words | tr '\n' '-' | sed 's/-$//') |
| 21 | + else |
| 22 | + TORC_TEST_PASSWORD=$(head -c 24 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 16) |
| 23 | + fi |
| 24 | + |
| 25 | + # Create htpasswd file with admin and current user |
| 26 | + echo "Creating auth users (admin, $USER)..." |
| 27 | + "$TORC_HTPASSWD_BIN" add --file "$htpasswd_file" admin --password "$TORC_TEST_PASSWORD" |
| 28 | + "$TORC_HTPASSWD_BIN" add --file "$htpasswd_file" "$USER" --password "$TORC_TEST_PASSWORD" |
| 29 | + |
| 30 | + # Export password so all torc CLI calls authenticate automatically |
| 31 | + export TORC_PASSWORD="$TORC_TEST_PASSWORD" |
| 32 | + |
| 33 | + echo "Starting torc server on ${host}:${port} with database $db_path (auth enabled)..." |
| 34 | + DATABASE_URL="sqlite:${db_path}" "$TORC_SERVER_BIN" run \ |
| 35 | + --host "$host" -p "$port" \ |
| 36 | + --require-auth \ |
| 37 | + --auth-file "$htpasswd_file" \ |
| 38 | + --admin-user admin \ |
| 39 | + --enforce-access-control \ |
| 40 | + --completion-check-interval-secs 5 \ |
| 41 | + >"$log_file" 2>&1 & |
| 42 | + SERVER_PID=$! |
| 43 | + |
| 44 | + export TORC_API_URL="http://${host}:${port}/torc-service/v1" |
| 45 | + |
| 46 | + # Wait for server to become healthy |
| 47 | + wait_for_server "$port" 30 |
| 48 | +} |
| 49 | + |
| 50 | +# wait_for_server PORT TIMEOUT_SECONDS |
| 51 | +# Polls the server health endpoint until it responds or timeout. |
| 52 | +wait_for_server() { |
| 53 | + local port="$1" |
| 54 | + local timeout="${2:-30}" |
| 55 | + local elapsed=0 |
| 56 | + |
| 57 | + while [ "$elapsed" -lt "$timeout" ]; do |
| 58 | + if "$TORC_BIN" ping >/dev/null 2>&1; then |
| 59 | + echo "Server is healthy (port $port)." |
| 60 | + return 0 |
| 61 | + fi |
| 62 | + sleep 1 |
| 63 | + elapsed=$((elapsed + 1)) |
| 64 | + done |
| 65 | + |
| 66 | + echo "ERROR: Server did not become healthy within ${timeout}s." |
| 67 | + if [ -f "${RUN_DIR}/server.log" ]; then |
| 68 | + echo "Server log (last 20 lines):" |
| 69 | + tail -20 "${RUN_DIR}/server.log" |
| 70 | + fi |
| 71 | + return 1 |
| 72 | +} |
| 73 | + |
| 74 | +# stop_server |
| 75 | +# Kills the server process if running. |
| 76 | +stop_server() { |
| 77 | + if [ -n "${SERVER_PID:-}" ] && kill -0 "$SERVER_PID" 2>/dev/null; then |
| 78 | + echo "Stopping server (PID $SERVER_PID)..." |
| 79 | + kill "$SERVER_PID" 2>/dev/null || true |
| 80 | + wait "$SERVER_PID" 2>/dev/null || true |
| 81 | + SERVER_PID="" |
| 82 | + fi |
| 83 | +} |
| 84 | + |
| 85 | +# is_server_alive |
| 86 | +# Returns 0 if the server process is still running and responsive, 1 otherwise. |
| 87 | +is_server_alive() { |
| 88 | + if [ -z "${SERVER_PID:-}" ]; then |
| 89 | + return 1 |
| 90 | + fi |
| 91 | + if ! kill -0 "$SERVER_PID" 2>/dev/null; then |
| 92 | + return 1 |
| 93 | + fi |
| 94 | + # Also verify the server is responsive |
| 95 | + "$TORC_BIN" ping >/dev/null 2>&1 |
| 96 | +} |
| 97 | + |
| 98 | +# find_free_port |
| 99 | +# Prints a free TCP port. Falls back to a random port in 10000-60000. |
| 100 | +find_free_port() { |
| 101 | + if command -v python3 &>/dev/null; then |
| 102 | + python3 -c " |
| 103 | +import socket |
| 104 | +s = socket.socket() |
| 105 | +s.bind(('', 0)) |
| 106 | +print(s.getsockname()[1]) |
| 107 | +s.close() |
| 108 | +" 2>/dev/null && return |
| 109 | + fi |
| 110 | + # Fallback: random port |
| 111 | + echo $((RANDOM % 50000 + 10000)) |
| 112 | +} |
0 commit comments