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

Commit 254a74d

Browse files
authored
Merge pull request #1255 from oxyc/templates
Allow provided templates to be extended through template inheritance
2 parents c73c462 + f821c32 commit 254a74d

17 files changed

+194
-44
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ Note: *If there are any errors during the course of running `vagrant up`, and it
9191
### 3 - Configure your host machine to access the VM.
9292

9393
1. [Edit your hosts file](http://www.rackspace.com/knowledge_center/article/how-do-i-modify-my-hosts-file), adding the line `192.168.88.88 drupalvm.dev` so you can connect to the VM.
94-
- You can have Vagrant automatically configure your hosts file if you install the `hostsupdater` plugin (`vagrant plugin install vagrant-hostsupdater`). All hosts defined in `apache_vhosts` or `nginx_hosts` will be automatically managed. `vagrant-hostmanager` is also supported.
94+
- You can have Vagrant automatically configure your hosts file if you install the `hostsupdater` plugin (`vagrant plugin install vagrant-hostsupdater`). All hosts defined in `apache_vhosts` or `nginx_vhosts` will be automatically managed. `vagrant-hostmanager` is also supported.
9595
- The `auto_network` plugin (`vagrant plugin install vagrant-auto_network`) can help with IP address management if you set `vagrant_ip` to `0.0.0.0` inside `config.yml`.
9696
2. Open your browser and access [http://drupalvm.dev/](http://drupalvm.dev/). The default login for the admin account is `admin` for both the username and password.
9797

default.config.yml

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ drupalvm_cron_jobs: []
9696
# Drupal VM automatically creates a drush alias file in your ~/.drush folder if
9797
# this variable is 'true'.
9898
configure_drush_aliases: true
99+
drush_aliases_host_template: "templates/drupalvm.aliases.drushrc.php.j2"
100+
drush_aliases_guest_template: "templates/drupalvm-local.aliases.drushrc.php.j2"
99101

100102
# Helper variable to configure the PHP-FPM connection for each Apache
101103
# VirtualHost in the `apache_vhosts` list.
@@ -165,6 +167,7 @@ nginx_hosts:
165167

166168
nginx_remove_default_vhost: true
167169
nginx_ppa_use: true
170+
nginx_vhost_template: "templates/nginx-vhost.conf.j2"
168171

169172
# MySQL databases and users.
170173
mysql_databases:

docs/configurations/webservers-nginx.md

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
To use Nginx instead of Apache, change the `drupalvm_webserver` variable inside your customized `config.yml`, from `apache` to `nginx`.
22

3-
Because Nginx server directives behave a little differently than Apache's VirtualHosts, Drupal VM includes a custom Drupal-optimized Nginx server block configuration, and you can control all the servers ('virtual hosts') Nginx will run using the `nginx_hosts` configuration. A few simple examples are shown in `default.config.yml`, but you have some extra flexibility if you need it. See the `nginx-vhost.conf.j2` template for more information.
3+
Because Nginx server directives behave a little differently than Apache's VirtualHosts, Drupal VM includes a custom Drupal-optimized Nginx server block configuration, and you can control all the servers ('virtual hosts') Nginx will run using the `nginx_vhosts` configuration. A few simple examples are shown in `default.config.yml`, but you have some extra flexibility if you need it. See the `nginx-vhost.conf.j2` template for more information.
44

55
Also, see the examples included in the [`geerlingguy.nginx` Ansible role's README](https://github.com/geerlingguy/ansible-role-nginx#readme) for more info, as well as many other variables you can override to configure Nginx exactly how you like it.
66

@@ -10,7 +10,7 @@ _Note: if you're using php-fpm, you may want to reflect your use of nginx by set
1010

1111
To enable SSL support for you virtual hosts you first need a certificate file. See the same section under the [Apache documentation](webservers-apache.md#enable-ssl-support-with-apache) for how to generate a self-signed certficiate.
1212

13-
Modify your nginx host configuration by adding the following `extra_parameters` to the first entry in `nginx_hosts`:
13+
Modify your nginx host configuration by adding the following `extra_parameters` to the first entry in `nginx_vhosts`:
1414

1515
```yaml
1616
- server_name: "{{ drupal_domain }} www.{{ drupal_domain }}"
@@ -39,3 +39,43 @@ If you are using Ubuntu as your base OS and you want to get started quickly with
3939
ssl_protocols TLSv1.1 TLSv1.2;
4040
ssl_ciphers HIGH:!aNULL:!MD5;
4141
```
42+
43+
## Customizing server block configuration
44+
45+
If you can't customize via variables because an option isn't exposed, you can override the template used to generate the the virtualhost configuration file.
46+
47+
```yaml
48+
nginx_vhost_template: "{{ config_dir }}/templates/nginx-vhost.conf.j2"
49+
```
50+
51+
You can either copy and modify the provided `nginx-vhost.conf.j2` template, or extend it and use [template inheritace](http://jinja.pocoo.org/docs/2.9/templates/#template-inheritance) to override the specific template block you need to change.
52+
53+
_If you extend Drupal VM's provided base template, the path referenced should to be relative to playbook.yml._
54+
55+
```
56+
{% extends 'templates/nginx-vhost.conf.j2' %}
57+
58+
{% block location_primary %}
59+
location / {
60+
try_files $uri @rewrite; # For Drupal <= 6
61+
}
62+
{% endblock %}
63+
64+
{% block location_image_styles %}
65+
location ~ ^/sites/.*/files/imagecache/ {
66+
try_files $uri @rewrite; # For Drupal <= 6
67+
}
68+
{% endblock %}
69+
```
70+
71+
If you need to append or prepend content to a block, you can use the `{{ super() }}` Jinja2 function to return the original block content from the base template.
72+
73+
```
74+
{% block location_deny %}
75+
{{ super() }}
76+
location ~* \.(txt|log)$ {
77+
allow 192.168.0.0/16;
78+
deny all;
79+
}
80+
{% endblock %}
81+
```

docs/deployment/multisite.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
For multisite installations, make the changes outlined in the [Local Drupal codebase](local-codebase.md) guide, but, using the `apache_vhosts` variable (or `nginx_hosts` if using Nginx), configure as many domains pointing to the same docroot as you need:
1+
For multisite installations, make the changes outlined in the [Local Drupal codebase](local-codebase.md) guide, but, using the `apache_vhosts` variable (or `nginx_vhosts` if using Nginx), configure as many domains pointing to the same docroot as you need:
22

33
```yaml
44
drupal_core_path: "/var/www/my-drupal-site"

docs/extras/drush.md

+16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ $ drush @drupalvm.dev status
3535

3636
Drupal VM automatically generates a drush alias file in `~/.drush/drupalvm.aliases.drushrc.php` with an alias for every site you have defined in the `apache_vhosts` variable.
3737

38+
If you want to customize the generated alias file you can override the `drush_aliases_host_template` and `drush_aliases_guest_template` variables in your `config.yml`.
39+
40+
```yaml
41+
drush_aliases_host_template: "{{ config_dir }}/templates/drupalvm.aliases.drushrc.php.j2"
42+
```
43+
44+
Eg. to only print the alias for your main domain, and not the subdomain you can override the file using a [Jinja2 child template](http://jinja.pocoo.org/docs/2.9/templates/#child-template).
45+
46+
```php
47+
{% extends 'templates/drupalvm.aliases.drushrc.php.j2' %}
48+
49+
{% block aliases %}
50+
{{ alias('drupalvm.dev', drupal_core_path) }}
51+
{% endblock %}
52+
```
53+
3854
You can disable Drupal VM's automatic Drush alias file management if you want to manage drush aliases on your own. Just set the `configure_drush_aliases` variable in `config.yml` to `false`.
3955

4056
## Using sql-sync

docs/extras/varnish.md

+30
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,34 @@ $settings['reverse_proxy_addresses'] = array('127.0.0.1');
3131

3232
If you don't set these values, Drupal will think all requests are coming from `127.0.0.1`. There are other settings you can change to make Drupal not store copies of cached pages in the Database (since Varnish is caching everything, this is redundant), but those other settings are not covered here.
3333

34+
## Extending the base `drupalvm.vcl.j2` template
35+
36+
If you can't customize via variables because an option isn't exposed, you can extend the base `drupalvm.vcl.j2` through [Jinja2 template inheritance](http://jinja.pocoo.org/docs/2.9/templates/#template-inheritance).
37+
38+
```yaml
39+
varnish_default_vcl_template_path: "{{ config_dir }}/templates/drupalvm.vcl.j2"
40+
```
41+
42+
Either copy the `drupalvm.vcl.j2` and modify it to your liking, or extend it and override the blocks you need to adjust.
43+
44+
_If you extend Drupal VM's provided base template, the path referenced should to be relative to playbook.yml._
45+
46+
```
47+
{% extends 'templates/drupalvm.vcl.j2' %}
48+
49+
{% block backend -%}
50+
{{ super() }}
51+
.connect_timeout = 1s;
52+
{% endblock %}
53+
54+
{% block vcl_deliver -%}
55+
unset resp.http.X-Url;
56+
unset resp.http.X-Host;
57+
unset resp.http.Purge-Cache-Tags;
58+
# Do not set X-Varnish-Cache headers.
59+
{% endblock %}
60+
```
61+
62+
The [`{{ super() }}` Jinja2 function](http://jinja.pocoo.org/docs/2.9/templates/#super-blocks) returns the original block content from the base template.
63+
3464
For a list of available role variables, see the [`geerlingguy.varnish` Ansible role's README](https://github.com/geerlingguy/ansible-role-varnish#readme).

examples/prod/prod.config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
drupal_domain: "drupalvm.dev"
44
vagrant_hostname: "{{ drupal_domain }}"
55

6-
# Add only the `apache_vhosts` or `nginx_hosts` you need. If installing a
6+
# Add only the `apache_vhosts` or `nginx_vhosts` you need. If installing a
77
# single Drupal site, the variable should look like this (Apache):
88
apache_vhosts:
99
- servername: "{{ drupal_domain }}"

lib/drupalvm/vagrant.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ def get_vhost_aliases(vconfig)
9595
if vconfig['drupalvm_webserver'] == 'apache'
9696
aliases = get_apache_vhosts(vconfig['apache_vhosts'])
9797
else
98-
aliases = get_nginx_vhosts(vconfig['nginx_hosts'])
98+
# @todo shim for `nginx_hosts`.
99+
aliases = get_nginx_vhosts(vconfig.fetch('nginx_hosts', vconfig['nginx_vhosts']))
99100
end
100101
aliases = aliases.uniq - [vconfig['vagrant_ip']]
101102
# Remove wildcard subdomains.

provisioning/playbook.yml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
- include: tasks/config.yml
1111
tags: ['always']
1212

13+
- include: tasks/backwards-compatibility.yml
14+
tags: ['always']
15+
static: no
16+
1317
- include: "tasks/init-{{ ansible_os_family }}.yml"
1418
static: no
1519

provisioning/roles/drupalvm.www/tasks/main.yml

-12
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,3 @@
8585
mode: 0755
8686
recurse: true
8787
when: drupalvm_webserver == 'apache'
88-
89-
- name: Copy Nginx vhosts into place.
90-
template:
91-
src: ../templates/nginx-vhost.conf.j2
92-
dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
93-
force: yes
94-
owner: root
95-
group: root
96-
mode: 0644
97-
with_items: "{{ nginx_hosts }}"
98-
notify: restart nginx
99-
when: drupalvm_webserver == 'nginx'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
- name: nginx_hosts shim
3+
set_fact:
4+
nginx_vhosts: "{{ nginx_hosts }}"
5+
when: nginx_hosts is defined

provisioning/tasks/drush-aliases.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Note that this doesn't work for Windows, since Ansible's running in the VM.
1919
- name: Configure host machine drush aliases.
2020
template:
21-
src: ../templates/drupalvm.aliases.drushrc.php.j2
21+
src: "{{ drush_aliases_host_template }}"
2222
dest: "~/.drush/{{ vagrant_machine_name }}.aliases.drushrc.php"
2323
delegate_to: 127.0.0.1
2424
become: no
@@ -30,7 +30,7 @@
3030

3131
- name: Configure drush aliases for vagrant user inside VM.
3232
template:
33-
src: ../templates/drupalvm-local.aliases.drushrc.php.j2
33+
src: "{{ drush_aliases_guest_template }}"
3434
dest: "~/.drush/{{ vagrant_machine_name }}.aliases.drushrc.php"
3535
become: no
3636
when: configure_drush_aliases
@@ -40,6 +40,6 @@
4040

4141
- name: Configure drush aliases for root user inside VM.
4242
template:
43-
src: ../templates/drupalvm-local.aliases.drushrc.php.j2
43+
src: "{{ drush_aliases_guest_template }}"
4444
dest: "~/.drush/{{ vagrant_machine_name }}.aliases.drushrc.php"
4545
when: configure_drush_aliases

provisioning/templates/dashboard.html.j2

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
{%- endif -%}
3434
{%- endfor -%}
3535
{%- elif drupalvm_webserver == 'nginx' -%}
36-
{%- for host in nginx_hosts -%}
36+
{%- for host in nginx_vhosts -%}
3737
{%- if host.root == path -%}
3838
{{ host.server_name }}
3939
{%- endif -%}
@@ -73,7 +73,7 @@
7373

7474
{%- endfor -%}
7575
{%- elif drupalvm_webserver == 'nginx' -%}
76-
{%- for host in nginx_hosts -%}
76+
{%- for host in nginx_vhosts -%}
7777

7878
{% for server_name in host.server_name.split(' ') -%}
7979
{{ printHostsEntry(vagrant_ip, server_name) }}
@@ -107,7 +107,7 @@
107107
{%- endif -%}
108108
{%- endfor -%}
109109
{%- elif drupalvm_webserver == 'nginx' -%}
110-
{%- for host in nginx_hosts -%}
110+
{%- for host in nginx_vhosts -%}
111111
{%- if host.root is defined -%}
112112
{%- for hostname in host.server_name.split() -%}
113113
{{ printSite(hostname, host.root) }}

provisioning/templates/drupalvm-local.aliases.drushrc.php.j2

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
* @see example.aliases.drushrc.php.
77
*/
88

9-
{% macro alias(host, root) %}
9+
{% macro alias(host, root) -%}
1010
{%- if root not in _devtool_docroots %}
1111
$aliases['{{ host }}'] = array(
1212
'uri' => '{{ host }}',
1313
'root' => '{{ root }}',
1414
);
1515

1616
{% endif -%}
17-
{% endmacro %}
17+
{% endmacro -%}
1818

19+
{% block aliases -%}
1920
{%- if drupalvm_webserver == 'apache' -%}
2021
{%- for vhost in apache_vhosts -%}
2122
{{ alias(vhost.servername, vhost.documentroot) }}
@@ -26,9 +27,10 @@ $aliases['{{ host }}'] = array(
2627
{%- endif -%}
2728
{%- endfor -%}
2829
{%- elif drupalvm_webserver == 'nginx' -%}
29-
{%- for host in nginx_hosts -%}
30+
{%- for host in nginx_vhosts -%}
3031
{%- for server_name in host.server_name.split() -%}
3132
{{ alias(server_name, host.root) }}
3233
{%- endfor -%}
3334
{%- endfor -%}
3435
{%- endif -%}
36+
{%- endblock %}

provisioning/templates/drupalvm.aliases.drushrc.php.j2

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @see example.aliases.drushrc.php.
77
*/
88

9-
{% macro alias(host, root) %}
9+
{% macro alias(host, root) -%}
1010
{%- if root not in _devtool_docroots %}
1111
$aliases['{{ host }}'] = array(
1212
'uri' => '{{ host }}',
@@ -20,8 +20,9 @@ $aliases['{{ host }}'] = array(
2020
);
2121

2222
{% endif -%}
23-
{% endmacro %}
23+
{% endmacro -%}
2424

25+
{% block aliases -%}
2526
{%- if drupalvm_webserver == 'apache' -%}
2627
{%- for vhost in apache_vhosts -%}
2728
{{ alias(vhost.servername, vhost.documentroot) }}
@@ -32,9 +33,10 @@ $aliases['{{ host }}'] = array(
3233
{%- endif -%}
3334
{%- endfor -%}
3435
{%- elif drupalvm_webserver == 'nginx' -%}
35-
{%- for host in nginx_hosts -%}
36+
{%- for host in nginx_vhosts -%}
3637
{%- for server_name in host.server_name.split() -%}
3738
{{ alias(server_name, host.root) }}
3839
{%- endfor -%}
3940
{%- endfor -%}
4041
{%- endif -%}
42+
{%- endblock %}

0 commit comments

Comments
 (0)