-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Vagrantfile
251 lines (212 loc) · 8.96 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
require 'yaml'
require 'json'
require_relative 'message'
require_relative 'find_project_dir'
VAGRANT_API_VERSION ||= '2'.freeze
VAGRANT_MIN_VERSION ||= '1.9.5'.freeze
ANSIBLE_MIN_VERSION ||= '2.4.0'.freeze
Vagrant.require_version ">= #{VAGRANT_MIN_VERSION}"
# Basic variables.
ui = Vagrant::UI::Colored.new
is_wsl = Vagrant::Util::Platform.wsl?
arguments = ARGV.join(' ')
project_root = find_project_dir.to_s
config_values = YAML.load_file(project_root + '/.cikit/config.yml')
vagrantfile_dir = File.dirname(__FILE__)
cikit_package_dir = File.realdirpath(vagrantfile_dir + '/..')
# The "site_url" will appear in "config.yml" via repository builder.
# See "scripts/init.yml".
project_hostname = config_values['site_url'].split('//').last
# Look up YAML or YML Platform.sh configuration.
%w[yaml yml].each do |extension|
platform_file_auth = project_root + '/.platform.app.json'
if Dir.glob("#{project_root}/*/.platform.app.#{extension}").length > 0
unless File.exist?(platform_file_auth)
raise Vagrant::Errors::VagrantError.new, <<-END.chomp.gsub(/^\s+\|/, '')
|The ".platform.app.#{extension}" indicates that the project has
|a Platform.sh integration and therefore you have to create
|the "#{platform_file_auth}" for local development.
|
|See https://cikit.tools/documentation/workflow/platformsh/#configure-hosting.
END
end
@platform_data_auth = JSON.parse(File.read(platform_file_auth))
%w[id token].each do |property|
if !@platform_data_auth.key?(property) || @platform_data_auth[property].empty?
raise Vagrant::Errors::VagrantError.new, <<-END.chomp.gsub(/^\s+\|/, '')
|The "#{property}" must not be empty in "#{platform_file_auth}".
END
end
end
break
end
end
# Check whether CIKit cannot be bootstrapped properly.
if (arguments.include?('up') || arguments.include?('provision')) && !arguments.include?('no-provision')
ANSIBLE = `bash -c "which ansible"`.chomp
if ANSIBLE.empty?
ui.say(:error, "You have to install Ansible #{ANSIBLE_MIN_VERSION} or better before continue.")
exit(1)
end
if Gem::Version.new(`#{ANSIBLE} --version | head -n1 | awk '{print $2}'`) < Gem::Version.new(ANSIBLE_MIN_VERSION)
ui.say(:error, "Installed version of Ansible must not be lower than #{ANSIBLE_MIN_VERSION}.")
exit(2)
end
end
# Ensure that configured IP ain't owned by other VM.
# This should be executed even when machine state is already "running" to always
# keep "hostsupdater" plugin updated. Otherwise it'll propose you to setup wrong IP.
if %w[up resume reload provision].include?(ARGV[0])
virtualbox = VagrantPlugins::ProviderVirtualBox::Driver::Meta.new
# The output looks like:
# "Windows7" {0c7dd8ed-8187-44c7-9d6e-c372305fb573}
# Iterate over the list of virtual machines.
virtualbox.execute('list', 'vms').scan(/".+?"/) do |vm_name|
# Trim quotes at the end and start.
vm_name = vm_name[1..-2]
# The output looks like:
# Name: /VirtualBox/GuestInfo/Net/1/V4/IP, value: 192.168.59.101, timestamp: 1489077258093051000, flags: TRANSIENT, TRANSRESET
# Property "/VirtualBox/GuestInfo/Net/0/V4/IP" contains internal IP, like "10.0.2.15".
vm_ip = virtualbox
.execute('guestproperty', 'enumerate', vm_name, '--patterns', '/VirtualBox/GuestInfo/Net/1/V4/IP')
.gsub(/.*value:\s+(.+?),.*/, '\1')
.chomp
if !vm_ip.empty? && vm_ip == config_values['vm']['ip'] && ![project_hostname, config_values['project']].include?(vm_name)
ip_sections = vm_ip.split('.')
# Increase last number of IP by 1.
ip_sections[-1] = ip_sections.last.to_i + 1
vm_ip = ip_sections.join('.')
ui.say(:warn, %([IP conflict resolved]: #{config_values['vm']['ip']} is used by the "#{vm_name}" so changed to #{vm_ip}.))
config_values['vm']['ip'] = vm_ip
end
end
# Smart "hostsupdater" plugin use configured IP of VM to clean up the hosts file, so we don't need to
# worry about this at the following commands: "destroy", "suspend", "reload", "halt".
elsif %w[destroy suspend reload halt].include?(ARGV[0])
end
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
if is_wsl
# Make sure that mounted shares will be within DrvFS.
# https://github.com/Microsoft/WSL/issues/1283#issuecomment-257107090
unless Vagrant::Util::Platform.wsl_path?(__dir__)
ui.say(:error, 'You cannot have a VM outside of the "/mnt" on WSL.')
exit(3)
end
# Under the WSL the "%SYSTEMROOT%\system32\drivers\etc\hosts" file must be modified
# in order to add IP aliases. Everybody knows this file is a system one and it's not
# available for modifications even when Bash is running with administrative privileges.
# Moreover, VirtualBox in WSL won't operate properly when Bash is in privileged mode.
# So, we simply do not need this plugin in WSL.
elsif !Vagrant.has_plugin?('vagrant-hostsupdater')
system('vagrant plugin install vagrant-hostsupdater')
end
# Automatically load custom provisioners.
Dir["#{vagrantfile_dir}/provisioners/*"].each do |file|
file += "/#{File.basename(file)}.rb"
require file if File.exist?(file)
end
Vagrant.configure(VAGRANT_API_VERSION) do |config|
project_aliases = []
if config.hostsupdater && File.exist?(project_root + '/.cikit/vars/web-server.yml')
web_server_config = YAML.load_file(project_root + '/.cikit/vars/web-server.yml')
if web_server_config.key?('web_server_vhosts')
project_aliases = []
web_server_config['web_server_vhosts'].each do |vhost|
if vhost.has_key?('dir') && !vhost['dir'].empty?
project_aliases << vhost['dir'] + '.' + project_hostname
end
end
config.hostsupdater.aliases = project_aliases
end
end
# Add an informative message about CIKit.
config.vm.post_up_message = cikit_message(
config_values['vm']['ip'],
project_hostname,
project_aliases,
is_wsl
)
# The "hostsupdater" plugin insert this value to the "hosts".
config.vm.hostname = project_hostname
# Handle port collisions.
config.vm.usable_port_range = (10200..10500)
# This Vagrant file is a third one which configures the VM. First one - within the
# project that has a bare minimum to use CIKit box. Second - within the box. Third -
# this one, included by second, which is within the box. The situation is that you
# can define a VM explicitly only via the first call of "Vagrant.configure()" and
# can't change this in other calls because the "default" has been already created
# and in use. So, uncommenting the next line will attempt to create a new, unneeded
# VM.
# config.vm.define(config.vm.hostname)
# Leaving the above line here for documentation purposes only.
config.vm.provider(:virtualbox) do |vb|
vb.name = config.vm.hostname
config_values['vm']['virtualbox']['modifyvm'].each do |key, value|
vb.customize(['modifyvm', :id, "--#{key}", value.to_s])
end
end
config.vm.network(
:private_network,
ip: config_values['vm']['ip'],
lxc__bridge_name: 'lxcbr0'
)
config_values['vm']['ports'].each do |port|
config.vm.network(
:forwarded_port,
host: port.to_i,
guest: port.to_i,
# https://github.com/mitchellh/vagrant/issues/8395#issuecomment-288379271
host_ip: '127.0.0.1',
auto_correct: true
)
end
# Mount the directory containing CIKit scripts additionally.
(config_values['vm']['folders'] ||= []).push(
'source' => "#{cikit_package_dir}/lib",
'target' => '/usr/local/share/cikit/lib',
# This directory is not available for mounting since CIKit is installed
# within the WSL on Windows. Only simple copy and paste. NFSd can't work
# there since "systemd" is not available. The "vboxsf" will be ended up
# with "Protocol error". So we go with simple "rsync" which will copy
# updated (if so) files every time the machine will be booted/rebooted.
# It's okay to do like this since there are executables only and they
# mustn't be modified.
'type' => is_wsl ? 'rsync' : 'nfs',
'nfs' => {
'udp' => true,
'version' => 3
}
)
config_values['vm']['folders'].each do |folder|
unless folder.key?('type')
# Use VBoxSF native system to mount folders on Windows WSL.
folder['type'] = is_wsl ? false : 'nfs'
end
config.vm.synced_folder(
folder['source'],
folder['target'],
id: folder['target'],
type: folder['type'],
nfs_udp: folder['nfs']['udp'],
nfs_version: folder['nfs']['version'].to_i.between?(2, 4) ? folder['nfs']['version'] : 3
)
end
config.vm.provision(
:cikit,
controller: "#{cikit_package_dir}/lib/cikit",
playbook: "#{cikit_package_dir}/scripts/provision"
)
# It's a Platform.sh-hosted project if the variable is defined.
if defined? @platform_data_auth
config.vm.provision(
:shell,
keep_color: true,
inline: "cikit #{cikit_package_dir}/lib/platformsh/setup.yml",
name: 'Setup Platform.sh integration',
run: 'always'
)
end
config.ssh.shell = 'sh'
config.ssh.insert_key = false
config.ssh.forward_agent = true
end