/
Vagrantfile
333 lines (305 loc) · 13 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'pathname'
require 'socket'
require 'timeout'
require 'yaml'
# is the config file present? if not, halt execution as there is nothing to do!
raise Vagrant::Errors::VagrantError.new, "ERROR: config.yml file missing." if not Pathname('config.yml').exist?
# setup settings variable, dnsserver and some defaults
settings = YAML.load_file('config.yml')
# dnsServer = `scutil --dns|awk '$0 ~ /nameserver/ {printf $3; exit}'`
dnsServer = '8.8.8.8'
defaults = {'timezone'=> 'America/Chicago',
'hostname'=> Socket.gethostname + '.dev',
'webroot'=> '/var/www/hid',
'aliases' => 'www.' + Socket.gethostname + '.dev',
}
# sanity checks to the yaml configuration file
def checkPlugin(pluginName)
unless Vagrant.has_plugin?(pluginName)
raise Vagrant::Errors::VagrantError.new, pluginName + ' plugin missing. Install it with "sudo vagrant plugin install ' + pluginName + '"'
end
end
['vagrant-cachier', 'vagrant-hostsupdater', 'vagrant-triggers'].each do |plugin|
checkPlugin(plugin)
end
def checkErrors setting
if setting['value'] == nil
raise Vagrant::Errors::VagrantError.new, "Configuration Error: #{setting['name']} not defined in config.yml file, setup cannot continue"
end
end
[
{'name' => 'database', 'value' => settings['database']},
{'name' => 'database name', 'value' => settings['database']['name']},
{'name' => 'database user', 'value' => settings['database']['user']},
{'name' => 'database pass', 'value' => settings['database']['pass']},
{'name' => 'database file', 'value' => settings['database']['file']},
{'name' => 'drush alias', 'value' => settings['drushalias']},
].each do |item|
checkErrors item
end
def checkWarnings settings, setting, default = nil
if settings[setting] == nil and default == nil
puts 'Warning: ' + setting + ' not defined in config.yml file. The setup should work, but are you sure this is what you want?'
elsif settings[setting] == nil and default != nil
puts 'Warning: ' + setting + ' not defined in config.yml file, assuming ' + default
settings[setting] = default
end
end
[
{'setting' => 'languages', 'default' => nil},
{'setting' => 'settingsphp', 'default' => nil},
{'setting' => 'shares', 'default' => nil},
{'setting' => 'aliases', 'default' => defaults['aliases']},
{'setting' => 'hostname', 'default' => defaults['hostname']},
{'setting' => 'timezone', 'default' => defaults['timezone']},
{'setting' => 'webroot', 'default' => defaults['webroot']},
].each do |item|
checkWarnings settings, item['setting'], item['default']
end
# output helper message if developer is re-provisioning the box
if ARGV[0] == 'provision' and ENV['REDODB'] == nil
puts "\nYou can reprovision the database by issuing the command below:"
puts "REDODB='yes' vagrant provision\n\n"
elsif ARGV[0] == 'provision' and ENV['REDODB'] == 'yes'
puts "\nVagrant will make puppet reprovision the database.\n\n"
end
# helper functions to manipulate the settings.php file
def resetSettingsFile settings, vagstring
settingsfile = settings['local'].gsub('~', ENV['HOME']) + '/sites/default/settings.php'
removeString = vagstring.gsub("\n",'')
if File.file?settingsfile
puts 'Restoring settings.php file'
File.chmod(0666, settingsfile)
settingslines = File.open(settingsfile,'r').readlines()
writefile = File.open(settingsfile,'w+')
settingslines.each do |line|
writefile.write(line) if line !~ /#{removeString}/
end
writefile.close()
end
end
def adjustSettingsFile settings, vagstring
settingsfile = settings['local'].gsub('~', ENV['HOME']) + '/sites/default/settings.php'
if not File.file?settingsfile
defsettingsfile = settings['local'].gsub('~', ENV['HOME']) + '/sites/default/default.settings.php'
FileUtils.cp(defsettingsfile, settingsfile)
else
resetSettingsFile settings, vagstring
end
puts 'Adjusting settings.php file'
File.chmod(0666, settingsfile)
settingslines = File.open(settingsfile,'r').readlines()
writefile = File.open(settingsfile,'w+')
settingslines.each do |line|
writefile.write(line) if line !~ /#{vagstring}/
end
if settings['settingsphp'] != nil
settings['settingsphp'].each do |settingline|
writefile.write(settingline.gsub('USER', ENV['USER'].upcase) + vagstring)
end
end
defaultDB = "$databases['default']['default'] = array("
defaultDB += "'driver' => 'mysql',"
defaultDB += "'database' => '" + settings['database']['name'] + "',"
defaultDB += "'username' => '" + settings['database']['user'] + "',"
defaultDB += "'password' => '" + settings['database']['pass'] + "',"
defaultDB += "'host' => '127.0.0.1',"
defaultDB += "'prefix' => '',"
defaultDB += ");" + vagstring
writefile.write(defaultDB)
if settings['languages'] != nil
settings['languages'].each do |lang|
lang.each do |item|
line = "$conf['language_domains']['#{item[0]}'] = 'http://#{item[1]}'; #{vagstring}"
writefile.write(line)
end
end
end
writefile.close()
end
def resetDrushAliasFile settings, vagstring
settingsfile = '~/.drush/vagrant.aliases.drushrc.php'.gsub('~', ENV['HOME'])
removeString = vagstring.gsub("\n",'') + '-' + settings['drushalias']
if File.file?settingsfile
tagstring = vagstring.gsub("\n",'') + "-" + settings['drushalias'] + "\n"
puts 'Restoring drush alias file'
File.chmod(0666, settingsfile)
settingslines = File.open(settingsfile,'r').readlines()
writefile = File.open(settingsfile,'w+')
settingslines.each do |line|
writefile.write(line) if line !~ /#{tagstring}/
end
writefile.close()
end
end
def adjustDrushAliasFile settings, vagstring
resetDrushAliasFile settings, vagstring
puts 'Adjusting drush alias file'
settingsfile = '~/.drush/vagrant.aliases.drushrc.php'.gsub('~', ENV['HOME'])
if not File.file?settingsfile
File.open(settingsfile, 'w+') do |writefile|
writefile.puts '<?php'
end
end
File.chmod(0666, settingsfile)
if settings['drushalias'] != nil
tagstring = vagstring.gsub("\n",'') + "-" + settings['drushalias'] + "\n"
File.open(settingsfile,'a+') do |writefile|
writefile.puts "$aliases['#{settings['drushalias']}'] = array( #{tagstring}"
writefile.puts " 'root' => '#{settings['webroot']}', #{tagstring}"
writefile.puts " 'uri' => 'http://#{settings['hostname']}', #{tagstring}"
writefile.puts " 'remote-host' => '#{settings['hostname']}', #{tagstring}"
writefile.puts "); #{tagstring}"
end
end
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|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = settings['box'].nil? ? "centos56" : settings['box']
config.vm.box_url = settings['box_url'].nil? ? "https://dl.dropbox.com/u/7196/vagrant/CentOS-56-x64-packages-puppet-2.6.10-chef-0.10.6.box" : settings['box_url']
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :box
config.cache.auto_detect = false
# config.cache.enable :yum
# config.cache.enable :npm
# config.cache.synced_folder_opts = {
# type: :nfs,
# mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
# }
end
# Disable automatic box Update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# 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.
# config.vm.network "forwarded_port", guest: 80, host: 8080
if settings['ports'] != nil
settings['ports'].each do |item|
config.vm.network "forwarded_port", guest: item['vm'], host: item['local']
end
end
# 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"
config.vm.hostname = settings['hostname']
if settings['languages'] != nil
settings['languages'].each do |item|
# settings['aliases'].merge!(item[1])
item.each do |lang|
settings['aliases'].concat([lang[1]])
end
end
end
config.hostsupdater.aliases = settings['aliases']
# config.hostsupdater.remove_on_suspend = true
config.vm.network "private_network", ip: settings['hostip']
# 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"
# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true
# 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"
config.vm.synced_folder ".", "/vagrant", :mount_options => ["dmode=777,fmode=766"]
settings['shares'].each do |item|
if item['vm'] == settings['webroot']
settings['local'] = item['local'] #if item['vm'] == settings['webroot']
config.vm.synced_folder item['local'], item['vm'], type: 'rsync', rsync__exclude: ['.git/','*/bower_components','*/node_modules','*/files'], rsync__auto: true
#config.vm.synced_folder item['local'], item['vm'], mount_options: ["dmode=777,fmode=766,uid=48,gid=48"]
else
config.vm.synced_folder item['local'], item['vm'], mount_options: ["dmode=777,fmode=766,uid=48,gid=48"]
end
end
# 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|
# # Don't boot with headless mode
# vb.gui = true
#
# # Use VBoxManage to customize the VM. For example to change memory:
vb.memory = 4096
vb.cpus = 2
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end
#
# View the documentation for the provider you're using for more
# information on available options.
config.vm.provision :shell, :inline => "rvm reset; if [ ! `rpm -q puppet | grep -E '^puppet-3'` ]; then echo search home.local > /etc/resolv.conf; echo 'nameserver #{dnsServer}' >> /etc/resolv.conf; yum update -y puppet; yum update -y rubygem-json; fi"
# Enable provisioning with Puppet stand alone. Puppet manifests
# are contained in a directory path relative to this Vagrantfile.
# You will need to create the manifests directory and a manifest in
# the file default.pp in the manifests_path directory.
#
config.vm.provision "puppet" do |puppet|
# puppet.manifests_path = "manifests"
# puppet.manifest_file = "default.pp"
puppet.module_path = "modules"
puppet.facter = {
"vagrant" => "1",
"dnsserver" => dnsServer,
"zonefile" => settings['timezone'],
"webroot" => settings['webroot'],
"webrootparsed" => settings['webroot'].gsub('/','\/'),
"webhost" => settings['hostname'],
}
settings['database'].each do |item|
puppet.facter.merge!({"db#{item[0]}" => item[1]})
end
if puppet.facter['dbfile'] != nil
dbfile = "data/#{puppet.facter['dbfile']}"
abort "Database file #{dbfile} not found." if not Pathname(dbfile).exist?
end
if settings['languages'] != nil
puppet.facter['languages'] = settings['languages'].to_yaml
end
if settings['aliases'] != nil
puppet.facter['serveralias'] = 'ServerAlias ' + settings['aliases'].join(' ')
end
if ENV['REDODB'] == 'yes'
puppet.facter['redodb'] = 'y'
end
if settings['themename'] != nil
puppet.facter['themename'] = settings['themename']
end
keyfile = '~/.ssh/id_rsa.pub'.gsub('~', ENV['HOME'])
if File.file?keyfile
puppet.facter['ssh_key'] = File.open(keyfile, 'rb').read
end
end
config.trigger.before :provision do
File.delete('data/insertlanguages.sql') if File.exist?('data/insertlanguages.sql')
end
# provisioning triggers stuff below
vagstring = ' ## vagrant-provisioner' + "\n"
config.trigger.before [:provision, :up, :resume] do
puts 'Running before provision triggers'
adjustSettingsFile settings, vagstring
adjustDrushAliasFile settings, vagstring
puts 'Synching changed files'
`vagrant rsync`
end
config.trigger.before [:destroy, :halt, :suspend] do
puts 'Running before destroy triggers'
resetSettingsFile settings, vagstring
resetDrushAliasFile settings, vagstring
puts 'Synching changed files'
`vagrant rsync`
end
end