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

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

def load_json(name)
  path = File.join(File.dirname(__FILE__), "nodes", name)
  JSON.load(File.open(path))
end

boxes = [
  { name: "puppet0.lctp", provision: :puppet, mac: "5CA1AB1E0F01"}, # uncomment to enable puppet
  { name: "node0.lctp", provision: :chef, role: :head_node,    mac: "5CA1AB1E0001", json: load_json("node0.json") },
  { name: "node1.lctp", provision: :chef, role: :compute_node, mac: "5CA1AB1E0002", json: load_json("node1.json") },
  { name: "node2.lctp", provision: :chef, role: :compute_node, mac: "5CA1AB1E0003", json: load_json("node2.json") }
]

["vbguest", "berkshelf"].each do |plugin|
  begin
    require "vagrant-#{plugin}"
  rescue LoadError
    puts "#{plugin} plugin not installed!"
    puts "run:"
    puts "\tvagrant plugin install vagrant-#{plugin}"
    exit(1)
  end
end


Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vbguest.auto_update = true
  config.vbguest.auto_reboot = true

  boxes.each do |box|
    config.vm.define box[:name] do |node|
      node.vm.provider :virtualbox do |vb|
        # access via tty => user: vagrant, password: vagrant
        #vb.gui = false

        # 1. adapter: NAT to allow vagrant setup the machine
        # 2. adapter: for internal network between nodes
        vb.customize ["modifyvm", :id,
                      "--nic1", "nat",
                      "--nictype1", "virtio",
                      "--nic2", "intnet",
                      "--nictype2", "virtio",
                      "--intnet2", "lctp",
                      "--macaddress2", box[:mac]]
      end

      config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"
      node.vm.hostname = box[:name]

      if box[:provision] == :chef
        node.vm.box = "opscode_ubuntu-12.04_chef-11.4.4"
        node.vm.box_url = "https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_chef-11.4.4.box"

        # Enabling the Berkshelf plugin. To enable this globally, add this configuration
        # option to your ~/.vagrant.d/Vagrantfile file
        node.berkshelf.enabled = true

        # Update Chef in VM to specific version before running chef provisioner
        node.vm.provision :shell do |shell|
          shell.path = "script/upgrade_chef.sh"
          # target version
          shell.args = "11.8.2"
        end
        node.vm.provision :chef_solo do |chef|
          # Verbose logging
          #chef.arguments = '-l debug'
          chef.cookbooks_path = "cookbooks"
          chef.data_bags_path = "data_bags"
          chef.roles_path     = "roles"
          chef.add_role box[:role].to_s
          chef.json = box[:json]
        end
      elsif box[:provision] == :puppet
        node.vm.box = "ubuntu-server-12042-x64-vbox4210"
        node.vm.box_url = "http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210.box"

        node.vm.provision :puppet, options: ["--pluginsync"] do |puppet|
          puppet.module_path = "modules"
        end
      else
        $stderr.puts "Unknown provisioning #{box[:provision]}"
      end
    end
  end
end
