forked from Seravo/wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
504 lines (426 loc) · 17.8 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
require 'mkmf'
require 'fileutils'
require 'socket'
# Prevent logs from mkmf
module MakeMakefile::Logging
@logfile = File::NULL
end
DIR = File.dirname(__FILE__)
# Create .vagrant folder
unless File.exist?(File.join(DIR,'.vagrant'))
FileUtils.mkdir_p( File.join(DIR,'.vagrant') )
end
# Create config file
config_file = File.join(DIR, 'config.yml')
sample_file = File.join(DIR, 'config-sample.yml')
unless File.exists?(config_file)
# Use sample instead
FileUtils.copy sample_file, config_file
puts '==> default: config.yml was not found. Copying defaults from sample configs..'
end
site_config = YAML.load_file(config_file)
# Create private ip address in file
private_ip_file = File.join(DIR,'.vagrant','private.ip')
unless File.exists?(private_ip_file)
private_ip = "192.168.#{rand(255)}.#{rand(2..255)}"
File.write(private_ip_file, private_ip)
else
private_ip = File.open(private_ip_file, 'rb') { |file| file.read }
end
# Multiple public_network mappings need at least 1.7.4
Vagrant.require_version '>= 1.7.4'
# Try to use the vagrant-triggers plugin when using a vagrant version below 2.1.0 or
# if the user has self chosen to do so by setting the VAGRANT_USE_VAGRANT_TRIGGERS
# environment variable.
use_triggers_plugin = false
triggers_plugin_installed = Vagrant.has_plugin? 'vagrant-triggers'
if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('2.1.0')
# Vagrant core triggers are available, but the user can also opt-in to
# use the plugin if it is installed
if triggers_plugin_installed
use_triggers_plugin = true
if ENV['VAGRANT_USE_VAGRANT_TRIGGERS'].nil?
# Exit here because starting Vagrant will not work if VAGRANT_USE_VAGRANT_TRIGGERS is
# not defined but at the same time the vagrant-triggers plugin is installed.
STDERR.puts 'ERROR: can not use the Vagrant trigger functionality'
STDERR.puts 'Please uninstall the vagrant-triggers plugin to fix the problem:'
STDERR.puts '$ vagrant plugin uninstall vagrant-triggers'
exit 1
end
end
else
# No choice but to use the plugin, but we need to ensure that it is installed
use_triggers_plugin = true
if !triggers_plugin_installed
STDERR.puts 'ERROR: can not use the Vagrant trigger functionality'
STDERR.puts 'Please update Vagrant to version 2.1.0 or newer to fix the problem'
exit 1
end
end
Vagrant.configure('2') do |config|
# Use host-machine ssh-key so we can log into production
config.ssh.forward_agent = true
# Minimum box version requirement for this Vagrantfile revision
config.vm.box_version = ">= 20200130.0.0"
# Use precompiled box
config.vm.box = 'seravo/wordpress'
# Use the name of the box as the hostname
config.vm.hostname = site_config['name']
# Only use avahi if config has this
# development:
# avahi: true
if site_config['development']['avahi'] && has_internet? and is_osx?
# The box uses avahi-daemon to make itself available to local network
config.vm.network "public_network", bridge: [
"en0: Wi-Fi (AirPort)",
"en1: Wi-Fi (AirPort)",
"wlan0"
]
end
# Use random ip address for box
# This is needed for updating the /etc/hosts file
config.vm.network :private_network, ip: private_ip
config.vm.define "#{site_config['name']}-box"
if Vagrant.has_plugin? 'vagrant-hostsupdater'
# Remove hosts when suspending too
config.hostsupdater.remove_on_suspend = true
domains = get_domains(site_config)
config.hostsupdater.aliases = domains - [config.vm.hostname]
else
puts 'vagrant-hostsupdater missing, please install the plugin:'
puts 'vagrant plugin install vagrant-hostsupdater'
exit 1
end
# Disable default vagrant share
config.vm.synced_folder ".", "/vagrant", disabled: true
# Sync the folders
# We have tried using NFS but it's super slow compared to synced_folder
config.vm.synced_folder DIR, '/data/wordpress/', owner: 'vagrant', group: 'vagrant', mount_options: ['dmode=775', 'fmode=775']
# For Self-signed ssl-certificate
ssl_cert_path = File.join(DIR,'.vagrant','ssl')
unless File.exists? File.join(ssl_cert_path,'development.crt')
config.vm.provision :shell, :inline => "wp-generate-ssl"
end
# Add SSH Public Key from developer home folder into vagrant
if File.exists? File.join(Dir.home, ".ssh", "id_rsa.pub")
id_rsa_ssh_key_pub = File.read(File.join(Dir.home, ".ssh", "id_rsa.pub"))
config.vm.provision :shell, :inline => "echo '#{id_rsa_ssh_key_pub }' >> /home/vagrant/.ssh/authorized_keys && chmod 600 /home/vagrant/.ssh/authorized_keys"
end
if use_triggers_plugin
vagrant_triggers_plugin_triggers(config, site_config)
else
vagrant_triggers(config, site_config)
end
config.vm.provider 'virtualbox' do |vb|
# Give VM access to all cpu cores on the host
cpus = case RbConfig::CONFIG['host_os']
when /darwin/ then `sysctl -n hw.physicalcpu`.to_i
when /linux/ then `nproc`.to_i
else 2
end
# Customize memory in MB
vb.customize ['modifyvm', :id, '--memory', 1536]
vb.customize ['modifyvm', :id, '--cpus', cpus]
# Fix for slow external network connections
vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
vb.customize ['modifyvm', :id, '--natdnsproxy1', 'on']
end
end
##
# Run Vagrant triggers using the vagrant core trigger feature
##
def vagrant_triggers(vagrant_config, site_config)
vagrant_config.trigger.after :up do |trigger|
trigger.ruby do |env, machine|
#Run all system commands inside project root
Dir.chdir(DIR)
# Install packages with Composer
# run it locally if possible
if find_executable 'composer' and system "composer validate &>/dev/null"
system "composer install"
else # run in vagrant
run_command("composer install --working-dir=/data/wordpress", machine)
end
# Sync plugin files from production is so configured to do
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil and site_config['development']['pull_production_plugins'] == 'always'
run_command("wp-pull-production-plugins", machine)
end
# Sync theme files from production is so configured to do
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil and site_config['development']['pull_production_themes'] == 'always'
run_command("wp-pull-production-themes", machine)
end
# Database imports
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil && site_config['development']['pull_production_db'] != 'never' && (site_config['development']['pull_production_db'] == 'always' or confirm("Pull database from production?", false))
# Seravo customers are asked if they want to pull the production database here
# Install WordPress with defaults first so the database is not empty. Will automatically skip if WP was already installed.
run_command("wp core install --url=https://#{site_config['name']}.local --title=#{site_config['name'].capitalize}\
--admin_email=vagrant@#{site_config['name']}.local --admin_user=vagrant --admin_password=vagrant", machine)
# Pull production DB
run_command("wp-pull-production-db", machine)
elsif File.exists?(File.join(DIR,'.vagrant','shutdown-dump.sql'))
# Return the state where we last left if WordPress isn't currently installed
# First part in the command prevents overriding existing database
run_command("wp core is-installed --quiet &>/dev/null || wp-vagrant-import-db", machine)
elsif File.exists?(File.join(DIR,'vagrant-base.sql'))
run_command("wp db import /data/wordpress/vagrant-base.sql", machine)
else
# If nothing else was specified just install basic WordPress
run_command("wp core install --url=https://#{site_config['name']}.local --title=#{site_config['name'].capitalize}\
--admin_email=vagrant@#{site_config['name']}.local --admin_user=vagrant --admin_password=vagrant", machine)
notice "Installed default WordPress with user:vagrant password:vagrant"
end
# Don't activate git hooks, just notify them
if File.exists?( File.join(DIR,'.git', 'hooks', 'pre-commit') )
puts "If you want to use a git pre-commit hook please run 'wp-activate-git-hooks' inside the Vagrant box."
end
case RbConfig::CONFIG['host_os']
when /darwin/
# Do OS X specific things
# Trust the self-signed cert in keychain
ssl_cert_path = File.join(DIR,'.vagrant','ssl')
unless File.exists?(File.join(ssl_cert_path,'trust.lock'))
if File.exists?(File.join(ssl_cert_path,'development.crt')) and confirm "Trust the generated ssl-certificate in OS-X keychain?"
system "sudo security add-trusted-cert -d -r trustRoot -k '/Library/Keychains/System.keychain' '#{ssl_cert_path}/development.crt'"
# Write lock file so we can remove it too
touch_file File.join(ssl_cert_path,'trust.lock')
end
end
when /linux/
# Do linux specific things
end
# Attempt to use the asset proxy for production url defined in config.yml
run_command("wp-use-asset-proxy", machine)
# Restart nginx because the file system might not have been ready when the certificate was created
run_command("wp-restart-nginx", machine)
# Run 'vagrant up' customizer script if it exists
if File.exist?(File.join(DIR, 'vagrant-up-customizer.sh'))
notice 'Found vagrant-up-customizer.sh and running it ...'
Dir.chdir(DIR)
system 'sh ./vagrant-up-customizer.sh'
end
puts "\n"
notice "Documentation available at https://seravo.com/docs/"
notice "Visit your site: https://#{site_config['name']}.local"
end
end
vagrant_config.trigger.before [:halt, :destroy] do |trigger|
trigger.info = "Dump WordPress database into: .vagrant/shutdown-dump.sql"
trigger.ruby do |env, machine|
# dump database when closing vagrant
if vagrant_running?
begin
run_command("wp-vagrant-dump-db", machine)
rescue => e
notice "Couldn't dump database. Skipping..."
end
end
end
end
end
##
# Helper function to run a command in a vagrant machine by providing the command
# and a Vagrant::Machine object as parameters. Heavily influenced by
# https://github.com/emyl/vagrant-triggers/blob/master/lib/vagrant-triggers/dsl.rb
##
def run_command(cmd, machine)
exit_code = 1
good_exit_codes = (0..255).to_a
begin
exit_code = machine.communicate.sudo(cmd, :elevated => true, :good_exit => good_exit_codes) do |channel, data|
machine.ui.send(:info, data)
end
rescue => e
machine.ui.send(:error, e.message)
end
if !good_exit_codes.include? exit_code
exit exit_code
end
return exit_code
end
##
# Run Vagrant triggers in a vagrant-triggers plugin compatible format
##
def vagrant_triggers_plugin_triggers(vagrant_config, site_config)
# Some useful triggers for better workflow
vagrant_config.trigger.after :up do
#Run all system commands inside project root
Dir.chdir(DIR)
# Install packages with Composer
# run it locally if possible
if find_executable 'composer' and system "composer validate &>/dev/null"
system "composer install"
else # run in vagrant
run_remote "composer install --working-dir=/data/wordpress"
end
# Sync plugin files from production is so configured to do
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil and site_config['development']['pull_production_plugins'] == 'always'
run_remote "wp-pull-production-plugins"
end
# Sync theme files from production is so configured to do
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil and site_config['development']['pull_production_themes'] == 'always'
run_remote "wp-pull-production-themes"
end
# Database imports
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil && site_config['development']['pull_production_db'] != 'never' && (site_config['development']['pull_production_db'] == 'always' or confirm("Pull database from production?", false))
# Seravo customers are asked if they want to pull the production database here
# Install WordPress with defaults first so the database is not empty. Will automatically skip if WP was already installed.
run_remote("wp core install --url=https://#{site_config['name']}.local --title=#{site_config['name'].capitalize}\
--admin_email=vagrant@#{site_config['name']}.local --admin_user=vagrant --admin_password=vagrant")
# Pull production DB
run_remote "wp-pull-production-db"
elsif File.exists?(File.join(DIR,'.vagrant','shutdown-dump.sql'))
# Return the state where we last left if WordPress isn't currently installed
# First part in the command prevents overriding existing database
run_remote "wp core is-installed --quiet &>/dev/null || wp-vagrant-import-db"
elsif File.exists?(File.join(DIR,'vagrant-base.sql'))
run_remote "wp db import /data/wordpress/vagrant-base.sql"
else
# If nothing else was specified just install basic WordPress
run_remote("wp core install --url=https://#{site_config['name']}.local --title=#{site_config['name'].capitalize}\
--admin_email=vagrant@#{site_config['name']}.local --admin_user=vagrant --admin_password=vagrant")
notice "Installed default WordPress with user:vagrant password:vagrant"
end
# Init git if it doesn't exist
if not File.exists?( File.join(DIR,".git") ) and confirm "There's no git repository. Should we create one?"
system "git init ."
end
# Don't activate git hooks, just notify them
if File.exists?( File.join(DIR,'.git', 'hooks', 'pre-commit') )
puts "If you want to use a git pre-commit hook please run 'wp-activate-git-hooks' inside the Vagrant box."
end
case RbConfig::CONFIG['host_os']
when /darwin/
# Do OS X specific things
# Trust the self-signed cert in keychain
ssl_cert_path = File.join(DIR,'.vagrant','ssl')
unless File.exists?(File.join(ssl_cert_path,'trust.lock'))
if File.exists?(File.join(ssl_cert_path,'development.crt')) and confirm "Trust the generated ssl-certificate in OS-X keychain?"
system "sudo security add-trusted-cert -d -r trustRoot -k '/Library/Keychains/System.keychain' '#{ssl_cert_path}/development.crt'"
# Write lock file so we can remove it too
touch_file File.join(ssl_cert_path,'trust.lock')
end
end
when /linux/
# Do linux specific things
end
# Attempt to use the asset proxy for production url defined in config.yml
run_remote "wp-use-asset-proxy"
# Restart nginx because the file system might not have been ready when the certificate was created
run_remote "wp-restart-nginx"
# Run 'vagrant up' customizer script if it exists
if File.exist?(File.join(DIR, 'vagrant-up-customizer.sh'))
notice 'Found vagrant-up-customizer.sh and running it ...'
Dir.chdir(DIR)
system 'sh ./vagrant-up-customizer.sh'
end
puts "\n"
notice "Documentation available at https://seravo.com/docs/"
notice "Visit your site: https://#{site_config['name']}.local"
end
vagrant_config.trigger.before :halt do
# dump database when closing vagrant
dump_wordpress_database
end
vagrant_config.trigger.before :destroy do
# dump database when closing vagrant
dump_wordpress_database
end
end
##
# Custom helpers
##
def notice(text)
puts "==> trigger: #{text}"
end
##
# Dump database into file in vagrant
##
def dump_wordpress_database
if vagrant_running?
begin
notice "dumping the database into: .vagrant/shutdown-dump.sql"
run_remote "wp-vagrant-dump-db"
rescue => e
notice "Couldn't dump database. Skipping..."
end
end
end
##
# Create empty file
##
def touch_file(path)
File.open(path, "w") {}
end
##
# Generate /etc/hosts domain additions
##
def get_domains(config)
unless config['development'].nil?
domains = config['development']['domains'] || []
domains << config['development']['domain'] unless config['development']['domain'].nil?
else
domains = []
end
# The main domain
domains << config['name']+".local"
# Add domain names for included applications for easier usage
subdomains = %w( www webgrind adminer mailcatcher browsersync info )
subdomains.each do |domain|
domains << "#{domain}.#{config['name']}.local"
end
domains.uniq #remove duplicates
end
##
# Get boolean answer for question string
##
def confirm(question,default=true)
if default
default = "yes"
else
default = "no"
end
confirm = nil
until ["Y","N","YES","NO",""].include?(confirm)
print "#{question} (#{default}): "
confirm = STDIN.gets.chomp
if (confirm.nil? or confirm.empty?)
confirm = default
end
confirm.strip!
confirm.upcase!
end
if confirm.empty? or confirm == "Y" or confirm == "YES"
return true
end
return false
end
##
# This is quite hacky but for my understanding the only way to check if the current box state
##
def vagrant_running?
system("vagrant status --machine-readable | grep state,running --quiet")
end
##
# On OS X we can use a few more features like zeroconf (discovery of .local addresses in local network)
##
def is_osx?
RbConfig::CONFIG['host_os'].include? 'darwin'
end
##
# Modified from: https://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/
# Returns true/false
##
def has_internet?
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
begin
UDPSocket.open { |s| s.connect '8.8.8.8', 1 }
return true
rescue Errno::ENETUNREACH
return false # Network is not reachable
end
ensure
Socket.do_not_reverse_lookup = orig
end