# Deploy Voxtral TTS (Mistral 4B multilingual streaming) to irv-ml1.
#
# Pulls the official vllm/vllm-omni image (Mistral's partner serving
# stack) — no local build. ~3-5 min cold pull on first deploy. First
# container start downloads Voxtral-4B-TTS-2603 (~8 GB BF16) into the
# bind-mounted HF cache. Generous /v1/models wait deadline accommodates
# the first model pull + vLLM warmup.
#
# Usage:
#   scripts/elway irv-ml1 --playbook playbooks/deploy-voxtral.yaml
#
# License caveat: Voxtral is CC BY-NC. Personal / research use only.
# See stacks/voxtral/README.md.
#
# Idempotent — every step is creates-/when-gated; rerun is safe.

vars:
  compose_dir: /opt/docker/compose/voxtral
  voices_dir: /worktank/voxtral/voices
  cache_dir: /worktank/voxtral/hf_cache
  host_port: "8197"

steps:
  # ── host-side dirs ──────────────────────────────────────────────────

  - name: Ensure /worktank/voxtral root exists (one-time, sudo)
    shell: mkdir -p /worktank/voxtral
    sudo: true
    creates: /worktank/voxtral

  - name: Chown /worktank/voxtral to lkraven
    shell: chown -R lkraven:lkraven /worktank/voxtral
    sudo: true
    when: "[ \"$(stat -c %U /worktank/voxtral)\" != \"lkraven\" ]"

  - name: Ensure voices dir exists
    shell: mkdir -p {{ voices_dir }}
    creates: "{{ voices_dir }}"

  - name: Ensure HF cache dir exists
    shell: mkdir -p {{ cache_dir }}
    creates: "{{ cache_dir }}"

  - name: Ensure compose dir exists
    shell: mkdir -p {{ compose_dir }}
    creates: "{{ compose_dir }}"

  # ── deploy compose + env ────────────────────────────────────────────

  - name: Upload compose.yaml
    upload:
      src: stacks/voxtral/compose.yaml
      dest: "{{ compose_dir }}/compose.yaml"
      mode: "0644"

  - name: Seed .env from template (only if absent)
    upload:
      src: stacks/voxtral/.env.example
      dest: "{{ compose_dir }}/.env"
      mode: "0644"
    when: "[ ! -f {{ compose_dir }}/.env ]"

  # ── pull + bring up ─────────────────────────────────────────────────

  - name: docker compose pull (~3-5 min cold)
    shell: cd {{ compose_dir }} && docker compose pull 2>&1 | tail -20

  - name: docker compose up -d
    shell: cd {{ compose_dir }} && docker compose up -d

  - name: Wait for /v1/models to report model loaded (allow ~10 min for first pull + warmup)
    shell: |
      for i in $(seq 1 120); do
        curl -sf -o /dev/null --max-time 3 http://localhost:{{ host_port }}/v1/models && exit 0
        sleep 5
      done
      exit 1
    changed_when: "false"

verify:
  - name: /v1/models reports loaded model
    shell: |
      curl -sf http://localhost:{{ host_port }}/v1/models \
        | python3 -c "import json,sys; d=json.load(sys.stdin); assert any('Voxtral' in m.get('id','') for m in d.get('data',[]))"
    changed_when: "false"

  - name: Container is running
    shell: docker inspect voxtral --format '{{.State.Status}}' | grep -q running
    changed_when: "false"
