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

VAGRANTFILE_API_VERSION = '2'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = 'generic/ubuntu1804'

  config.vm.provider "libvirt" do |lv|
    lv.cpu_mode = 'host-passthrough'
  end
  config.vm.define 'bifrost' do |bifrost|
    bifrost.vm.provider :virtualbox do |vb|
      vb.customize ['modifyvm', :id, '--memory', '8196', '--cpus', '4']
      # the setting below are to improve performance on mac's and should have little impact elsewhere.
      vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
      vb.customize ["modifyvm", :id, "--nictype1", "Am79C973"]
      vb.customize ["modifyvm", :id, "--nictype2", "Am79C973"]
      vb.customize ["modifyvm", :id, "--nictype3", "Am79C973"]
    end

    bifrost.vm.provider "libvirt" do |lv|
      lv.memory = 8196
      lv.cpus = 2
      lv.nested = true
      lv.volume_cache = 'none'
    end

    bifrost.vm.provision "shell", inline: <<-SHELL
      echo "if [ \"$IFACE\" = \"eth0\" ]; then route del default dev eth0 ; fi" >> /etc/network/if-up.d
      echo "[Match]\nName=eth0\n[Network]\nDHCP=yes\n[DHCP]\nUseRoutes=false" >>/etc/systemd/network/no-default-route.network
    SHELL

    # Set up private NAT'd network
    bifrost.vm.network 'private_network', ip: '192.168.99.10', use_dhcp_assigned_default_route: false

    # This assumes you have DHCP on your bridged network. if not you will need
    # to statically configure to allow  Bifrost to manage hardware attached to
    # the bridged interface.
    # NOTE(TheJulia): Is there a way to abstract the bridged networking...
    # NOTE(NobodyCam): until the above is  done this needs to be set to a valid interface
    # NOTE(TheJulia): Virtualbox will ask you. libvirt won't and you need to use the dev
    # field instead of bridge.
    bifrost.vm.network "public_network", bridge: 'enp3s0', dev: 'enp3s0'


    # be sure we get the entire bifrost directory tree
    bifrost.vm.synced_folder "../../.", "/home/vagrant/bifrost", type: "rsync"

    bifrost.vm.provision 'ansible' do |ansible|
      ansible.verbose = 'v'
      ansible.playbook = 'vagrant.yml'
      ansible.extra_vars = {
          # set key file name here
          public_key: 'id_rsa.pub',
          # Edit the network_interface to match your needs:
          #  eth0 - connected to a Nat network
          #  eth1 - connected to Host-only network named: 'vboxnet1'
          #  eth2 - bridged - Interface must also be set above
          network_interface: 'eth0'
      }
    end
  end
end
