#cloud-config

# Usage example:
#   multipass launch noble --cloud-init ./cos-lite-canonical-k8s-sandbox.conf --name cos --memory 8G --cpus 4 --disk 40G
#   multipass exec cos -- tail -f /var/log/cloud-init-output.log

snap:
  commands:
  - snap install juju --channel=3.6/stable
  - snap install k8s --classic --channel=1.33-classic/stable

runcmd:
  - |
    echo "Setting up K8s...""
    k8s bootstrap
    k8s status --wait-ready
    k8s enable local-storage
    k8s enable load-balancer
    k8s set load-balancer.l2-mode=true load-balancer.cidrs="$(ip -4 -j route get 2.2.2.2 | jq -r '.[] | .prefsrc')/32"
    k8s status --wait-ready

    echo "Adding K8s cloud to Juju..."
    k8s kubectl config view --raw | sudo -u ubuntu /snap/juju/current/bin/juju add-k8s ck8s --client
    echo "Bootstrapping K8s controller..."
    sudo -u ubuntu juju bootstrap ck8s

    echo "Deploying COS Lite..."
    sudo -u ubuntu juju add-model cos
    sudo -u ubuntu curl -o ~ubuntu/offers-overlay.yaml -fL https://raw.githubusercontent.com/canonical/cos-lite-bundle/main/overlays/offers-overlay.yaml
    sudo -u ubuntu juju deploy cos-lite --trust --overlay ~ubuntu/offers-overlay.yaml
    echo "Waiting for model to settle..."
    sudo -u ubuntu juju wait-for model cos --query='forEach(units, unit => unit.agent-status == "idle") && forEach(applications, app => app.status == "active")' --timeout=10m

final_message: "The COS Lite appliance is ready, after $UPTIME seconds"
