# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# Vagrant box for testing k3s with the btrfs snapshotter. Usage:
# VAGRANT_EXPERIMENTAL=disks vagrant up

Vagrant.configure("2") do |config|
  config.vagrant.plugins = {
    'vagrant-k3s' => {:version => '~> 0.1.3'},
  }
  config.vm.box = "opensuse/Leap-15.3.x86_64"
  config.vm.boot_timeout = ENV['TEST_VM_BOOT_TIMEOUT'] || 600 # seconds
  config.vm.synced_folder '../../../../dist/artifacts', '/vagrant', type: 'rsync', disabled: false,
    rsync__exclude: ENV['RSYNC_EXCLUDE'] || '*.tar.*'

  config.vm.define 'snapshotter-btrfs', primary: true do |test|
    test.vm.hostname = 'smoke'
    test.vm.provision 'k3s-prepare', type: 'shell', run: 'once', privileged: true do |sh|
      sh.inline = <<~EOF
        #!/usr/bin/env bash
        set -eu -o pipefail
        zypper install -y apparmor-parser btrfsprogs hostname
        mkdir -p /var/lib/rancher/k3s /etc/rancher/k3s /usr/local/bin
        if ! mountpoint -q /var/lib/rancher/k3s; then
            : ${BTRFS_DEV:=#{ENV['BTRFS_DEV']}}
            for disk in sd[b-d] vd[b-d] xd[b-d]; do
                if [ -n "${BTRFS_DEV}" ]; then break; fi
                : ${BTRFS_DEV:=$(test -b /dev/$disk && echo $disk)}
            done
            btrfs filesystem show /dev/${BTRFS_DEV:?unable to determine automatically, please specify} 2>/dev/null || mkfs -t btrfs /dev/${BTRFS_DEV}
            mountpoint -q /mnt || mount -t btrfs /dev/${BTRFS_DEV} /mnt
            btrfs subvolume show /mnt/@k3s 2>/dev/null || btrfs subvolume create /mnt/@k3s
            umount /mnt
            mount -t btrfs -o subvol=@k3s /dev/${BTRFS_DEV} /var/lib/rancher/k3s
        fi
        # Install k3s binary
        install -m 755 /vagrant/k3s /usr/local/bin
        if [ -e /vagrant/*.tar ]; then
            mkdir -vp /var/lib/rancher/k3s/agent/images
            for tar in /vagrant/*.tar; do
                cp -vf $tar /var/lib/rancher/k3s/agent/images/
            done
        fi
      EOF
    end
    test.vm.provision 'k3s-install', type: 'k3s', run: 'once' do |k3s|
      k3s.args = %w[server --snapshotter=btrfs]
      k3s.env = %w[INSTALL_K3S_NAME=server INSTALL_K3S_SKIP_DOWNLOAD=true K3S_TOKEN=vagrant]
      k3s.config = {
        'disable' => %w[local-storage metrics-server servicelb traefik],
        'disable-helm-controller' => true,
        'disable-network-policy' => true,
        'write-kubeconfig-mode' => '0644',
      }
      k3s.config_mode = '0644' # side-step https://github.com/k3s-io/k3s/issues/4321
    end
    test.vm.provision "k3s-wait-for-node", type: "shell", run: ENV['CI'] == 'true' ? 'never' : 'once' do |sh|
      sh.env = { :PATH => "/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" }
      sh.inline = <<~SHELL
        #!/usr/bin/env bash
        set -eu -o pipefail
        echo 'Waiting for node to be ready ...'
        time timeout 300 bash -c 'while ! (kubectl wait --for condition=ready node/$(hostnamectl --static) 2>/dev/null); do sleep 5; done'
        kubectl get node,all -A -o wide
      SHELL
    end
    test.vm.provision "k3s-wait-for-coredns", type: "shell", run: ENV['CI'] == 'true' ? 'never' : 'once' do |sh|
      sh.env = { :PATH => "/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" }
      sh.inline = <<~SHELL
        #!/usr/bin/env bash
        set -eu -o pipefail
        time timeout 300 bash -c 'while ! (kubectl --namespace kube-system rollout status --timeout 10s deploy/coredns 2>/dev/null); do sleep 5; done'
      SHELL
    end
    test.vm.provision "k3s-status", type: "shell", run: ENV['CI'] == 'true' ? 'never' : 'once' do |sh|
      sh.env = { :PATH => "/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" }
      sh.inline = <<~SHELL
        #!/usr/bin/env bash
        set -eux -o pipefail
        kubectl get node,all -A -o wide
      SHELL
    end
    test.vm.provision "k3s-snapshots", type: "shell", run: ENV['CI'] == 'true' ? 'never' : 'once' do |sh|
      sh.env = { :PATH => "/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" }
      sh.inline = <<~SHELL
        #!/usr/bin/env bash
        set -eux -o pipefail
        btrfs subvolume list /var/lib/rancher/k3s/agent/containerd/io.containerd.snapshotter.v1.btrfs
      SHELL
    end
  end

  %w[libvirt virtualbox].each do |p|
    config.vm.provider p do |v|
      v.cpus = ENV['TEST_VM_CPUS'] || 2
      v.memory = ENV['TEST_VM_MEMORY'] || 2048
    end
  end

  config.vm.provider :libvirt do |v,o|
    v.storage :file, :size => '8G'
  end

  config.vm.provider :virtualbox do |v,o|
    v.gui = false
    v.check_guest_additions = false
    o.vm.disk :disk, name: "btrfs", size: "8GB" # Requires VAGRANT_EXPERIMENTAL="disks"
  end
end
