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

nodes = {
  grizzly: {
    box: 'latest_grizzly',
    url: 'http://172.18.236.71/clean_boxes/latest_grizzly.json',
    ip: '192.168.1.2',
    ip2: '172.16.1.2',
    memory: 4096,
    role: :openstack,
    cpus: 2
  },
  icehouse: {
    box: 'latest_icehouse',
    url: 'http://172.18.236.71/clean_boxes/latest_icehouse.json',
    ip: '192.168.1.3',
    ip2: '172.16.1.3',
    memory: 4096,
    role: :openstack,
    cpus: 2
  },
  juno: {
    box: 'latest_juno',
    url: 'http://172.18.236.71/clean_boxes/latest_juno.json',
    ip: '192.168.1.8',
    ip2: '172.16.1.8',
    memory: 4096,
    role: :openstack,
    cpus: 2
  },
  nfs: {
    box: 'latest_nfs',
    url: 'http://172.18.236.71/clean_boxes/latest_nfs.json',
    ip: '192.168.1.10',
    ip2: '172.16.1.10',
    role: :other,
    memory: 1024,
    cpus: 1
  }
}

public_key_path = '.ssh/id_rsa.pub'
private_key_path = '.ssh/id_rsa'
migrate_user = 'migration'
password = 'migpasswrd'

Vagrant.require_version '>= 1.6.0'

Vagrant.configure(2) do |config|
  etc_hosts = nodes.map { |name, data| [data[:ip], name].join(' ') }.join("\n")

  nodes.each do |nodename, nodedata|
    config.vm.define nodename do |thisnode|
      role = nodedata[:role]
      thisnode.vm.box_url = nodedata[:url]

      thisnode.vm.box = nodedata[:box]
      thisnode.vm.hostname = nodename.to_s
      thisnode.vm.provision 'shell', inline: "echo '#{etc_hosts}' >> /etc/hosts"
      thisnode.vm.provision 'shell',
                            path: './provision/add_migration_user.sh',
                            args: [migrate_user, password]
      thisnode.vm.provision 'shell',
                            path: './provision/keys.sh',
                            args: [File.read("#{ENV['HOME']}/#{public_key_path}"),
                                   File.read("#{ENV['HOME']}/#{private_key_path}"),
                                   migrate_user, 'root']
      thisnode.vm.network 'private_network', ip: nodedata[:ip], auto_config: (role != :openstack)
      thisnode.vm.network 'private_network', ip: nodedata[:ip2], auto_config: (role != :openstack)

      thisnode.vm.provider 'virtualbox' do |v|
        v.memory = nodedata[:memory]
        v.cpus = nodedata[:cpus]
        v.customize ['modifyvm', :id, '--nicpromisc2', 'allow-all']
        v.customize ['modifyvm', :id, '--nicpromisc3', 'allow-all']
        v.customize ['modifyvm', :id, '--cpuexecutioncap', '90']
      end
    end
  end
end
