# This file builds a ubuntu 15.04 given the Json file nodes.json.

nodes_config = (JSON.parse(File.read("nodes.json")))['nodes']

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Ubuntu 15.04: vivid-64-bit
  #config.vm.box = "ubuntu/vivid64"
  #config.vm.box = "larryli/vivid64" 
  config.vm.box = "ubuntu/trusty64"

  nodes_config.each do |node|
    node_name = node[0] # name of node 
    node_values = node[1] # content of node

    config.vm.define node_name do |config|
      # configures all forwarding ports in JSON array
      ports = node_values['ports']
      ports.each do |port|
        config.vm.network :forwarded_port,
        host:  port[':host'],
        guest: port[':guest'],
        id:    port[':id']
      end

      config.vm.hostname = node_name
      config.vm.network :private_network, ip: node_values[':ip'], virtualbox__intnet: node_values[':intnet']

      config.vm.provider :virtualbox do |vb|
        vb.customize ["modifyvm", :id, "--memory", node_values[':memory']]
        vb.customize ["modifyvm", :id, "--name", node_name]
      end

      #config.vm.provision :shell, :path => node_values[':bootstrap']
    end
  end
end
