#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" # Create .env if missing (do not overwrite) cp -n "${ROOT}/.env.example" "${ROOT}/.env" || true # shellcheck disable=SC1090 source "${ROOT}/.env" ts(){ date -Is; } log_dir="${ROOT}/logs" mkdir -p "$log_dir" log_file="${log_dir}/bootstrap-$(date -Iseconds).log" # log everything (stdout+stderr) exec > >(tee -a "$log_file") 2>&1 echo "[$(ts)] bootstrap: starting (ROOT=$ROOT)" echo "[$(ts)] bootstrap: docker compose up -d" docker compose -f "${ROOT}/docker-compose.yml" up -d echo "[$(ts)] bootstrap: waiting for Ollama API at ${OLLAMA_URL} ..." for i in {1..120}; do if curl -sS "${OLLAMA_URL}/api/tags" >/dev/null 2>&1; then echo "[$(ts)] bootstrap: Ollama API is up." break fi if [[ $i -eq 120 ]]; then echo "[$(ts)] bootstrap: ERROR: API did not come up in time." exit 1 fi sleep 1 done echo "[$(ts)] bootstrap: pulling base model: ${BASE_MODEL}" docker exec -it ollama ollama pull "${BASE_MODEL}" if [[ -n "${EXTRA_MODELS:-}" ]]; then for m in ${EXTRA_MODELS}; do echo "[$(ts)] bootstrap: pulling extra model: $m" docker exec -it ollama ollama pull "$m" done fi echo "[$(ts)] bootstrap: building expert model: ${EXPERT_MODEL}" tmp="$(mktemp)" sed "s/\${BASE_MODEL}/${BASE_MODEL}/g" "${ROOT}/Modelfile" > "$tmp" # Copy Modelfile into container and build from explicit path (robust) docker cp "$tmp" ollama:/tmp/Modelfile.jr-sql-expert docker exec -it ollama ollama create "${EXPERT_MODEL}" -f /tmp/Modelfile.jr-sql-expert rm -f "$tmp" echo "[$(ts)] bootstrap: verifying model exists..." docker exec -it ollama ollama list | grep -F "${EXPERT_MODEL}" >/dev/null && \ echo "[$(ts)] bootstrap: OK: ${EXPERT_MODEL} is available." # End-to-end test if [[ ! -x "${ROOT}/bin/sqlai" ]]; then echo "[$(ts)] bootstrap: ERROR: ${ROOT}/bin/sqlai not found or not executable" ls -la "${ROOT}/bin" || true exit 2 fi echo "[$(ts)] bootstrap: test (running one request)..." echo "SELECT 1;" | "${ROOT}/bin/sqlai" analyze-tsql echo "[$(ts)] bootstrap: test done" echo "[$(ts)] bootstrap: done"