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

Vagrant.configure('2') do |config|
  config.berkshelf.enabled = false if Vagrant.has_plugin? 'vagrant-berkshelf'

  config.vm.hostname = 'builderator-<%= build_name %>'

  ## Local Provider
  config.vm.provider '<%= profile.current.vagrant.local.provider %>' do |local, override|
    local.memory = <%= profile.current.vagrant.local.memory %>
    local.cpus = <%= profile.current.vagrant.local.cpus %>

    override.vm.box = '<%= profile.current.vagrant.local.box %>'
<% if profile.current.vagrant.local.has?(:box_url) -%>
    override.vm.box_url = '<%= profile.current.vagrant.local.box_url %>'
<% end -%>
  end

  ## EC2 Provider
  config.vm.provider '<%= profile.current.vagrant.ec2.provider %>' do |ec2, override|
    override.vm.box = '<%= profile.current.vagrant.ec2.box %>'
    override.vm.box_url = '<%= profile.current.vagrant.ec2.box_url %>'
    override.ssh.username = '<%= profile.current.vagrant.ec2.ssh_username %>'

    ec2.region = '<%= profile.current.vagrant.ec2.region %>'
    ec2.subnet_id = '<%= profile.current.vagrant.ec2.subnet_id %>'

    ## VPN Connected VPC
    ec2.associate_public_ip = <%= profile.current.vagrant.ec2.associate_public_ip %>
    ec2.ssh_host_attribute = :<%= profile.current.vagrant.ec2.ssh_host_attribute %>

    ec2.instance_type = '<%= profile.current.vagrant.ec2.instance_type %>'
    ec2.security_groups = <%= profile.current.vagrant.ec2.security_groups %>
    ec2.iam_instance_profile_arn = '<%= profile.current.vagrant.ec2.iam_instance_profile_arn %>'

    ec2.ami = '<%= profile.current.vagrant.ec2.source_ami %>'
  end

  # config.vm.network :forwarded_port, :host => 9200, :guest => 9200

  ##
  # Use the standard data directory for Chef
  ##
  config.vm.provision :shell,
                      :inline => "sudo mkdir -p <%= chef.staging_directory %>/cache && "\
                                 "sudo chown $(whoami) -R <%= chef.staging_directory %>",
                      :privileged => false

  ##
  # Sync build artifacts to the VM
  ##
<% profile.current.artifact.each do |name, artifact| -%>
  config.vm.provision :file,
                      :source => '<%= artifact.path %>',
                      :destination => '<%= artifact.destination %>'
<% end -%>

  config.omnibus.chef_version = '<%= chef.version %>'
  config.vm.provision :chef_solo do |chef|
    chef.log_level = :<%= chef.log_level %>

    chef.cookbooks_path = '<%= local.cookbook_path %>'
<% unless local.data_bag_path.nil? -%>
    chef.data_bags_path = '<%= local.data_bag_path %>'
<% end -%>
<% unless local.environment_path.nil? -%>
    chef.environments_path = '<%= local.environment_path %>'
<% end -%>
    chef.provisioning_path = '<%= chef.staging_directory %>'

    chef.run_list = <%= profile.current.chef.run_list %>
<% unless profile.current.chef.environment.nil? -%>
    chef.environment = '<%= profile.current.chef.environment %>'
<% end -%>

<% if profile.current.chef.node_attrs.nil? -%>
    chef.json = {}
<% else -%>
    chef.json = <%= profile.current.chef.node_attrs %>
<% end -%>

    ## Tell Chef that this is a Vagrant build
    chef.json[:vagrant] = true
  end
end