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

require 'yaml'
require 'ipaddr'

vagrant_config = YAML.load_file("provisioning/vm_config.conf.yml")

ovn_external = ENV[ 'OVN_EXTERNAL' ]

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.provider "libvirt" do |lv, override|
    override.vm.box = "ceph/ubuntu-xenial"
    lv.driver = "kvm"
  end

  # Use the ipaddr library to calculate the netmask of a given network
  net = IPAddr.new vagrant_config['public_network']
  netmask = net.inspect().split("/")[1].split(">")[0]

  # Bring up the Devstack ovsdb/ovn-northd node on Virtualbox
  config.vm.define "k8s-master" do |k8smaster|
    k8smaster.vm.host_name = vagrant_config['k8smaster']['host_name']
    if !ovn_external.nil?
        k8smaster.vm.network "public_network", bridge: ovn_external
    else
        k8smaster.vm.network "private_network", ip: vagrant_config['k8smaster']['public-ip'], netmask: netmask
    end
    k8smaster.vm.provision "shell", path: "provisioning/setup-hostnames.sh", privileged: true,
      :args => "#{vagrant_config['k8smaster']['public-ip']} #{vagrant_config['k8smaster']['short_name']} #{vagrant_config['k8sminion1']['public-ip']} #{vagrant_config['k8sminion1']['short_name']} #{vagrant_config['k8sminion2']['public-ip']} #{vagrant_config['k8sminion2']['short_name']}"
    k8smaster.vm.provision "shell", path: "provisioning/setup-master.sh", privileged: false,
      :args => "#{vagrant_config['k8smaster']['public-ip']} #{vagrant_config['k8sminion1']['public-ip']} #{vagrant_config['k8sminion2']['public-ip']} #{vagrant_config['k8smaster']['short_name']} #{netmask} #{vagrant_config['public_gateway']} #{ovn_external}"
    k8smaster.vm.provider "virtualbox" do |vb|
       vb.name = vagrant_config['k8smaster']['short_name']
       vb.memory = vagrant_config['k8smaster']['memory']
       vb.cpus = vagrant_config['k8smaster']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end
    k8smaster.vm.provider "libvirt" do |domain|
      domain.memory = vagrant_config['k8smaster']['memory']
      domain.cpus = vagrant_config['k8smaster']['cpus']
      domain.nic_model_type = "virtio"
    end
  end

  config.vm.define "k8s-minion1" do |k8sminion1|
    k8sminion1.vm.host_name = vagrant_config['k8sminion1']['host_name']
    k8sminion1.vm.host_name = "k8sminion1"
    if !ovn_external.nil?
        k8sminion1.vm.network "public_network", bridge: ovn_external
    else
        k8sminion1.vm.network "private_network", ip: vagrant_config['k8sminion1']['public-ip'], netmask: netmask
    end
    k8sminion1.vm.provision "shell", path: "provisioning/setup-hostnames.sh", privileged: true,
      :args => "#{vagrant_config['k8smaster']['public-ip']} #{vagrant_config['k8smaster']['short_name']} #{vagrant_config['k8sminion1']['public-ip']} #{vagrant_config['k8sminion1']['short_name']} #{vagrant_config['k8sminion2']['public_ip']} #{vagrant_config['k8sminion2']['short_name']}"
    k8sminion1.vm.provision "shell", path: "provisioning/setup-minion.sh", privileged: false,
      :args => "#{vagrant_config['k8sminion1']['public-ip']} #{vagrant_config['k8smaster']['public-ip']} #{vagrant_config['k8sminion2']['public-ip']} #{netmask} #{vagrant_config['k8sminion1']['short_name']} #{vagrant_config['public_gateway']} #{ovn_external}"
    k8sminion1.vm.provider "virtualbox" do |vb|
       vb.name = vagrant_config['k8sminion1']['short_name']
       vb.memory = vagrant_config['k8sminion1']['memory']
       vb.cpus = vagrant_config['k8sminion1']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end
    k8sminion1.vm.provider "libvirt" do |domain|
      domain.memory = vagrant_config['k8sminion1']['memory']
      domain.cpus = vagrant_config['k8sminion1']['cpus']
      domain.nic_model_type = "virtio"
    end
  end

  config.vm.define "k8s-minion2" do |k8sminion2|
    k8sminion2.vm.host_name = vagrant_config['k8sminion2']['host_name']
    k8sminion2.vm.host_name = "k8sminion2"
    if !ovn_external.nil?
        k8sminion2.vm.network "public_network", bridge: ovn_external
    else
        k8sminion2.vm.network "private_network", ip: vagrant_config['k8sminion2']['public-ip'], netmask: netmask
    end
    k8sminion2.vm.provision "shell", path: "provisioning/setup-hostnames.sh", privileged: true,
      :args => "#{vagrant_config['k8smaster']['public-ip']} #{vagrant_config['k8smaster']['short_name']} #{vagrant_config['k8sminion1']['public-ip']} #{vagrant_config['k8sminion1']['short_name']} #{vagrant_config['k8sminion2']['public-ip']} #{vagrant_config['k8sminion2']['short_name']}"
    k8sminion2.vm.provision "shell", path: "provisioning/setup-minion.sh", privileged: false,
      :args => "#{vagrant_config['k8sminion2']['public-ip']} #{vagrant_config['k8smaster']['public-ip']} #{vagrant_config['k8sminion1']['public-ip']} #{netmask} #{vagrant_config['k8sminion2']['short_name']} #{vagrant_config['public_gateway']} #{ovn_external}"
    k8sminion2.vm.provider "virtualbox" do |vb|
       vb.name = vagrant_config['k8sminion2']['short_name']
       vb.memory = vagrant_config['k8sminion2']['memory']
       vb.cpus = vagrant_config['k8sminion2']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end
    k8sminion2.vm.provider "libvirt" do |domain|
      domain.memory = vagrant_config['k8sminion2']['memory']
      domain.cpus = vagrant_config['k8sminion2']['cpus']
      domain.nic_model_type = "virtio"
    end
  end

  config.vm.provider "virtualbox" do |v|
    v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
    v.customize ["modifyvm", :id, "--nictype1", "virtio"]
  end
end
