-
Notifications
You must be signed in to change notification settings - Fork 8
/
Vagrantfile
283 lines (242 loc) · 9.84 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
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
settings = YAML::load(File.read("config.yaml"))
post_script = "post"
aliases = "aliases"
script_dir = File.expand_path("scripts", File.dirname(__FILE__))
supported_php = ["5", "5.6", "7.0", "7.1", "7.2"]
supported_ubuntu = ["14.04", "16.04"]
supported_webservers = ["apache", "nginx"]
supported_mysql = ["5.5", "5.6", "5.7"]
default_chromedriver = "latest"
default_php = "7.2"
default_selenium = "3.9.1"
default_ubuntu = "16.04"
default_webserver = "nginx"
if settings.has_key?("ubuntu")
unless supported_ubuntu.include?(settings["ubuntu"])
abort("Web server #{settings["webserver"]} not recognised. Only #{supported_webservers} are currenly supported with DevBox.")
end
end
if settings.has_key?("webserver")
unless supported_webservers.include?(settings["webserver"])
abort("Web server #{settings["webserver"]} not recognised. Only #{supported_webservers} are currenly supported with DevBox.")
end
webserver = settings["webserver"]
else
webserver = default_webserver
end
hostname = settings["hostname"] ||= "devbox"
name = settings["name"] ||= "devbox"
# Default ports for forwarding
default_ports = {
80 => 8000,
3306 => 33060
}
Vagrant.configure("2") do |config|
# Set the VM provider
ENV['VAGRANT_DEFAULT_PROVIDER'] = settings["provider"] ||= "virtualbox"
# Create bash aliases
if File.exists? aliases
config.vm.provision "file", source: aliases, destination: "~/.bash_aliases"
end
# Check the guest additions when booting this machine
if Vagrant.has_plugin?("vagrant-vbguest")
unless settings.has_key?("vbguest") && settings["vbguest"] == true
config.vbguest.auto_update = false
end
end
# Enable SSH agent forwarding
config.ssh.forward_agent = true
# Configure the vagrant box
config.vm.define name
config.vm.hostname = hostname
config.vm.box = settings["box"] ||= "damianlewis/ubuntu-#{settings["ubuntu"] ||= default_ubuntu}-#{webserver == "apache" ? "lamp" : "lemp"}"
config.vm.box_version = settings["version"] ||= ">= 1.0"
# Configure default ports
default_ports.each do |guest, host|
config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
end
# Configure additional ports to forward
if settings.has_key?("ports")
settings["ports"].each do |port|
config.vm.network "forwarded_port", guest: port["guest"], host: port["host"], auto_correct: true
end
end
# Configure networks
if settings.has_key?("networks")
settings["networks"].each do |network|
if network.has_key?("type")
if network["type"] == "private"
if network.has_key?("ip")
config.vm.network "private_network", ip: network["ip"]
else
config.vm.network "private_network", type: "dhcp"
end
end
if network["type"] == "bridged"
if network.has_key?("ip")
config.vm.network "public_network", ip: network["ip"], bridge: network["bridge"] ||= nil
else
config.vm.network "public_network", bridge: network["bridge"] ||= nil
end
end
end
end
else
config.vm.network "private_network", type: "dhcp"
end
# Configure VirtualBox settings
config.vm.provider "virtualbox" do |vb|
vb.name = name
vb.customize ["modifyvm", :id, "--memory", settings["memory"] ||= "2048"]
vb.customize ["modifyvm", :id, "--cpus", settings["cpus"] ||= "1"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", settings["natdnshostresolver"] ||= "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", settings["natdnsproxy"] ||= "on"]
vb.customize ["modifyvm", :id, "--ostype", "Ubuntu_64"]
if settings.has_key?("gui") && settings["gui"] == true
vb.gui = true
end
end
# Configure shared folders
if settings.has_key?("folders")
settings["folders"].each do |folder|
if folder["type"] == "nfs"
config.vm.synced_folder folder["map"], folder["to"], type: "nfs", mount_options: ['actimeo=1', 'nolock']
else
config.vm.synced_folder folder["map"], folder["to"]
end
end
else
config.vm.synced_folder ".", "/vagrant"
end
# Install additional Ubuntu packages
if settings.has_key?("apt-packages")
settings["apt-packages"].each do |package|
config.vm.provision "shell" do |s|
s.name = "Installing APT package [#{package}]"
s.path = "#{script_dir}/install-apt-package"
s.args = package
end
end
config.vm.provision "shell" do |s|
s.name = "Restarting PHP-FPM"
s.path = "#{script_dir}/restart-php"
s.args = supported_php
end
end
# Install global Composer packages
if settings.has_key?("composer-packages")
settings["composer-packages"].each do |package|
config.vm.provision "shell" do |s|
s.name = "Installing Composer package [#{package}]"
s.path = "#{script_dir}/install-composer-package"
s.args = package
s.privileged = false
end
end
end
# Install components for browser testing
if settings.has_key?("browser-testing") && settings["browser-testing"] == true
config.vm.provision "shell" do |s|
s.name = "Installing Java JRE"
s.path = "#{script_dir}/install-java"
s.args = settings["java"] ||= nil
end
config.vm.provision "shell" do |s|
s.name = "Installing Selenium Server"
s.path = "#{script_dir}/install-selenium"
s.args = settings["selenium"] ||= nil
end
config.vm.provision "shell" do |s|
s.name = "Installing Google Chrome"
s.path = "#{script_dir}/install-google-chrome"
end
config.vm.provision "shell" do |s|
s.name = "Installing ChromeDriver"
s.path = "#{script_dir}/install-chromedriver"
s.args = settings["chromedriver"] ||= nil
end
config.vm.provision "shell" do |s|
s.name = "Installing Xvfb"
s.path = "#{script_dir}/install-xvfb"
end
config.vm.provision "shell" do |s|
s.name = "Adding Selenium Server bash commands"
s.path = "#{script_dir}/add-selenium-commands"
end
end
# Fix for issue with Nginx 1.13.12 not starting on damianlewis/ubuntu-16.04-lemp box
if config.vm.box == "damianlewis/ubuntu-16.04-lemp"
config.vm.provision "shell", run: "always" do |s|
s.name = "Starting Nginx"
s.inline = "service nginx restart > /dev/null 2>&1"
end
end
# Create all the configured sites
if settings.has_key?("sites")
settings["sites"].each do |site|
config.vm.provision "shell" do |s|
s.name = "Creating SSL certificate for [#{site["url"]}]"
s.path = "#{script_dir}/create-ssl-certificate"
s.args = [site["url"]]
end
if site.has_key?("php")
unless supported_php.include?(site["php"])
abort("PHP version #{site["php"]} not recognised. Only versions #{supported_php} are currenly supported with DevBox.")
end
end
config.vm.provision "shell" do |s|
s.name = "Creating site [#{site["url"]}] with PHP #{site["php"] ||= default_php}"
s.path = "#{script_dir}/serve-#{webserver}"
s.args = [site["url"], site["root"], site["php"] ||= default_php]
end
end
config.vm.provision "shell" do |s|
s.name = "Restarting #{webserver.capitalize}"
s.inline = "service $1 restart > /dev/null 2>&1"
s.args = [webserver == "apache" ? "apache2" : "nginx"]
end
end
# Install alternatative version of MySQL
if settings.has_key?("mysql")
unless supported_mysql.include?(settings["mysql"])
abort("MySQL version #{settings["mysql"]} not recognised. Only versions #{supported_mysql} are currenly supported with DevBox.")
end
config.vm.provision "shell" do |s|
s.name = "Installing MySQL #{settings["mysql"]}"
s.path = "#{script_dir}/install-mysql"
s.args = settings["mysql"]
end
end
# Create all the configured MySQL databases
if settings.has_key?("databases")
settings["databases"].each do |db|
config.vm.provision "shell" do |s|
s.name = "Creating MySQL database [#{db["name"]}]"
s.path = "#{script_dir}/create-mysql"
s.args = [db["name"], db["user"] ||= "damianlewis", db["password"] ||= "secret"]
end
end
config.vm.provision "shell" do |s|
s.name = "Restarting MySQL"
s.inline = "service mysql restart > /dev/null 2>&1"
end
end
# Set alternative PHP version for CLI
if settings.has_key?("php-cli")
unless supported_php.include?(settings["php-cli"])
abort("PHP version #{settings["php-cli"]} not recognised. Only versions #{supported_php} are currenly supported with DevBox.")
end
config.vm.provision "shell" do |s|
s.name = "Setting alternative PHP CLI to PHP #{settings["php-cli"]}"
s.path = "#{script_dir}/set-php-cli"
s.args = settings["php-cli"]
end
end
# Run post provisioning script
if File.exists? post_script
config.vm.provision "shell", path: post_script
end
end