forked from hashbangcode/vlad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
266 lines (220 loc) · 8.77 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vlad - Vagrant LAMP Ansible Drupal
# A Drupal development platform in a box, with everything you would need to develop Drupal websites.
# See the readme file (README.md) for more information.
# Contribute to this project at : https://github.com/hashbangcode/vlad
# If vagrant-trigger isn't installed then exit
if !Vagrant.has_plugin?("vagrant-triggers")
puts "Vlad requires the plugin 'vagrant-triggers'"
puts "This can be installed by running:"
puts
puts " vagrant plugin install vagrant-triggers"
puts
exit
end
# Find the current vagrant directory & create additional vars from it
vagrant_dir = File.expand_path(File.dirname(__FILE__))
parent_dir = File.dirname(vagrant_dir)
vlad_hosts_file = vagrant_dir + '/vlad/host.ini'
# Load settings and overrides files
settings_files = {
"Vlad settings" => [vagrant_dir + "/vlad/settings.yml",
vagrant_dir + "/settings/vlad_settings.yml",
parent_dir + "/settings/vlad_settings.yml"
],
"local overrides" => [vagrant_dir + "/vlad/local_settings.yml",
vagrant_dir + "/settings/vlad_local_settings.yml",
parent_dir + "/settings/vlad_local_settings.yml"
]
}
vconfig = YAML::load_file(vagrant_dir + "/vlad/playbooks/vars/defaults/vagrant.yml")
# Iterate over the settings files and load the first file that is found for each type, then
# merge them over the base/default settings loaded in vconfig
loaded_vlad_settings = false
puts "\nChecking for Vlad settings and local overrides..."
settings_files.each do |type, paths|
paths.each do |file|
if File.exists?(file)
puts "Loading #{type} file: #{file}"
loaded_vlad_settings = true
settings_to_merge = YAML::load_file(file)
vconfig = vconfig.merge settings_to_merge
break
end
end
end
# Warn if we didn't find any files to load
unless loaded_vlad_settings
puts "No Vlad settings or local overrides files found (will use default settings)."
end
puts
# Set box configuration options
boxipaddress = vconfig['boxipaddress']
boxname = vconfig['boxname']
synced_folder_type = vconfig['synced_folder_type']
vlad_os = vconfig['vlad_os']
# Detect the current provider and set a variable.
if ARGV[1] and (ARGV[1].split('=')[0] == "--provider" or ARGV[2])
provider = (ARGV[1].split('=')[1] || ARGV[2])
else
provider = (ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
end
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Configure virtual machine options.
config.vm.hostname = boxname
config.vm.network :private_network, ip: boxipaddress
# Allow caching to be used (see the vagrant-cachier plugin)
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :machine
config.cache.synced_folder_opts = { type: :nfs }
config.cache.auto_detect = false
if vlad_os == "centos66"
config.cache.enable :yum
else
config.cache.enable :apt
end
config.cache.enable :gem
config.cache.enable :npm
end
# Set *Vagrant* VM name (e.g. "vlad_myboxname_74826748251406_66388")
config.vm.define boxname do |boxname|
end
# Calculate "auto" values for CPUs & memory
host = RbConfig::CONFIG['host_os']
# Give VM 1/4 system memory & access to all CPU cores on the host
if host =~ /darwin/
auto_cpus = `sysctl -n hw.ncpu`.to_i
# sysctl returns Bytes and we need to convert to MB
auto_memory = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 4
elsif host =~ /linux/
auto_cpus = `nproc`.to_i
# meminfo shows KB and we need to convert to MB
auto_memory = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 4
else
# static defaults as fallback (Windows will get this)
auto_cpus = 2
auto_memory = 1024
end
# Clearer vars
settings_cpus = vconfig['vm_cpus']
settings_memory = vconfig['vm_memory']
# CPUs to use
if settings_cpus == "auto"
# Auto
vagrant_cpus = auto_cpus
else
# Explicit or default
vagrant_cpus = settings_cpus
end
# Memory to use
if settings_memory == "auto"
# Auto
vagrant_memory = auto_memory
else
# Explicit or default
vagrant_memory = settings_memory
end
if provider == "vmware_fusion"
# Configure VMWare setup.
config.vm.provider "vmware_fusion" do |v|
# Add VMWare box.
config.vm.box = "hashicorp/precise64"
v.gui = false
v.vmx["numvcpus"] = vagrant_cpus
v.vmx["memsize"] = vagrant_memory
end
else
# Configure VirtualBox setup.
config.vm.provider "virtualbox" do |v|
if vlad_os == "centos66"
# Add a Centos VirtualBox box
config.vm.box = "hansode/centos-6.6-x86_64"
elsif vlad_os == "ubuntu14"
# Add a Ubuntu VirtualBox box
config.vm.box = "ubuntu/trusty64"
else
# Add a Ubuntu VirtualBox box
config.vm.box = "ubuntu/precise64"
end
v.gui = false
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
# Set *provider* VM name (e.g. "myboxname_vlad")
v.name = boxname + "_vlad"
v.cpus = vagrant_cpus
v.memory = vagrant_memory
end
end
if synced_folder_type == 'nfs'
# Set up NFS drive.
nfs_setting = RUBY_PLATFORM =~ /darwin/ || RUBY_PLATFORM =~ /linux/
# Setup synced folder for site files
config.vm.synced_folder vconfig['host_synced_folder'], "/var/www/site/docroot", type: "nfs", create: true, id: "vagrant-webroot"
# Setup auxiliary synced folder
config.vm.synced_folder vconfig['aux_synced_folder'], "/var/www/site/vlad_aux", type: "nfs", create: true, id: "vagrant-aux"
elsif synced_folder_type == 'rsync'
# Setup synced folder for site files
config.vm.synced_folder vconfig['host_synced_folder'], "/var/www/site/docroot", type: "rsync", create: true, id: "vagrant-webroot"
# Setup auxiliary synced folder
config.vm.synced_folder vconfig['aux_synced_folder'], "/var/www/site/vlad_aux", type: "rsync", create: true, id: "vagrant-aux"
else
puts "Vlad requires the synced_folder setting to be one of the following:"
puts " - nfs"
puts " - rsync"
puts
puts "Please check your settings and try again."
exit
end
# SSH setup
# Vagrant >= 1.7.0 defaults to using a randomly generated RSA key.
# We need to disable this in order to pass the correct identity from host to guest.
config.ssh.insert_key = false
# Allow identities to be passed from host to guest.
config.ssh.forward_agent = true
# Run an Ansible playbook on setting the box up
if !File.exist?(vlad_hosts_file)
config.trigger.before [:up, :resume], :stdout => true, :force => true do
info "Executing 'up' setup trigger"
run 'ansible-playbook -i ' + boxipaddress + ', --ask-sudo-pass ' + vagrant_dir + '/vlad/playbooks/local_up.yml --extra-vars "local_ip_address=' + boxipaddress + '"'
end
end
# Run the halt/destroy playbook upon halting or destroying the box
if File.exist?(vlad_hosts_file)
config.trigger.before [:halt, :destroy], :stdout => true, :force => true do
info "Executing 'halt/destroy' trigger"
run 'ansible-playbook --ask-sudo-pass ' + vagrant_dir + '/vlad/playbooks/local_halt_destroy.yml --extra-vars "local_ip_address=' + boxipaddress + '"'
end
end
config.trigger.after :up, :stdout => true, :force => true do
info "Executing 'up' services trigger"
run 'ansible-playbook ' + vagrant_dir + '/vlad/playbooks/local_up_services.yml --extra-vars "local_ip_address=' + boxipaddress + '"'
end
config.trigger.after :up, :stdout => true, :force => true do
info 'Vlad setup complete!'
end
# Provision vagrant box with Ansible.
config.vm.provision "ansible" do |ansible|
ansible.playbook = vagrant_dir + "/vlad/playbooks/site.yml"
ansible.extra_vars = {ansible_ssh_user: 'vagrant'}
ansible.host_key_checking = false
ansible.raw_ssh_args = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o IdentitiesOnly=yes'
if vconfig['ansible_verbosity'] != ''
ansible.verbose = vconfig['ansible_verbosity']
end
end
# Run the custom Ansible playbook if the custom role exists
if File.exist?("../vlad_custom/tasks/main.yml")
config.vm.provision "ansible" do |ansible|
ansible.playbook = vagrant_dir + "/vlad/playbooks/site_custom.yml"
ansible.extra_vars = {ansible_ssh_user: 'vagrant'}
ansible.host_key_checking = false
ansible.raw_ssh_args = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o PasswordAuthentication=no -o IdentitiesOnly=yes'
ansible.limit = 'all'
if vconfig['ansible_verbosity'] != ''
ansible.verbose = vconfig['ansible_verbosity']
end
end
end
end