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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

#############################
#### BEGIN CUSTOMIZATION ####
#############################
#define number of nodes
num_APPLICATION       = 0
num_LEAF_INSTANCES    = 0
num_DB_INSTANCES      = 2
#
#define number of cores for guest
num_CORE              = 2

# define memory for each type of node in MBytes

# for leaf nodes, the minimun can be  2300, otherwise pre-check will fail for
# automatic ulimit values calculated based on ram

# for database nodes, the minimum suggested is 3072 for standard cluster
# for flex cluster, consider 4500 or more

memory_APPLICATION    = 1500
memory_LEAF_INSTANCES = 2300
memory_DB_INSTANCES   = 9472

# size of shared disk in GB
size_shared_disk      = 15
# number of shared disks
count_shared_disk     = 2
#
#############################
##### END CUSTOMIZATION #####
#############################

# variable used to provide information only once
give_info ||=true

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.ssh.insert_key = false
  config.vm.box = "Rendanic/oraclelinux-8.x"

  config.vm.provision "shell", inline: <<-SHELL
  # copy public key to vagrant user
  ssh_public_key=/vagrant/id_rsa.pub
  if test -f "$ssh_public_key" ; then
    echo "Check if public key $ssh_public_key is valid."
    if ssh-keygen -l -f "$ssh_public_key" ; then
      echo "Add public key to /home/vagrant/.ssh/authorized_keys"
      cat "$ssh_public_key" >> /home/vagrant/.ssh/authorized_keys
    fi
  fi

  sed -i 's/^PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
  systemctl restart sshd.service

  yum install -y gcc make perl kernel-uek-devel
  yum update -y
  /sbin/rcvboxadd quicksetup all
SHELL

  (1..num_DB_INSTANCES).each do |i|
    # this is to start machines higher to lower
    i = num_DB_INSTANCES+1-i
    config.vm.define vm_name = "racnode%01d" % i do |config|

      config.vm.disk :disk, size: "150GB", name: "#{vm_name}_oracle"
      config.vm.synced_folder ENV['VAGRANT_ANSIBLE_ORACLE_SW'], "/sw/oracle", disabled: false, mount_options: ["ro"]

      puts " "
      config.vm.hostname = "#{vm_name}-192-168-56-#{i+190}.nip.io"
      lanip = "192.168.56.#{i+190}"
      puts vm_name + " eth1 lanip  :" + lanip
      privip = "192.168.57.#{i+170}"
      puts vm_name + " eth2 privip :" + privip
      config.vm.provider :virtualbox do |vb|
        vb.name = vm_name + "." + Time.now.strftime("%y%m%d%H%M")
        vb.customize ["modifyvm", :id, "--memory", memory_DB_INSTANCES]
        vb.customize ["modifyvm", :id, "--cpus", num_CORE]
        vb.customize ["modifyvm", :id, "--groups", "/ansible-oracle"]

        # first shared disk port
        port=2
        # how many shared disk
        (1..count_shared_disk).each do |disk|
          file_to_dbdisk = "racattack-shared-disk"
          if !File.exist?("#{file_to_dbdisk}#{disk}.vdi") and num_DB_INSTANCES==i
            unless give_info==false
              puts "on first boot shared disks will be created, this will take some time"
              give_info=false
            end
            vb.customize ['createhd', '--filename', "#{file_to_dbdisk}#{disk}.vdi", '--size', (size_shared_disk * 1024).floor, '--variant', 'fixed']
            vb.customize ['modifyhd', "#{file_to_dbdisk}#{disk}.vdi", '--type', 'shareable']
          end
          vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', port, '--device', 0, '--type', 'hdd', '--medium', "#{file_to_dbdisk}#{disk}.vdi"]
          port=port+1
        end
      end
      config.vm.network :private_network, ip: lanip
      config.vm.network :private_network, ip: privip
    end
  end
end
