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

Vagrant.configure(2) do |config|

  # See: https://app.vagrantup.com/bento/boxes/ubuntu-18.04
  config.vm.box = "bento/ubuntu-18.04"
  config.vm.box_check_update = false

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--hwvirtex", "off"]
  end
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end

  # use http proxy if avaiable
  if ENV['http_proxy'] && Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.http     = ENV['http_proxy']
    config.proxy.https    = ENV['http_proxy']
    config.proxy.no_proxy = "localhost,127.0.0.1,10.0.2.15"
  end

  vmcpu=(ENV['SPDK_VAGRANT_VMCPU'] || 10)
  vmram=(ENV['SPDK_VAGRANT_VMRAM'] || 8192)
  spdk_dir=(ENV['SPDK_DIR'] || "none")

  config.ssh.forward_agent = true
  config.ssh.forward_x11 = true

  # Change root passwd and allow root SSH
  config.vm.provision "shell", inline: 'echo -e "root\nroot" | sudo passwd root'
  config.vm.provision "shell", inline: 'sudo sh -c "echo \"PermitRootLogin yes\" >> /etc/ssh/sshd_config"'
  config.vm.provision "shell", inline: 'useradd -m -p sys_sgci -s /bin/bash sys_sgci'
  config.vm.provision "shell", inline: 'usermod -aG sudo sys_sgci'

  # Install needed deps
  $apt_script = <<-SCRIPT
  sudo apt -y update
  sudo DEBIAN_FRONTEND=noninteractive apt -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
  SCRIPT
  config.vm.provision "shell", inline: $apt_script

  # TODO: Next 2 lines break any future ssh communication via "vagrant ssh"
  # I'd be good to check NIC names in ifconfig and then sed them in /etc/network/interfaces to eht0, eht1, and so on
  config.vm.provision "shell", inline: 'sudo sh -c "echo \"auto eth0\" >> /etc/network/interfaces"'
  config.vm.provision "shell", inline: 'sudo sh -c "echo \"iface eth0 inet dhcp\" >> /etc/network/interfaces"'

  # This is to avoid annoying "Start job is running for wait for network to be configured" 2 minute timeout
  # in case of not-so-perfect NIC and virtual network configuration for the VM
  config.vm.provision "shell", inline: 'systemctl disable systemd-networkd-wait-online.service'
  config.vm.provision "shell", inline: 'systemctl mask systemd-networkd-wait-online.service'

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--ioapic", "on"]
    vb.memory = "#{vmram}"
    vb.cpus = "#{vmcpu}"

    #support for the SSE4.x instruction is required in some versions of VB.
    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.1", "1"]
    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.2", "1"]
  end

  if spdk_dir != "none"
    config.vm.synced_folder "#{spdk_dir}", "/home/vagrant/spdk", type: "rsync",
      rsync__exclude: ["ubuntu18"]
  end

  # Install needed drivers and tools
  config.vm.provision "shell", inline: 'sudo apt-get install -y libibverbs* librdmacm* libnuma-dev libaio-dev libcunit1-dev ibverbs-utils rdma-core'

  # Copy in the user's tools if they exists
  if File.directory?(File.expand_path("~/vagrant_tools"))
    config.vm.synced_folder "~/vagrant_tools", "/home/vagrant/tools", type: "rsync", rsync__auto: false
  end

  config.vm.provision "shell", inline: 'sudo useradd -s /bin/bash -d /opt/stack -m stack | echo "stack ALL=(ALL) NOPASSWD: ALL" | sudo tee /etc/sudoers.d/stack'
  config.vm.provision "shell", inline: 'sudo su - stack bash -c "git clone -b stable/stein https://git.openstack.org/openstack-dev/devstack /opt/stack/devstack"'
  config.vm.provision "file", source: "#{spdk_dir}/scripts/vagrant/local.conf", destination: "/home/vagrant/local.conf"
  config.vm.provision "shell", inline: 'sudo su - stack bash -c "cp /home/vagrant/local.conf /opt/stack/devstack"'
  config.vm.provision "shell", inline: 'sudo su - stack bash -c "cd /opt/stack/devstack; ./stack.sh"'
end
