# -*- mode: ruby -*-
# vi: set ft=ruby ts=2 sw=2 sts=2 et :

# This Vagrantfile creates a quick OOR testbed with several VMs with OOR
# running, pre-configured for various roles (map server, mobile node, etc.)

ipv4prefix = "192.168.127."
ipv6prefix = "fdab:cc19:b80e::"
ipv6enabled = false

def create_vm(config, name, ipv4rloc, ipv6rloc, ipv6enabled)
  config.vm.define name do |node|
    node.vm.hostname = name
    node.vm.network "private_network", ip: ipv4rloc
    if ipv6enabled
      node.vm.network "private_network", ip: ipv6rloc
    end
    node.vm.provision "shell", path: "install-oor.sh", privileged: false
    node.vm.provision "shell", path: "config-oor.sh", args: name
    # If you prefer to run OOR manually, comment the next line
    node.vm.provision "shell", path: "run-oor.sh"
    node.vm.provider :virtualbox do |vb|
      vb.name = name
    end
  end
end

# Vagrant 2.0.2 is the minimum required version due to this issue:
# https://github.com/hashicorp/vagrant/issues/9134
# The issue was fixed with https://github.com/hashicorp/vagrant/pull/9338
# which is part of 2.0.2
#
# If you can't use Vagrant 2.0.2, you can try switching the box from
# `bento/ubuntu-17.10` to `bento/ubuntu-16.04`.
Vagrant.require_version ">= 2.0.2"
Vagrant.configure(2) do |config|
  # vagrant-cachier caches apt/yum etc. to speed subsequent `vagrant up`
  # To enable, run `vagrant plugin install vagrant-cachier`
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
    config.cache.synced_folder_opts = {
      owner: '_apt',
      group: '_apt',
      mount_options: ["dmode=777", "fmode=666"]
    }
  end
  config.vm.box = "bento/ubuntu-17.10"
  config.vm.box_check_update = false
  # TODO need to figure out how to use #{config.ssh.username} instead of
  # "ubuntu" to be more general, if the box we use changes the user
  username = "vagrant"
  config.vm.synced_folder "..", "/vagrant"
  config.vm.synced_folder "..", "/home/#{username}/oor"
  config.vm.provision "shell", path: "install-prereqs.sh"
  config.vm.provision "shell", path: "install-optional.sh", args: username
  config.vm.provision "shell", path: "install-unprivileged.sh", privileged: false
  config.vm.provision "shell", path: "config-dotfiles.sh", privileged: false
  config.vm.provider :virtualbox do |vb|
    vb.cpus = 1
    vb.memory = 256
    vb.linked_clone = true
    vb.customize [ "modifyvm", :id, "--description", "Open Overlay Router testbed VM" ]
    vb.customize [ "modifyvm", :id, "--nicpromisc2", "allow-all"]
    vb.customize [ "modifyvm", :id, "--nicpromisc3", "allow-all"]
    # This disables generating a log file with boot messages. If you need to
    # debug the boot process, comment the following line.
    vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
  end

  create_vm(config, "msmr", ipv4prefix + "2", ipv6prefix + "2", ipv6enabled)
  create_vm(config, "rtr", ipv4prefix + "6", ipv6prefix + "6", ipv6enabled)
  create_vm(config, "mn1", ipv4prefix + "11", ipv6prefix + "11", ipv6enabled)
  create_vm(config, "mn2", ipv4prefix + "12", ipv6prefix + "12", ipv6enabled)

  # Android mobile node specific configuration
  config.vm.define "mn3-android", autostart: false do |node|
    node.vm.hostname = "mn3-android"
    node.vm.network "private_network", ip: ipv4prefix + "13"
    if ipv6enabled
      node.vm.network "private_network", ip: ipv6prefix + "13"
    end
    node.vm.provision "shell", path: "install-oracle-java8.sh"
    node.vm.provision "shell", path: "config-dotfiles.sh", privileged: false
    node.vm.provision "shell", path: "install-android-sdk.sh", privileged: false
    node.vm.provision "shell", path: "build-oor-apk.sh", privileged: false
    node.vm.provider :virtualbox do |vb|
      vb.name = "mn3-android"
      vb.cpus = 4
      vb.memory = 4096
      vb.customize [ "modifyvm", :id, "--description", "Open Overlay Router testbed VM -- Android mobile node" ]
    end
  end

  # OpenDaylight based map server specific configuration
  config.vm.define "msmr-odl", autostart: false do |node|
    node.vm.hostname = "msmr-odl"
    node.vm.network "private_network", ip: ipv4prefix + "3"
    if ipv6enabled
      node.vm.network "private_network", ip: ipv6prefix + "3"
    end
    node.vm.provision "shell", path: "install-oracle-java8.sh"
    node.vm.provision "shell", path: "install-odl.sh", privileged: false
    node.vm.provider :virtualbox do |vb|
      vb.name = "msmr-odl"
      vb.cpus = 4
      vb.memory = 4096
      vb.customize [ "modifyvm", :id, "--description", "Open Overlay Router testbed VM -- OpenDaylight based map server" ]
    end
  end
end
