mirror of
https://codeberg.org/scottslowe/learning-tools.git
synced 2026-03-11 09:04:37 +00:00
Update Vagrantfile and associated data files (YAML and/or JSON) to provide better multi-platform support via provider overrides.
92 lines
3.3 KiB
Ruby
92 lines
3.3 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'
|
|
|
|
# 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['vmw_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 (Fusion)
|
|
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 (VirtualBox)
|
|
srv.vm.provider 'virtualbox' do |vb, override|
|
|
vb.memory = server['ram']
|
|
vb.cpus = server['vcpu']
|
|
override.vm.box = server['vb_box']
|
|
end # srv.vm.provider virtualbox
|
|
end # config.vm.define
|
|
end # servers.each
|
|
end # Vagrant.configure
|