-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVagrantfile
247 lines (220 loc) · 11.2 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# _meta parameters for VMs:
# - vagrant_image - defaults to ubuntu/trusty64
# - vagrant_ram - defaults 256
require 'json'
require 'pp'
# get configured vagrant boxes
# filelist = Dir.glob("./provisioning/*.json").sort
filelist_lb = Dir.glob("./provisioning/*lb*.json").sort
filelist_s3 = Dir.glob("./provisioning/*storage-s3*.json").sort
filelist_dev = Dir.glob("./provisioning/*desktop*.json").sort
# concat filelist1
filelist = filelist_s3 + filelist_lb + filelist_dev
filelist.each do |filepath|
filename = File.basename("#{filepath}", ".json")
end
hosts = Hash.new
filelist.each do |filepath|
host = File.basename("#{filepath}", ".json")
# get vagrant parameters
if File.exist?("./provisioning/%s.json" % host)
config_file = File.read("./provisioning/%s.json" % host)
config_data = JSON.parse(config_file)
else
puts "Konfiguration ./provisioning/#{host}.json for #{host} not found"
exit
end
# get image namespace
$imagedata = config_data["hostvars"]["vagrant_image"].split('/')
$namespace = $imagedata[0]
if ($namespace == "vagrant-epr-dev")
$boxname = $imagedata[1]
else
$boxname = (config_data["hostvars"]["vagrant_image"] != nil && config_data["hostvars"]["vagrant_image"] != "")?config_data["hostvars"]["vagrant_image"]:"ubuntu/trusty64"
end
# set parameter
hosts[host] = { "vagrant_image" => config_data["hostvars"]["vagrant_image"],
"vagrant_image_version" => config_data["hostvars"]["vagrant_image_version"],
"vagrant_boxname" => $boxname,
"vagrant_namespace" => $namespace,
"vagrant_ram" => config_data["hostvars"]["vagrant_ram"],
"vagrant_cpu" => config_data["hostvars"]["vagrant_cpu"],
"vagrant_net" => config_data["hostvars"]["vagrant_net"],
"vb_hostname" => config_data["hostvars"]["vb_hostname"],
"vb_guest_os" => config_data["hostvars"]["vb_guest_os"],
"vb_group" => config_data["hostvars"]["vb_group"],
"vb_gateway" => config_data["hostvars"]["vb_gateway"],
"vb_dnshostresolve" => config_data["hostvars"]["vb_dnshostresolve"],
"vb_network" => config_data["hostvars"]["vb_network"],
"vb_port_forwarding" => config_data["hostvars"]["vb_port_forwarding"],
"vb_share" => config_data["hostvars"]["vb_share"],
"vb_shell_provisioner" => config_data["hostvars"]["vb_shell_provisioner"],
"vb_ansible_provisioner" => config_data["hostvars"]["vb_ansible_provisioner"],
"vb_trigger_destroy" => config_data["hostvars"]["vb_trigger_destroy"],
"vb_rdp_port" => config_data["hostvars"]["vb_rdp_port"],
}
end
#--- use the host variable to set up the VMs
Vagrant.configure("2") do |config|
hosts.each do |name, hostInfo|
config.vm.define name do |machine|
machine.vbguest.auto_update = false
machine.vm.box = hostInfo["vagrant_boxname"]
if (hostInfo["vagrant_namespace"] == "vagrant-epr-dev")
# set vagrant-epr-dev environment
ENV['LC_ARTIFACTORY_APITOKEN'] = ENV['VAGRANT_ARTIFACTORY_APITOKEN']
ENV['ATLAS_TOKEN'] = ENV['VAGRANT_ARTIFACTORY_ATLASTOKEN']
ENV['VAGRANT_SERVER_URL'] = "http://repo.corp.conextrade.com:8081/artifactory/api/vagrant"
# set box url
machine.vm.box_url = "http://repo.corp.conextrade.com:8081/artifactory/api/vagrant/vagrant-epr-dev/#{hostInfo["vagrant_boxname"]}"
end
if (hostInfo["vagrant_image_version"] != nil && hostInfo["vagrant_image_version"] != "")
machine.vm.box_version = hostInfo["vagrant_image_version"]
end
if (hostInfo["vb_hostname"] != nil)
machine.vm.hostname = "%s" % hostInfo["vb_hostname"]
else
machine.vm.hostname = "%s" % name
end
if (hostInfo["vb_guest_os"] == "win")
machine.vm.communicator = "winrm"
machine.vbguest.auto_update = false
end
# define description
$version = (hostInfo["vagrant_image_version"] != nil && hostInfo["vagrant_image_version"] != "")?hostInfo["vagrant_image_version"]:"latest"
$description = "Baseimage: " + hostInfo["vagrant_image"] + " (" + $version + ")"
# create network cards
if (hostInfo["vb_network"] != nil)
hostInfo["vb_network"].each do |network|
if (network["nettype"] == nil || network["nettype"] == "internal")
if (network["netname"] == nil)
$netname = "internal-net"
else
$netname = network["netname"]
end
machine.vm.network :private_network, ip: network["ip"], netmask: network["netmask"], virtualbox__intnet: $netname, nic_type: network["nictype"], :mac => network["mac"]
elsif (network["nettype"] == "hostonly")
if (network["netname"] == nil)
$adaptername = "VirtualBox Host-Only Ethernet Adapter"
else
$adaptername = network["adapter"]
end
machine.vm.network "private_network", ip: network["ip"], netmask: network["netmask"], nic_type: network["nictype"], :name => $adaptername, :mac => network["mac"]
else
puts "Unknown net type #{network["nettype"]}"
end
end
end
# create port forwarding
if (hostInfo["vb_port_forwarding"] != nil && hostInfo["vb_port_forwarding"] != "")
hostInfo["vb_port_forwarding"].each do |forwarding|
forwarding["host_ip"] == nil ? ($fwhostip = "127.0.0.1") : ($fwhostip = forwarding["host_ip"])
machine.vm.network "forwarded_port", id: forwarding["id"], auto_correct: forwarding["auto_correct"], protocol: forwarding["protocol"], guest: forwarding["guest"], host: forwarding["host"], host_ip: $fwhostip, guest_ip: forwarding["guest_ip"]
end
end
# create rdp forwarding
if (hostInfo["vb_rdp_port"] != nil && hostInfo["vb_rdp_port"] != "" )
machine.vm.network "forwarded_port", guest: 3389, host: hostInfo["vb_rdp_port"], host_ip: "127.0.0.1"
end
machine.vm.provider "virtualbox" do |v|
v.name = name
v.cpus = (hostInfo["vagrant_cpu"] != nil && hostInfo["vagrant_cpu"] != "")?hostInfo["vagrant_cpu"]:2
ram = (hostInfo["vagrant_ram"] != nil && hostInfo["vagrant_ram"] != "")?hostInfo["vagrant_ram"]:512
v.customize ["modifyvm", :id, "--description", "#{$description}"]
v.customize ["modifyvm", :id, "--memory", ram ]
if (hostInfo["vb_dnshostresolve"] == true)
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
v.customize ["modifyvm", :id, "--groups", "/%s" % hostInfo["vb_group"] ]
end
# Define ssh configuration
machine.ssh.insert_key = false
machine.ssh.forward_env = ["LC_ARTIFACTORY_APITOKEN"]
# Autoconfigure hosts. This will copy the private network addresses from
# each VM and update hosts entries on all other machines. No further
# configuration is needed.
machine.vm.provision :hosts, :sync_hosts => true, :run => 'always'
# create standard kitchen shared_files share
#machine.vm.synced_folder ".", "/vagrant", owner: "vagrant", group: "vagrant", mount_options: ["dmode=775,fmode=774"]
machine.vm.synced_folder ".", "/vagrant", owner: "vagrant", group: "vagrant", mount_options: ["dmode=777,fmode=777"]
$shareddirname = "%s/shared_files" % ENV['VAGRANT_KITCHEN_ROOT']
if File.directory?($shareddirname)
machine.vm.synced_folder $shareddirname, "/shared_files", SharedFoldersEnableSymlinksCreate: true
else
puts "shared_files directory doesn't exists"
end
# create share
if (hostInfo["vb_share"] != nil && hostInfo["vb_share"] != "")
hostInfo["vb_share"].each do |share|
machine.vm.synced_folder "#{share["srcdir"]}", "#{share["desdir"]}", SharedFoldersEnableSymlinksCreate: true
end
end
# search for provisioning file (without ps1 extension)
if (hostInfo["vb_shell_provisioner"] != nil && hostInfo["vb_shell_provisioner"] != "")
hostInfo["vb_shell_provisioner"].each do |provisioner|
if File.exist?("./provisioning/%s" % provisioner["script"])
machine.vm.provision "shell" do |cmd|
cmd.path = "./provisioning/%s" % provisioner["script"]
end
else
puts "Provisioning file #{provisioner["script"]} doesn't exists"
end
end
end
# configure default gateway for unix based system
if (hostInfo["vb_gateway"] != nil && hostInfo["vb_gateway"] != "")
if (hostInfo["vb_guest_os"] == "ubuntu")
$gateway = hostInfo["vb_gateway"]
machine.vm.provision "shell" do |cmd|
cmd.path = "./files/bashshell/ubuntu-set-gateway.sh"
cmd.args = [hostInfo["vb_gateway"]]
end
elsif (hostInfo["vb_guest_os"] == "centos")
$gateway = hostInfo["vb_gateway"]
machine.vm.provision "shell" do |cmd|
cmd.path = "./files/bashshell/centos-set-gateway.sh"
cmd.args = [hostInfo["vb_gateway"]]
end
elsif (hostInfo["vb_guest_os"] == "win")
hostInfo["vb_network"].each do |network|
machine.vm.provision "shell" do |cmd|
cmd.path = "./files/powershell/win-set-gateway.ps1"
cmd.args = [ network["ip"],
hostInfo["vb_gateway"]
]
end
end
else
$osname = hostInfo["vb_guest_os"]
puts "OS #{$osname} not supported"
end
end
if (hostInfo["vb_ansible_provisioner"] != nil && hostInfo["vb_ansible_provisioner"] != "")
hostInfo["vb_ansible_provisioner"].each do |provisioner|
# ansible provisioning
machine.vm.provision :ansible_local do |ansible|
ansible.playbook = provisioner["playbook"]
ansible.provisioning_path = provisioner["provisioning_path"]
ansible.verbose = provisioner["verbose"]
ansible.install = provisioner["install"]
ansible.limit = provisioner["limit"]
ansible.inventory_path = provisioner["inventory_path"]
end
end
end
if (hostInfo["vb_trigger_destroy"] != nil && hostInfo["vb_trigger_destroy"] != "")
hostInfo["vb_trigger_destroy"].each do |shutdown|
$cmd = "./provisioning/%s" % shutdown["script"]
# trigger destroy
machine.trigger.before :destroy do |trigger|
trigger.info = "Call cluster destroy trigger script"
trigger.run_remote = {path: "#{$cmd}"}
end
end
end
end
end
end