#/////////////////////////////////////////////////////////////////////////
#//
#// (c) University of Southampton IT Innovation Centre, 2017
#//
#// Copyright in this software belongs to University of Southampton
#// IT Innovation Centre of Gamma House, Enterprise Road,
#// Chilworth Science Park, Southampton, SO16 7NS, UK.
#//
#// This software may not be used, sold, licensed, transferred, copied
#// or reproduced in whole or in part in any manner or form or in or
#// on any media by any person other than in accordance with the terms
#// of the Licence Agreement supplied with the software, or otherwise
#// without the prior written consent of the copyright owners.
#//
#// This software is distributed WITHOUT ANY WARRANTY, without even the
#// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#// PURPOSE, except where stated in the Licence Agreement supplied with
#// the software.
#//
#//      Created By :            Michael Boniface
#//      Created Date :          13/12/2017
#//      Created for Project :   FLAME
#//
#/////////////////////////////////////////////////////////////////////////
# Requirements
require 'getoptlong'
require 'yaml'

# Custom options:
#   --fixture <fixturedir>

# Set defaults
DEFAULT_FIXTURE = "streaming"

# Define custom options
opts = GetoptLong.new(
  [ '--fixture', GetoptLong::OPTIONAL_ARGUMENT]
)

# Retrieve custom option values
fixture = DEFAULT_FIXTURE
opts.each do |opt, arg|
 case opt
   when '--fixture'
    fixture = arg    
 end
end

# load custom config file
puts "loading custom infrastructure configuration: #{fixture}"
puts "custom config file: /src/test/clmctest/#{fixture}/rspec.yml"
host_rspec_file = "src/test/clmctest/#{fixture}/rspec.yml"
hosts = YAML.load_file(host_rspec_file)

# Start creating VMS using xenial64 as the base box
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
  #config.vm.box = "hashicorp/precise32"

  # Dynamic VMs
  hosts['hosts'].each do |host|
    #p host["name"]
    instance_name = host["name"]
    config.vm.define instance_name do |instance_config|

      # Specify VM properties
      instance_config.vm.hostname = instance_name
      instance_config.disksize.size = host["disk"]
      instance_config.vm.provider "virtualbox" do |v|
        v.customize ["modifyvm", :id, "--memory", host["memory"]]
        v.customize ["modifyvm", :id, "--cpus", host["cpus"]]
      end

      # Configure network, not that we only expect 1 test to be running so we have one internal network
      instance_config.vm.network :private_network, ip: "#{host["ip_address"]}", virtualbox__intnet: "clmc-net"

      # Port forwarding
      puts "Forwarding the following specified ports for #{host["name"]}:"
      if host.has_key? 'forward_ports'
        host['forward_ports'].each do |port|
          puts "Forwarding guest:#{port["guest"]} => host:#{port["host"]}"
          instance_config.vm.network "forwarded_port", guest: port["guest"], host: port["host"]
        end
      end
      
      # Switch case added here to make clmc-service provisioning simple without having to have a complex rspec.yml file
      # We only run a service installation script and the agent installation script when creating a specific service VM, not the clmc-service VM
      
      puts "Instance name #{instance_name}:"
      case instance_name
        when 'test-runner'
          instance_config.vm.provision :shell, :path => "src/test/clmctest/services/pytest/install.sh"
        when 'clmc-service'
          instance_config.vm.provision :shell, :path => "scripts/clmc-service/install.sh", :args => "#{host["influxdb_url"]} #{host["database_name"]} #{host["report_period"]}", env: {"REPO_ROOT" => "/vagrant"}       
        else
          # specific service install
          instance_config.vm.provision :shell, :path => "src/test/clmctest/services/#{host["service_name"]}/install.sh", env: {"REPO_ROOT" => "/vagrant"}
    
          # CLMC agent install
          instance_config.vm.provision "file", source: "reporc", destination: "/vagrant/reporc"
          instance_config.vm.provision :shell, :path => "scripts/clmc-agent/install.sh", env: {"REPO_ROOT" => "/vagrant"}

          # CLMC agent service specific input configuration
          instance_config.vm.provision :shell, inline: <<-SHELL

            cp /vagrant/scripts/clmc-agent/telegraf.conf /etc/telegraf/  

            cp /vagrant/scripts/clmc-agent/telegraf_output.conf /etc/telegraf/telegraf.d/                        
            
            cp /vagrant/src/test/clmctest/services/#{host["service_name"]}/telegraf_#{host["service_name"]}.conf /etc/telegraf/telegraf.d/ 

          SHELL
          
          # CLMC agent general and output configuration
          #instance_config.vm.provision :shell, :path => "scripts/clmc-agent/configure_template.sh"

          instance_config.vm.provision :shell, :path => "scripts/clmc-agent/configure.sh", :args => "#{host["location"]} #{host["sfc_id"]} #{host["sfc_id_instance"]} #{host["sf_id"]} #{host["sf_id_instance"]} #{host["ipendpoint_id"]} #{host["sr_id"]} #{host["influxdb_url"]} #{host["database_name"]}"

          # CLMC start agent
          instance_config.vm.provision :shell, inline: "service telegraf restart"            
      end

      
	  end
  end
 
end