-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vagrantfile
179 lines (155 loc) · 6.29 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
class Hash
def slice(*keep_keys)
h = {}
keep_keys.each { |key| h[key] = fetch(key) if has_key?(key) }
h
end unless Hash.method_defined?(:slice)
def except(*less_keys)
slice(*keys - less_keys)
end unless Hash.method_defined?(:except)
end
# 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.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.provision "apt", :type => :shell, :run => 'always', :inline => <<-SHELL
last_apt_update_path=/var/cache/last_apt_update
if [ ! -e $last_apt_update_path ] || [ `expr $(date "+%s") - $(stat -c "%Y" $last_apt_update_path)` -gt 86400 ]; then
apt-get update && touch $last_apt_update_path
fi
SHELL
config.vm.provision "sync_clock", :type => :shell, :path => 'setup/sync_clock.sh', :run => 'always'
config.vm.provision "vagrant_user", :type => :shell, :path => 'setup/create_vagrant_user.sh'
def provision_site(site_config)
# mysql-server
site_config.vm.provision "mysql", :type => :shell, :path => 'setup/install-mysql-server.sh'
# nginx
site_config.vm.provision "nginx", :type => :shell, :inline => <<-SHELL
which nginx >/dev/null 2>&1
if [ $? -ne 0 ]; then
sudo apt-get install -y nginx
fi
SHELL
# git
site_config.vm.provision "git", :type => :shell, :inline => 'sudo apt-get install -y git'
# ruby-mri
site_config.vm.provision "ruby", :type => :shell, :inline => 'sudo apt-get install -y ruby ruby-dev libxml2 build-essential'
# hailstorm-site
site_config.vm.provision "hailstorm_site", :type => :shell, :path => 'setup/hailstorm-site/install-hailstorm-site.sh'
site_config.vm.provision "hailstorm_service", :type => :shell, :inline => 'sudo systemctl start hailstorm-site.service', run: :always
end
def aws_keys
credentials_file_path = File.join(ENV['HOME'], '.aws', 'credentials')
if File.exist?(credentials_file_path)
keys = File.open(credentials_file_path, 'r') do |f|
f.readlines.collect {|line|
line.chomp
}
.reduce({}) {|k, e|
if e =~ /^\[(.+?)\]$/
profile = $1
k.merge(profile => {}, last: profile)
elsif e =~ /^(.+?)\s*=\s*['"]?(.+?)['"]?$/
profile = k[:last]
k[profile][$1] = $2
k
else
k
end
}
end
profile_name = ENV['AWS_PROFILE'] || 'default'
profile = keys.key?(profile_name) ? keys.fetch(profile_name) : {}
[profile['aws_access_key_id'], profile['aws_secret_access_key']]
else
raise "#{credentials_file_path} not found"
end
end
config.vm.define "aws-site", autostart: false do |site|
site.vm.box = "dummy"
site.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: [".git/", ".gitignore/", "log/", "tmp/", "build/"]
site.vm.provider :aws do |ec2, override|
ec2.access_key_id, ec2.secret_access_key = aws_keys
require 'yaml'
aws_conf = YAML.load_file('setup/vagrant-aws.yml').reduce({}) { |a, e| a.merge(e[0].to_sym => e[1]) }
ec2.ami = aws_conf[:ami]
ec2.keypair_name = aws_conf[:keypair_name]
ec2.instance_type = aws_conf[:instance_type] || "t2.medium"
ec2.region = aws_conf[:region] || "us-east-1"
ec2.security_groups = aws_conf[:security_groups]
ec2.subnet_id = aws_conf[:subnet_id] if aws_conf.key?(:subnet_id)
ec2.tags = {
Name: ["Vagrant - Hailstorm Site", ENV['TAG_VALUE']].join(' ')
}
ec2.terminate_on_shutdown = false
ec2.block_device_mapping = [{"DeviceName" => "/dev/sda1", "Ebs.VolumeSize" => 8}]
ec2.elastic_ip = false
ec2.associate_public_ip = true
override.ssh.username = "ubuntu"
override.ssh.private_key_path = aws_conf[:private_key_path]
end
provision_site(site)
site.vm.provision "reset_site_ownership", :type => :shell, :inline => 'sudo chmod -R 777 /vagrant/tmp', run: :always
end
config.vm.define "data-center-site", autostart: false do |site_local|
site_local.vm.box = "ubuntu/focal64"
site_local.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 = "2048"
vb.cpus = 2
# vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
end
site_local.vm.network "private_network", ip: "192.168.20.100"
provision_site(site_local)
end
(1..2).each do |serial|
config.vm.define "data-center-agent-#{serial}", autostart: false do |hsdc|
hsdc.vm.box = 'ubuntu/focal64'
hsdc.vm.provider 'virtualbox' do |vb|
vb.memory = 2048
end
hsdc.vm.network 'private_network', ip: "192.168.20.#{serial * 10}"
hsdc.vm.hostname = "data-center-agent-#{serial}"
hsdc.vm.provision 'java', type: :shell do |s|
s.inline = <<-X
which java
if [ $? -ne 0 ]; then
chmod +x /vagrant/setup/data-center/install_java.sh && \
/vagrant/setup/data-center/install_java.sh
fi
X
end
hsdc.vm.provision 'jmeter', type: :shell do |s|
s.inline = <<-X
if [ ! -e /root/jmeter ]; then
chmod +x /vagrant/setup/data-center/install_jmeter.sh && \
/vagrant/setup/data-center/install_jmeter.sh \
https://archive.apache.org/dist/jmeter/binaries/apache-jmeter-5.2.1.tgz \
/root
fi
X
end
hsdc.vm.provision 'ssh', type: :shell do |s|
s.inline = <<-X
if [ ! -e /root/insecure_key.pub ]; then
cp /vagrant/setup/data-center/insecure_key.pub /root/insecure_key.pub
mkdir -p /root/.ssh && chmod 700 /root/.ssh
cat /root/insecure_key.pub >> /root/.ssh/authorized_keys
chmod 400 /root/.ssh/authorized_keys
fi
X
end
end
end
end