Skip to content

Commit

Permalink
Define most options in external config file, with ability to override
Browse files Browse the repository at this point in the history
  • Loading branch information
bbatsche committed Jun 6, 2018
1 parent 2b3872d commit d89ec17
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,4 @@ tmtags
### Other Files ###
*.log
/roles/*
/config.yml
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gem 'rspec', '~> 3.3'
gem 'serverspec', '~> 2.24'
gem 'net-ssh', '~> 4.0'
gem 'docker-api', '~> 1.33'
gem 'deep_merge', '~> 1.2'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
deep_merge (1.2.1)
diff-lcs (1.3)
docker-api (1.34.2)
excon (>= 0.47.0)
Expand Down Expand Up @@ -45,6 +46,7 @@ PLATFORMS

DEPENDENCIES
bundler (~> 1.15)
deep_merge (~> 1.2)
docker-api (~> 1.33)
net-ssh (~> 4.0)
rake (~> 12.0)
Expand Down
44 changes: 35 additions & 9 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure('2') do |config|
config.vm.define 'trusty' do |trusty|
trusty.vm.box = 'ubuntu/trusty64'
end
require "yaml"

config.vm.define 'xenial' do |xenial|
xenial.vm.box = 'ubuntu/xenial64'
end
default_config_file = File.join(File.dirname(__FILE__), "config.yml.dist")
local_config_file = File.join(File.dirname(__FILE__), "config.yml")

config_data = YAML.load_file default_config_file
config_data = Vagrant::Util::DeepMerge.deep_merge(config_data, YAML.load_file(local_config_file)) if File.exists? local_config_file

Vagrant.configure("2") do |config|
["trusty", "xenial", "bionic"].each do |distro|
config.vm.define distro do |target_env|
vm_config = Vagrant::Util::DeepMerge.deep_merge config_data["vm"]["global"], config_data["vm"][distro]

target_env.vm.box = vm_config["box"]

config.vm.define 'bionic' do |xenial|
xenial.vm.box = 'ubuntu/bionic64'
target_env.vm.provider :virtualbox do |vb, override|
vb.memory = vm_config["ram"]
vb.cpus = vm_config["num_cpus"]

vb.linked_clone = true
end

target_env.vm.provider :vmware_fusion do |vmw, override|
vmw.vmx["memsize"] = vm_config["ram"]
vmw.vmx["numvcpus"] = vm_config["num_cpus"]
end

target_env.vm.provider :parallels do |p, override|
p.memory = vm_config["ram"]
p.cpus = vm_config["num_cpus"]

p.check_guest_tools = false
p.update_guest_tools = false
end
end
end

config.vm.provision :ansible do |ansible|
ansible.playbook = "provision-playbook.yml"

ansible.compatibility_mode = "2.0"

ansible.extra_vars = config_data["ansible"]["vars"]
end

if Vagrant.has_plugin? 'vagrant-vbguest'
Expand Down
14 changes: 14 additions & 0 deletions config.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
vm:
global:
ram: 1024
num_cpus: 2
trusty:
box: ubuntu/trusty64
xenial:
box: ubuntu/xenial64
bionic:
box: ubuntu/bionic64
ansible:
vars:
swapfile_size: 1G
11 changes: 10 additions & 1 deletion spec/lib/docker_env.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require "docker"
require "tempfile"
require "yaml"
require "deep_merge"
require "json"

class DockerEnv
attr_reader :name, :image
Expand All @@ -14,13 +17,19 @@ def up
end

def provision
default_config_file = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "config.yml.dist")
local_config_file = File.join(File.dirname(File.dirname(File.dirname(__FILE__))), "config.yml")

config_data = YAML.load_file default_config_file
config_data.deep_merge!(YAML.load_file local_config_file) if File.exists? local_config_file

inventory = Tempfile.new("inventory")

inventory << inventory_line

inventory.close

command = "ansible-playbook", "-i", inventory.path, "-l", @name, "provision-playbook.yml"
command = "ansible-playbook", "-i", inventory.path, "-l", @name, "provision-playbook.yml", "-e", config_data["ansible"]["vars"].to_json

output = []
IO.popen(command, {:err => [:child, :out]}) do |io|
Expand Down

0 comments on commit d89ec17

Please sign in to comment.