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

# This Vagrantfile was inspired by Joshua Timberman's blog
# http://jtimberman.housepub.org/blog/2012/03/18/multivm-vagrantfile-for-chef/

#
# Vagrantfile for two-machine test topology. Runs with a private network
# and port forwarding from 3001 on appserver guest, to port 303n on host (i.e. 3031 for topo with id 1). 
# ipaddresses are 10.0.1.2, 10.0.1.3 for dbserver, appserver. 

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

id = 1 
qual = "0#{id}"

 # Define the options for each node
nodes = {
	:dbserver => {
		:hostname => "dbserver#{qual}",
		:ipaddress => "10.0.#{id}.2"
	},
	:appserver => {
		:hostname => "appserver#{qual}",
		:ipaddress => "10.0.#{id}.3",
		:forwardports => [ 
		{
			:guest => 3001, 
			:host => (3030 +  id) 
		}
		]
	}
}

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

	# setup each node using config ("options") defined above
	nodes.each do |node, options|
		config.vm.define node do |node_config|
		
			# setup networking using private network, with port forwarding where needed to access
			# TODO how to let ip be optional in either case
			net_type = ( options.has_key?(:public)) ? :public_network : :private_network
			if options.has_key?(:public)
    			node_config.vm.network net_type, ip: options[:ipaddress]			
			else
				node_config.vm.network net_type, ip: options[:ipaddress]		
			end
			
			if options.has_key?(:forwardports)
				options[:forwardports].each do |port|
					node_config.vm.network :forwarded_port, guest: port[:guest], host: port[:host]
				end
			end
			node_config.vm.hostname = options[:hostname]
		end
	end

  # Setup the generic config across both servers
  config.vm.box = "ubuntu64"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"

end