| 1 |
def vm(opt) |
| 2 |
module_name = opt.fetch(:module).to_s || raise(ArgumentError, 'Must provide puppet module name') |
| 3 |
hostname = opt.fetch(:hostname, module_name).to_s |
| 4 |
memory = opt.fetch(:memory, 512) |
| 5 |
cpu = opt.fetch(:cpu, 1) |
| 6 |
box = opt.fetch(:box).to_s || raise(ArgumentError, 'Must provide box type.') |
| 7 |
url = opt.fetch(:url, '').to_s |
| 8 |
os_type = opt[:os_type] || opt[:type] || :linux |
| 9 |
gui = opt.fetch(:gui, false) |
| 10 |
ports = Array(opt.fetch(:port, [])) |
| 11 |
iso = opt.fetch(:iso, nil) |
| 12 |
proj_root = File.expand_path(File.join(File.dirname(__FILE__))) |
| 13 |
fixture_modules = File.join(proj_root, 'spec', 'fixtures', 'modules') |
| 14 |
|
| 15 |
Vagrant.configure('2') do |conf| |
| 16 |
|
| 17 |
# forward all the ports |
| 18 |
ports.each do |p| |
| 19 |
conf.vm.network(:forwarded_port, guest: p, host: p, auto_correct: true) |
| 20 |
end |
| 21 |
|
| 22 |
if os_type == :windows |
| 23 |
conf.ssh.username = 'vagrant' |
| 24 |
conf.winrm.username = 'vagrant' |
| 25 |
conf.winrm.password = 'vagrant' |
| 26 |
end |
| 27 |
|
| 28 |
conf.vm.define hostname.to_sym do |mod| |
| 29 |
mod.vm.box = box |
| 30 |
mod.vm.box_url = url |
| 31 |
|
| 32 |
if os_type == :windows |
| 33 |
mod.vm.guest = :windows |
| 34 |
mod.vm.communicator = 'winrm' |
| 35 |
mod.vm.synced_folder './' , "/ProgramData/PuppetLabs/puppet/etc/modules/#{module_name}" |
| 36 |
mod.vm.synced_folder 'spec/fixtures/modules' , '/temp/modules' |
| 37 |
else |
| 38 |
mod.vm.synced_folder './', "/etc/puppet/modules/#{module_name}" |
| 39 |
mod.vm.synced_folder 'spec/fixtures/modules', '/tmp/puppet/modules' |
| 40 |
end |
| 41 |
|
| 42 |
mod.vm.hostname = hostname |
| 43 |
|
| 44 |
mod.vm.provider :vmware_fusion do |f| |
| 45 |
f.gui = gui |
| 46 |
f.vmx['displayName'] = hostname |
| 47 |
f.vmx['memsize'] = memory |
| 48 |
f.vmx['numvcpus'] = cpu |
| 49 |
if iso |
| 50 |
f.vmx['ide1:0.devicetype'] = "cdrom-image" |
| 51 |
f.vmx['ide1:0.filename'] = iso |
| 52 |
end |
| 53 |
end |
| 54 |
|
| 55 |
mod.vm.provider :vmware_workstation do |f| |
| 56 |
f.gui = gui |
| 57 |
f.vmx['displayName'] = hostname |
| 58 |
f.vmx['memsize'] = memory |
| 59 |
f.vmx['numvcpus'] = cpu |
| 60 |
if iso |
| 61 |
f.vmx['ide1:0.devicetype'] = "cdrom-image" |
| 62 |
f.vmx['ide1:0.filename'] = iso |
| 63 |
end |
| 64 |
end |
| 65 |
|
| 66 |
mod.vm.provider :virtualbox do |v| |
| 67 |
v.gui = gui |
| 68 |
v.name = hostname |
| 69 |
v.memory = memory |
| 70 |
v.cpus = cpu |
| 71 |
end |
| 72 |
|
| 73 |
if os_type == :windows |
| 74 |
manifest = ENV['VAGRANT_MANIFEST'] || 'init.pp' |
| 75 |
#mod.vm.provision :shell, :inline => "@powershell -NoProfile -ExecutionPolicy Bypass -Command \"iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))\" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" |
| 76 |
#mod.vm.provision :shell, :inline => "choco install puppet" |
| 77 |
mod.vm.provision :shell, :inline => "puppet apply --modulepath 'C:/ProgramData/PuppetLabs/puppet/etc/modules;C:/temp/modules' --verbose C:/ProgramData/PuppetLabs/puppet/etc/modules/#{module_name}/tests/#{manifest}" |
| 78 |
else |
| 79 |
mod.vm.provision :puppet do |p| |
| 80 |
p.manifests_path = 'tests' |
| 81 |
# p.hiera_config_path = File.join(fixture_modules, 'hieradata', 'hiera.yaml') |
| 82 |
p.manifest_file = ENV['VAGRANT_MANIFEST'] || 'init.pp' |
| 83 |
#p.module_path = fixture_modules |
| 84 |
# because of how symlinks are handled via the spec_helper we are forced to mount the modules is different locations |
| 85 |
# otherwise we could just use the above option |
| 86 |
p.options = '--modulepath="/etc/puppet/modules:/tmp/puppet/modules"' |
| 87 |
end |
| 88 |
end |
| 89 |
end |
| 90 |
end |
| 91 |
end |
| 92 |
module_name = File.basename(File.expand_path(File.join(File.dirname(__FILE__)))) |
| 93 |
vm :hostname => 'win2012r2', :module => module_name, :box => 'opentable/win-2012r2-standard-amd64-nocm', :url => 'opentable/win-2012r2-standard-amd64-nocm', :os_type => :windows, :cpu => 1, :memory => 4096, :gui => true |
| 94 |
vm :hostname => 'win2008r2', :module => module_name, :box => 'opentable/win-2008r2-standard-amd64-nocm', :url => 'opentable/win-2008r2-standard-amd64-nocm', :os_type => :windows, :cpu => 1, :memory => 4096, :gui => true |
| 95 |
vm :hostname => 'centos6', :module => module_name, :box => 'puppetlabs/centos-6.6-64-puppet', :url => 'puppetlabs/centos-6.6-64-puppet', :cpu => 1, :memory => 2048, :gui => false |