# -*- mode: ruby -*-
# vi: set ft=ruby :
# To use these virtual machines install Vagrant and VirtuaBox.
# vagrant up [git, jenkins, nexus, sonar]
Vagrant.require_version ">= 2.0.0"
# Require JSON module
require 'json'
# Read JSON file with config details
guests = JSON.parse(File.read(File.join(File.dirname(__FILE__), 'config.json')))

# Local PATH_SRC for mounting
$PathSrc = ENV['PATH_SRC'] || "."

Vagrant.configure(2) do |config|

  config.vagrant.plugins = ["vagrant-hostmanager", "vagrant-vbguest"]

  # check for updates of the base image
  config.vm.box_check_update = true
  # wait a while longer
  config.vm.boot_timeout = 1200

  # disable update guest additions
  if Vagrant.has_plugin?("vagrant-vbguest")
    config.vbguest.auto_update = false
  end

  # enable ssh agent forwarding
  config.ssh.forward_agent = true

  # use the standard vagrant ssh key
  config.ssh.insert_key = false

  # manage /etc/hosts
  config.hostmanager.enabled = true
  config.hostmanager.include_offline = true
  config.hostmanager.manage_guest = true
  config.hostmanager.manage_host = true

  # disable guest additions
  config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true

  # Iterate through entries in JSON file
  guests.each do |guest|
    config.vm.define guest['name'] do |srv|
      srv.vm.box = guest['box']
      srv.vm.hostname = guest['name']
      srv.vm.network 'private_network', ip: guest['ip_addr']
      srv.vm.network :forwarded_port, host: guest['forwarded_port'], guest: guest['app_port']

      # set no_share to false to enable file sharing
      srv.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: guest['no_share']
      srv.vm.provider :virtualbox do |virtualbox|
        virtualbox.customize ["modifyvm", :id,
           "--audio", "none",
           "--cpus", guest['cpus'],
           "--memory", guest['memory'],
           "--graphicscontroller", "VMSVGA",
           "--vram", "64"
        ]
        virtualbox.gui = guest['gui']
        virtualbox.name = guest['name']
      end
    end
  end
  config.vm.provision "ansible" do |ansible|
    ansible.compatibility_mode = "2.0"
    ansible.galaxy_role_file = "roles/requirements.yml"
    ansible.galaxy_roles_path = "roles"
    ansible.groups = {
      "nexus" => ["server04"],
      "jenkins" => ["server05"],
      "git" => ["server0 "],
      "sonar" => ["server07"]
    }
    ansible.extra_vars = {  
      pypi_index_host: "server04"
  }
    ansible.playbook = "playbook.yml"
    ansible.verbose = "vv"
  end
end
