#cloud-config

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

packages:
- s3cmd

snap:
  commands:
  - snap install microceph --channel=squid/stable
  - snap refresh --hold microceph
  - snap install juju --channel=3.6/stable
  - snap install k8s --classic --channel=1.33-classic/stable
  - snap install terraform --classic

runcmd:
  - |
    # [docs:setup-s3]
    IPADDR=$(ip -4 -j route get 2.2.2.2 | jq -r '.[] | .prefsrc')

    # Ref: https://canonical-microceph.readthedocs-hosted.com/latest/tutorial/get-started/
    echo "Setting up microceph..."
    microceph cluster bootstrap
    microceph disk add loop,4G,3
    ceph status

    # Ref: https://canonical-microceph.readthedocs-hosted.com/latest/reference/commands/enable/
    # (Traefik will take ports 80, 443)
    microceph enable rgw --port 8080 --ssl-port 8443
    microceph.ceph -s
    microceph.radosgw-admin user create --uid=user --display-name=User
    microceph.radosgw-admin key create --uid=user --key-type=s3 --access-key=access-key --secret-key=secret-key
    # [docs:setup-s3-end]

    echo "Setting up K8s..."
    # Adjust system defaults to avoid "failed to create fsnotify watcher: too many open files"
    # https://github.com/canonical/k8s-snap/pull/1619
    sysctl -w fs.inotify.max_user_instances=1024
    sysctl -w fs.inotify.max_user_watches=1048576

    k8s bootstrap
    k8s status --wait-ready
    k8s enable local-storage
    k8s enable load-balancer
    k8s set load-balancer.l2-mode=true load-balancer.cidrs="$IPADDR/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


    # Set up terraform plan for COS
    # [docs:create-terraform-module]
    sudo -u ubuntu mkdir ~ubuntu/cos
    sudo -u ubuntu tee ~ubuntu/cos/cos-demo.tf << EOF 
    terraform {
      required_version = ">= 1.5"
      required_providers {
        juju = {
          source  = "juju/juju"
          version = "~> 1.0"
        }
      }
    }

    resource "juju_model" "cos" {
      name  = "cos"
      config = { logging-config = "<root>=WARNING; unit=DEBUG" }
    }
    
    module "cos" {
      source                          = "git::https://github.com/canonical/observability-stack//terraform/cos?ref=track/2"
      model_uuid                      = juju_model.cos.uuid
      channel                         = "2/stable"
      anti_affinity                   = false
      internal_tls                    = false
      external_certificates_offer_url = null
      s3_endpoint                     = "http://$IPADDR:8080"
      s3_secret_key                   = "secret-key"
      s3_access_key                   = "access-key"
      ssc                             = { channel = "1/stable" }
      traefik                         = { channel = "latest/stable" }
    }
    EOF
    # [docs:create-terraform-module-end]

    echo "Deploying COS..."
    sudo -u ubuntu terraform -chdir=$(echo ~ubuntu/cos) init
    sudo -u ubuntu terraform -chdir=$(echo ~ubuntu/cos) apply -auto-approve
    # The first "terraform apply" might fail due to https://github.com/juju/terraform-provider-juju/issues/767.
    # Running apply again resolves this.
    sudo -u ubuntu terraform -chdir=$(echo ~ubuntu/cos) apply -auto-approve
    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"
