scottslowe-learning-tools/docker-swarm-ha/Vagrantfile
Scott Lowe 81b1458b0f Add Ruby code to all Vagrantfiles
Add Ruby code to determine full path to supporting files referenced
in all Vagrantfiles. This will allow more effective use of Vagrant
commands when not in the same directory as the Vagrantfile.
2016-01-13 13:48:02 -07:00

93 lines
3.4 KiB
Ruby

# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify Vagrant version, Vagrant API version, and Vagrant clone location
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
ENV['VAGRANT_VMWARE_CLONE_DIRECTORY'] = '~/.vagrant'
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_fusion'
#ENV['VAGRANT_DEFAULT_PROVIDER'] = 'vmware_appcatalyst'
# Require 'yaml', 'fileutils', and 'erb' modules
require 'yaml'
require 'fileutils'
require 'erb'
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Be sure to edit servers.yml to provide correct IP addresses
servers = YAML.load_file(File.join(File.dirname(__FILE__), 'servers.yml'))
# Use template to create server-specific configuration files
template = File.join(File.dirname(__FILE__), 'config.json.erb')
content = ERB.new File.new(template).read
# Populate cluster list
cluster_list = []
servers.each do |server|
if server['role'] == 'consul'
cluster_list << "\"#{server['priv_ip']}\""
end # if server['role']
end # servers.each
# Write out server-specific configuration files
servers.each do |server|
if server['role'] == 'consul'
ip = server['priv_ip']
target = File.join(File.dirname(__FILE__), "#{server['name']}.config.json")
File.open(target, 'w') { |f| f.write(content.result(binding)) }
end # if server['role']
end # servers.each
# Create and configure the VMs
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Always use Vagrant's default insecure key
config.ssh.insert_key = false
# Iterate through entries in YAML file to create VMs
servers.each do |server|
config.vm.define server['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
# Specify hostname and Vagrant box
srv.vm.hostname = server['name']
srv.vm.box = server['box']
# Assign an additional static private network
srv.vm.network 'private_network', ip: server['priv_ip']
# Specify default synced folder; requires VMware Tools
srv.vm.synced_folder '.', '/vagrant', disabled: true
# Configure VM based on "role" value from servers.yml
if server['role'] == 'consul'
# Copy Consul configuration files
srv.vm.provision 'file', source: "#{server['name']}.config.json", destination: '/home/vagrant/config.json'
srv.vm.provision 'file', source: 'consul.conf', destination: '/home/vagrant/consul.conf'
# Provision consul to the VMs
srv.vm.provision 'shell', path: 'consul.sh'
end # if server['role']
if server['role'] == 'docker'
srv.vm.provision 'docker', images: ['swarm']
srv.vm.provision 'shell', inline: 'echo DOCKER_OPTS=\"-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375\" >> /etc/default/docker', privileged: true
srv.vm.provision 'shell', inline: 'service docker restart', privileged: true
end
# Configure VMs with RAM and CPUs per settings in servers.yml
srv.vm.provider :vmware_fusion do |vmw|
vmw.vmx['memsize'] = server['ram']
vmw.vmx['numvcpus'] = server['vcpu']
end # srv.vm.provider vmware_fusion
# Configure VMs with RAM and CPUs per settings in servers.yml
srv.vm.provider :vmware_appcatalyst do |vmw|
vmw.vmx['memsize'] = server['ram']
vmw.vmx['numvcpus'] = server['vcpu']
end # srv.vm.provider vmware_appcatalyst
end # config.vm.define
end # servers.each
end # Vagrant.configure