-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vagrantfile
392 lines (291 loc) · 13.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
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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
require './util'
# Build script settings.
RUN_TESTS = false # Run tests?
DESTROY_DB = false # Destroy the database?
ENGAGE_CAKE = true # Engage cake? yes
DEPLOY = true # Deploy the app?
INSERT_TEST_DATA = true # Insert test data upon provision step?
CREATE_SSL = true # Create an SSL Certificate
USE_PUBLIC_REPO = true # Use a public repository URL in case the private one is no more or inaccessible?
# Load YAML file containing IP addresses, as well as other variables.
VARIABLES = YAML.load_file('variables.yml')
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure('2') do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# SSH key doesn't exist on host?
unless File.exist? File.expand_path(VARIABLES['ssh-key-location'])
# They want to use a private repo?
unless USE_PUBLIC_REPO
puts "SSH key located at #{File.expand_path(VARIABLES['ssh-key-location'])} does not exist, yet you want to use a private repository."
puts "`vagrant up` will surely fail due to the `git clone` command failing."
puts "Halting!"
exit(1)
end
end
# Disable synced folder by default to enforce cloning from git!
config.vm.synced_folder '.', '/vagrant', disabled: true
# SSH settings.
config.ssh.insert_key = false
config.ssh.username = 'vagrant'
config.ssh.password = 'vagrant'
# Storage box.
config.vm.define 'storage' do |storage|
storage.vm.box = './packer/output/ubuntu-storage.box'
storage.vm.network 'private_network', ip: VARIABLES['storage']['ip']
storage.vm.hostname = VARIABLES['storage']['hostname']
storage.vm.provider 'virtualbox' do |vb|
vb.gui = false
vb.memory = '512'
end
# Copy directory exports file for NFS server.
storage.vm.provision :file, source: './vagrant-config/config-files/nfs/exports', destination: '/tmp/exports'
# Configure NFS server.
storage.vm.provision :shell, path: 'vagrant-config/scripts/configure-nfs-server.sh', env: {
:SUBNET_IP => VARIABLES['subnet'],
:HOST_IP => VARIABLES['host-ip'],
}
# Expose port for monitoring via netdata.
storage.vm.network 'forwarded_port', guest: 19999, host: VARIABLES['storage']['netdata-port'], host_ip: '127.0.0.1'
end
# MySQL box.
config.vm.define 'db' do |db|
db.vm.box = './packer/output/ubuntu-mysql.box'
# Sourced from https://stackoverflow.com/questions/24867252/allow-two-or-more-vagrant-vms-to-communicate-on-their-own-network
# This creates a private network specified in a configuration file.
db.vm.network 'private_network', ip: VARIABLES['db']['ip']
db.vm.hostname = VARIABLES['db']['hostname']
db.vm.provider 'virtualbox' do |vb|
vb.gui = false
vb.memory = '1024'
end
# Expose port for monitoring via netdata.
db.vm.network 'forwarded_port', guest: 19999, host: VARIABLES['db']['netdata-port'], host_ip: '127.0.0.1'
# Clone GitHub repo.
clone_repo(db, VARIABLES, USE_PUBLIC_REPO)
# Copy my.cnf to MySQL server.
db.vm.provision 'file', source: './vagrant-config/config-files/mysql/my.cnf', destination: '/tmp/my.cnf'
db.vm.provision 'shell', inline: <<-SCRIPT
sudo mv /tmp/my.cnf /etc/mysql/my.cnf
SCRIPT
# Set up MySQL.
db.vm.provision :shell, path: 'vagrant-config/scripts/setup-mysql.sh'
# Set up MySQL users to allow web box to connect to db box's MySQL server.
db.vm.provision :shell, env: {
:WEB_IP_ADDR => VARIABLES['web']['ip'],
:USERNAME => VARIABLES['db']['username'],
:PASSWORD => VARIABLES['db']['password'],
:DB_SCHEMA => VARIABLES['db']['schema']
},
path: 'vagrant-config/scripts/setup-mysql-users.sh'
end
# Webserver box.
config.vm.define 'web' do |web|
web.vm.box = './packer/output/ubuntu-webserver.box'
web.vm.provider 'virtualbox' do |vb|
vb.gui = false
vb.memory = '1536'
end
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = false
end
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 8080, host: 8080, host_ip: "127.0.0.1"
# Expose HTTP Port to host computer.
web.vm.network 'forwarded_port', guest: 8080, host: 8080, host_ip: '127.0.0.1'
# Expose HTTPS Port to host computer.
web.vm.network 'forwarded_port', guest: 443, host: 443, host_ip: '127.0.0.1'
# Expose port for monitoring via netdata.
web.vm.network 'forwarded_port', guest: 19999, host: VARIABLES['web']['netdata-port'], host_ip: '127.0.0.1'
# Add webserver to a private network.
web.vm.network 'private_network', ip: VARIABLES['web']['ip']
web.vm.hostname = VARIABLES['web']['hostname']
# Test that the webserver can ping the database server.
web.vm.provision 'shell', run: 'always', env: {:DB_IP_ADDR => VARIABLES['db']['ip']},
inline: <<-SCRIPT
# Ping DB once.
ping -c1 $DB_IP_ADDR
# If last error code is zero (success), then...
if [ "$?" = 0 ]; then
echo "MySQL server at $DB_IP_ADDR is ping-able!"
else
echo "MySQL server at $DB_IP_ADDR could not be pinged! Halting!"
false # This will force an error.
fi
SCRIPT
# Mount NFS drive.
web.vm.provision :shell, path: 'vagrant-config/scripts/configure-nfs-client.sh', env: {
:NFS_SERVER_IP => VARIABLES['storage']['ip'],
:MOUNT_LOCATION => VARIABLES['web']['mount-location'],
}
# Test that the webserver has correctly mounted the storage box's drive over nfs.
web.vm.provision 'shell', run: 'always', env: {
:MOUNT_LOCATION => VARIABLES['web']['mount-location'],
},
inline: <<-SCRIPT
if [[ ! -d $MOUNT_LOCATION ]]; then
echo "NFS folder at $MOUNT_LOCATION does not exist!"
false
else
echo "NFS folder is mounted at $MOUNT_LOCATION successfully. Testing if I can create files."
echo "test" > $MOUNT_LOCATION/test_file.txt
if [[ ! -f $MOUNT_LOCATION/test_file.txt ]]; then
echo "Somehow test file does not exist after we have created it."
false
else
echo "Successfully created test file in network-mounted drive!"
sudo rm $MOUNT_LOCATION/test_file.txt
fi
fi
SCRIPT
# Test that the webserver can ping the storage box.
web.vm.provision 'shell', run: 'always', env: {:STORAGE_IP_ADDR => VARIABLES['storage']['ip']},
inline: <<-SCRIPT
# Ping storage box once.
ping -c1 ${STORAGE_IP_ADDR}
# If last error code is zero (success), then...
if [ "$?" = 0 ]; then
echo "Storage box at ${STORAGE_IP_ADDR} is ping-able!"
else
echo "Storage box at ${STORAGE_IP_ADDR} could not be pinged! Halting!"
false # This will force an error.
fi
SCRIPT
# Test that the web box can connect to the MySQL server running on the database box.
web.vm.provision 'shell', run: 'always', env: {
:DB_IP_ADDR => VARIABLES['db']['ip'],
:USERNAME => VARIABLES['db']['username'],
:PASSWORD => VARIABLES['db']['password'],
:DB_SCHEMA => VARIABLES['db']['schema']
}, inline: <<-SCRIPT
# Ping DB once.
ping -c1 $DB_IP_ADDR
# Show all tables to verify connection.
mysql -u$USERNAME -p$PASSWORD -h $DB_IP_ADDR $DB_SCHEMA -e "SHOW TABLES;"
# Create a test table.
mysql -u$USERNAME -p$PASSWORD -h $DB_IP_ADDR $DB_SCHEMA -e "CREATE TABLE test (id integer);"
# Drop that table.
mysql -u$USERNAME -p$PASSWORD -h $DB_IP_ADDR $DB_SCHEMA -e "DROP TABLE test;"
echo "MySQL connection OK!"
SCRIPT
# TODO We can safely remove this line as it is in the packer build step. This affects ALL team members though.
# Did not remove to avoid having others being forced to rebuild.
web.vm.provision 'ffmpeg', type: 'shell', inline: 'sudo add-apt-repository ppa:jonathonf/ffmpeg-4 ; sudo apt install ffmpeg -y'
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
clone_repo(web, VARIABLES, USE_PUBLIC_REPO)
# Modify database.properties to make web box's app use db box's database
web.vm.provision :shell, env: {
:DB_IP_ADDR => VARIABLES['db']['ip'],
:DB_PORT => VARIABLES['db']['port'],
:DB_USERNAME => VARIABLES['db']['username'],
:DB_PASSWORD => VARIABLES['db']['password'],
:DB_SCHEMA => VARIABLES['db']['schema'],
},
inline: <<-SCRIPT
# Go to where database.properties file is.
cd /home/vagrant/2019-team-07f/server/WebContent/META-INF/
# Delete it!
rm database.properties
# Make an empty one.
touch database.properties
# Populate it with the correct information.
echo "host=$DB_IP_ADDR:$DB_PORT" >> database.properties
echo "database=$DB_SCHEMA" >> database.properties
echo "user=$DB_USERNAME" >> database.properties
echo "password=$DB_PASSWORD" >> database.properties
# Show the contents of the file.
cat database.properties
SCRIPT
# Refresh environment variables.
web.vm.provision :shell, path: 'vagrant-config/scripts/refresh-env.sh' #TODO does this actually work? Is a restart required?
# Set up maven.
web.vm.provision :shell, path: 'vagrant-config/scripts/setup-maven.sh'
# Set up tomcat.
web.vm.provision :shell, path: 'vagrant-config/scripts/setup-tomcat.sh'
# Run tests.
if RUN_TESTS
# Test our Python libraries.
web.vm.provision :shell, path: 'vagrant-config/scripts/test-python.sh', run: 'always'
# Test jep.
web.vm.provision :shell, path: 'vagrant-config/scripts/test-jep.sh', run: 'always'
end
if CREATE_SSL
# Try to create SSL Certificate
web.vm.provision :shell, path: 'vagrant-config/scripts/create_ssl_cert.sh', run: 'always', env: {
:WEB_IP_ADDR => VARIABLES['web']['ip'],
:TEAM_NAME => VARIABLES['ssl-cert']['team-name'],
:TEAM_ORG => VARIABLES['ssl-cert']['team-org'],
:COUNTRY => VARIABLES['ssl-cert']['country'],
:STATE => VARIABLES['ssl-cert']['state'],
}
end
if DESTROY_DB
# Destroy database.
web.vm.provision :shell, path: 'vagrant-config/scripts/destroy-db.sh', run: 'always'
end
if DEPLOY
# Deploy our app.
web.vm.provision :shell, path: 'vagrant-config/scripts/deploy-app.sh', env: {
:REPO_PATH => VARIABLES['repo_location'],
}
# At this point, going to http://127.0.0.1:8080/searchable-video-library/ should yield some HTML page.
end
# If we want to insert test data like test users,
if INSERT_TEST_DATA
web.vm.provision :shell, path: 'vagrant-config/scripts/insert-test-users.sh', env: {
:REPO_PATH => VARIABLES['repo_location'],
:DB_IP_ADDR => VARIABLES['db']['ip'],
:DB_PORT => VARIABLES['db']['port'],
:DB_USERNAME => VARIABLES['db']['username'],
:DB_PASSWORD => VARIABLES['db']['password'],
:DB_SCHEMA => VARIABLES['db']['schema'],
}
end
end
end