-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
489 lines (446 loc) · 17.7 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
require 'yaml'
VAGRANTFILE_API_VERSION = '2'.freeze
VALID_KEYS = [
'dhcp_fix',
'public_ip',
'private_ip',
'domain_name',
'additional_hosts',
'box',
'puppet_master',
'disks',
'cluster',
'puppet_installer',
'required_plugins',
'software_files',
'protocol',
'type',
'cpucount',
'ram',
'virtualboxorafix',
'needs_storage',
'custom_facts',
]
class FilesNotFoundError < Vagrant::Errors::VagrantError
def error_message
'Missing software files'
end
end
class InvalidDefinition < Vagrant::Errors::VagrantError
def error_message
'Invalid definition in server.yaml'
end
end
def validate_definitions(content)
errors = []
content.each do | key, values|
errors << "Node #{key} needs and 'ml-' prefix for masterless or an 'pe-' prefix for Puppset agent" if key[0,3] != 'ml-' && key[0,3] != 'pe-'
unknown_keys = values.keys - VALID_KEYS
next if unknown_keys.empty?
errors << "Node #{key} contains unkown entries #{unknown_keys.join(', ')}"
end
errors
end
def servers
content = YAML.load_file("#{VAGRANT_ROOT}/servers.yaml")
defaults = content.delete('defaults') || {}
pe_defaults = content.delete('pe-defaults') || {}
ml_defaults = content.delete('ml-defaults') || {}
content.each do |key, values|
case key[0, 3]
when 'ml-'
content[key] = defaults.merge(ml_defaults).merge(values)
when 'pe-'
content[key] = defaults.merge(pe_defaults).merge(values)
end
end
errors = validate_definitions(content)
if errors.any?
puts 'servers.yaml contains following errors:'
errors.each { |e| puts "- #{e}\n" }
raise InvalidDefinition
end
content
end
# Return a shell command that ensures that all vagrant hosts are in /etc/hosts
def hosts_file(vms, ostype)
if ostype == 'linux'
commands = 'sed -i -e /127.0.*.*/d /etc/hosts;'
vms.each do |k, v|
hostname = k[3..]
domain = v['domain_name']
fqdn = "#{hostname}.#{domain}"
if v['public_ip']
commands << "grep -q #{fqdn} /etc/hosts || " \
"echo #{v['public_ip']} #{fqdn} #{hostname} " \
'>> /etc/hosts;'
end
next unless v['additional_hosts']
v['additional_hosts'].each do |key, value|
fqdn = "#{key}.#{domain}"
commands << "grep -q #{fqdn} /etc/hosts || " \
"echo #{value['ip']} #{fqdn} #{key} " \
'>> /etc/hosts;'
end
end
else
commands = 'puppet apply c:\vagrant\windows_hosts_file.pp'
win_hosts = ''
vms.each do |k, v|
hostname = k[3..]
domain = v['domain_name']
fqdn = "#{hostname}.#{domain}"
win_hosts << "host { '#{fqdn}': ip => '#{v['public_ip']}', host_aliases => '#{hostname}' }\n" if v['public_ip']
next unless v['additional_hosts']
v['additional_hosts'].each do |key, value|
fqdn = "#{key}.#{domain}"
win_hosts << "host { '#{fqdn}': ip => '#{value['ip']}', host_aliases => '#{key}' }\n"
end
end
win_hosts = win_hosts.split("\n").uniq.join("\n")
File.open(File.join(VAGRANT_ROOT, 'windows_hosts_file.pp'), 'w') do |f|
f.write(win_hosts)
end
end
commands
end
# Returns a shell command that sets the custom facts
def facter_overrides(facts, ostype)
if ostype == 'linux'
facter_overrides = facts.map { |key, value| "export FACTER_#{key}=\"#{value}\"" }.join('\n')
"echo -e '#{facter_overrides}' > /etc/profile.d/facter_overrides.sh"
else
facter_overrides = facts.map { |key, value| "Write-Host #{key}=#{value}" }.join('`r')
"echo '#{facter_overrides}' > C:\\ProgramData\\PuppetLabs\\facter\\facts.d\\facter_overrides.ps1"
end
end
# Read YAML file with box details
pe_puppet_user_id = 988
pe_puppet_group_id = 988
VAGRANT_ROOT = File.dirname(__FILE__)
home = ENV['HOME']
def masterless_setup(server, srv, hostname)
if srv.vm.communicator == 'ssh'
if server['custom_facts']
@provisioners << { shell: { inline: facter_overrides(server['custom_facts'], 'linux'),
name: 'facter_overrides' } }
end
@provisioners << { shell: { inline: hosts_file(servers, 'linux'),
name: 'hosts_file' } }
@provisioners << { shell: { inline: 'bash /vagrant/vm-scripts/install_puppet.sh',
name: 'install_puppet.sh' } }
@provisioners << { shell: { inline: 'bash /vagrant/vm-scripts/setup_puppet.sh',
name: 'setup_puppet.sh' } }
@provisioners << { puppet: { manifests_path: ['vm', '/vagrant/manifests'],
manifest_file: 'site.pp',
options: '--test' } }
else
@provisioners << { shell: { inline: %(Set-ExecutionPolicy Bypass -Scope Process -Force
cd c:\\vagrant\\vm-scripts
.\\install_puppet.ps1
cd c:\\vagrant\\vm-scripts
.\\setup_puppet.ps1
iex "& 'C:\\Program Files\\Puppet Labs\\Puppet\\bin\\puppet' resource service puppet ensure=stopped"),
name: 'install and setup puppet' } }
@provisioners << { shell: { inline: hosts_file(servers, 'windows'),
name: 'hosts_file' } }
if server['custom_facts']
@provisioners << { shell: { inline: facter_overrides(server['custom_facts'], 'windows'),
name: 'facter_overrides' } }
end
@provisioners << { shell: { inline: %(Set-ExecutionPolicy Bypass -Scope Process -Force
cd c:\\vagrant\\vm-scripts
.\\run_puppet.ps1),
name: 'run puppet' } }
end
end
def puppet_master_setup(srv, server, puppet_installer, pe_puppet_user_id, pe_puppet_group_id, hostname)
srv.vm.synced_folder '.', '/vagrant', owner: pe_puppet_user_id, group: pe_puppet_group_id
if server['custom_facts']
@provisioners << { shell: { inline: facter_overrides(server['custom_facts'], 'linux'),
name: 'facter_overrides' } }
end
@provisioners << { shell: { inline: hosts_file(servers, 'linux'),
name: 'hosts_file' } }
@provisioners << { shell: { inline: "bash /vagrant/vm-scripts/install_puppet_server.sh #{puppet_installer} #{server['domain_name']}",
name: 'install_puppet_server.sh' } }
@provisioners << { puppet_server: { puppet_server: "#{server['puppet_master']}.#{server['domain_name']}",
options: '--test' } }
end
def puppet_agent_setup(server, srv, hostname)
if srv.vm.communicator == 'ssh'
if server['custom_facts']
@provisioners << { shell: { inline: facter_overrides(server['custom_facts'], 'linux'),
name: 'facter_overrides' } }
end
@provisioners << { shell: { inline: hosts_file(servers, 'linux'),
name: 'hosts_file' } }
@provisioners << { shell: { inline: "bash /vagrant/vm-scripts/install_puppet_agent.sh #{server['puppet_master']}.#{server['domain_name']}",
name: 'install_puppet_agent.sh' } }
@provisioners << { shell: { inline: 'systemctl stop puppet; pkill -9 -f "puppet.*agent.*"; true',
name: 'stop puppet' } }
@provisioners << { puppet_server: { puppet_server: "#{server['puppet_master']}.#{server['domain_name']}",
puppet_node: "#{hostname}.#{server['domain_name']}",
options: '--test' } }
@provisioners << { shell: { inline: 'systemctl start puppet',
name: 'start puppet' } }
else
@provisioners << { shell: { inline: %(Set-ExecutionPolicy Bypass -Scope Process -Force
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile('https://#{server['puppet_master']}.#{server['domain_name']}:8140/packages/current/install.ps1', 'install.ps1')
.\\install.ps1
iex 'puppet resource service puppet ensure=stopped'),
name: 'install puppet agent' } }
if server['custom_facts']
@provisioners << { shell: { inline: facter_overrides(server['custom_facts'], 'windows'),
name: 'facter_overrides' } }
end
@provisioners << { shell: { inline: hosts_file(servers, 'windows'),
name: 'hosts_file' } }
@provisioners << { puppet_server: { puppet_server: "#{server['puppet_master']}.#{server['domain_name']}",
puppet_node: "#{hostname}.#{server['domain_name']}",
options: '--test' } }
end
end
# Fix setup for Oracle applications
def virtualboxorafix(vb)
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/Leaf', '0x4']
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/SubLeaf', '0x4']
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/eax', '0']
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/ebx', '0']
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/ecx', '0']
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/edx', '0']
vb.customize ['setextradata', :id, 'VBoxInternal/CPUM/HostCPUID/Cache/SubLeafMask', '0xffffffff']
end
# Configure VirtualBox disks attached to the virtual machine
def configure_disks(vb, server, hostname, name)
vminfo = vm_info(name)
disks = server['disks'] || {}
unless vminfo =~ /Storage Controller Name \(1\): *SATA Controller/ || vminfo =~ /#\d+: 'SATA Controller'/
# puts "Attaching SATA Controller"
vb.customize [
'storagectl', :id,
'--name', 'SATA Controller',
'--add', 'sata',
'--portcount', disks.size
]
end
disks.each_with_index do |disk, i|
disk_name = disk.first
disk_size = disk.last['size']
disk_uuid = disk.last['uuid']
real_uuid = "00000000-0000-0000-0000-#{disk_uuid.rjust(12,'0')}"
disk_filename = server['cluster'] ? File.join(VAGRANT_ROOT, "#{disk_name}_#{server['cluster']}.vdi") : File.join(VAGRANT_ROOT, "#{disk_name}.vdi")
if File.file?(disk_filename)
# puts "Disk #{disk_filename} already created"
disk_hash = `VBoxManage showmediuminfo "#{disk_filename}"`.scan(/(.*): *(.*)/).to_h
current_uuid = disk_hash['UUID']
else
# puts "Creating disk #{disk_filename}"
current_uuid = '0'
if server['cluster']
vb.customize [
'createhd',
'--filename', disk_filename,
'--size', disk_size.to_s,
'--variant', 'Fixed'
]
vb.customize [
'modifyhd', disk_filename,
'--type', 'shareable'
]
else
vb.customize [
'createhd',
'--filename', disk_filename,
'--size', disk_size.to_s,
'--variant', 'Standard'
]
end
end
# Conditional for adding disk_uuid
if server['cluster'] && current_uuid == real_uuid
# puts "Attaching shareable disk #{disk_filename}"
vb.customize [
'storageattach', :id,
'--storagectl', 'SATA Controller',
'--port', (i + 1).to_s,
'--device', 0,
'--type', 'hdd',
'--medium', disk_filename,
'--mtype', 'shareable'
]
elsif server['cluster']
# puts "Attaching shareable disk #{disk_filename}, adding UUID #{real_uuid}"
vb.customize [
'storageattach', :id,
'--storagectl', 'SATA Controller',
'--port', (i + 1).to_s,
'--device', 0,
'--type', 'hdd',
'--medium', disk_filename,
'--mtype', 'shareable',
'--setuuid', real_uuid
]
elsif current_uuid == real_uuid
# puts "Attaching normal disk #{disk_filename}"
vb.customize [
'storageattach', :id,
'--storagectl', 'SATA Controller',
'--port', (i + 1).to_s,
'--device', 0,
'--type', 'hdd',
'--medium', disk_filename
]
else
# puts "Attaching normal disk #{disk_filename}, adding UUID #{real_uuid}"
vb.customize [
'storageattach', :id,
'--storagectl', 'SATA Controller',
'--port', (i + 1).to_s,
'--device', 0,
'--type', 'hdd',
'--medium', disk_filename,
'--setuuid', real_uuid
]
end
end
end
# Raises missing plugin error
def plugin_check(plugin_name)
return if Vagrant.has_plugin?(plugin_name)
raise "#{plugin_name} is not installed, please run: vagrant plugin install #{plugin_name}"
end
# Check if all required software files from servers.yaml are present in repo.
def local_software_file_check(srv, file_names)
srv.trigger.before [:up, :reload, :provision] do |trigger|
trigger.ruby do |env, machine|
files_found = true
file_names.each do |file_name|
file_path = "#{VAGRANT_ROOT}/modules/software/files/#{file_name}"
unless File.exist?(file_path) # returns true for directories
files_found = false
env.ui.error "Missing software file: #{file_name}"
end
end
unless files_found
env.ui.error 'Please add missing file(s) to the: ./modules/software/files/ directory.'
raise FilesNotFoundError
end
end
end
end
def vbox_manage?
@vbox_manage ||= !`which VBoxManage`.chomp.empty?
end
def vm_boxes
boxes = {}
if vbox_manage?
vms = `VBoxManage list vms`
vms.split("\n").each do |vm|
x = vm.split
k = x[0].gsub('"','') # vm name
v = x[1].gsub(/[{}]/,'') # vm UUID
boxes[k] = v
end
end
boxes
end
def vm_exists?(vmname)
vm_boxes[vmname] ? true : false
end
def vm_info(vmname)
vm_exists?(vmname) ? `VBoxManage showvminfo #{vmname}` : ''
end
#
# Vagrant setup
#
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.ssh.insert_key = false
File.open("#{VAGRANT_ROOT}/puppet_version", 'w') { |file| file.write(ENV['PUPPET_VERSION']) } if ENV['PUPPET_VERSION']
servers.each do |name, server|
# Fetch puppet installer version if it is present
puppet_installer = server['puppet_installer']
# Start VM configuration
config.vm.define name do |srv|
#
# Perform plugin checks before main setup
#
server['required_plugins']&.each { |plugin| plugin_check(plugin) }
#
# Perform software checks before main setup
#
local_software_file_check(srv, server['software_files']) if server['software_files']
local_software_file_check(srv, [puppet_installer]) if puppet_installer # Check if installer folder is present
if server['dhcp_fix']
srv.trigger.before :up do |trigger|
if Gem.win_platform?
trigger.info = "DHCP fix process doesn't work on Windows..."
else
trigger.info = 'Starting DHCP fix process...'
trigger.run = { inline: "sh -c \"until vboxmanage guestcontrol #{name} run \"/usr/bin/sudo\" --username vagrant --password vagrant --verbose --wait-stdout dhclient; do c=$((${c:-1}+1)); test $c -gt 50 && exit; sleep 20; done > /dev/null 2>&1 &\"" }
end
end
end
srv.vm.communicator = server['protocol'] || 'ssh'
srv.vm.box = server['box']
hostname = name[3..]
if srv.vm.communicator == 'ssh'
srv.vm.hostname = "#{hostname}.#{server['domain_name']}"
config.ssh.forward_agent = true
config.ssh.forward_x11 = true
else
srv.vm.hostname = hostname.to_s
config.winrm.ssl_peer_verification = false
config.winrm.retry_delay = 60
config.winrm.username = 'Administrator'
config.winrm.password = 'vagrant'
config.winrm.retry_limit = 10
end
srv.vm.network 'private_network', ip: server['public_ip'] if server['public_ip']
srv.vm.network 'private_network', ip: server['private_ip'], virtualbox__intnet: true if server['private_ip']
#
# Copy all everything to the box including software
#
srv.vm.synced_folder '.', '/vagrant', type: :virtualbox
@provisioners = []
#
# Depending on the machine type, perform setup
#
case server['type']
when 'masterless'
masterless_setup(server, srv, hostname)
when 'pe-master'
puppet_master_setup(srv, server, puppet_installer, pe_puppet_user_id, pe_puppet_group_id, hostname)
when 'pe-agent'
puppet_agent_setup(server, srv, hostname)
begin
"#{server['type']} is invalid."
rescue => exception
else
ensure
end
end
srv.vm.provider :virtualbox do |vb|
# vb.gui = true
vb.cpus = server['cpucount'] || 1
vb.memory = server['ram'] || 4096
vb.name = name
# Prevent vagrant from setting up dns proxy, and thus changing /etc/resolv.conf
vb.auto_nat_dns_proxy = false
vb.customize ['modifyvm', :id, '--natdnsproxy1', 'off']
vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'off']
# Setup config fixes for Oracle product
virtualboxorafix(vb) if server['virtualboxorafix']
# Attach disks if the setup needs virtual drives
configure_disks(vb, server, hostname, name) if server['needs_storage']
end
@provisioners.each do |provisioner|
provisioner.each do |type, options|
srv.vm.provision type, **options.transform_keys(&:to_sym)
end
end
end
end
end