This repository was archived by the owner on Feb 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 641
Vagrantfile cleanup #1332
Merged
Merged
Vagrantfile cleanup #1332
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae4c803
Split Vagrantfile logic into separate lib file
oxyc ec3ffd9
Use Hash fetch() to simplify and/or require variables
oxyc 0e60964
More cleanup
oxyc 200978f
Fix rubocop warnings and add some exceptions.
oxyc e3f5751
Update rsync options to be inline with Vagrant defaults
oxyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require 'yaml' | ||
|
||
# Cross-platform way of finding an executable in the $PATH. | ||
def which(cmd) | ||
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] | ||
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| | ||
exts.each do |ext| | ||
exe = File.join(path, "#{cmd}#{ext}") | ||
return exe if File.executable?(exe) && !File.directory?(exe) | ||
end | ||
end | ||
nil | ||
end | ||
|
||
# Recursively walk an tree and run provided block on each value found. | ||
def walk(obj, &fn) | ||
if obj.is_a?(Array) | ||
obj.map { |value| walk(value, &fn) } | ||
elsif obj.is_a?(Hash) | ||
obj.each_pair { |key, value| obj[key] = walk(value, &fn) } | ||
else | ||
obj = yield(obj) | ||
end | ||
end | ||
|
||
# Resolve jinja variables in hash. | ||
def resolve_jinja_variables(vconfig) | ||
walk(vconfig) do |value| | ||
while value.is_a?(String) && value.match(/{{ .* }}/) | ||
value = value.gsub(/{{ (.*?) }}/) { vconfig[Regexp.last_match(1)] } | ||
end | ||
value | ||
end | ||
end | ||
|
||
# Return the combined configuration content all files provided. | ||
def load_config(files) | ||
vconfig = {} | ||
files.each do |config_file| | ||
if File.exist?(config_file) | ||
optional_config = YAML.load_file(config_file) | ||
vconfig.merge!(optional_config) if optional_config | ||
end | ||
end | ||
resolve_jinja_variables(vconfig) | ||
end | ||
|
||
# Return the path to the ansible-playbook executable. | ||
def ansible_bin | ||
@ansible_bin ||= which('ansible-playbook') | ||
end | ||
|
||
# Return the ansible version parsed from running the executable path provided. | ||
def ansible_version | ||
/^[^\s]+ (.+)$/.match(`#{ansible_bin} --version`) { |match| return match[1] } | ||
end | ||
|
||
# Require that if installed, the ansible version meets the requirements. | ||
def require_ansible_version(requirement) | ||
return unless ansible_bin | ||
req = Gem::Requirement.new(requirement) | ||
return if req.satisfied_by?(Gem::Version.new(ansible_version)) | ||
raise_message "You must install an Ansible version #{requirement} to use this version of Drupal VM." | ||
end | ||
|
||
def raise_message(msg) | ||
raise Vagrant::Errors::VagrantError.new, msg | ||
end | ||
|
||
# Return which Vagrant provisioner to use. | ||
def vagrant_provisioner | ||
ansible_bin ? :ansible : :ansible_local | ||
end | ||
|
||
def get_apache_vhosts(vhosts) | ||
aliases = [] | ||
vhosts.each do |host| | ||
aliases.push(host['servername']) | ||
aliases.concat(host['serveralias'].split) if host['serveralias'] | ||
end | ||
aliases | ||
end | ||
|
||
def get_nginx_vhosts(vhosts) | ||
aliases = [] | ||
vhosts.each do |host| | ||
aliases.push(host['server_name']) | ||
aliases.concat(host['server_name_redirect'].split) if host['server_name_redirect'] | ||
end | ||
aliases | ||
end | ||
|
||
# Return a list of all virtualhost server names and aliases from a config hash. | ||
def get_vhost_aliases(vconfig) | ||
if vconfig['drupalvm_webserver'] == 'apache' | ||
aliases = get_apache_vhosts(vconfig['apache_vhosts']) | ||
else | ||
aliases = get_nginx_vhosts(vconfig['nginx_hosts']) | ||
end | ||
aliases = aliases.uniq - [vconfig['vagrant_ip']] | ||
# Remove wildcard subdomains. | ||
aliases.delete_if { |vhost| vhost.include?('*') } | ||
end | ||
|
||
# Return a default post_up_message. | ||
def get_default_post_up_message(vconfig) | ||
'Your Drupal VM Vagrant box is ready to use!'\ | ||
"\n* Visit the dashboard for an overview of your site: http://dashboard.#{vconfig['vagrant_hostname']} (or http://#{vconfig['vagrant_ip']})"\ | ||
"\n* You can SSH into your machine with `vagrant ssh`."\ | ||
"\n* Find out more in the Drupal VM documentation at http://docs.drupalvm.com" | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really see the point of defining this when it could technically be overridden by an earlier
vagrant
command.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1