# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrant box for testing kind with non-Ubuntu
Vagrant.configure("2") do |config|
  # `config.vm.box = "fedora/37-cloud-base"` seems flaky,
  # so we specify the URL explicitly.
  # Mirrors can be found at here: https://admin.fedoraproject.org/mirrormanager/mirrors/Fedora/37/x86_64
  config.vm.box = "dummy"
  config.vm.box_url = "https://iad.mirror.rackspace.com/fedora/releases/37/Cloud/x86_64/images/Fedora-Cloud-Base-Vagrant-37-1.7.x86_64.vagrant-virtualbox.box"
  # assume some ram is needed for the host environment but very little CPU
  memory = 10240
  cpus = 3
  config.vm.provider :virtualbox do |v|
    v.memory = memory
    v.cpus = cpus
  end
  config.vm.provision "install-packages", type: "shell", run: "once" do |sh|
    sh.inline = <<~SHELL
    set -eux -o pipefail
    # Ensure network-related modules to be loaded
    modprobe tap ip_tables iptable_nat ip6_tables ip6table_nat

    # The moby-engine package included in Fedora lacks support for rootless,
    # So we need to install docker-ce and docker-ce-rootless-extras from the upstream.
    curl -fsSL https://get.docker.com | sh
    dnf install -y golang-go make kubernetes-client podman docker-ce-rootless-extras
    systemctl enable --now docker

    # Configuration for rootless: https://kind.sigs.k8s.io/docs/user/rootless/
    mkdir -p "/etc/systemd/system/user@.service.d"
    cat <<EOF >"/etc/systemd/system/user@.service.d/delegate.conf"
    [Service]
    Delegate=yes
    EOF
    systemctl daemon-reload
    loginctl enable-linger vagrant
    SHELL
  end
  config.vm.provision "install-kind", type: "shell", run: "once" do |sh|
    sh.inline = <<~SHELL
    set -eux -o pipefail
    git config --global --add safe.directory /vagrant
    make -C /vagrant install INSTALL_DIR=/usr/local/bin
    SHELL
  end
end
