1
1
# -*- mode: ruby -*-
2
2
# vi: set ft=ruby :
3
3
4
- require 'yaml'
5
-
6
- ip = '192.168.50.5' # pick any local IP
7
- cpus = 1
8
- memory = 1024 # in MB
9
-
10
4
ANSIBLE_PATH = __dir__ # absolute path to Ansible directory on host machine
11
5
ANSIBLE_PATH_ON_VM = '/home/vagrant/trellis' # absolute path to Ansible directory on virtual machine
12
6
13
- # Set Ansible paths relative to Ansible directory
14
- ENV [ 'ANSIBLE_CONFIG' ] = ANSIBLE_PATH
15
- ENV [ 'ANSIBLE_CALLBACK_PLUGINS' ] = "~/.ansible/plugins/callback_plugins/:/usr/share/ansible_plugins/callback_plugins:#{ File . join ( ANSIBLE_PATH , 'lib/trellis/plugins/callback' ) } "
16
- ENV [ 'ANSIBLE_FILTER_PLUGINS' ] = "~/.ansible/plugins/filter_plugins/:/usr/share/ansible_plugins/filter_plugins:#{ File . join ( ANSIBLE_PATH , 'lib/trellis/plugins/filter' ) } "
17
- ENV [ 'ANSIBLE_LIBRARY' ] = "/usr/share/ansible:#{ File . join ( ANSIBLE_PATH , 'lib/trellis/modules' ) } "
18
- ENV [ 'ANSIBLE_ROLES_PATH' ] = File . join ( ANSIBLE_PATH , 'vendor' , 'roles' )
19
- ENV [ 'ANSIBLE_VARS_PLUGINS' ] = "~/.ansible/plugins/vars_plugins/:/usr/share/ansible_plugins/vars_plugins:#{ File . join ( ANSIBLE_PATH , 'lib/trellis/plugins/vars' ) } "
7
+ require File . join ( ANSIBLE_PATH , 'lib' , 'trellis' , 'vagrant' )
8
+ require 'yaml'
20
9
21
- config_file = File . join ( ANSIBLE_PATH , 'group_vars' , 'development' , 'wordpress_sites. yml' )
10
+ vconfig = YAML . load_file ( " #{ ANSIBLE_PATH } /vagrant.default. yml" )
22
11
23
- def fail_with_message ( msg )
24
- fail Vagrant ::Errors ::VagrantError . new , msg
12
+ if File . exist? ( "#{ ANSIBLE_PATH } /vagrant.local.yml" )
13
+ local_config = YAML . load_file ( "#{ ANSIBLE_PATH } /vagrant.local.yml" )
14
+ vconfig . merge! ( local_config ) if local_config
25
15
end
26
16
27
- if File . exists? ( config_file )
28
- wordpress_sites = YAML . load_file ( config_file ) [ 'wordpress_sites' ]
29
- fail_with_message "No sites found in #{ config_file } ." if wordpress_sites . to_h . empty?
30
- else
31
- fail_with_message "#{ config_file } was not found. Please set `ANSIBLE_PATH` in your Vagrantfile."
32
- end
17
+ wordpress_sites = load_wordpress_sites
18
+ site_hosts = hosts ( wordpress_sites )
33
19
34
20
Vagrant . require_version '>= 1.8.5'
35
21
36
22
Vagrant . configure ( '2' ) do |config |
37
- config . vm . box = 'bento/ubuntu-16.04'
38
- config . vm . box_version = '2.2.9'
23
+ config . vm . box = vconfig . fetch ( 'vagrant_box' )
24
+ config . vm . box_version = vconfig . fetch ( 'vagrant_box_version' )
39
25
config . ssh . forward_agent = true
40
-
41
26
config . vm . post_up_message = post_up_message
42
27
43
28
# Fix for: "stdin: is not a tty"
44
29
# https://github.com/mitchellh/vagrant/issues/1673#issuecomment-28288042
45
30
config . ssh . shell = %{bash -c 'BASH_ENV=/etc/profile exec bash'}
46
31
47
32
# Required for NFS to work
48
- config . vm . network :private_network , ip : ip , hostsupdater : 'skip'
49
-
50
- site_hosts = wordpress_sites . flat_map { |( _name , site ) | site [ 'site_hosts' ] }
51
-
52
- site_hosts . each do |host |
53
- if !host . is_a? ( Hash ) or !host . has_key? ( 'canonical' )
54
- fail_with_message File . read ( File . join ( ANSIBLE_PATH , 'roles/common/templates/site_hosts.j2' ) ) . sub! ( '{{ env }}' , 'development' ) . gsub! ( /com$/ , 'dev' )
55
- end
56
- end
33
+ config . vm . network :private_network , ip : vconfig . fetch ( 'vagrant_ip' ) , hostsupdater : 'skip'
57
34
58
35
main_hostname , *hostnames = site_hosts . map { |host | host [ 'canonical' ] }
59
36
config . vm . hostname = main_hostname
60
37
61
- redirects = site_hosts . flat_map { |host | host [ 'redirects' ] } . compact
62
-
63
38
if Vagrant . has_plugin? ( 'vagrant-hostmanager' ) && !multisite_subdomains? ( wordpress_sites )
39
+ redirects = site_hosts . flat_map { |host | host [ 'redirects' ] } . compact
40
+
64
41
config . hostmanager . enabled = true
65
42
config . hostmanager . manage_host = true
66
43
config . hostmanager . aliases = hostnames + redirects
67
44
elsif Vagrant . has_plugin? ( 'landrush' ) && multisite_subdomains? ( wordpress_sites )
68
45
config . landrush . enabled = true
69
46
config . landrush . tld = config . vm . hostname
70
- hostnames . each { |host | config . landrush . host host , ip }
47
+ hostnames . each { |host | config . landrush . host host , vconfig . fetch ( 'vagrant_ip' ) }
71
48
else
72
49
fail_with_message "vagrant-hostmanager missing, please install the plugin with this command:\n vagrant plugin install vagrant-hostmanager\n \n Or install landrush for multisite subdomains:\n vagrant plugin install landrush"
73
50
end
@@ -78,6 +55,7 @@ Vagrant.configure('2') do |config|
78
55
wordpress_sites . each_pair do |name , site |
79
56
config . vm . synced_folder local_site_path ( site ) , remote_site_path ( name , site ) , owner : 'vagrant' , group : 'www-data' , mount_options : [ 'dmode=776' , 'fmode=775' ]
80
57
end
58
+
81
59
config . vm . synced_folder ANSIBLE_PATH , ANSIBLE_PATH_ON_VM , mount_options : [ 'dmode=755' , 'fmode=644' ]
82
60
config . vm . synced_folder File . join ( ANSIBLE_PATH , 'bin' ) , bin_path , mount_options : [ 'dmode=755' , 'fmode=755' ]
83
61
else
@@ -88,37 +66,49 @@ Vagrant.configure('2') do |config|
88
66
config . vm . synced_folder local_site_path ( site ) , nfs_path ( name ) , type : 'nfs'
89
67
config . bindfs . bind_folder nfs_path ( name ) , remote_site_path ( name , site ) , u : 'vagrant' , g : 'www-data' , o : 'nonempty'
90
68
end
69
+
91
70
config . vm . synced_folder ANSIBLE_PATH , '/ansible-nfs' , type : 'nfs'
92
71
config . bindfs . bind_folder '/ansible-nfs' , ANSIBLE_PATH_ON_VM , o : 'nonempty' , p : '0644,a+D'
93
72
config . bindfs . bind_folder bin_path , bin_path , perms : '0755'
94
73
end
95
74
end
96
75
76
+ vconfig . fetch ( 'vagrant_synced_folders' , [ ] ) . each do |folder |
77
+ options = {
78
+ type : folder . fetch ( 'type' , 'nfs' ) ,
79
+ create : folder . fetch ( 'create' , false ) ,
80
+ mount_options : folder . fetch ( 'mount_options' , [ ] )
81
+ }
82
+
83
+ config . vm . synced_folder folder [ 'local_path' ] , folder [ 'destination' ] , options
84
+
85
+ if folder . fetch ( 'bindfs' , true )
86
+ config . bindfs . bind_folder folder [ 'local_path' ] , folder [ 'destination' ] , options
87
+ end
88
+ end
89
+
97
90
provisioner = Vagrant ::Util ::Platform . windows? ? :ansible_local : :ansible
98
91
provisioning_path = Vagrant ::Util ::Platform . windows? ? ANSIBLE_PATH_ON_VM : ANSIBLE_PATH
92
+
99
93
config . vm . provision provisioner do |ansible |
100
94
if Vagrant ::Util ::Platform . windows?
101
95
ansible . install_mode = 'pip'
102
96
ansible . provisioning_path = provisioning_path
103
- ansible . version = '2.2.0'
97
+ ansible . version = vconfig . fetch ( 'vagrant_ansible_version' )
104
98
end
105
99
106
100
ansible . playbook = File . join ( provisioning_path , 'dev.yml' )
107
- unless ENV [ 'SKIP_GALAXY' ]
108
- ansible . galaxy_role_file = File . join ( provisioning_path , 'requirements.yml' )
109
- end
101
+ ansible . galaxy_role_file = File . join ( provisioning_path , 'requirements.yml' ) unless vconfig . fetch ( 'vagrant_skip_galaxy' ) || ENV [ 'SKIP_GALAXY' ]
110
102
ansible . galaxy_roles_path = File . join ( provisioning_path , 'vendor/roles' )
111
103
112
104
ansible . groups = {
113
105
'web' => [ 'default' ] ,
114
106
'development' => [ 'default' ]
115
107
}
116
108
117
- if tags = ENV [ 'ANSIBLE_TAGS' ]
118
- ansible . tags = tags
119
- end
109
+ ansible . tags = ENV [ 'ANSIBLE_TAGS' ]
110
+ ansible . extra_vars = { 'vagrant_version' => Vagrant ::VERSION }
120
111
121
- ansible . extra_vars = { 'vagrant_version' => Vagrant ::VERSION }
122
112
if vars = ENV [ 'ANSIBLE_VARS' ]
123
113
extra_vars = Hash [ vars . split ( ',' ) . map { |pair | pair . split ( '=' ) } ]
124
114
ansible . extra_vars . merge ( extra_vars )
@@ -128,8 +118,8 @@ Vagrant.configure('2') do |config|
128
118
# Virtualbox settings
129
119
config . vm . provider 'virtualbox' do |vb |
130
120
vb . name = config . vm . hostname
131
- vb . customize [ 'modifyvm' , :id , '--cpus' , cpus ]
132
- vb . customize [ 'modifyvm' , :id , '--memory' , memory ]
121
+ vb . customize [ 'modifyvm' , :id , '--cpus' , vconfig . fetch ( 'vagrant_cpus' ) ]
122
+ vb . customize [ 'modifyvm' , :id , '--memory' , vconfig . fetch ( 'vagrant_memory' ) ]
133
123
134
124
# Fix for slow external network connections
135
125
vb . customize [ 'modifyvm' , :id , '--natdnshostresolver1' , 'on' ]
@@ -140,43 +130,16 @@ Vagrant.configure('2') do |config|
140
130
[ 'vmware_fusion' , 'vmware_workstation' ] . each do |provider |
141
131
config . vm . provider provider do |vmw , override |
142
132
vmw . name = config . vm . hostname
143
- vmw . vmx [ 'numvcpus' ] = cpus
144
- vmw . vmx [ 'memsize' ] = memory
133
+ vmw . vmx [ 'numvcpus' ] = vconfig . fetch ( 'vagrant_cpus' )
134
+ vmw . vmx [ 'memsize' ] = vconfig . fetch ( 'vagrant_memory' )
145
135
end
146
136
end
147
137
148
138
# Parallels settings
149
139
config . vm . provider 'parallels' do |prl , override |
150
140
prl . name = config . vm . hostname
151
- prl . cpus = cpus
141
+ prl . cpus = vconfig . fetch ( 'vagrant_cpus' )
142
+ prl . memory = vconfig . fetch ( 'vagrant_memory' )
152
143
prl . update_guest_tools = true
153
- prl . memory = memory
154
144
end
155
-
156
- end
157
-
158
- def local_site_path ( site )
159
- File . expand_path ( site [ 'local_path' ] , ANSIBLE_PATH )
160
- end
161
-
162
- def multisite_subdomains? ( wordpress_sites )
163
- wordpress_sites . any? { |( _name , site ) | site [ 'multisite' ] . fetch ( 'enabled' , false ) && site [ 'multisite' ] . fetch ( 'subdomains' , false ) }
164
- end
165
-
166
- def nfs_path ( site_name )
167
- "/vagrant-nfs-#{ site_name } "
168
- end
169
-
170
- def post_up_message
171
- msg = 'Your Trellis Vagrant box is ready to use!'
172
- msg << "\n * Composer and WP-CLI commands need to be run on the virtual machine."
173
- msg << "\n * You can SSH into the machine with `vagrant ssh`."
174
- msg << "\n * Then navigate to your WordPress sites at `/srv/www`"
175
- msg << "\n or to your Trellis files at `#{ ANSIBLE_PATH_ON_VM } `."
176
-
177
- msg
178
- end
179
-
180
- def remote_site_path ( site_name , site )
181
- "/srv/www/#{ site_name } /#{ site [ 'current_path' ] || 'current' } "
182
145
end
0 commit comments