# -*- 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",
  },
}

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

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

  config.vm.network "private_network", type: "dhcp"
  config.vm.network "public_network", dev: get_default(:bridge_interface), bridge: get_default(:bridge_interface), mode: 'bridge', type: 'bridge'

  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/2/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)
    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)
          end
          node.hostmanager.aliases = hostname
        end
      end
    end
  end
end
