solstice-ci/.mise/tasks/ci/local

106 lines
3.6 KiB
Text
Raw Permalink Normal View History

#!/usr/bin/env bash
set -euo pipefail
# End-to-end local run:
# - Start RabbitMQ via docker compose
# - Build workspace
# - Run orchestrator with local defaults
# - Enqueue a job for the current repo/commit
# - Stream logs briefly, then clean up
ROOT_DIR=$(cd "$(dirname "$0")/../../.." && pwd)
cd "$ROOT_DIR"
command -v cargo >/dev/null 2>&1 || { echo "cargo is required" >&2; exit 127; }
# Defaults
export RUST_LOG=${RUST_LOG:-info}
export ORCH_CONFIG=${ORCH_CONFIG:-examples/orchestrator-image-map.yaml}
export AMQP_URL=${AMQP_URL:-amqp://127.0.0.1:5672/%2f}
export AMQP_EXCHANGE=${AMQP_EXCHANGE:-solstice.jobs}
export AMQP_QUEUE=${AMQP_QUEUE:-solstice.jobs.v1}
export AMQP_ROUTING_KEY=${AMQP_ROUTING_KEY:-jobrequest.v1}
export AMQP_PREFETCH=${AMQP_PREFETCH:-2}
export GRPC_ADDR=${GRPC_ADDR:-0.0.0.0:50051}
# Will be used by orchestrator cloud-init to let runner call back
export ORCH_CONTACT_ADDR=${ORCH_CONTACT_ADDR:-$GRPC_ADDR}
# Bring up deps
"$ROOT_DIR/.mise/tasks/dev/up"
# Ensure cleanup
ORCH_PID=""
SERVE_PID=""
cleanup() {
set +e
if [[ -n "$ORCH_PID" ]] && kill -0 "$ORCH_PID" 2>/dev/null; then
echo "Stopping orchestrator (pid=$ORCH_PID)" >&2
kill "$ORCH_PID" 2>/dev/null || true
# give it a moment
sleep 1
kill -9 "$ORCH_PID" 2>/dev/null || true
fi
if [[ -n "$SERVE_PID" ]] && kill -0 "$SERVE_PID" 2>/dev/null; then
echo "Stopping runner server (pid=$SERVE_PID)" >&2
kill "$SERVE_PID" 2>/dev/null || true
fi
"$ROOT_DIR/.mise/tasks/dev/down" || true
}
trap cleanup EXIT INT TERM
# Build required crates (debug)
cargo build -p orchestrator -p forge-integration -p workflow-runner
# Start static server to host the runner for VMs
SOL_RUNNER_PORT=${SOL_RUNNER_PORT:-8089}
# Detect a likely host IP for default libvirt network (virbr0), else fallback to 127.0.0.1
if command -v ip >/dev/null 2>&1 && ip addr show virbr0 >/dev/null 2>&1; then
HOST_IP=$(ip -o -4 addr show virbr0 | awk '{print $4}' | cut -d/ -f1 | head -n1)
else
HOST_IP=${HOST_IP_OVERRIDE:-127.0.0.1}
fi
# Orchestrator contact address for runner to stream logs back
export ORCH_CONTACT_ADDR=${ORCH_CONTACT_ADDR:-$HOST_IP:50051}
# Runner URL used by cloud-init bootstrap
export SOLSTICE_RUNNER_URL=${SOLSTICE_RUNNER_URL:-http://$HOST_IP:$SOL_RUNNER_PORT/solstice-runner}
(
exec "$ROOT_DIR/.mise/tasks/run/runner-serve" >/dev/null 2>&1
) &
SERVE_PID=$!
# Start orchestrator in background
# Enable persistence by default in CI: use local Postgres from docker-compose
export ORCH_SKIP_PERSIST=${ORCH_SKIP_PERSIST:-false}
export DATABASE_URL=${DATABASE_URL:-postgres://solstice:solstice@127.0.0.1:5432/solstice}
LOGFILE=${SOL_ORCH_LOG:-"$ROOT_DIR/target/orchestrator.local.log"}
echo "Starting orchestrator... (logs: $LOGFILE)" >&2
(
exec "$ROOT_DIR/.mise/tasks/run/orchestrator" >"$LOGFILE" 2>&1
) &
ORCH_PID=$!
echo "Runner URL: $SOLSTICE_RUNNER_URL" >&2
echo "Orchestrator contact: $ORCH_CONTACT_ADDR" >&2
# Wait for it to start
sleep 3
# Enqueue a job for this repo/commit
"$ROOT_DIR/.mise/tasks/run/forge-enqueue"
# Tail logs for a short time (or override with SOL_TAIL_SECS)
TAIL_SECS=${SOL_TAIL_SECS:-15}
echo "Tailing orchestrator logs for ${TAIL_SECS}s..." >&2
if command -v timeout >/dev/null 2>&1; then
(timeout ${TAIL_SECS}s tail -f "$LOGFILE" || true) 2>/dev/null
elif command -v gtimeout >/dev/null 2>&1; then
(gtimeout ${TAIL_SECS}s tail -f "$LOGFILE" || true) 2>/dev/null
else
# Fallback: background tail and sleep
tail -f "$LOGFILE" &
TAIL_PID=$!
sleep "$TAIL_SECS" || true
kill "$TAIL_PID" 2>/dev/null || true
fi
echo "Done. Artifacts/logs in $LOGFILE. Use RUST_LOG=debug for more detail." >&2