forked from Vincit/wordpress-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
352 lines (288 loc) · 10.9 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
# -*- 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'
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 = ">= 20160718.15.0122"
# 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=776', '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
# Do vagrant specific scripts here
unless site_config['name'].nil?
config.vm.provision :shell, :inline => "sudo wp-vagrant-activation"
end
# Some useful triggers for better workflow
if Vagrant.has_plugin? 'vagrant-triggers'
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
# Database imports
if site_config['production'] != nil && site_config['production']['ssh_port'] != nil and confirm("Pull database from production?",false)
# Seravo customers are asked if they want to pull the production database here
# Install WordPress with defaults first
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")
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
# Activate githooks for testing, etc...
git_hooks_dir = File.join(DIR,".git","hooks")
unless ( Vagrant::Util::Platform.windows? or File.exists?(File.join(git_hooks_dir,'.activated')) )
if confirm "Activate git hooks in scripts/hooks?"
# symlink git on remote
run_remote "wp-activate-git-hooks"
# create hook folder (if not exists) and symlink git on host machine
Dir.mkdir git_hooks_dir unless File.exists? git_hooks_dir
Dir.chdir git_hooks_dir
# Symlink all files from scripts to here
system "ln -sf ../../scripts/hooks/* ."
FileUtils.chmod_R(0755,File.join(DIR,'scripts','hooks'))
# Touch .activated file so that we don't have to do this again
touch_file( File.join(git_hooks_dir,'.activated'))
end
end
case RbConfig::CONFIG['host_os']
when /darwin/
# Do OS X specific things
# Trust the self-signed cert in keychain
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 &> /dev/null"
# Restart nginx because the file system might not have been ready when the certificate was created
run_remote "wp-restart-nginx &> /dev/null"
# 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
config.trigger.before :halt do
# dump database when closing vagrant
dump_wordpress_database
end
config.trigger.before :destroy do
# dump database when destroying vagrant
dump_wordpress_database
end
else
puts 'vagrant-triggers missing, please install the plugin:'
puts 'vagrant plugin install vagrant-triggers'
exit 1
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.ncpu`.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
##
# 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)
confirm = ask "#{question} (#{default}): "
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