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

require 'yaml'
require 'ipaddr'

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

Vagrant.configure(2) do |config|
  config.vm.box = vagrant_config['box']

  if Vagrant.has_plugin?("vagrant-cachier")
    # Configure cached packages to be shared between instances of the same base box.
    # More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage
    config.cache.scope = :box
  end

  #config.vm.synced_folder File.expand_path("..") + "/devstack", "/home/vagrant/devstack"
  config.vm.synced_folder File.expand_path(".."), "/home/vagrant/networking-ovn"

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

  # Build the common args for the setup-base.sh scripts.
  setup_base_common_args = "#{vagrant_config['ovndb']['ip']} #{vagrant_config['ovndb']['short_name']} " +
                           "#{vagrant_config['ovncontroller']['ip']} #{vagrant_config['ovncontroller']['short_name']} " +
                           "#{vagrant_config['ovncompute1']['ip']} #{vagrant_config['ovncompute1']['short_name']} " +
                           "#{vagrant_config['ovncompute2']['ip']} #{vagrant_config['ovncompute2']['short_name']} " +
                           "#{vagrant_config['ovnvtep']['ip']} #{vagrant_config['ovnvtep']['short_name']} "

  # Bring up the Devstack ovsdb/ovn-northd node on Virtualbox
  config.vm.define "ovn-db" do |ovndb|
    ovndb.vm.host_name = vagrant_config['ovndb']['host_name']
    ovndb.vm.network "private_network", ip: vagrant_config['ovndb']['ip']
    ovndb.vm.network "private_network", ip: vagrant_config['ovndb']['prov-ip'], netmask: netmask
    ovndb.vm.provision "shell", path: "provisioning/setup-base.sh", privileged: false,
      :args => "#{vagrant_config['ovndb']['mtu']} #{setup_base_common_args}"
    ovndb.vm.provision "shell", path: "provisioning/setup-db.sh", privileged: false, :args => "#{vagrant_config['ovncontroller']['ip']}"
    ovndb.vm.provider "virtualbox" do |vb|
       vb.memory = vagrant_config['ovndb']['memory']
       vb.cpus = vagrant_config['ovndb']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end

    ovndb.vm.provider 'parallels' do |vb, override|
       vb.memory = vagrant_config['ovndb']['memory']
       vb.cpus = vagrant_config['ovndb']['cpus']
       vb.customize ['set', :id, '--nested-virt', 'on']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end

    ovndb.vm.provider 'libvirt' do |vb, override|
       vb.memory = vagrant_config['ovndb']['memory']
       vb.cpus = vagrant_config['ovndb']['cpus']
       vb.nested        = true
       vb.graphics_type = 'spice'
       vb.video_type    = 'qxl'
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end

  end

  # Bring up the Devstack controller node on Virtualbox
  config.vm.define "ovn-controller", primary: true do |ovncontroller|
    ovncontroller.vm.host_name = vagrant_config['ovncontroller']['host_name']
    ovncontroller.vm.network "private_network", ip: vagrant_config['ovncontroller']['ip']
    ovncontroller.vm.network "private_network", ip: vagrant_config['ovncontroller']['prov-ip'], netmask: netmask
    ovncontroller.vm.provision "shell", path: "provisioning/setup-base.sh", privileged: false,
      :args => "#{vagrant_config['ovncontroller']['mtu']} #{setup_base_common_args}"
    ovncontroller.vm.provision "shell", path: "provisioning/setup-controller.sh", privileged: false,
      :args => "#{vagrant_config['ovndb']['ip']} #{vagrant_config['provider_start_ip']} #{vagrant_config['provider_end_ip']} " +
               "#{vagrant_config['provider_gateway']} #{vagrant_config['provider_network']} #{vagrant_config['ovn_vm_subnet']}"
    ovncontroller.vm.provider "virtualbox" do |vb|
       vb.memory = vagrant_config['ovncontroller']['memory']
       vb.cpus = vagrant_config['ovncontroller']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end
    ovncontroller.vm.provider 'parallels' do |vb, override|
       vb.memory = vagrant_config['ovncontroller']['memory']
       vb.cpus = vagrant_config['ovncontroller']['cpus']
       vb.customize ['set', :id, '--nested-virt', 'on']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end

    ovncontroller.vm.provider 'libvirt' do |vb, override|
       vb.memory = vagrant_config['ovncontroller']['memory']
       vb.cpus = vagrant_config['ovncontroller']['cpus']
       vb.nested        = true
       vb.graphics_type = 'spice'
       vb.video_type    = 'qxl'
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end
  end

  config.vm.define "ovn-vtep", autostart: false do |ovnvtep|
    ovnvtep.vm.host_name = vagrant_config['ovnvtep']['host_name']
    ovnvtep.vm.network "private_network", ip: vagrant_config['ovnvtep']['ip']
    ovnvtep.vm.network "private_network", ip: vagrant_config['ovnvtep']['prov-ip'], netmask: netmask
    ovnvtep.vm.provision "shell", path: "provisioning/setup-base.sh", privileged: false,
      :args => "#{vagrant_config['ovnvtep']['mtu']} #{setup_base_common_args}"
    ovnvtep.vm.provision "shell", path: "provisioning/setup-vtep.sh", privileged: false, :args => "#{vagrant_config['ovncontroller']['ip']} #{vagrant_config['ovndb']['ip']}"
    ovnvtep.vm.provider "virtualbox" do |vb|
       vb.memory = vagrant_config['ovnvtep']['memory']
       vb.cpus = vagrant_config['ovnvtep']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end

    ovnvtep.vm.provider 'parallels' do |vb, override|
       vb.memory = vagrant_config['ovnvtep']['memory']
       vb.cpus = vagrant_config['ovnvtep']['cpus']
       vb.customize ['set', :id, '--nested-virt', 'on']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end

    ovnvtep.vm.provider 'libvirt' do |vb, override|
       vb.memory = vagrant_config['ovnvtep']['memory']
       vb.cpus = vagrant_config['ovnvtep']['cpus']
       vb.nested        = true
       vb.graphics_type = 'spice'
       vb.video_type    = 'qxl'
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end
  end

  # Bring up the first Devstack compute node on Virtualbox
  config.vm.define "ovn-compute1" do |ovncompute1|
    ovncompute1.vm.host_name = vagrant_config['ovncompute1']['host_name']
    ovncompute1.vm.network "private_network", ip: vagrant_config['ovncompute1']['ip']
    ovncompute1.vm.network "private_network", ip: vagrant_config['ovncompute1']['prov-ip'], netmask: netmask
    ovncompute1.vm.provision "shell", path: "provisioning/setup-base.sh", privileged: false,
      :args => "#{vagrant_config['ovncompute1']['mtu']} #{setup_base_common_args}"
    ovncompute1.vm.provision "shell", path: "provisioning/setup-compute.sh", privileged: false,
      :args => "#{vagrant_config['ovncontroller']['ip']} #{vagrant_config['ovndb']['ip']}"
    ovncompute1.vm.provider "virtualbox" do |vb|
       vb.memory = vagrant_config['ovncompute1']['memory']
       vb.cpus = vagrant_config['ovncompute1']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end
    ovncompute1.vm.provider 'parallels' do |vb, override|
       vb.memory = vagrant_config['ovncompute1']['memory']
       vb.cpus = vagrant_config['ovncompute1']['cpus']
       vb.customize ['set', :id, '--nested-virt', 'on']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end

    ovncompute1.vm.provider 'libvirt' do |vb, override|
       vb.memory = vagrant_config['ovncompute1']['memory']
       vb.cpus = vagrant_config['ovncompute1']['cpus']
       vb.nested        = true
       vb.graphics_type = 'spice'
       vb.video_type    = 'qxl'
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end
  end

  # Bring up the second Devstack compute node on Virtualbox
  config.vm.define "ovn-compute2" do |ovncompute2|
    ovncompute2.vm.host_name = vagrant_config['ovncompute2']['host_name']
    ovncompute2.vm.network "private_network", ip: vagrant_config['ovncompute2']['ip']
    ovncompute2.vm.network "private_network", ip: vagrant_config['ovncompute2']['prov-ip'], netmask: netmask
    ovncompute2.vm.provision "shell", path: "provisioning/setup-base.sh", privileged: false,
      :args => "#{vagrant_config['ovncompute2']['mtu']} #{setup_base_common_args}"
    ovncompute2.vm.provision "shell", path: "provisioning/setup-compute.sh", privileged: false,
      :args => "#{vagrant_config['ovncontroller']['ip']} #{vagrant_config['ovndb']['ip']}"
    ovncompute2.vm.provider "virtualbox" do |vb|
       vb.memory = vagrant_config['ovncompute2']['memory']
       vb.cpus = vagrant_config['ovncompute2']['cpus']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
    end
    ovncompute2.vm.provider 'parallels' do |vb, override|
       vb.memory = vagrant_config['ovncompute2']['memory']
       vb.cpus = vagrant_config['ovncompute2']['cpus']
       vb.customize ['set', :id, '--nested-virt', 'on']
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end

    ovncompute2.vm.provider 'libvirt' do |vb, override|
       vb.memory = vagrant_config['ovncompute2']['memory']
       vb.cpus = vagrant_config['ovncompute2']['cpus']
       vb.nested        = true
       vb.graphics_type = 'spice'
       vb.video_type    = 'qxl'
       vb.customize [
           'modifyvm', :id,
           '--nicpromisc3', "allow-all"
          ]
       vb.customize [
           "guestproperty", "set", :id,
           "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000
          ]
       override.vm.box = ENV.fetch('VAGRANT_OVN_VM_BOX', 'boxcutter/ubuntu1404')
    end
  end
end
