#cloud-config
# StackShip — OpenClaw Pro Tier Cloud-Init
# Bootstraps Docker + OpenClaw + Ollama + Qdrant on first boot

package_update: true
package_upgrade: true

packages:
  - apt-transport-https
  - ca-certificates
  - curl
  - gnupg
  - lsb-release
  - jq
  - parted
  - xfsprogs

write_files:
  - path: /opt/stackship/docker-compose.yml
    permissions: "0644"
    content: |
      version: "3.8"
      services:
        openclaw:
          image: ghcr.io/openclaw/openclaw:latest
          container_name: stackship-openclaw
          restart: unless-stopped
          ports:
            - "${gateway_port}:18789"
          volumes:
            - openclaw_data:/home/node/.openclaw
          environment:
            - OLLAMA_HOST=http://ollama:11434
            - QDRANT_URL=http://qdrant:6333
%{ if anthropic_api_key != "" ~}
            - ANTHROPIC_API_KEY=${anthropic_api_key}
%{ endif ~}
          depends_on:
            ollama:
              condition: service_started
            qdrant:
              condition: service_healthy
          networks:
            - stackship

        ollama:
          image: ollama/ollama:latest
          container_name: stackship-ollama
          restart: unless-stopped
%{ if gpu_enabled ~}
          deploy:
            resources:
              reservations:
                devices:
                  - driver: nvidia
                    count: all
                    capabilities: [gpu]
%{ endif ~}
          volumes:
            - ollama_models:/root/.ollama
            - /mnt/data/ollama:/mnt/data/ollama
          networks:
            - stackship

        qdrant:
          image: qdrant/qdrant:latest
          container_name: stackship-qdrant
          restart: unless-stopped
          volumes:
            - qdrant_data:/qdrant/storage
          healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:6333/healthz"]
            interval: 10s
            timeout: 5s
            retries: 3
          networks:
            - stackship

      volumes:
        openclaw_data:
        ollama_models:
        qdrant_data:

      networks:
        stackship:
          driver: bridge

  - path: /opt/stackship/pull-models.sh
    permissions: "0755"
    content: |
      #!/bin/bash
      set -e
      echo "[stackship] Waiting for Ollama to be ready..."
      for i in $(seq 1 60); do
        if curl -sf http://localhost:11434/api/tags >/dev/null 2>&1; then
          echo "[stackship] Ollama is ready."
          break
        fi
        sleep 5
      done
%{ for model in models_to_pull ~}
      echo "[stackship] Pulling model: ${model}"
      docker exec stackship-ollama ollama pull ${model} || true
%{ endfor ~}
      echo "[stackship] All models pulled."

  - path: /opt/stackship/setup.sh
    permissions: "0755"
    content: |
      #!/bin/bash
      set -ex

      echo "[stackship] === Starting StackShip setup ==="

      # --- Format and mount data disk (lun0 = /dev/sdc on most Azure VMs) ---
      DATA_DISK="/dev/sdc"
      if [ -b "$DATA_DISK" ]; then
        if ! blkid "$DATA_DISK" | grep -q xfs; then
          echo "[stackship] Formatting data disk..."
          mkfs.xfs "$DATA_DISK"
        fi
        mkdir -p /mnt/data
        mount "$DATA_DISK" /mnt/data || true
        echo "$DATA_DISK /mnt/data xfs defaults,nofail 0 0" >> /etc/fstab
        mkdir -p /mnt/data/ollama /mnt/data/qdrant
      fi

      # --- Install Docker ---
      echo "[stackship] Installing Docker..."
      curl -fsSL https://get.docker.com | sh
      systemctl enable docker
      systemctl start docker

%{ if gpu_enabled ~}
      # --- Install NVIDIA drivers + container toolkit ---
      echo "[stackship] Installing NVIDIA drivers..."
      apt-get install -y linux-modules-extra-$(uname -r) || true
      curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | \
        gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
      curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
        sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
        tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
      apt-get update
      apt-get install -y nvidia-driver-535 nvidia-container-toolkit
      nvidia-ctk runtime configure --runtime=docker
      systemctl restart docker
%{ endif ~}

      # --- Start the stack ---
      echo "[stackship] Starting OpenClaw stack..."
      cd /opt/stackship
      docker compose up -d

      # --- Pull models in background ---
      echo "[stackship] Pulling models in background..."
      nohup /opt/stackship/pull-models.sh > /var/log/stackship-models.log 2>&1 &

      echo "[stackship] === StackShip setup complete ==="

runcmd:
  - /opt/stackship/setup.sh
