# coding: utf-8
# copyright Utrecht University
# -*- mode: ruby -*-
# vi: set ft=ruby :

# GS: run script after install:
$post_script = <<SCRIPT
yum install epel-release -y
yum install htop -y
yum install net-tools -y
useradd ger
mkdir /home/ger/.ssh
chown ger:ger /home/ger/.ssh/
chmod 700 /home/ger/.ssh/
cat /tmp/ger.pubkey >> /home/ger/.ssh/authorized_keys
mkdir /root/.ssh
chown root:root /root/.ssh/
chmod 700 /root/.ssh/
cat /tmp/ger.pubkey >> /root/.ssh/authorized_keys
cp /tmp/hosts /etc/hosts
cp /tmp/fire_stop.sh /root/fire_stop.sh
SCRIPT

# Retrieve instance from command line.
require 'getoptlong'

opts = GetoptLong.new(
  [ '--instance', GetoptLong::OPTIONAL_ARGUMENT ]
)

instance='combined'
opts.each do |opt, arg|
  case opt
    when '--instance'
      instance=arg
  end
end

# Configuration variables.
VAGRANTFILE_API_VERSION = "2"

BOX = 'centos/7'
GUI = false
CPU = 1
RAM = 1024

DOMAIN  = ".ger.test"
NETWORK = "192.168.50."
NETMASK = "255.255.255.0"

if instance == "scoop" then
  HOSTS = {
    "portal"   => [NETWORK+"11", CPU, RAM, GUI, BOX],
    "icat"     => [NETWORK+"12", CPU, RAM, GUI, BOX],
    "resc1"    => [NETWORK+"13", CPU, RAM, GUI, BOX],
    "resc2"    => [NETWORK+"14", CPU, RAM, GUI, BOX],
  }
else
  HOSTS = {
    "combined" => [NETWORK+"10", 2, 4096, GUI, BOX],
  }
end

if instance == "irods" then
  HOSTS = { 
   "irods02"   => [NETWORK+"16", CPU, RAM, GUI, BOX],
  }
end

if instance == "kubernetes" then
  HOSTS = { 
   "master"  => [NETWORK+"21", CPU, RAM, GUI, BOX],
   "worker1"  => [NETWORK+"22", CPU, RAM, GUI, BOX],
   "worker2"  => [NETWORK+"23", CPU, RAM, GUI, BOX],
  }
end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  #config.ssh.insert_key ='true' 
  config.vm.provision "file", source: "/home/ger/.ssh/id_ecdsa.pub", destination: "/tmp/ger.pubkey"  
  config.vm.provision "file", source: "/etc/hosts", destination: "/tmp/hosts"
  config.vm.provision "file", source: "/home/ger/fire_stop.sh", destination: "/tmp/fire_stop.sh"

  HOSTS.each do | (name, cfg) |
    ipaddr, cpu, ram, gui, box = cfg

    config.vm.define name do |machine|
      machine.vm.box = box

      machine.vm.provider "virtualbox" do |vbox|
        vbox.gui    = gui
        vbox.cpus   = cpu
        vbox.memory = ram
        vbox.name   = name
        vbox.customize ["guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000]
      end

      machine.vm.hostname = name + DOMAIN
      machine.vm.network 'private_network', ip: ipaddr, netmask: NETMASK
      machine.vm.synced_folder ".", "/vagrant", disabled: true
      machine.vm.provision "shell",
        inline: "sudo timedatectl set-timezone Europe/Amsterdam"
      machine.vm.provision "shell", 
        inline: "cat /tmp/ger.pubkey >> /home/vagrant/.ssh/authorized_keys"
      machine.vm.provision "shell", 
        inline: $post_script
    end
  end

end