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

class VagrantConfigMissing < StandardError
end

class KollaMesosNotImplemented < StandardError
end

# Vagrantfile.custom contains user customization for the Vagrantfile
# You shouldn't have to edit the Vagrantfile, ever.
vagrant_dir = File.expand_path(File.dirname(__FILE__))
if File.exists?(File.join(vagrant_dir, 'Vagrantfile.custom'))
  eval(IO.read(File.join(vagrant_dir, 'Vagrantfile.custom')), binding)
end

# Libvirt for Linux, virtualbox for Mac OS
PROVIDER ||= "libvirt"

PROVIDER_DEFAULTS ||= {
  libvirt: {
    base_image: "centos/7",
    bridge_interface: "virbr0",
    vagrant_shared_folder: "/home/vagrant/sync",
    sync_method: "nfs",
  },
  virtualbox: {
    base_image: "centos/7",
    bridge_interface: "en0: Wi-Fi (AirPort)",
    vagrant_shared_folder: "/vagrant",
    sync_method: "virtualbox",
  },
}

# Whether the host network adapter is Wi-Fi.
# On VirtualBox, the user must first manually create a NAT-Network
# named "OSNetwork". The default network CIDR must be changed.
# The Neutron external interface will be connected to this Network.
WIFI = false unless self.class.const_defined?(:WIFI)

MULTINODE = false unless self.class.const_defined?(:MULTINODE)

NUMBER_OF_MASTER_NODES ||= 3
NUMBER_OF_CONTROLLER_NODES ||= 3
NUMBER_OF_COMPUTE_NODES ||= 1
NUMBER_OF_STORAGE_NODES ||= 1

NODE_SETTINGS ||= {
  aio: {
    cpus: 4,
    memory: 4096
  },
  master: {
    cpus: 1,
    memory: 1024
  },
  controller: {
    cpus: 2,
    memory: 2048
  },
  compute: {
    cpus: 1,
    memory: 1024
  },
  storage: {
    cpus: 1,
    memory: 1024
  }
}

# Configure a new SSH key and config so the operator is able to connect with
# the other cluster nodes.
unless File.file?(File.join(vagrant_dir, 'vagrantkey'))
  system("ssh-keygen -f #{File.join(vagrant_dir, 'vagrantkey')} -N '' -C this-is-vagrant")
end

def get_default(setting)
  PROVIDER_DEFAULTS[PROVIDER.to_sym][setting]
rescue
  raise VagrantConfigMissing,
    "Missing configuration for PROVIDER_DEFAULTS[#{PROVIDER}][#{setting}]"
end

def get_setting(node, setting)
  NODE_SETTINGS[node][setting]
rescue
  raise VagrantConfigMissing,
    "Missing configuration for NODE_SETTINGS[#{node}][#{setting}]"
end

def configure_wifi_vbox_networking(vm)
  # Even if adapters 1 & 2 don't need to be modified, if the order is to be
  # maintained, some modification has to be done to them. This will maintain
  # the association inside the guest OS: NIC1 -> eth0, NIC2 -> eth1, NIC3 ->
  # eht2. The modifications for adapters 1 & 2 only change optional properties.
  # Adapter 3 is enabled and connected to the NAT-Network named "OSNetwork",
  # while also changing its optional properties. Since adapter 3 is used by
  # Neutron for the external network, promiscuous mode is set to "allow-all".
  # Also, use virtio as the adapter type, for better performance.
  vm.customize ["modifyvm", :id, "--nictype1", "virtio"]
  vm.customize ["modifyvm", :id, "--cableconnected1", "on"]
  vm.customize ["modifyvm", :id, "--nicpromisc2", "deny"]
  vm.customize ["modifyvm", :id, "--nictype2", "virtio"]
  vm.customize ["modifyvm", :id, "--cableconnected2", "on"]
  vm.customize ["modifyvm", :id, "--nic3", "natnetwork"]
  vm.customize ["modifyvm", :id, "--nat-network3", "OSNetwork"]
  vm.customize ["modifyvm", :id, "--nicpromisc3", "allow-all"]
  vm.customize ["modifyvm", :id, "--nictype3", "virtio"]
  vm.customize ["modifyvm", :id, "--cableconnected3", "on"]
end

def configure_wifi_if_enabled(vm)
  if WIFI
    case PROVIDER
    when "virtualbox"
      configure_wifi_vbox_networking(vm)
#   TODO(lucian-serb): Configure networking on Wi-Fi for other hypervisors.
#   when "libvirt"
#     configure_wifi_libvirt_networking(vm)
    end
  end
end

Vagrant.configure(2) do |config|
  config.vm.box = get_default(:base_image)

  config.vm.network "private_network", type: "dhcp"
  # On VirtualBox hosts with Wi-Fi, do not create a public bridged interface.
  # A NAT-Network will be used instead.
  # TODO(lucian-serb): Do the same for other hypervisors as well?
  unless PROVIDER == "virtualbox" && WIFI
    config.vm.network "public_network", dev: get_default(:bridge_interface), bridge: get_default(:bridge_interface), mode: 'bridge', type: 'bridge'
  end

  my_privatekey = File.read(File.join(vagrant_dir, "vagrantkey"))
  my_publickey = File.read(File.join(vagrant_dir, "vagrantkey.pub"))

  config.vm.provision :shell, inline: <<-EOS
    mkdir -p /root/.ssh
    echo '#{my_privatekey}' > /root/.ssh/id_rsa
    chmod 600 /root/.ssh/id_rsa
    echo '#{my_publickey}' > /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys
    echo '#{my_publickey}' > /root/.ssh/id_rsa.pub
    chmod 644 /root/.ssh/id_rsa.pub
    mkdir -p /home/vagrant/.ssh
    echo '#{my_privatekey}' >> /home/vagrant/.ssh/id_rsa
    chmod 600 /home/vagrant/.ssh/*
    echo 'Host *' > ~vagrant/.ssh/config
    echo StrictHostKeyChecking no >> ~vagrant/.ssh/config
    chown -R vagrant: /home/vagrant/.ssh
  EOS

  config.hostmanager.enabled = false
  config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
    case PROVIDER
    when "libvirt"
      if vm.name
        `python newest_dhcp_lease.py #{vm.name}`.chop
      end
    when "virtualbox"
      if vm.id
        `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP"`.split()[1]
      end
    end
  end
  config.hostmanager.manage_host = true

  config.vm.define "operator" do |admin|
    hostname = "operator"
    admin.vm.hostname = "#{hostname}.local"
    admin.vm.synced_folder "..", "/home/vagrant/kolla-mesos", type: get_default(:sync_method)
    admin.vm.provider PROVIDER do |vm|
      vm.cpus = get_setting(:aio, :cpus)
      vm.memory = get_setting(:aio, :memory)
      configure_wifi_if_enabled(vm)
    end
    admin.vm.provision :hostmanager
    admin.vm.provision :shell, path: "provision.sh", args: "operator #{MULTINODE ? 'multinode' : 'aio'}"
    admin.hostmanager.aliases = hostname
  end

  if MULTINODE
    ['master', 'controller', 'compute'].each do |node_type|
      (1..self.class.const_get("NUMBER_OF_#{node_type.upcase}_NODES")).each do |i|
        hostname = "#{node_type}0#{i}"
        config.vm.define hostname do |node|
          node.vm.hostname = "#{hostname}.local"
          node.vm.provision :hostmanager
          node.vm.provision :shell, path: "provision.sh", args: "#{hostname} multinode"
          node.vm.provider PROVIDER do |vm|
            vm.memory = get_setting(node_type.to_sym, :memory)
            vm.cpus = get_setting(node_type.to_sym, :cpus)
            configure_wifi_if_enabled(vm)
          end
          node.hostmanager.aliases = hostname
        end
      end
    end
  end
end
