Skip to content
This repository was archived by the owner on Feb 13, 2023. It is now read-only.

Commit ae4c803

Browse files
committed
Split Vagrantfile logic into separate lib file
1 parent b25caf7 commit ae4c803

File tree

2 files changed

+112
-78
lines changed

2 files changed

+112
-78
lines changed

Vagrantfile

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# vi: set ft=ruby :
33
VAGRANTFILE_API_VERSION = '2' unless defined? VAGRANTFILE_API_VERSION
44

5+
require './lib/drupalvm/vagrant'
6+
57
# Absolute paths on the host machine.
68
host_drupalvm_dir = File.dirname(File.expand_path(__FILE__))
79
host_project_dir = ENV['DRUPALVM_PROJECT_ROOT'] || host_drupalvm_dir
@@ -14,58 +16,14 @@ guest_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "/vagrant/#{ENV['DRUPALVM_CONFIG
1416

1517
drupalvm_env = ENV['DRUPALVM_ENV'] || 'vagrant'
1618

17-
# Cross-platform way of finding an executable in the $PATH.
18-
def which(cmd)
19-
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
20-
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
21-
exts.each do |ext|
22-
exe = File.join(path, "#{cmd}#{ext}")
23-
return exe if File.executable?(exe) && !File.directory?(exe)
24-
end
25-
end
26-
nil
27-
end
28-
29-
def get_ansible_version(exe)
30-
/^[^\s]+ (.+)$/.match(`#{exe} --version`) { |match| return match[1] }
31-
end
32-
33-
def walk(obj, &fn)
34-
if obj.is_a?(Array)
35-
obj.map { |value| walk(value, &fn) }
36-
elsif obj.is_a?(Hash)
37-
obj.each_pair { |key, value| obj[key] = walk(value, &fn) }
38-
else
39-
obj = yield(obj)
40-
end
41-
end
42-
43-
require 'yaml'
44-
# Load default VM configurations.
45-
vconfig = YAML.load_file("#{host_drupalvm_dir}/default.config.yml")
46-
# Use optional config.yml and local.config.yml for configuration overrides.
47-
['config.yml', 'local.config.yml', "#{drupalvm_env}.config.yml"].each do |config_file|
48-
if File.exist?("#{host_config_dir}/#{config_file}")
49-
optional_config = YAML.load_file("#{host_config_dir}/#{config_file}")
50-
vconfig.merge!(optional_config) if optional_config
51-
end
52-
end
53-
54-
# Replace jinja variables in config.
55-
vconfig = walk(vconfig) do |value|
56-
while value.is_a?(String) && value.match(/{{ .* }}/)
57-
value = value.gsub(/{{ (.*?) }}/) { vconfig[Regexp.last_match(1)] }
58-
end
59-
value
60-
end
61-
62-
Vagrant.require_version ">= #{vconfig['drupalvm_vagrant_version_min']}"
19+
vconfig = load_config([
20+
"#{host_drupalvm_dir}/default.config.yml",
21+
"#{host_config_dir}/config.yml",
22+
"#{host_config_dir}/local.config.yml",
23+
"#{host_config_dir}/#{drupalvm_env}.config.yml",
24+
])
6325

64-
ansible_bin = which('ansible-playbook')
65-
ansible_version = Gem::Version.new(get_ansible_version(ansible_bin)) if ansible_bin
66-
ansible_version_min = Gem::Version.new(vconfig['drupalvm_ansible_version_min'])
67-
68-
provisioner = ansible_bin && !vconfig['force_ansible_local'] ? :ansible : :ansible_local
26+
provisioner = vconfig['force_ansible_local'] ? :ansible_local : get_provisioner
6927
if provisioner == :ansible
7028
playbook = "#{host_drupalvm_dir}/provisioning/playbook.yml"
7129
config_dir = host_config_dir
@@ -74,9 +32,9 @@ else
7432
config_dir = guest_config_dir
7533
end
7634

77-
if provisioner == :ansible && ansible_version < ansible_version_min
78-
raise Vagrant::Errors::VagrantError.new, "You must update Ansible to at least #{ansible_version_min} to use this version of Drupal VM."
79-
end
35+
# Verify version requirements.
36+
require_ansible_version ">= #{vconfig['drupalvm_ansible_version_min']}"
37+
Vagrant.require_version ">= #{vconfig['drupalvm_vagrant_version_min']}"
8038

8139
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
8240
# Networking configuration.
@@ -100,32 +58,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
10058
# Vagrant box.
10159
config.vm.box = vconfig['vagrant_box']
10260

103-
if vconfig.include?('vagrant_post_up_message')
104-
config.vm.post_up_message = vconfig['vagrant_post_up_message']
105-
else
106-
config.vm.post_up_message = 'Your Drupal VM Vagrant box is ready to use!'\
107-
"\n* Visit the dashboard for an overview of your site: http://dashboard.#{vconfig['vagrant_hostname']} (or http://#{vconfig['vagrant_ip']})"\
108-
"\n* You can SSH into your machine with `vagrant ssh`."\
109-
"\n* Find out more in the Drupal VM documentation at http://docs.drupalvm.com"
110-
end
61+
# Display an introduction message after `vagrant up` and `vagrant provision`.
62+
config.vm.post_up_message = vconfig.fetch('vagrant_post_up_message', get_default_post_up_message(vconfig))
11163

11264
# If a hostsfile manager plugin is installed, add all server names as aliases.
113-
aliases = []
114-
if vconfig['drupalvm_webserver'] == 'apache'
115-
vconfig['apache_vhosts'].each do |host|
116-
aliases.push(host['servername'])
117-
aliases.concat(host['serveralias'].split) if host['serveralias']
118-
end
119-
else
120-
vconfig['nginx_hosts'].each do |host|
121-
aliases.concat(host['server_name'].split)
122-
aliases.concat(host['server_name_redirect'].split) if host['server_name_redirect']
123-
end
124-
end
125-
aliases = aliases.uniq - [config.vm.hostname, vconfig['vagrant_ip']]
126-
# Remove wildcard subdomains.
127-
aliases.delete_if { |vhost| vhost.include?('*') }
128-
65+
aliases = get_vhost_aliases(vconfig) - [config.vm.hostname]
12966
if Vagrant.has_plugin?('vagrant-hostsupdater')
13067
config.hostsupdater.aliases = aliases
13168
elsif Vagrant.has_plugin?('vagrant-hostmanager')

lib/drupalvm/vagrant.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
require 'yaml'
2+
3+
# Cross-platform way of finding an executable in the $PATH.
4+
def which(cmd)
5+
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
6+
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
7+
exts.each do |ext|
8+
exe = File.join(path, "#{cmd}#{ext}")
9+
return exe if File.executable?(exe) && !File.directory?(exe)
10+
end
11+
end
12+
nil
13+
end
14+
15+
# Recursively walk an tree and run provided block on each value found.
16+
def walk(obj, &fn)
17+
if obj.is_a?(Array)
18+
obj.map { |value| walk(value, &fn) }
19+
elsif obj.is_a?(Hash)
20+
obj.each_pair { |key, value| obj[key] = walk(value, &fn) }
21+
else
22+
obj = yield(obj)
23+
end
24+
end
25+
26+
# Resolve jinja variables in hash.
27+
def resolve_jinja_variables(vconfig)
28+
walk(vconfig) do |value|
29+
while value.is_a?(String) && value.match(/{{ .* }}/)
30+
value = value.gsub(/{{ (.*?) }}/) { vconfig[Regexp.last_match(1)] }
31+
end
32+
value
33+
end
34+
end
35+
36+
# Return the combined configuration content all files provided.
37+
def load_config(files)
38+
vconfig = {}
39+
files.each do |config_file|
40+
if File.exist?(config_file)
41+
optional_config = YAML.load_file(config_file)
42+
vconfig.merge!(optional_config) if optional_config
43+
end
44+
end
45+
resolve_jinja_variables(vconfig)
46+
end
47+
48+
49+
# Return the ansible version parsed from running the executable path provided.
50+
def get_ansible_version(exe)
51+
/^[^\s]+ (.+)$/.match(`#{exe} --version`) { |match| return match[1] }
52+
end
53+
54+
# Require that if installed, the ansible version meets the requirements.
55+
def require_ansible_version(requirement)
56+
if !(ansible_bin = which('ansible-playbook'))
57+
return
58+
end
59+
ansible_version = get_ansible_version(ansible_bin)
60+
req = Gem::Requirement.new(requirement)
61+
if req.satisfied_by?(Gem::Version.new(ansible_version))
62+
return
63+
end
64+
raise Vagrant::Errors::VagrantError.new, "You must install an Ansible version #{requirement} to use this version of Drupal VM."
65+
end
66+
67+
# Return which Vagrant provisioner to use.
68+
def get_provisioner
69+
which('ansible-playbook') ? :ansible : :ansible_local
70+
end
71+
72+
# Return a list of all virtualhost server names and aliases from a config hash.
73+
def get_vhost_aliases(vconfig)
74+
aliases = []
75+
if vconfig['drupalvm_webserver'] == 'apache'
76+
vconfig['apache_vhosts'].each do |host|
77+
aliases.push(host['servername'])
78+
aliases.concat(host['serveralias'].split) if host['serveralias']
79+
end
80+
else
81+
vconfig['nginx_hosts'].each do |host|
82+
aliases.concat(host['server_name'].split)
83+
aliases.concat(host['server_name_redirect'].split) if host['server_name_redirect']
84+
end
85+
end
86+
aliases = aliases.uniq - [vconfig['vagrant_ip']]
87+
# Remove wildcard subdomains.
88+
aliases.delete_if { |vhost| vhost.include?('*') }
89+
end
90+
91+
# Return a default post_up_message.
92+
def get_default_post_up_message(vconfig)
93+
'Your Drupal VM Vagrant box is ready to use!'\
94+
"\n* Visit the dashboard for an overview of your site: http://dashboard.#{vconfig['vagrant_hostname']} (or http://#{vconfig['vagrant_ip']})"\
95+
"\n* You can SSH into your machine with `vagrant ssh`."\
96+
"\n* Find out more in the Drupal VM documentation at http://docs.drupalvm.com"
97+
end

0 commit comments

Comments
 (0)