# R15 + R16 box stand-up on irv-ml1 — SUDO phase (run AS infra-ops).
#
#   scripts/elway infra-ops@10.100.79.3 --playbook playbooks/irv-ml1-r15-r16-sudo.yaml
#
# infra-ops has NOPASSWD sudo (see playbooks/bootstrap-infra-ops-user.yaml), so
# this runs unattended — no operator password prompt.
#
# Does two things:
#   1. R16 soundgen engine: install R + the system libs soundgen's CRAN deps
#      need (fftw/sndfile + the usual R-build trio). The soundgen package itself
#      compiles separately (slow) — kicked outside this playbook.
#   2. R15 GPU placement: pin the Ollama service to the A6000 so inference lands
#      in the A6000's free headroom. yt-voice-clipper + R14 keep running on the
#      same card — we share the ~31.5 GB free, we do NOT free the card.
#
# A6000 pinned BY UUID, not index. CUDA's default ordering (FASTEST_FIRST) does
# not match nvidia-smi's PCI order on this box (R14 sees the A6000 as cuda:0
# while nvidia-smi lists it as index 1), so an index pin is ambiguous and could
# select the 3090. The UUID is unambiguous.

vars:
  a6000_uuid: "GPU-9672f0d5-3caf-b15c-898a-cd9d653c654b"

steps:
  - name: apt update
    sudo: true
    shell: apt-get update -qq

  - name: Install R + soundgen system deps (fftw/sndfile + R-build libs)
    sudo: true
    shell: >
      DEBIAN_FRONTEND=noninteractive apt-get install -y
      r-base libfftw3-dev libsndfile1-dev
      libcurl4-openssl-dev libssl-dev libxml2-dev
    when: "! command -v Rscript >/dev/null 2>&1"

  # gcc/gfortran version mismatch fix. irv-ml1's default `gcc` is gcc-11
  # (deliberate — the CUDA/torch R14 stack pins it), but the only gfortran is
  # gfortran-12, whose libgfortran-12-dev puts the unversioned `libgfortran.so`
  # ONLY in the gcc-12 dir. R links shared objects with SHLIB_LD=$(CC)=gcc-11 +
  # `-lgfortran`, so every Fortran package (quadprog→tseries→soundgen, etc.)
  # fails "cannot find -lgfortran". libgfortran-11-dev adds the symlink to the
  # gcc-11 dir. Additive — does NOT change the default toolchain.
  - name: Ensure the default gcc's Fortran link lib resolves (gcc-11 here)
    sudo: true
    shell: DEBIAN_FRONTEND=noninteractive apt-get install -y libgfortran-11-dev
    when: "! gcc -print-file-name=libgfortran.so | grep -q /"

  - name: Pin the Ollama service to the A6000 (UUID) via systemd drop-in
    sudo: true
    shell: |
      install -d -m 755 /etc/systemd/system/ollama.service.d
      printf '[Service]\nEnvironment=CUDA_VISIBLE_DEVICES=%s\n' '{{ a6000_uuid }}' \
        > /etc/systemd/system/ollama.service.d/gpu-pin.conf
      systemctl daemon-reload
      systemctl restart ollama
    when: "! systemctl show ollama -p Environment 2>/dev/null | grep -q '{{ a6000_uuid }}'"

verify:
  - name: Rscript present
    shell: command -v Rscript
    changed_when: "false"
  - name: Ollama service carries the A6000 UUID pin
    sudo: true
    shell: systemctl show ollama -p Environment | grep -q '{{ a6000_uuid }}'
    changed_when: "false"
  - name: Ollama API healthy on :11434 after restart
    shell: curl -sf http://localhost:11434/api/tags >/dev/null && echo ok
    changed_when: "false"
