-
Notifications
You must be signed in to change notification settings - Fork 658
/
Vagrantfile
476 lines (440 loc) · 17.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
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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# ////README////
#
# This Vagrantfile is designed to instantiate an Evernym Development Environment
# using Virtualbox as the vagrant provider and automates the following:
#
# 1. Clone all the required source code from github, or use clones you have
# already created.
# 1.1. Source code should reside on the Virtualbox host (your desktop
# environment) and be available in the Virtualbox guest as a "shared
# folder". This allows the developer the option of using an IDE in their
# own graphical desktop environment with their own license.
# 1.2. If you have not already cloned source, this Vagrant file assumes that
# you have already forked the project and will prompt you for a username
# and clone from "https://github.com/<username>/<project>.git" where
# <username> is your username, and <project> is the target repo(s).
# 2. Install build tools/dependencies.
# 3. (Optional) Build source code on 'vagrant up'. Defaults to (N)o.
# 4. (Optional) Run unit tests on 'vagrant up'. Defaults to (N)o.
#
# //Prerequisites://
# 1. Install VirtualBox
# - Note that Vagrant 2.0.0 doesn't seem to work with Virtualbox 5.2.
# If you are using Vagrant <= 2.0.0, please use Virtualbox 5.1.30
# 2. Install Vagrant
# - See Note under Install Virtualbox above
# 3. Install Git
# 4. Set the 'branch' for each of the development_repos below.
# TODO: Display all branches, designate which branch the repo is on, and
# allow the user to select a different branch. The following git
# command should be helpful.
# git branch --list --format "%(refname:short) %(HEAD)" | sort -u
#
# //How to use://
# Run 'vagrant up' and answer any questions this Vagrantfile or the Vagrant VM
# instance (i.e. network bridge) needs to complete the process. If you run into
# problems (i.e. shared folders, etc.), try 'vagrant reload --provision'. Doing
# so will restart the VM and re-run the provisioning script. A slightly more
# expensive operation would be to 'vagrant destroy' followed by 'vagrant up'.
#
# //Some details to be aware of://
# 1. All files and folders in the Vagrant project folder show up as shared
# resources in /vagrant on all Vagrant VM(s)/guests instantiated by this
# Vagrantfile. This is a design descision made by the Vagrant folks.
# 2. Source code will be cloned into this Vagrant project folder using the git
# project name and then shared in /src/<git project name> in the Vagrant
# guest VM(s). The .gitignore file in this Vagrant project folder ignores
# these folders.
# 3. If you have your own source code clones, a symbolic link is
# created in this Vagrant project folder. Doing so simplifies some discovery
# logic in this Vagrantfile (ruby script). A side-effect of symlinking in
# this Vagrant project folder is that the symlink is broken inside the
# Virtualbox VM(s)/guests. Please do not remove or try to fix broken symlinks
# in /vagrant in the Virtualbox VM(s)/guests. And be assured that the source
# code associated with the symlinks on your Vagrant host are available in
# /src/<git project name> in your Vagrant VM(s)/guests.
# //Make sure to be using the vagrant-vbguest plugin.//
development_box = 'bento/ubuntu-16.04'
development_cpus = '2'
development_memory = '4096'
development_branch = `git rev-parse --abbrev-ref HEAD`
development_run_build = 'N'
development_run_tests = 'N'
development_run_cleanup = 'N'
# Code should reside on the Vagrant host and be mounted to the Vagrant VM
# using Vagrant's Shared Folder feature. Doing so allows a developer to use
# their favorite IDE/tools to write code and have a cheap, fast way to build
# and test their changes.
# //Define development repos and setup default Shared Folder path.//
#
# If you do not want to develop one or more of the following repos, you can
# "Skip" the repo when prompted.
#
# TODO: Decide how to chain dependendencies. For example, AWS Lambda code deps
# indy-sdk. Perhaps this is where a providing both .deb and .rpm will help
# if a dependency isn't being cloned and built from source, then a bundled
# .deb or .rpm will be installed to provide the dependency?
development_repos = {
"indy-sdk" => {
"path" => "/vagrant/indy-sdk",
"url" => "https://github.com/<USERNAME>/indy-sdk.git",
"branch" => "master"
},
#"indy" => {
# "path" => "/vagrant/indy",
# "url" => "https://github.com/<USERNAME>/indy.git",
# "branch" => "master"
#},
#"indy-self-serve" => {
# "path" => "/vagrant/indy-self-serve",
# "url" => "https://gitlab.corp.evernym.com/<USERNAME>/indy -self-serve.git",
# "branch" => "master"
#},
}
# //- Pseudocode -//
# For each 'repo' in development_repos
# Detect 'repo' in Vagrant project directory root
# If 'repo' detected
# Use the default directory path when defining synced folder
# Else
# Prompt the user for one of three options:
# Option 1: Skip - I will not be developing 'repo'
# Go to next repo
# Option 2: Enter path of 'repo' git clone on Vagrant host
# Update the <path> for 'repo' => <path> in the development_repos
# associative array
# Option 3: Clone 'repo' for me. Note that this option requires that you
# to have a fork of the 'repo' codebase on github/gitlab. The
# clone will be created in the root of this Vagrant project.
# - This option displays the repo URL and prompts the user for
# their github userid and builds the 'repo' URL.
# - The active branch will be the branch defined in the
# development_repos hash above.
# Clone the repo
# If failed to clone (404, etc.)
# Exit with a verbose error message
username = ""
filtered_development_repos = {}
development_repos.each_pair { |key, value|
filepath = File.expand_path(File.dirname(__FILE__))
directory = File.join("#{filepath}", "#{key}")
puts "Check if directory #{directory} exists ..."
if File.directory?("#{directory}")
puts "Directory #{directory} exists. Will mount #{directory} as "\
"/src/#{key}"
value["path"] = "#{directory}"
filtered_development_repos["#{key}"] = value
else
prompt = "> "
puts "Unable to detect a clone of repo '#{key}'. What would you like to do?"
puts "\t1. Skip - I will not be developing/testing code for #{key}."
puts "\t2. Enter a full path to repo '#{key}' on my Vagrant host."
puts "\t3. I have forked '#{key}'. Clone it for me."
print prompt
loop do
user_input = $stdin.gets.chomp
case user_input
when "1"
puts "Skipping ..."
# Create an empty directory using the repo name (key) so the developer is
# never prompted again. TODO: use properties file to manage the Skip
# feature (and others).
Dir.mkdir(key) unless Dir.exist?(key)
# Write a README file to the empty directory informing them
File.open("#{key}/README", "w") {
|f| f.write('You chose to skip this codebase. If you change your mind'\
', do the following:' + "\n\n" + '1. Delete this directory.' + "\n" +
'2. Run \'vagrant reload --provision\' -OR- \'vagrant destroy &&'\
' vagrant up\'')
}
break
when "2"
puts "Enter a full path to repo '#{key}' on your Vagrant host."
print prompt
absolute_path = $stdin.gets.chomp
if !File.directory?(absolute_path)
$stderr.puts "#{absolute_path} does not exist."
exit 1
else
puts "Verified #{absolute_path} exists ..."
end
puts "Creating symbolic link '#{key}' in Vagrant project to #{absolute_path} ..."
require 'open3'
stdout, stderr, status = Open3.capture3("ln -sf #{absolute_path} #{directory}")
if status.exitstatus != 0
$stderr.puts "Failed to create sybolic link"
$stderr.puts stderr
exit 1
end
value['path'] = absolute_path
filtered_development_repos[key] = value
break
when "3"
url = value['url']
puts "Enter your github username for url #{url}."
print prompt
username = $stdin.gets.chomp
url = url.sub("<USERNAME>", username)
value['url'] = url
branch = value['branch']
require 'open3'
stdout, stderr, status = Open3.capture3("git clone -b #{branch} #{url}")
if status.exitstatus != 0
$stderr.puts "Failed to clone #{url}"
$stderr.puts stderr
exit 1
end
value['path'] = Dir.pwd + "/#{key}"
filtered_development_repos[key] = value
break
else
puts "Please select either 1, 2, or 3"
print prompt
end
if do_checkout
case user_input
when "2", "3"
branch = value['branch']
puts "Setting '#{key}' branch to '#{branch}' ..."
require 'open3'
stdout, stderr, status = Open3.capture3("cd #{key} && git checkout #{branch}")
if status.exitstatus != 0
$stderr.puts "Failed checkout branch #{branch}"
$stderr.puts stderr
exit 1
end
break
end
end
end
end
}
if ARGV.include?("up")
development_run_cleanup = "Y"
end
if ARGV.include?("up") || ARGV.include?("provision") || ARGV.include?("--provision")
puts "Bringing up VM(s) ..."
prompt = "> "
puts "Build source? (y/N)"
print prompt
loop do
user_input = $stdin.gets.chomp
case user_input
when "Y", "y"
puts "Code will be built during VM provisioning ..."
development_run_build = "Y"
break
when "N", "n", ""
puts "Code will NOT be built during VM provisioning ..."
development_run_build = "N"
break
else
puts "Please enter Y, y, N, or n"
print prompt
end
end
# Only run tests if a build will be performed.
puts development_run_build
if development_run_build == "Y"
puts "Run tests? (y/N)"
print prompt
loop do
user_input = $stdin.gets.chomp
case user_input
when "Y", "y"
puts "Test will be run during VM provisioning ..."
development_run_tests = 'Y'
break
when "N", "n", ""
puts "Tests will NOT be run during VM provisioning ..."
development_run_tests = 'N'
break
else
puts "Please enter Y, y, N, or n"
print prompt
end
end
end
end
$script = <<SCRIPT
#!/bin/bash -e
BRANCH=$1
RUN_BUILD=$2
RUN_TESTS=$3
RUN_CLEANUP=$4
echo "Setting up development environment for branch $BRANCH"
echo "Run build? $RUN_BUILD"
echo "Run tests? $RUN_TESTS"
echo "Run cleanup? $RUN_CLEANUP"
#--------------------------------------------------------
echo 'Setting Up Networking'
if [ -f /vagrant/etc/hosts ]; then
cp /vagrant/etc/hosts /etc/hosts
fi
perl -p -i -e 's/(PasswordAuthentication\s+)no/$1yes/' /etc/ssh/sshd_config
service sshd restart
#--------------------------------------------------------
echo "Installing Required Packages"
# Refresh package lists from existing repos
apt-get update
apt-get install -y software-properties-common python-software-properties
# The following two lines are required to install a indy cli
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys CE7709D068DB5E88
add-apt-repository "deb https://repo.sovrin.org/deb xenial stable"
# Download the package lists from added repos
apt-get update
# NOTE: Packages required by indy cli: debsig*, apt-transport-https, indy
DEBIAN_FRONTEND=noninteractive apt-get install -y \
curl \
dialog \
figlet \
python-pip \
python3-pip \
python3.5-dev \
unzip \
make \
screen \
tmux \
vim \
wget \
debsigs \
debsig-verify \
apt-transport-https \
indy-node
echo "Install docker ..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
#--------------------------------------------------------
echo "Install docker SDK for Python ..."
pip3 install docker
#--------------------------------------------------------
# The following steps were derived from:
# https://github.com/hyperledger/indy-sdk/blob/master/docs/build-guides/ubuntu-build.md
#
echo "Installing Rust and rustup non-interactively as user vagrant ..."
echo "Note that rustup typically runs interactively and allows you to "
echo "customize installation. Defaults are used in this case. You can "
echo "reinstall Rust and rustup if you would like. See the following URL "
echo "for details: https://www.rust-lang.org/install.html"
curl https://sh.rustup.rs -sSf > rust.install.sh
chmod u+x ./rust.install.sh
chown vagrant:vagrant ./rust.install.sh
su -c "./rust.install.sh -y" vagrant
apt-get install -y \
build-essential \
pkg-config \
cmake \
libssl-dev \
libsqlite3-dev \
libsodium-dev
#--------------------------------------------------------
# Build
#
# For some reason, sourcing the vagrant users .profile isn't working.
# Must use the absolute path to /home/vagrant/.cargo/bin/cargo to get
# the build to work. Note that the .profile IS sourced when the
# vagrant user logs in (next time 'vagrant ssh development' is run)
#
# Sourcing the /home/vagrant/.profile file as root didn't work
# source /home/vagrant/.profile
if [ "$RUN_BUILD" == "Y" ]; then
# Only build indy-sdk if the source is present.
if [ -d "/src/indy-sdk" ]; then
cd /src/indy-sdk/libindy
su -c "/home/vagrant/.cargo/bin/cargo build" vagrant
fi
fi
#--------------------------------------------------------
# Run tests?
if [ "$RUN_TESTS" == "Y" ]; then
# Only run indy-sdk tests if the source is present
if [ -d "/src/indy-sdk" ]; then
cd /src/indy-sdk
sudo /vagrant-common/indypool.py start
#docker build -f ci/indy-pool.dockerfile -t indy_pool .
#docker run -itd --name indy_pool -p 9701-9708:9701-9708 indy_pool
cd /src/indy-sdk/libindy
su -c "RUST_TEST_THREADS=1 /home/vagrant/.cargo/bin/cargo test" vagrant
fi
fi
#--------------------------------------------------------
if [ "$RUN_CLEANUP" == "Y" ]; then
echo 'Cleaning Up'
rm /etc/update-motd.d/10-help-text
rm /etc/update-motd.d/97-overlayroot
fi
#--------------------------------------------------------
echo "Post-Build/Test setup..."
if [ -d "/src/indy-sdk" ]; then
echo "Making sure indy_pool is running ..."
sudo /vagrant-common/indypool.py start
echo "Setting up vagrant user's .bashrc to use indy-sdk Python bindings ..."
# Only add indy-sdk python wrappers to the PTHONPATH if indy-sdk source is present
vagrant_bashrc="/home/vagrant/.bashrc"
python_wrappers_path='export PYTHONPATH=$PYTHONPATH:/src/indy-sdk/wrappers/python'
if ! grep -q "${python_wrappers_path}" ${vagrant_bashrc}; then
echo "Add libindy python wrappers to the PYTHONPATH in ${vagrant_bashrc} ..."
echo "${python_wrappers_path}" >> ${vagrant_bashrc}
fi
libindy_debug_lib_path='export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/src/indy-sdk/libindy/target/debug'
if ! grep -q "${libindy_debug_lib_path}" ${vagrant_bashrc}; then
echo "Add libindy debug libs to LD_LIBRARY_PATH in ${vagrant_bashrc} ..."
echo "${libindy_debug_lib_path}" >> ${vagrant_bashrc}
fi
fi
# Removed, because Python 3.5 works and 3.6 from ppa:jonathonf has a known issue
# on Ubuntu 16.04: https://bugs.python.org/issue29307
#--------------------------------------------------------
#echo "Install Python 3.6 ..."
#add-apt-repository ppa:jonathonf/python-3.6
#apt-get update
#apt-get install -y python3.6
#--------------------------------------------------------
echo "Setup default .vimrc ..."
echo ":set colorcolumn=80" >> /home/vagrant/.vimrc
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
updatedb
SCRIPT
Vagrant.configure("2") do |config|
config.vm.define "development" do |development|
development.vm.box = development_box
development.vm.host_name = "development.sandbox.evernym.lab"
development.vm.network 'public_network'
development.ssh.private_key_path = '~/.vagrant.d/insecure_private_key'
development.ssh.insert_key = false
development.vm.provider "virtualbox" do |vb|
vb.name = "sandbox-development"
vb.gui = false
vb.memory = development_memory
vb.cpus = development_cpus
vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
end
# Uncomment the following line to set the default bridge.
# The bridge string MUST match your connection exactly as
# it appears when you are prompted to select a bridge during
# provisioning.
#default.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
development.vm.provision "shell" do |s|
s.inline = $script
s.args = [
development_branch,
development_run_build,
development_run_tests,
development_run_cleanup
]
end
filtered_development_repos.each do |key, value|
path = value['path']
#print "Setup rsync-ed folder: development.vm.synced_folder '#{value}', '/vagrant/#{key}', type: 'rsync'\n"
development.vm.synced_folder "#{path}", "/vagrant/#{key}"
end
# Include all DevelopmentEnvironment common scripts/tools. These tools will
# be referenced with an absolute path in the guest VM.
development.vm.synced_folder Dir.pwd + "/../common", "/vagrant-common"
end
end