-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVagrantfile
360 lines (316 loc) · 12.3 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Author: Felipe Pfeifer Rubin
# Contact: felipe.rubin@edu.pucrs.br
# About: A Vagrantfile that allows the
# provisioning of a multi-machine environment
# by reading a quite simple, YAML file definition.
# Read YAML with host configurations
require 'yaml'
require 'ipaddr'
hosts = nil
if File.exists?(File.join(File.dirname(__FILE__), 'hosts.yaml'))
hosts = YAML.load_file(File.join(File.dirname(__FILE__), 'hosts.yaml'))
elsif File.exists?(File.join(File.dirname(__FILE__), 'hosts.yml'))
hosts = YAML.load_file(File.join(File.dirname(__FILE__), 'hosts.yml'))
else
puts "No hosts.yaml found, exited."
exit(0)
end
# Discover the provider
provider = (ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
ARGV.each do |a|
if a.split("=")[0] == "--provider"
provider = a.split("=")[1]
end
end
# Parallels Desktop Disable Parallel Provisioning
# If running with Paralllels Desktop, do not run it in parallel.
# There's currently a bug which I've already created an issue with
# the Parallels (see [issue](https://github.com/Parallels/vagrant-parallels/issues/357))
if "#{provider}" == "parallels"
ENV['VAGRANT_NO_PARALLEL'] = 'yes'
end
# Workaround to this implementation, otherwise vmware fusion will always create
# a disk. It's a language issue. Fix me later.
if provider == "vmware_fusion"
enabled_vmware_fusion_disk_creation = true
else
enabled_vmware_fusion_disk_creation = false
end
def to_bool(str,default)
if str.nil? then
return default
end
return str == "true"
end
# Deep copy of hashmap for example
def deep_copy(obj)
Marshal.load(Marshal.dump(obj))
end
# Seeking $$ and replacing with replicaid
def set_replicaid_references(aux)
(aux["script"].to_a).each do |script|
if script.key?("path")
script["path"] = script["path"].gsub("\$\$","#{aux["replicaid"]}")
end
if script.key?("cmd")
script["cmd"] = script["cmd"].gsub("\$\$","#{aux["replicaid"]}")
end
if script["args"].is_a? String
script["args"] = script["args"].gsub("\$\$","#{aux["replicaid"]}")
else
(script["args"].to_a).each_index do |arg_i|
script["args"][arg_i] = script["args"][arg_i].gsub("\$\$","#{aux["replicaid"]}")
end
end
end
(aux["share"].to_a).each do |share|
share["source"] = share["source"].gsub("\$\$","#{aux["replicaid"]}")
share["target"] = share["target"].gsub("\$\$","#{aux["replicaid"]}")
end
end
# Replica Parsing
hosts.each do |hnode|
hnode["replicaid"] = 0 unless hnode.key?("replicaid")
if hnode.key?("replicas")
(hnode["replicaid"]+1..hnode["replicas"]-1+hnode["replicaid"]).each do |replicaid|
aux = deep_copy(hnode)
aux["replicaid"] = replicaid
aux["name"] = hnode["name"]+"#{replicaid}"
aux["replicas"] = 0
(aux["network"].to_a).each do |net|
if net.key?("ip")
net["ip"] = IPAddr.new(IPAddr.new(net["ip"]).to_i+replicaid-hnode["replicaid"],Socket::AF_INET).to_s
end
end
hosts.append(aux)
end
if hnode["replicas"] > 1
hnode["replicas"] = 0
hnode["name"]=hnode["name"]+"#{hnode["replicaid"]}"
end
end
set_replicaid_references(hnode)
end
def configure_virtualbox(hnode,h)
hnode.vm.provider :virtualbox do |v, override|
system "vagrant plugin install vagrant-vbguest" unless Vagrant.has_plugin? "vagrant-vbguest"
v.memory = h["mem"]
v.cpus = h["cpu"]
v.gui = "#{h["gui"]}"
v.linked_clone = "#{h["linkedclone"]}"
if h["namelabel"] == "true"
v.name = h["name"]
end
if "#{h["nestedvt"]}" == "true"
v.customize ["modifyvm",:id,"--hwvirtex", "on"]
v.customize ["modifyvm",:id,"--nested-hw-virt", "on"]
v.customize ["modifyvm",:id,"--nestedpaging", "on"]
v.customize ["modifyvm",:id,"--largepages", "on"]
v.customize ["modifyvm",:id,"--vtxux", "on"]
v.customize ["modifyvm",:id,"--vtxvpid", "on"]
end
h["disk"].to_a.each_index do |disk|
diskname = File.join(File.dirname(File.expand_path(__FILE__)), ".virtualbox", "#{hnode.vm.hostname}-#{disk+1}.vdi")
unless File.exist?(diskname)
v.customize ['createhd', '--filename', diskname, '--size', h["disk"][disk]["size"] * 1024]
end
v.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', disk+1, '--device', 0, '--type', 'hdd', '--medium', diskname]
end
# Video Configurations
if "#{h["video3d"]}" == "true"
v.customize ["modifyvm", :id, "--accelerate3d", "on"]
else
v.customize ["modifyvm", :id, "--accelerate3d", "off"]
end
if h["vram"].to_i > 0
v.customize ["modifyvm", :id, "--vram",h["vram"]]
end
end
end
def configure_parallels(hnode,h)
hnode.vm.provider :parallels do |v, override|
system "vagrant plugin install vagrant-parallels" unless Vagrant.has_plugin? "vagrant-parallels"
if h["vmlabel"] == "true"
v.name = h["name"]
end
v.memory = h["mem"]
v.cpus = h["cpu"]
if "#{h["nestedvt"]}" == "true"
v.customize ["set", :id, "--nested-virt", "on"]
end
#v.check_guest_tools = true
#v.update_guest_tools = true
# or any of <coherence | fullscreen | modality | window | headless >
if "#{h["gui"]}" == "true"
v.customize ["set", :id, "--startup-view", "window"]
else
v.customize ["set", :id, "--startup-view", "headless"]
end
#Enables adaptive hypervisor, better core usage
v.customize ["set", :id, "--adaptive-hypervisor","on"]
v.customize ["set", :id, "--sync-ssh-ids","on"]
v.customize ["set", :id, "--time-sync","on"]
# v.customize ["set", :id, "--device-add", "hdd",
v.linked_clone = "#{h["linkedclone"]}"
h["disk"].to_a.each_index do |disk|
v.customize ['set', :id, '--device-add', 'hdd', '--size', h["disk"][disk]["size"] * 1024]
end
# Video Configurations
if "#{h["video3d"]}" == "true"
v.customize ["set", :id, "--3d-accelerate","highest"]
else
# could also be off,highest, dx9
v.customize ["set", :id, "--3d-accelerate","off"]
end
if h["vram"].to_i > 0
v.customize ["set", :id, "--videosize", h["vram"]]
end
end
end
def configure_vmware_fusion(hnode,h)
hnode.vm.provider :vmware_fusion do |v, override|
if h["namelabel"]
v.vmx['displayname'] = h["name"]
end
v.vmx['memsize'] = h["mem"]
v.vmx['numvcpus'] = h["cpu"]
v.gui = "#{h["gui"]}"
v.linked_clone = "#{h["linkedclone"]}"
if "#{h["nestedvt"]}" == "true"
v.vmx["vhv.enable"] = "TRUE"
end
# if $enabled_vmware_fusion_disk_creation
vdiskmanager = "/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager"
dir = File.join(File.dirname(File.expand_path(__FILE__)), ".vmware")
if File.exist?(vdiskmanager)
h["disk"].to_a.each_index do |disk|
unless File.directory?( dir )
Dir.mkdir dir
end
diskname = File.join(dir, "#{hnode.vm.hostname}-#{disk+1}.vmdk")
unless File.exist?(diskname)
`/Applications/VMware\\ Fusion.app/Contents/Library/vmware-vdiskmanager -c -s #{h["disk"][disk]["size"]}GB -a lsilogic -t 1 #{diskname}`
end
v.vmx["scsi0:#{disk+1}.filename"] = diskname
v.vmx["scsi0:#{disk+1}.present"] = 'TRUE'
v.vmx["scsi0:#{disk+1}.redo"] = ''
end
end
# end
# Video Configs
vrambytes = h["vram"].to_i * 1024
if vrambytes > 65535
v.vmx["svga.vramSize"] = "#{vrambytes}"
end
if "#{h["video3d"]}" == "true"
v.vmx['mks.enable3d'] = "TRUE"
else
v.vmx['mks.enable3d'] = "FALSE"
end
end
end
Vagrant.configure("2") do |config|
# Supported Hypervisors (try order)
# config.vm.provider "parallels"
# config.vm.provider "vmware_fusion"
# config.vm.provider "virtualbox"
# hosts.size()
# puts "string"
# puts "#{a.class}" # Prints class of object
# Ruby also supports x == y ? z : w
# OBS: Use triggers later on with maybe a counter,
# to run ansible only after all vms are provisioned.
# Use this to know when the last VM will be provisioned.
# config.ssh.forward_agent = true
# if "#{provider}"
global_box="generic/ubuntu1804"
hosts.each_index do |gi|
h = hosts[gi]
# If not defined, set defaults
h["provider"] = provider unless h.key?("provider")
# Physical Resource Configuration
h["linkedclone"] = "true" unless h.key?("linkedclone")
# h["linkedclone"] = true unless h.key?("linkedclone")
h["nestedvt"] = "false" unless h.key?("nestedvt")
h["mem"] = 512 unless h.key?("mem")
h["cpu"] = 1 unless h.key?("cpu")
if h.key?("box")
global_box=h["box"]
else
h["box"] = global_box
end
h["box"] = "bento/ubuntu-18.04" unless h.key?("box")
h["namelabel"] = true unless h.key?("namelabel")
# Video Configurations
h["x11"] = "false" unless h.key?("x11")
h["gui"] = "false" unless h.key?("gui")
h["vram"] = "0" unless h.key?("vram")
h["video3d"] = "false" unless h.key?("video3d")
config.vm.define h["name"] do |hnode|
case "#{h["provider"]}"
when "parallels"
configure_parallels(hnode,h)
when "vmware_fusion"
configure_vmware_fusion(hnode,h)
when "virtualbox"
configure_virtualbox(hnode,h)
end
######## Begin Network Configurations ########
(h["network"].to_a).each do |net|
net["auto_config"] = true unless net.key?("auto_config")
net["kind"] = "private_network" unless net.key?("kind")
net["type"] = "dhcp" unless net.key?("type") or net.key?("ip")
if net["type"] == "forwarded_port"
hnode.vm.network net["type"], guest: net["guest"], host: net["host"]
elsif net["type"] == "dhcp"
if net.key?("bridge")
hnode.vm.network net["kind"], type: "dhcp", auto_config: net["auto_config"], bridge: net["bridge"]
else
hnode.vm.network net["kind"], type: "dhcp", auto_config: net["auto_config"]
end
else
# Public or Private Network
if net.key?("bridge")
hnode.vm.network net["kind"], ip: net["ip"], netmask: net["mask"], auto_config: net["auto_config"], bridge: net["bridge"]
else
hnode.vm.network net["kind"], ip: net["ip"], netmask: net["mask"], auto_config: net["auto_config"]
end
end
end
######## End Network Configurations ########
hnode.vm.hostname = h["name"] # Set the hostname
hnode.vm.box = h["box"]
hnode.ssh.forward_x11 = "#{h["x11"]}" # Enables or not X11 defaults to disabled
hnode.vm.synced_folder '.', '/vagrant', disabled: true
(h["share"].to_a).each_with_index do |share,idx|
share["source"] = "." unless share.key?("source")
share["target"] = "/vagrant" unless share.key?("target")
share["disabled"] = false unless share.key?("disabled")
share["create"] = false unless share.key?("create")
share["owner"] = "vagrant" unless share.key?("owner")
share["group"] = "vagrant" unless share.key?("group")
share["options"] = [] unless share.key?("options")
share["sync"] = true unless share.key?("sync")
if share["sync"]
# Note: Files by themselves cannot be synced, only folders
hnode.vm.synced_folder share["source"], share["target"], disabled: share["disabled"], create: share["create"], owner: share["owner"], group: share["group"], mount_options: share["options"]# For now, don't support synced_folder
elsif ! share["disabled"]
hnode.vm.provision "file", source: share["source"], destination: share["target"]
end
end
# Commands Exclusive to Each Machine, All support the
# very 'same' specs but through different commands
(h["script"].to_a).each_with_index do |script,script_index|
script["privileged"] = true unless script.key?("privileged")
script["args"] = [] unless script.key?("args")
if script.key?("cmd")
hnode.vm.provision "Script #{h["name"]}.#{script_index}: #{script["path"]}", type: 'shell', privileged: script["privileged"], run: "once", preserve_order: true, inline: script["cmd"], args: script["args"]
else
hnode.vm.provision "Script #{h["name"]}.#{script_index}: #{script["path"]}", type: 'shell', privileged: script["privileged"], run: "once", preserve_order: true, path: script["path"], args: script["args"]
end
end
end
end
end