#!/bin/bash # start.sh — Launch fine-tuner with SSH tunnel to PostgreSQL # # Usage: # ./scripts/start.sh # run fine-tuner # ./scripts/start.sh --dry-run # dry run (no training) # ./scripts/start.sh --status # show status only set -euo pipefail cd "$(dirname "$0")/.." TUNNEL_PORT="${TUNNEL_PORT:-5434}" SSH_HOST="${FT_SSH_HOST:?Set FT_SSH_HOST before running this script}" REMOTE_PG_PORT="${REMOTE_PG_PORT:-5432}" echo "[start.sh] Opening SSH tunnel to ${SSH_HOST}:${REMOTE_PG_PORT} -> localhost:${TUNNEL_PORT}" # Kill any existing tunnel on that port pkill -f "ssh.*${TUNNEL_PORT}:localhost:${REMOTE_PG_PORT}" 2>/dev/null || true sleep 1 # Open tunnel in background ssh -N -L "${TUNNEL_PORT}:localhost:${REMOTE_PG_PORT}" "${SSH_HOST}" & TUNNEL_PID=$! # Wait for tunnel to be ready sleep 3 echo "[start.sh] Tunnel established (PID: ${TUNNEL_PID})" # Cleanup tunnel on exit cleanup() { echo "[start.sh] Closing SSH tunnel (PID: ${TUNNEL_PID})" kill "${TUNNEL_PID}" 2>/dev/null || true } trap cleanup EXIT # Run fine-tuner with tunnel DB URL # Fine-tuner reads FT_DB_URL (see src/main.py load_config) export FT_DB_URL="${FT_DB_URL:-postgresql://llm@localhost:${TUNNEL_PORT}/llm_gateway}" export FT_GATEWAY_URL="${FT_GATEWAY_URL:-http://localhost:3100}" export FT_OLLAMA_URL="${FT_OLLAMA_URL:-http://localhost:11434}" echo "[start.sh] Starting fine-tuner..." # Route --status / --dry-run / --task-type / --general / --dpo to manual_trigger.py # No args = daemon loop via python3 -m src.main if [ "$#" -eq 0 ]; then python3 -m src.main else python3 scripts/manual_trigger.py "$@" fi