-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vagrantfile
373 lines (313 loc) · 12.4 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
# One Vagrantfile to rule them all!
#
# This is a generic Vagrantfile that can be used without modification in
# a variety of situations. Hosts and their properties are specified in
# `vagrant-hosts.yml`. Provisioning is done by an Ansible playbook,
# `ansible/site.yml`.
#
# See https://github.com/jclaveau/ansible-vagrant-modules for details
#
# Copyright 2021 Jean Claveau (https://github.com/jclaveau/ansible-vagrant-modules)
# Copyright 2017 - 2020 Bert Van Vreckem (https://github.com/bertvv/ansible-skeleton/commits?author=bertvv)
# Copyright 2018 Jonas Verhofsté (https://github.com/bertvv/ansible-skeleton/commits?author=JonasVerhofste)
# Copyright 2018 Mathias Stadler (https://github.com/bertvv/ansible-skeleton/commits?author=MathiasStadler)
# Copyright 2017 Jeroen De Meerleer (https://github.com/bertvv/ansible-skeleton/commits?author=JeroenED)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'rbconfig'
require 'yaml'
# require 'getoptlong'
require 'optparse'
# set default LC_ALL for all BOXES
ENV["LC_ALL"] = "en_US.UTF-8"
# When set to `true`, Ansible will be forced to be run locally on the VM
# instead of from the host machine (provided Ansible is installed).
FORCE_LOCAL_RUN = false
#
# No changes needed below this point
#
VAGRANTFILE_API_VERSION = '2'
# set custom vagrant-hosts file
vagrant_hosts = ENV['VAGRANT_HOSTS'] ? ENV['VAGRANT_HOSTS'] : 'vagrant-hosts.yml'
hosts = YAML.load_file(File.join(Dir.pwd, vagrant_hosts))
vagrant_groups = ENV['VAGRANT_GROUPS'] ? ENV['VAGRANT_GROUPS'] : 'vagrant-groups.yml'
if File.file?(vagrant_groups)
groups = YAML.load_file(File.join(Dir.pwd, vagrant_groups))
else
groups = []
end
requested_provider = (ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
OptionParser.new do |opt|
opt.on('--provider PROVIDER') { |o| requested_provider = o }
end
# {{{ Helper functions
def run_locally?
windows_host? || FORCE_LOCAL_RUN
end
def windows_host?
Vagrant::Util::Platform.windows?
end
class VagrantPlugins::ProviderVirtualBox::Action::Network
def dhcp_server_matches_config?(dhcp_server,config)
true
end
end
# Set options for the network interface configuration. All values are
# optional, and can include:
# - ip (default = DHCP)
# - netmask (default value = 255.255.255.0
# - mac
# - auto_config (if false, Vagrant will not configure this network interface
# - intnet (if true, an internal network adapter will be created instead of a
# host-only adapter)
def network_options(host)
options = {}
if host.key?('ip')
options[:ip] = host['ip']
options[:netmask] = host['netmask'] ||= '255.255.255.0'
else
options[:type] = 'dhcp'
end
options[:mac] = host['mac'].gsub(/[-:]/, '') if host.key?('mac')
options[:auto_config] = host.key?('auto_config') ? host['auto_config'] : true
options[:virtualbox__intnet] = true if host.key?('intnet') && host['intnet']
options
end
def custom_synced_folders(vm, host)
return unless host.key?('synced_folders')
folders = host['synced_folders']
folders.each do |folder|
vm.synced_folder folder['src'], folder['dest'], folder['options']
end
end
# Adds forwarded ports to your Vagrant machine
#
# example:
# forwarded_ports:
# - guest: 88
# host: 8080
# - guest: 22
# host: 2270
# id: ssh
def forwarded_ports(vm, host)
if host.has_key?('forwarded_ports')
host['forwarded_ports'].each do |port_config|
port_options = {}
port_config.each do |key, value|
eval("port_options[:"+key+"] = value")
end
vm.network(:forwarded_port, **port_options)
end
end
end
def provision_ansible(node, host, groups)
return unless host.key?('ansible')
# https://www.vagrantup.com/docs/provisioning/ansible_intro
# https://www.vagrantup.com/docs/provisioning/ansible
ansible_mode = run_locally? ? 'ansible_local' : 'ansible'
node.vm.provision ansible_mode do |ansible|
ansible.compatibility_mode = '2.0'
host['ansible'].each do |key, value|
# ansible[key] = host['ansible'][key] produces a bug due to the config implementation i guess
eval("ansible."+key+" = host['ansible'][key]")
end
ansible.limit = 'all' if ! ansible.has? 'limit'
ansible.limit = true if ! ansible.has? 'become'
if ! groups.nil?
ansible.groups = groups
end
end
end
def provision_shell(node, host)
return unless host.key?('shell')
node.vm.provision "shell" do |shell|
# https://www.vagrantup.com/docs/provisioning/shell
host['shell'].each do |key, value|
# shell[key] = host['shell'][key] produces a bug due to the config implementation i guess
eval("shell."+key+" = host['shell'][key]")
end
end
end
def virtualbox_guest_additions(node, host)
return unless host.key?('vbguest')
if Vagrant.has_plugin?("vagrant-vbguest") then
# https://github.com/dotless-de/vagrant-vbguest
# https://subscription.packtpub.com/book/virtualization_and_cloud/9781786464910/1/ch01lvl1sec12/enabling-virtualbox-guest-additions-in-vagrant
host['vbguest'].each do |key, value|
# shell[key] = host['shell'][key] produces a bug due to the config implementation i guess
eval("node.vbguest."+key+" = host['vbguest'][key]")
end
node.vbguest = false if ! ansible.has? 'auto_update' # Disables vbguest auto_update by default as it slows down tests
end
end
def configure_provider_virtualbox(node, host)
# https://docs.oracle.com/en/virtualization/virtualbox/6.0/user/vboxmanage-modifyvm.html
node.vm.provider :virtualbox do |provider|
provider.memory = host['memory'] if host.key? 'memory'
provider.cpus = host['cpus'] if host.key? 'cpus'
if host.key? 'virtualbox_options' then
host['virtualbox_options'].each do |key, value|
if key.match(/^--/) then
provider.customize ['modifyvm', :id, key, value]
else
eval("provider."+key+" = value")
end
end
end
configure_provider_options_inline(host, provider)
end
end
def configure_provider_libvirt(node, host)
# https://github.com/vagrant-libvirt/vagrant-libvirt#provider-options
# TODO check that it is required
# Alternative solution https://stackoverflow.com/a/57708188/2714285 ?
# if Vagrant.has_plugin?("vagrant-libvirt") then
# puts "The vagrant-libvirt plugin is required. Please install it with:"
# puts "$ vagrant plugin install vagrant-libvirt"
# return
# end
node.vm.provider :libvirt do |provider|
provider.memory = host['memory'] if host.key? 'memory' # default 512
provider.cpus = host['cpus'] if host.key? 'cpus' # default 1
if host.key? 'libvirt_options' then
host['libvirt_options'].each do |key, value|
is_method_call = false
if value.is_a?(Array) then
value.each do |array_item|
if array_item.is_a?(Array) and (array_item.length() == 1 or array_item.length() == 2) and
array_item[0].is_a?(Symbol) and
(array_item.length() == 1 or (array_item[1].is_a?(Array) or array_item[1].is_a?(Hash))) then
is_method_call = true
end
end
end
if is_method_call then
value.each do |array_item|
provider.send(key, *array_item)
end
else
# use send? https://stackoverflow.com/questions/3167966/ruby-using-object-send-for-assigning-variables
eval("provider."+key+" = value")
end
end
configure_provider_options_inline(host, provider)
end
end
end
def configure_provider_docker(node, host)
# https://docs.docker.com/config/containers/resource_constraints/
# https://stackoverflow.com/questions/47919339/vagrant-with-docker-provider-specifying-cpu-and-memory
# https://www.vagrantup.com/docs/providers/docker/configuration
# provider.image = "foo/bar" # dup of box? https://www.vagrantup.com/docs/providers/docker/basics
# https://www.vagrantup.com/docs/providers/docker/boxes
node.vm.provider :docker do |provider|
# !!! This will loop several times
if host.key? 'memory' then
if host['docker_options']['create_args'].is_a?(Hash) then
if host['docker_options']['create_args'].key? '--memory' then
warn(
"Warning: 'memory' parameter " +
host['memory'].to_s +
" overriden by create_args --memory=" +
host['docker_options']['create_args']['--memory'].to_s
)
else
host['docker_options']['create_args']['--memory'] = host['memory']
end
end
end
if host.key? 'cpus' then
if host['docker_options']['create_args'].is_a?(Hash) then
if host['docker_options']['create_args'].key? '--cpuset-cpus' then
warn(
"Warning: 'cpus' parameter " +
host['cpus'].to_s +
" overriden by create_args --cpuset-cpus=" +
host['docker_options']['create_args']['--cpuset-cpus'].to_s
)
else
host['docker_options']['create_args']['--cpuset-cpus'] = host['cpus']
end
end
end
if host.key? 'docker_options' then
# enabling ssh by default for provisionning
host['docker_options']['has_ssh'] = true if not host['docker_options'].key? 'has_ssh'
host['docker_options']['remains_running'] = true if not host['docker_options'].key? 'remains_running'
host['docker_options'].each do |key, value|
if key == 'create_args' and value.is_a?(Hash) then
host['docker_options']['create_args'] = host['docker_options']['create_args'].map do |k, v|
k.to_s + '=' + v.to_s
end
end
if key == 'link' and value.is_a?(Array) and len(value) then
value.each do |array_item|
provider.send(key, *array_item)
end
else
# use send? https://stackoverflow.com/questions/3167966/ruby-using-object-send-for-assigning-variables
eval("provider."+key+" = value")
end
end
configure_provider_options_inline(host, provider)
end
end
end
def configure_provider_custom(node, host, provider)
p provider
node.vm.provider provider do |provider|
configure_provider_options_inline(host, provider)
end
end
def configure_provider_options_inline(host, provider)
if host.key? 'provider_options_inline' then
host['provider_options_inline'].each do |line|
eval("provider."+line)
end
end
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
hosts.each do |host|
config.vm.define host['name'] do |node|
node.vm.box = host['box'] if host.key? 'box'
node.vm.box_url = host['box_url'] if host.key? 'box_url'
node.vm.hostname = host['name']
# disable box update checking by default
node.vm.box_check_update = (host.key? 'box_check_update') ? host['box_check_update'] : false
node.vm.network :private_network, **network_options(host)
custom_synced_folders(node.vm, host)
forwarded_ports(node.vm, host)
virtualbox_guest_additions(node, host)
host_provider = requested_provider
host_provider = host['provider'].to_sym if host.key? 'provider'
if host_provider == :virtualbox then
configure_provider_virtualbox(node, host)
elsif host_provider == :libvirt then
configure_provider_libvirt(node, host)
elsif host_provider == :docker then
configure_provider_docker(node, host)
elsif host_provider then:
configure_provider_custom(node, host, host_provider)
end
# Shell provisioning
provision_shell(node, host)
# Ansible provisioning
provision_ansible(node, host, groups)
end
end
end
# -*- mode: ruby -*-
# vi: ft=ruby :