From 7e8077310d28624e89a071a782c2eb2d263abf50 Mon Sep 17 00:00:00 2001 From: "Scott S. Lowe" Date: Tue, 4 Oct 2016 09:41:29 -0600 Subject: [PATCH] Re-factor Docker+Vagrat+YAML environment Move host Vagrantfile into main directory, and refactor to use external YAML configuration file. Add multi-platform support. --- vagrant-docker-yaml/Vagrantfile | 20 ++++----- vagrant-docker-yaml/VagrantfileHost | 61 ++++++++++++++++++++++++++++ vagrant-docker-yaml/host/Vagrantfile | 27 ------------ vagrant-docker-yaml/hostvms.yml | 7 ++++ 4 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 vagrant-docker-yaml/VagrantfileHost delete mode 100644 vagrant-docker-yaml/host/Vagrantfile create mode 100644 vagrant-docker-yaml/hostvms.yml diff --git a/vagrant-docker-yaml/Vagrantfile b/vagrant-docker-yaml/Vagrantfile index 0aea644..52f5dc9 100644 --- a/vagrant-docker-yaml/Vagrantfile +++ b/vagrant-docker-yaml/Vagrantfile @@ -2,8 +2,8 @@ # vi: set ft=ruby : # Specify Vagrant version, Vagrant API version, and Vagrant clone location -Vagrant.require_version ">= 1.6.0" -VAGRANTFILE_API_VERSION = "2" +Vagrant.require_version '>= 1.6.0' +VAGRANTFILE_API_VERSION = '2' ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker' # Require 'yaml' module @@ -19,30 +19,30 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # Perform one-time configuration of Docker provider to specify # location of Vagrantfile for host VM; comment out this section # to use default boot2docker box - config.vm.provider "docker" do |docker| - docker.vagrant_vagrantfile = "host/Vagrantfile" + config.vm.provider 'docker' do |docker| + docker.vagrant_vagrantfile = 'VagrantfileHost' end # config.vm.provider # Iterate through the entries in the YAML file containers.each do |container| - config.vm.define container["name"] do |cntnr| + config.vm.define container['name'] do |cntnr| # Disable synced folders for the Docker container # (prevents an NFS error on "vagrant up") - cntnr.vm.synced_folder ".", "/vagrant", disabled: true + cntnr.vm.synced_folder '.', '/vagrant', disabled: true # Configure the Docker provider for Vagrant - cntnr.vm.provider "docker" do |docker| + cntnr.vm.provider 'docker' do |docker| # Specify the Docker image to use, pull value from YAML file - docker.image = container["image"] + docker.image = container['image'] # Specify port mappings, pull value from YAML file # If omitted, no ports are mapped! - docker.ports = container["ports"] + docker.ports = container['ports'] # Specify a friendly name for the Docker container, pull from YAML file - docker.name = container["name"] + docker.name = container['name'] end # contnr.vm.provider end # config.vm.define end # containers.each diff --git a/vagrant-docker-yaml/VagrantfileHost b/vagrant-docker-yaml/VagrantfileHost new file mode 100644 index 0000000..3ec98c6 --- /dev/null +++ b/vagrant-docker-yaml/VagrantfileHost @@ -0,0 +1,61 @@ +# -*- 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' module +require 'yaml' + +# Read YAML file with VM details (box, CPU, RAM, IP addresses) +# Edit host.yml to change VM configuration details +hostvms = YAML.load_file(File.join(File.dirname(__FILE__), 'hostvms.yml')) + +# Create and configure the VM(s) +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 + hostvms.each do |host| + + # Configure the VMs per details in YAML file + config.vm.define host['name'] do |srv| + + # Don't check for box updates + srv.vm.box_check_update = false + + # Specify the hostname of the VM + srv.vm.hostname = host['name'] + + # Specify the Vagrant box to use (use VMware box by default) + srv.vm.box = host['vmw_box'] + + # Disable synced folders (prevents an NFS error on "vagrant up") + srv.vm.synced_folder '.', '/vagrant', disabled: true + + # Assign additional private network + if host['ip_addr'] != nil + srv.vm.network 'private_network', ip: host['ip_addr'] + end # if host['ip_addr'] + + # Configure CPU & RAM per settings in hostvms.yml (Fusion) + srv.vm.provider 'vmware_fusion' do |vmw| + vmw.vmx['memsize'] = host['ram'] + vmw.vmx['numvcpus'] = host['vcpu'] + end # srv.vm.provider 'vmware_fusion' + + # Configure CPU & RAM per settings in hostvms.yml (VirtualBox) + srv.vm.provider 'virtualbox' do |vb, override| + vb.memory = host['ram'] + vb.cpus = host['vcpu'] + override.vm.box = host['vb_box'] + end # srv.vm.provider 'virtualbox' + + # Provision the VM with Docker + srv.vm.provision 'docker' + end # config.vm.define + end # hostvsm.each +end # Vagrant.configure diff --git a/vagrant-docker-yaml/host/Vagrantfile b/vagrant-docker-yaml/host/Vagrantfile deleted file mode 100644 index 967b9bb..0000000 --- a/vagrant-docker-yaml/host/Vagrantfile +++ /dev/null @@ -1,27 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -# Specify Vagrant version and Vagrant API version -Vagrant.require_version ">= 1.6.0" -VAGRANTFILE_API_VERSION = "2" - -# Create and configure the VM(s) -Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - - # Assign a friendly name to this host VM - config.vm.hostname = "docker-host" - - # Skip checking for an updated Vagrant box - config.vm.box_check_update = false - - # Always use Vagrant's default insecure key - config.ssh.insert_key = false - - # Spin up a "host box" for use with the Docker provider - # and then provision it with Docker - config.vm.box = "slowe/ubuntu-trusty-x64" - config.vm.provision "docker" - - # Disable synced folders (prevents an NFS error on "vagrant up") - config.vm.synced_folder ".", "/vagrant", disabled: true -end diff --git a/vagrant-docker-yaml/hostvms.yml b/vagrant-docker-yaml/hostvms.yml new file mode 100644 index 0000000..9f67afb --- /dev/null +++ b/vagrant-docker-yaml/hostvms.yml @@ -0,0 +1,7 @@ +--- +- name: "default" + vmw_box: "slowe/ubuntu-trusty-x64" + vb_box: "ubuntu/trusty64" + ram: "1024" + vcpu: "1" + ip_addr: "192.168.100.101"