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

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

NUM_BOXES=2
PROJECT_NAME="hazelcast-integration-amazon-ec2"
CHEF_COOKBOOKS_PATH="../chef/cookbooks"
CHEF_RECIPE=PROJECT_NAME

Vagrant.require_version ">= 1.5.0"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # A Vagrant plugin that ensures the desired version of Chef is installed via the platform-specific Omnibus packages. 
  # This proves very useful when using Vagrant with provisioner-less baseboxes OR cloud images.
  #
  # Make sure you've pre-installed vagrant-omnibus by running this on the command line :-
  #
  # vagrant plugin install vagrant-omnibus
  
  config.omnibus.chef_version = :latest

  # Enable Berkshelf cookbook dependency manager (http://berkshelf.com/)
  # If you're using a later version of Chef DK it will come pre-installed.
  
  config.berkshelf.enabled = true
  config.berkshelf.berksfile_path = CHEF_COOKBOOKS_PATH+"/"+CHEF_RECIPE+"/Berksfile"

  # Attach Local Release Directory to Virtual Machines
  config.vm.synced_folder "../../../target/","/mnt/hazelcast-integration-amazon-ec2/target"

  # VIRTUAL BOX PROVIDER

  config.vm.provider :virtualbox do |vb, override|

    override.vm.box = "ubuntu/trusty64"

    # Make sure you put your own network interface name in here
    override.vm.network :public_network, bridge: "en3: Thunderbolt Ethernet"

    override.vm.provision "chef_solo" do |chef|
      chef.cookbooks_path = CHEF_COOKBOOKS_PATH
      chef.add_recipe "hazelcast-integration-amazon-ec2"
    end
  end

  # AMAZON EC2 PROVIDER

  # To use the Amazon provider you'll need to install the AWS Vagrant Plugin
  # Follow the instructions found here :-
  # https://github.com/mitchellh/vagrant-aws

  config.vm.provider :aws do |aws, override|

    override.vm.box = "dummy"
    override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"

    aws.region =  "us-east-1"
    aws.access_key_id = ENV['AWS_ACCESS_KEY']
    aws.secret_access_key = ENV['AWS_SECRET_KEY']
    aws.instance_type = 'm3.medium'
    aws.ami = "ami-9eaa1cf6" # Ubuntu Server 14.04 LTS (HVM), SSD Volume Type
    aws.keypair_name = 'david'
    aws.security_groups = 'david-us-east-1-sg'

    aws.tags = {'hazelcast_service' => 'true'}

    override.ssh.username = 'ubuntu'
    override.ssh.private_key_path = '~/.ssh/aws.pem'

    override.vm.provision "chef_solo" do |chef|
      chef.cookbooks_path = CHEF_COOKBOOKS_PATH
      chef.json = {
                      :hazelcast => {
                      :network_aws_access_key => ENV['AWS_ACCESS_KEY'],
                      :network_aws_secret_key => ENV['AWS_SECRET_KEY'],
                      :network_aws_region => "us-east-1",
                      :network_aws_host_header => "https://ec2.us-east-1.amazonaws.com",
                      :network_aws_security_group => "david-us-east-1-sg",
                      :network_aws_tag_key => "hazelcast_service",
                      :network_aws_tag_value => "true",
                      :network_multicast_enabled => "false",
                      :network_aws_enabled => "true" 
                    }}
      chef.add_recipe "hazelcast-integration-amazon-ec2"
    end

  end

  (1..NUM_BOXES).each do |i|    
    config.vm.define "hazelcast#{i}".to_sym do |vm|
    end
  end

end