-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVagrantfile
843 lines (659 loc) · 21.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# -*- mode: ruby -*-
# vim: set ft=ruby tabstop=2 softtabstop=0 expandtab shiftwidth=2 smarttab :
# Date: 2016/02/28
# Version 0.5
# License: AGPLv3
# Author: mrjk
# Source: https://github.com/mrjk/easy-vagrant
# Contact: bXJqay43OCBbYXRdIGdtYWlsIFtkb3RdIGNvbQ==
########################################
# Simplified vagrant architecture spwaner
########################################
# Please edit the "conf/vagrant.yml" file to build your own architecture.
# Do not edit what's bellow unless you know what you do (and Ruby)
Vagrant.require_version ">= 1.7.0"
VAGRANTFILE_API_VERSION = "2"
require 'yaml'
# Internal variables
show_banner = true
dry_run = false
show_config_merged = false
show_config_parsed = false
show_config_instances = true
# Determine project root folder
vagrant_root = File.dirname(__FILE__) + '/'
########################################
# Display banner
########################################
if show_banner
puts "INFO: ===================================================="
puts "INFO: == Welcome on Easy-vagrant =="
puts "INFO: == by mrjk =="
puts "INFO: ===================================================="
end
########################################
# Default configuration
########################################
# This is the default structure. All of this can
# be overriden into userland. Do not edit this structure
# unless you know what you do.
conf_default = {
'settings' => {
'defaults' => {
'flavor' => 'tiny',
'box' => 'centos7',
'provider' => 'libvirt',
'prefix' => 'prefix-',
'sufix' => '-sufix',
'provisionners' => {},
},
'flavors' => {
'nano' => {
'cpu' => '1',
'memory' => '128',
'disk' => '5'
},
'micro' => {
'cpu' => '2',
'memory' => '512',
'disk' => '10'
},
'small' => {
'cpu' => '2',
'memory' => '1024',
'disk' => '10'
},
'medium' => {
'cpu' => '2',
'memory' => '2048',
'disk' => '20'
},
'large' => {
'cpu' => '3',
'memory' => '4096',
'disk' => '30'
},
'xlarge' => {
'cpu' => '4',
'memory' => '8192',
'disk' => '40'
}
},
'boxes' => {
'debian9' => 'debian/stretch64',
'debian8' => 'debian/jessie64',
'debian7' => 'wholebits/debian7-64',
'ubuntu1604' => 'wholebits/ubuntu16.10-64',
'ubuntu1404' => 'wholebits/ubuntu14.04-64',
'centos7' => 'centos/7',
'centos6' => 'centos/6',
'archlinux' => 'archlinux/archlinux',
},
'providers' => {
'libvirt' => {
'settings' => {
'driver' => 'nil',
'host' => 'nil',
'connect_via_ssh' => 'nil',
'username' => 'nil',
'password' => 'nil',
'id_ssh_key_file' => '~/.ssh/id_rsa',
'socket' => 'nil',
'uri' => 'nil'
},
},
'virtualbox' => {
'instances' => {
'linked_clones' => 'true',
'execution_cap' => '50',
'page_fusion' => 'on',
'cpuhotplug' => 'on',
'pae' => 'on',
'largepages' => 'on',
'guestmemoryballoon' => '128',
},
'boxes' => {
'debian8' => 'minimal/jessie64',
'debian7' => 'minimal/wheezy64',
'ubuntu1604' => 'minimal/xenial64',
'ubuntu1404' => 'minimal/trusty64',
'centos7' => 'minimal/centos7',
'centos6' => 'minimal/centos6',
}
},
'docker' => {
'settings' => {
'force_host_vm' => 'false',
'pull' => 'false',
'remains_running' => 'true',
'stop_timeout' => '30',
},
'boxes' => {
'scratch' => 'scratch',
'alpine' => 'alpine:latest',
'debian8' => 'debian:8',
'debian7' => 'debian:7',
'ubuntu1604' => 'ubuntu:xenial',
'ubuntu1404' => 'ubuntu:trusty',
'ubuntu1204' => 'ubuntu:precise',
'centos7' => 'centos:7',
'centos6' => 'centos:6',
'centos5' => 'centos:5',
'archlinux' => 'archlinuxjp/archlinux'
}
}
}
},
'provisionners' => {
'test_shell_script' => {
'type' => 'shell',
'description' => 'This execute a shell script',
'params' => {
'path' => vagrant_root + 'conf/scripts/test_script.sh',
'args' => [
'Provisionning',
'shell_script',
'worked as expected as non root.',
],
'privileged' => false,
},
},
'install_python_for_ansible' => {
'type' => 'shell',
'priority' => '90',
'description' => 'This will install Python on the target',
'params' => {
'path' => vagrant_root + 'conf/scripts/install_python.sh',
'privileged' => true,
},
},
'test_shell_cli' => {
'type' => 'shell',
'description' => 'This execute a shell command',
'params' => {
'inline' => 'echo $1 $2 $3 > /tmp/vagrant_provisionning_cli',
'args' => [
'Provisionning',
'shell_cli',
'worked as expected as root.',
],
'privileged' => true,
},
},
'ansible_test' => {
'type' => 'ansible',
'description' => 'Test Ansible run correctly',
'params' => {
'playbook' => vagrant_root + 'conf/ansible/test.yml',
},
},
'ansible_deploy_key' => {
'type' => 'ansible',
'description' => 'Deploy an SSH pubkey to specific user',
'params' => {
'playbook' => vagrant_root + 'conf/ansible/deploy_key.yml',
'extra_vars' => {
'target_user' => 'root',
},
},
},
},
}
########################################
# Define functions
########################################
# This function checks if a subelement of a
# hash exists and it is not set to the 'null'
# string. Return a boolean.
def attribute_is_defined(object, key)
if object.class == Hash and object.key?(key)
value = object[key].to_s
#puts "DEBUG: object" , object.class
#puts "DEBUG: key" , value
#puts
if value.casecmp("nil") != 0
return true
end
end
return false
end
# This function helps to retrieve the merge strategy key.
# Returns a Hash with merge settings.
def get_merge_params(hash, key)
merge_string = '_'
if hash.has_key? key + merge_string
if hash[key + merge_string].class == String
out = {}
out['action'] = hash[key + merge_string]
out['options'] = nil
out
elsif hash[key + merge_string].class == Hash
out = hash[key + merge_string]
out
else
print 'ERROR: Action must be a String of a Hash and not ', hash[key + merge_string].class, "\n"
end
else
out = {}
out['action'] = 'union'
out['options'] = {}
out
end
end
# This function is used to merge recursively two hashes. The
# right array always win except when a '*_' key is found
# which can override the default behavior. See the documentation
# for usage and complete examples. Return the merged array.
def merge_recursively(o_src, o_over, p=nil)
# o_src for source object
# o_over for overriding object
# p for params (string or hash)
result = 'UNSET'
# Retrieve parameters via yaml object or\
# Retrieve parameters via parameters
if p.class == String
action = p
options = nil
elsif p.class == Hash
action = p.fetch('action','union').to_s
options = p.fetch('options', nil)
else
action = 'union'
options = nil
end
# Debug
#print "===> (", action, ") Working with two objects: src: ",o_src.class, ", dst: ", o_over.class, "\n"
#print "====> Work on key: ", key, " (", action ,") \n== OLD (", val_old.class ,") : ", YAML::dump(val_old), "\n== NEW (" , val_new.class ,") : " , YAML::dump(val_new) , "\n\n"
#print "====> Work on key: ", key, " (", action ,") \n== OLD (", val_old.class ,") : ", val_old, "\n== NEW (" , val_new.class ,") : " , val_new , "\n\n"
# Do the action
case action
when 'replace'
# Human name: replace
# Return inconditionnaly the overriding object, even if undefined/empty.
result = o_over
when 'unset'
# Human name: unset
# Unset the value
result = nil
when 'union'
# Human name: merge
# Merge all objects, existing object are overrided
# Is overrinding object equal nil or empty?
if o_over.nil? or o_over.to_s.empty?
result = o_src
# Are both object Hash ?
elsif o_src.class == Hash and o_over.class == Hash
result = o_src.merge(o_over)
# Work on sub-objects
result = result.each {|k, v|
result[k] = merge_recursively(o_src[k], o_over[k], get_merge_params(o_over, k) )
}
# Then the new object type takes precedence
else
result = o_over
end
when 'inherit'
# Human name: common
# Merge only new objects, discard parent objects
# Is overrinding object equal nil ?
if o_over == nil or o_over.to_s.empty?
result = o_src
# Are both object Hash ?
elsif o_src.class == Hash and o_over.class == Hash
result = merge_recursively(o_src, o_over, 'union')
# Delete key
result = result.delete_if {|k, v|
not o_over.has_key?(k)
}
else
result = nil
end
when 'intersect'
# Human name: common only
# Merge only common objects, existing object are overrided
# Is overrinding object equal nil ?
if o_over == nil or o_over.to_s.empty?
result = o_src
# Are both object Hash ?
elsif o_src.class == Hash and o_over.class == Hash
result = merge_recursively(o_src, o_over, 'union')
result = result.delete_if {|k, v|
not ( o_src.has_key?(k) and o_over.has_key?(k) )
}
else
result = nil
end
when 'difference'
# Human name: exclude
# Remove all listed objects, existing object are overrided
# Is overrinding object equal nil ?
if o_over == nil or o_over.to_s.empty?
result = o_src
# Are both object Hash ?
elsif o_src.class == Hash and o_over.class == Hash
# Remove source objects if defined in options
o_src.each {|k, v|
if options.class == Hash or options.class == Array
if options.class == Hash and options.has_key?(k)
o_src.delete(k)
elsif options.class == Array and options.include?(k)
o_src.delete(k)
end
else
print "WARN: Options for difference must be a Hash or Array for key: ", k, "\n"
end
}
# Merge and override all objects
result = merge_recursively(o_src, o_over, 'union')
else
result = nil
end
when 'complement'
# Human name: invert
# Remove all listed objects, existing object are overrided, new objects are discarded if not previously defined
# Is overrinding object equal nil ?
if o_over == nil or o_over.to_s.empty?
result = o_src
# Are both object Hash ?
elsif o_src.class == Hash and o_over.class == Hash
result = {}
o_src.each {|k, v|
if ( options.class == Hash and not options.has_key?(k) ) or (options.class == Array and not options.include?(k))
# Check if the key is defined and not nil/empty in o_over
if o_over.has_key?(k) and (o_over[k] != nil and not o_over[k].to_s.empty? )
result[k] = merge_recursively(v, o_over[k], 'union')
else
result[k] = v
end
elsif options.class != Hash and options.class != Array
print "WARN: Options for difference must be a Hash or Array for key: ", k, "\n"
end
}
return result
else
# Return object only if previously defined
if o_src.class == Hash and o_over.class == Hash
result = o_over
else
result = nil
end
end
else
print "WARN: Action is not recognized: ", action, "\n"
result nil
end
# Debug
#print "Returns: ", YAML::dump(result)
# Return the result
if result == "UNSET"
puts "ERROR: You found a bug, please contact the maintener"
return nil
elsif result.class == Hash
return result.delete_if {|k, v| k.end_with?('_')}
else
return result
end
end
########################################
# Load and merge config settings
########################################
# Load default configuration
conf_merged = conf_default
# Load mainteneur configuration
if File.file?(vagrant_root + 'conf/vagrant.yml')
conf_merged = merge_recursively(conf_merged, YAML.load_file(vagrant_root + 'conf/vagrant.yml') )
end
# Load user configuration
if File.file?(vagrant_root + 'local.yml')
conf_merged = merge_recursively(conf_merged, YAML.load_file(vagrant_root + 'local.yml') )
end
# Display configuration if requested
if show_config_merged
puts "NOTICE: This is the merged raw configuration:"
print YAML::dump(conf_merged)
end
########################################
# Resolve configuration links
########################################
conf_final = {}
# Settings: Boxes
# =====================
conf_final['providers'] = conf_merged['settings']['providers']
# Boxes
conf_merged['settings']['providers'].each do |key, value|
# Get common boxes
global_boxes = conf_merged['settings']['boxes']
# Get local provider box override
if attribute_is_defined(value, 'boxes')
provider_boxes = value['boxes']
else
provider_boxes = {}
end
# Update config
conf_final['providers'][key]['boxes'] = merge_recursively(global_boxes, provider_boxes)
end
# Instances: flavors, boxes, number
# =====================
conf_final['instances'] = {}
# Check for defined instances
if not conf_merged.has_key?('instances')
print "ERROR: No instances are defined.\n"
exit 1
end
# Iterate over instances
conf_merged['instances'].each do |key, value|
vm_config = {}
# Do some basic checks
# ====================
# Manage flavors and key parameters
# =====================
# Get flavor name (default value)
if attribute_is_defined(value, 'flavor')
# Instance flavor is set
vm_flavor = value['flavor']
else
# Fall back on default flavor
vm_flavor = conf_merged['settings']['defaults']['flavor']
end
# Check flavor existance
if not attribute_is_defined(conf_merged['settings']['flavors'],vm_flavor)
print "ERROR: Flavor '", vm_flavor, "' is not defined.\n"
exit 1
end
# Define cpu
if attribute_is_defined(value, 'cpu') and value['cpu'].to_i > 0
vm_config['cpu'] = value['cpu']
elsif conf_merged['settings']['flavors'][vm_flavor]['cpu'].to_i > 0
vm_config['cpu'] = conf_merged['settings']['flavors'][vm_flavor]['cpu']
else
vm_config['cpu'] = 1
print "WARN: Cpu for instance '", key, "' is not valid. Fall back to 1 cpu.\n"
end
# Define memory
if attribute_is_defined(value, 'memory') and value['memory'].to_i > 0
vm_config['memory'] = value['memory']
elsif conf_merged['settings']['flavors'][vm_flavor]['memory'].to_i > 0
vm_config['memory'] = conf_merged['settings']['flavors'][vm_flavor]['memory']
else
vm_config['memory'] = 512
print "WARN: Memory for instance '", key, "' is not valid. Fall back to 512 (Mb).\n"
end
# Define disk
if attribute_is_defined(value, 'disk') and value['memory'].to_i > 0
vm_config['disk'] = value['disk']
elsif conf_merged['settings']['flavors'][vm_flavor]['disk'].to_i > 0
vm_config['disk'] = conf_merged['settings']['flavors'][vm_flavor]['disk']
else
vm_config['disk'] = 10
print "WARN: Disk for instance '", key, "' is not valid. Fall back to 10 (Gb).\n"
end
# Define box
if attribute_is_defined(value, 'box') and attribute_is_defined(conf_merged['settings']['boxes'], value['box'])
vm_config['box'] = value['box']
elsif attribute_is_defined(conf_merged['settings']['boxes'],conf_merged['settings']['defaults']['box'])
vm_config['box'] = conf_merged['settings']['defaults']['box']
else
print "ERROR: Box '", conf_merged['settings']['defaults']['box'] ,"' for '", key, "' is not valid.\n"
exit 1
end
# Manage VirtualBox ports
# =====================
vm_config['ports'] = []
if attribute_is_defined(value, 'ports')
if value['ports'].class == Hash
value['ports'].each do |port|
if port.has_key?('guest') and port.has_key?('host') \
and port['guest'].to_i > 0 and port['host'].to_i > 0
# TODO: Check if port is already defined/used and warn user
p = {}
p['guest'] = port['guest'].to_i
p['host'] = port['host'].to_i
if port.has_key?('protocol') \
and ( port['protocol'] == 'tcp' or port['protocol'] == 'udp' )
p['protocol'] = port['protocol']
else
p['protocol'] = 'tcp'
end
vm_config['ports'].push(p)
end
end
else
print "WARN: Ports list for instance '", key, "' should be a list of hashes, not '", value['ports'].class ,"'.\n"
end
end
# Manage provisionners
# =====================
vm_provisionners = {}
# Detect which provisionners should be applied on instance
if conf_merged['settings']['defaults']['provisionners'] != {}
vm_provisionners = merge_recursively(conf_merged['provisionners'], conf_merged['settings']['defaults']['provisionners'], 'inherit')
end
# Merge instance provisionners with global provisionners
vm_provisionners.each do |name, settings|
# Check if provisionner exists
if conf_merged['provisionners'][name].class == Hash
# Check if we need to override object
if settings.class != Hash
settings = conf_merged['provisionners'][name]
else
settings = merge_recursively(conf_merged['provisionners'][name], settings)
end
end
# Check if provisionners are correctly set
if settings.class != Hash
puts "ERROR: The provisionner '" + name + "' is not correctly set."
abort
end
# Ensure priority attribute is defined, or defaulted to 0
if attribute_is_defined(settings, 'priority')
settings['priority'] = settings['priority'].to_i
else
settings['priority'] = 0
end
# Merge the config
vm_provisionners[name] = settings
end
# Reverse sort provisionners
vm_config['provisionners'] = vm_provisionners.sort_by { |name, settings| settings['priority'] }.reverse.to_h
# Manage instance number
# =====================
# Detect how many instances
if attribute_is_defined(value, 'number') and value['number'].to_i > 1
# Generate the request number of instances
for number in 1..value['number'] do
# TODO: Do IP increment here
conf_final['instances'][key + number.to_s] = vm_config.clone
end
elsif (
attribute_is_defined(value, 'number') \
and value['number'] != 0 \
) or not attribute_is_defined(value, 'number')
# Write config
conf_final['instances'][key] = vm_config
end
end
# Final configuration is ready
# =====================
# Show debugging
if show_config_instances
puts "NOTICE: This is the instances configuration:"
print YAML::dump(conf_final['instances'])
end
# Show debugging
if show_config_parsed
puts "NOTICE: This is the final configuration:"
print YAML::dump(conf_final)
end
# Check dry run
if dry_run
puts "NOTICE: Dry run mode enabled, not going to Vagrant."
exit 0
end
########################################
# Magic business
########################################
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
conf_final['instances'].each do |key, value|
# Display instance config
# =====================
# puts "Define instance: " + key
# print YAML::dump(value)
# puts ""
# Define instance
# =====================
config.vm.define key do |instance|
# Generic Instance Definition
# =====================
instance.vm.hostname = key
# Disable synced folders
instance.vm.synced_folder ".", "/vagrant", :disabled => true
# Provisionners
# =====================
value['provisionners'].each do |name, settings|
instance.vm.provision settings['type'] do |s|
# TODO: Make this called one time at the end for Ansible
settings['params'].each do |k, v|
s.send("#{k}=",v)
end
# This is what the above block dynamically do :-D
#s.inline = settings['params']['inline']
#s.args = settings['params']['args']
end
end
# Provider: Libvirt
# =====================
instance.vm.provider "libvirt" do |l, o|
# Instance provider
l.cpus = value["cpu"].to_i
l.memory = value["memory"].to_i
l.nested = true
l.volume_cache = 'none'
l.keymap = 'en-us'
# Override
o.vm.box = conf_final['providers']['libvirt']['boxes'][value["box"]]
# Connection settings
l.host = conf_final['providers']['libvirt']['settings']['host']
l.connect_via_ssh = conf_final['providers']['libvirt']['settings']['connect_via_ssh']
l.username = conf_final['providers']['libvirt']['settings']['username']
end
# Provider: VirtualBox
# =====================
instance.vm.provider "virtualbox" do |v, o|
# Instance provider
v.customize ["modifyvm", :id, "--cpus", value["cpu"]]
v.customize ["modifyvm", :id, "--memory", value["memory"]]
v.customize ["modifyvm", :id, "--usb", "off"]
v.customize ["modifyvm", :id, "--usbehci", "off"]
# Override
o.vm.box = conf_final['providers']['virtualbox']['boxes'][value["box"]]
# Open network ports
value['ports'].each do |port|
o.vm.network 'forwarded_port', guest: port['guest'], host: port['host'], protocol: port['protocol'], auto_correct: true
end
end
# Provider: Docker
# =====================
# Note: Todo
end
end
end