scottslowe-learning-tools/docker-swarm-ha/Vagrantfile
Scott S. Lowe 62837642cb Shift to nested YAML data files
Edit machines.yml to use nested YAML data structures for box names. Edit Vagrantfile to reference updated YAML data appropriately.

Signed-off-by: Scott S. Lowe <scott.lowe@scottlowe.org>
2016-11-09 23:35:13 -07:00

105 lines
3.8 KiB
Ruby

# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
# Require 'yaml', 'fileutils', and 'erb' modules
require 'yaml'
require 'fileutils'
require 'erb'
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Edit machines.yml to change VM configuration details
machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.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
# Build array of IP addresses for Consul cluster
consul_cluster_list = []
machines.each do |machine|
if machine['role'] == 'consul'
consul_cluster_list << "\"#{machine['ip_addr']}\""
end # if machine['role']
end # machines.each
# Write out VM-specific configuration files
machines.each do |machine|
if machine['role'] == 'consul'
ip = machine['ip_addr']
target = File.join(File.dirname(__FILE__), "#{machine['name']}.config.json")
File.open(target, 'w') { |f| f.write(content.result(binding)) }
end # if machine['role']
end # machines.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
machines.each do |machine|
# Configure the VMs per details in machines.yml
config.vm.define machine['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
# Specify the hostname of the VM
srv.vm.hostname = machine['name']
# Specify the Vagrant box to use (use VMware box by default)
srv.vm.box = machine['box']['vmw']
# Configure default synced folder (disable by default)
if machine['sync_disabled'] != nil
srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled']
else
srv.vm.synced_folder '.', '/vagrant', disabled: true
end #if machine['sync_disabled']
# Assign additional private network
if machine['ip_addr'] != nil
srv.vm.network 'private_network', ip: machine['ip_addr']
end # if machine['ip_addr']
# Configure CPU & RAM per settings in machines.yml (Fusion)
srv.vm.provider 'vmware_fusion' do |vmw|
vmw.vmx['memsize'] = machine['ram']
vmw.vmx['numvcpus'] = machine['vcpu']
if machine['nested'] == true
vmw.vmx['vhv.enable'] = 'TRUE'
end #if machine['nested']
end # srv.vm.provider 'vmware_fusion'
# Configure CPU & RAM per settings in machines.yml (VirtualBox)
srv.vm.provider 'virtualbox' do |vb, override|
vb.memory = machine['ram']
vb.cpus = machine['vcpu']
override.vm.box = machine['box']['vb']
end # srv.vm.provider 'virtualbox'
# Configure VMs with the 'consul' role assigned in machines.yml
if machine['role'] == 'consul'
# Copy Consul configuration files
srv.vm.provision 'file', source: "#{machine['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 # machine['role'] == 'consul'
if machine['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 # if machine['role'] == 'docker'
end # config.vm.define
end # machines.each
end # Vagrant.configure