11# -*- mode: ruby -*-
22# vi: set ft=ruby :
3- VAGRANTFILE_API_VERSION = '2' unless defined? VAGRANTFILE_API_VERSION
3+
4+ require './lib/drupalvm/vagrant'
45
56# Absolute paths on the host machine.
67host_drupalvm_dir = File . dirname ( File . expand_path ( __FILE__ ) )
@@ -14,58 +15,19 @@ guest_config_dir = ENV['DRUPALVM_CONFIG_DIR'] ? "/vagrant/#{ENV['DRUPALVM_CONFIG
1415
1516drupalvm_env = ENV [ 'DRUPALVM_ENV' ] || 'vagrant'
1617
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
18+ default_config_file = "#{ host_drupalvm_dir } /default.config.yml"
19+ unless File . exist? ( default_config_file )
20+ raise_message "Configuration file not found! Expected in #{ default_config_file } "
4121end
4222
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' ] } "
63-
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' ] )
23+ vconfig = load_config ( [
24+ default_config_file ,
25+ "#{ host_config_dir } /config.yml" ,
26+ "#{ host_config_dir } /local.config.yml" ,
27+ "#{ host_config_dir } /#{ drupalvm_env } .config.yml"
28+ ] )
6729
68- provisioner = ansible_bin && ! vconfig [ 'force_ansible_local' ] ? :ansible : :ansible_local
30+ provisioner = vconfig [ 'force_ansible_local' ] ? :ansible_local : vagrant_provisioner
6931if provisioner == :ansible
7032 playbook = "#{ host_drupalvm_dir } /provisioning/playbook.yml"
7133 config_dir = host_config_dir
7436 config_dir = guest_config_dir
7537end
7638
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
39+ # Verify version requirements.
40+ require_ansible_version ">= #{ vconfig [ 'drupalvm_ansible_version_min' ] } "
41+ Vagrant . require_version ">= #{ vconfig [ 'drupalvm_vagrant_version_min' ] } "
42+
43+ Vagrant . configure ( '2' ) do |config |
44+ # Set the name of the VM. See: http://stackoverflow.com/a/17864388/100134
45+ config . vm . define vconfig [ 'vagrant_machine_name' ]
8046
81- Vagrant . configure ( VAGRANTFILE_API_VERSION ) do |config |
8247 # Networking configuration.
8348 config . vm . hostname = vconfig [ 'vagrant_hostname' ]
84- if vconfig [ 'vagrant_ip' ] == '0.0.0.0' && Vagrant . has_plugin? ( 'vagrant-auto_network' )
85- config . vm . network :private_network , ip : vconfig [ 'vagrant_ip' ] , auto_network : true
86- else
87- config . vm . network :private_network , ip : vconfig [ 'vagrant_ip' ]
88- end
49+ config . vm . network :private_network ,
50+ ip : vconfig [ 'vagrant_ip' ] ,
51+ auto_network : vconfig [ 'vagrant_ip' ] == '0.0.0.0' && Vagrant . has_plugin? ( 'vagrant-auto_network' )
8952
90- if !vconfig [ 'vagrant_public_ip' ] . empty? && vconfig [ 'vagrant_public_ip' ] == '0.0.0.0'
91- config . vm . network :public_network
92- elsif !vconfig [ 'vagrant_public_ip' ] . empty?
93- config . vm . network :public_network , ip : vconfig [ 'vagrant_public_ip' ]
53+ unless vconfig [ 'vagrant_public_ip' ] . empty?
54+ config . vm . network :public_network ,
55+ ip : vconfig [ 'vagrant_public_ip' ] != '0.0.0.0' ? vconfig [ 'vagrant_public_ip' ] : nil
9456 end
9557
9658 # SSH options.
@@ -100,32 +62,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
10062 # Vagrant box.
10163 config . vm . box = vconfig [ 'vagrant_box' ]
10264
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
65+ # Display an introduction message after `vagrant up` and `vagrant provision`.
66+ config . vm . post_up_message = vconfig . fetch ( 'vagrant_post_up_message' , get_default_post_up_message ( vconfig ) )
11167
11268 # 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-
69+ aliases = get_vhost_aliases ( vconfig ) - [ config . vm . hostname ]
12970 if Vagrant . has_plugin? ( 'vagrant-hostsupdater' )
13071 config . hostsupdater . aliases = aliases
13172 elsif Vagrant . has_plugin? ( 'vagrant-hostmanager' )
@@ -137,20 +78,17 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
13778 # Synced folders.
13879 vconfig [ 'vagrant_synced_folders' ] . each do |synced_folder |
13980 options = {
140- type : synced_folder . include? ( 'type' ) ? synced_folder [ 'type' ] : vconfig [ 'vagrant_synced_folder_default_type' ] ,
141- rsync__auto : 'true' ,
81+ type : synced_folder . fetch ( 'type' , vconfig [ 'vagrant_synced_folder_default_type' ] ) ,
14282 rsync__exclude : synced_folder [ 'excluded_paths' ] ,
143- rsync__args : [ '--verbose' , '--archive' , '--delete' , '-z' , '--chmod=ugo=rwX' ] ,
83+ rsync__args : [ '--verbose' , '--archive' , '--delete' , '-z' , '--copy-links' , '-- chmod=ugo=rwX'] ,
14484 id : synced_folder [ 'id' ] ,
145- create : synced_folder . include? ( 'create' ) ? synced_folder [ 'create' ] : false ,
146- mount_options : synced_folder . include? ( 'mount_options' ) ? synced_folder [ 'mount_options' ] : [ ]
85+ create : synced_folder . fetch ( 'create' , false ) ,
86+ mount_options : synced_folder . fetch ( 'mount_options' , [ ] )
14787 }
148- if synced_folder . include? ( 'options_override' )
149- synced_folder [ 'options_override' ] . each do |key , value |
150- options [ key . to_sym ] = value
151- end
88+ synced_folder . fetch ( 'options_override' , { } ) . each do |key , value |
89+ options [ key . to_sym ] = value
15290 end
153- config . vm . synced_folder synced_folder [ 'local_path' ] , synced_folder [ 'destination' ] , options
91+ config . vm . synced_folder synced_folder . fetch ( 'local_path' ) , synced_folder . fetch ( 'destination' ) , options
15492 end
15593
15694 # Allow override of the default synced folder type.
@@ -196,9 +134,6 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
196134 p . update_guest_tools = true
197135 end
198136
199- # Set the name of the VM. See: http://stackoverflow.com/a/17864388/100134
200- config . vm . define vconfig [ 'vagrant_machine_name' ]
201-
202137 # Cache packages and dependencies if vagrant-cachier plugin is present.
203138 if Vagrant . has_plugin? ( 'vagrant-cachier' )
204139 config . cache . scope = :box
0 commit comments