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

Commit dcc66c9

Browse files
committed
Merge pull request #694 from oxyc/drupal-project
Issue #506: Add support for composer based site builds
2 parents 6f7647e + c5c8c8a commit dcc66c9

File tree

11 files changed

+199
-15
lines changed

11 files changed

+199
-15
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ env:
55
global:
66
- CONFIG: tests/config.yml
77
MAKEFILE: example.drupal.make.yml
8+
COMPOSERFILE: example.drupal.composer.json
89
HOSTNAME: drupalvm.dev
910
MACHINE_NAME: drupalvm
1011
IP: 192.168.88.88
@@ -57,6 +58,7 @@ script:
5758
# Copy configuration files into place.
5859
- 'sudo docker exec "$(cat ${container_id})" cp ${DRUPALVM_DIR}/${CONFIG} ${config_dir:-$DRUPALVM_DIR}/config.yml'
5960
- 'sudo docker exec "$(cat ${container_id})" cp ${DRUPALVM_DIR}/${MAKEFILE} ${config_dir:-$DRUPALVM_DIR}/drupal.make.yml'
61+
- 'sudo docker exec "$(cat ${container_id})" cp ${DRUPALVM_DIR}/${COMPOSERFILE} ${config_dir:-$DRUPALVM_DIR}/drupal.composer.json'
6062

6163
# Override configuration variables.
6264
- '[[ ! -z "${local_config}" ]] && sudo docker exec "$(cat ${container_id})" bash -c "cp ${DRUPALVM_DIR}/${local_config} ${config_dir:-$DRUPALVM_DIR}/local.config.yml" || true'

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ If you want to switch from Drupal 8 (default) to Drupal 7 on the initial install
5252

5353
## Quick Start Guide
5454

55-
This Quick Start Guide will help you quickly build a Drupal 8 site on the Drupal VM using the included example Drush make file. You can also use the Drupal VM with a [Local Drupal codebase](http://docs.drupalvm.com/en/latest/deployment/local-codebase/) or even a [Drupal multisite installation](http://docs.drupalvm.com/en/latest/deployment/multisite/).
55+
This Quick Start Guide will help you quickly build a Drupal 8 site on the Drupal VM using the included example Drush make file. You can also use the Drupal VM with a [Composer based installation](http://docs.drupalvm.com/en/latest/deployment/composer/), with a [Local Drupal codebase](http://docs.drupalvm.com/en/latest/deployment/local-codebase/) or even a [Drupal multisite installation](http://docs.drupalvm.com/en/latest/deployment/multisite/).
5656

5757
### 1 - Install Vagrant
5858

default.config.yml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ vagrant_public_ip: ""
2121
# http://docs.drupalvm.com/en/latest/extras/syncing-folders/ for more info.
2222
vagrant_synced_folders:
2323
# The first synced folder will be used for the default Drupal installation, if
24-
# build_makefile: is 'true'. By default it's set to the drupal-vm folder.
24+
# any of the build_* settings are 'true'. By default the folder is set to
25+
# the drupal-vm folder.
2526
- local_path: .
2627
destination: /var/www/drupalvm
2728
type: nfs
@@ -34,17 +35,29 @@ vagrant_cpus: 2
3435
# The web server software to use. Can be either 'apache' or 'nginx'.
3536
drupalvm_webserver: apache
3637

37-
# Set this to false if you are using a different site deployment strategy and
38+
# Set this to 'false' if you are using a different site deployment strategy and
3839
# would like to configure 'vagrant_synced_folders' and 'apache_vhosts' manually.
3940
build_makefile: true
4041
drush_makefile_path: "{{ config_dir }}/drupal.make.yml"
4142

42-
# Set this to false if you don't need to install drupal (using the drupal_*
43+
# Set 'build_makefile' to 'false' and this to 'true' if you are using a
44+
# composer based site deployment strategy.
45+
build_composer: false
46+
drupal_composer_path: "{{ config_dir }}/drupal.composer.json"
47+
drupal_composer_install_dir: "/var/www/drupalvm/drupal"
48+
drupal_composer_dependencies: []
49+
50+
# Set this to 'true' and 'build_makefile', 'build_composer' to 'false' if you
51+
# are using Composer's create-project as a site deployment strategy.
52+
build_composer_project: false
53+
drupal_composer_project_package: ""
54+
drupal_composer_project_options: "--prefer-dist --stability dev --no-interaction"
55+
56+
# Set this to 'false' if you don't need to install drupal (using the drupal_*
4357
# settings below), but instead copy down a database (e.g. using drush sql-sync).
4458
install_site: true
4559

46-
# Settings for building a Drupal site from a makefile (if 'build_makefile:'
47-
# is 'true').
60+
# Settings for installing a Drupal site (if 'install_site:' is 'true').
4861
drupal_major_version: 8
4962
drupal_core_path: "/var/www/drupalvm/drupal"
5063
drupal_domain: "{{ vagrant_hostname }}"
@@ -138,8 +151,8 @@ nginx_hosts:
138151
nginx_remove_default_vhost: true
139152
nginx_ppa_use: true
140153

141-
# MySQL Databases and users. If build_makefile: is true, first database will
142-
# be used for the makefile-built site.
154+
# MySQL Databases and users. If 'install_site' is 'true', first database will
155+
# be used for the site.
143156
mysql_databases:
144157
- name: "{{ drupal_mysql_database }}"
145158
encoding: utf8
@@ -227,8 +240,8 @@ composer_path: /usr/bin/composer
227240
composer_home_path: "/home/{{ drupalvm_user }}/.composer"
228241
composer_home_owner: "{{ drupalvm_user }}"
229242
composer_home_group: "{{ drupalvm_user }}"
230-
# composer_global_packages:
231-
# - { name: phpunit/phpunit, release: '@stable' }
243+
composer_global_packages:
244+
- { name: hirak/prestissimo, release: '^0.2' }
232245

233246
# Run specified scripts before or after VM is provisioned. Path is relative to
234247
# the `provisioning/playbook.yml` file.

docs/deployment/composer.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Drupal VM is configured to use [Drush make](drush-make.md) by default but supports building Drupal from a custom composer.json or creating a project from a composer package (`composer create-project`).
2+
3+
## Using composer.json
4+
5+
1. Copy `example.drupal.composer.json` to `drupal.composer.json` and modify it to your liking.
6+
2. Switch the build system by setting `build_makefile: false` and `build_composer: true` in your `config.yml`.
7+
3. Configure `drupal_core_path` to point to the webroot directory: `drupal_core_path: {{ drupal_composer_install_dir }}/docroot`
8+
9+
```yaml
10+
build_makefile: false
11+
build_composer: true
12+
drupal_core_path: "{{ drupal_composer_install_dir }}/docroot"
13+
```
14+
15+
_The file set in `drupal_composer_path` (which defaults to `drupal.composer.json`) will be copied from your host computer into the VM's `drupal_composer_install_dir` and renamed `composer.json`. If you already have a composer.json within that directory, you can set `drupal_composer_path: false`._
16+
17+
## Using Composer when [Drupal VM is a composer dependency itself](../other/drupalvm-composer-dependency.md)
18+
19+
In the scenario where you already have an existing `composer.json` in the root of your project, follow the usual steps for installing with a composer.json but instead of creating a `drupal.composer.json` file, disable the transfering of the file by setting `drupal_composer_path` to `false`, and change `drupal_composer_install_dir` to point to the the directory where it will be located. If `drupal_composer_path` is not truthy, Drupal VM assumes it already exists.
20+
21+
```yaml
22+
build_makefile: false
23+
build_composer: true
24+
drupal_composer_path: false
25+
drupal_composer_install_dir: "/var/www/drupalvm"
26+
drupal_core_path: "{{ drupal_composer_install_dir }}/docroot"
27+
```
28+
29+
## Creating a project from a composer package: `composer create-project`
30+
31+
There's a couple of things you need to configure in your `config.yml`:
32+
33+
- Switch the build system by setting `build_makefile: false`, `build_composer: false` and `build_composer_project: true`.
34+
- Configure the composer package in `drupal_composer_project_package`.
35+
- Additionally you can adjust the create-project CLI options in `drupal_composer_project_options` as well as add additional dependencies in `drupal_composer_dependencies`.
36+
- Ensure that the webroot configured in the composer package matches the one set in `drupal_core_path`.
37+
38+
With [drupal-composer/drupal-project](https://github.com/drupal-composer/drupal-project) as an example your `config.yml` overrides would be:
39+
40+
```yaml
41+
build_makefile: false
42+
build_composer: false
43+
build_composer_project: true
44+
45+
drupal_composer_project_package: "drupal-composer/drupal-project:8.x-dev"
46+
# Added `--no-dev` to avoid installing development dependencies.
47+
drupal_composer_project_options: "--prefer-dist --stability dev --no-interaction --no-dev"
48+
drupal_composer_dependencies:
49+
- "drupal/devel:8.*"
50+
51+
drupal_core_path: "{{ drupal_composer_install_dir }}/web"
52+
```
53+
54+
## Improving composer build performance
55+
56+
Opting for composer based installs will most likely increase your VM's time to provision considerably.
57+
58+
If you manage multiple VM's own your computer, you can use the [`vagrant-cachier` plugin](http://fgrehm.viewdocs.io/vagrant-cachier/) to share Composer's package cache across all VM's. The first build will be as slow as before but subsequent builds with the same `vagrant_box` (eg `geerlingguy/ubuntu1604`) will be much faster.
59+
60+
1. Install the plugin on your host computer: `vagrant plugin install vagrant-cachier`
61+
2. Create a `Vagrantfile.local` next to your `config.yml` with the following:
62+
63+
```rb
64+
config.cache.scope = :box
65+
config.cache.auto_detect = false
66+
config.cache.enable :generic, { :cache_dir => "/home/vagrant/.composer/cache" }
67+
```
68+
69+
_Note: Out of the box, sharing the Composer cache is not supported as the plugin requires PHP to be installed when the VM first boots up. This is why the generic cache bucket is used instead._
70+
71+
_You can also use this plugin to share other package manager caches. For more information read the [documentation](http://fgrehm.viewdocs.io/vagrant-cachier/usage/)._

example.drupal.composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "custom-project/drupal-vm",
3+
"description": "",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "",
9+
"role": ""
10+
}
11+
],
12+
"repositories": [
13+
{
14+
"type": "composer",
15+
"url": "https://packagist.drupal-composer.org"
16+
}
17+
],
18+
"require": {
19+
"composer/installers": "^1.0.20",
20+
"drupal-composer/drupal-scaffold": "^2.0.1",
21+
"drupal/core": "~8.0",
22+
"drupal/devel": "8.*"
23+
},
24+
"minimum-stability": "dev",
25+
"prefer-stable": true,
26+
"extra": {
27+
"installer-paths": {
28+
"docroot/core": ["type:drupal-core"],
29+
"docroot/modules/contrib/{$name}": ["type:drupal-module"],
30+
"docroot/profiles/contrib/{$name}": ["type:drupal-profile"],
31+
"docroot/themes/contrib/{$name}": ["type:drupal-theme"],
32+
"drush/contrib/{$name}": ["type:drupal-drush"]
33+
}
34+
}
35+
}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pages:
1212
- Home: 'index.md'
1313
- Drupal Deployment Strategies:
1414
- 'Drush Make file': 'deployment/drush-make.md'
15+
- 'Composer': 'deployment/composer.md'
1516
- 'Local Drupal codebase': 'deployment/local-codebase.md'
1617
- 'Drupal multisite': 'deployment/multisite.md'
1718
- Extra Software and Setup:

provisioning/playbook.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@
8080
- name: Check if Drupal is already set up.
8181
stat: "path={{ drupal_core_path }}/index.php"
8282
register: drupal_site
83+
ignore_errors: true
84+
85+
- name: Define drupal_site_exists
86+
set_fact:
87+
drupal_site_exists: "{{ drupal_site.stat.exists|default(false) }}"
8388

8489
- include: tasks/sshd.yml
8590
- include: tasks/extras.yml
@@ -91,6 +96,14 @@
9196
- include: tasks/build-makefile.yml
9297
when: build_makefile
9398

99+
# Build with composer if configured.
100+
- include: tasks/build-composer.yml
101+
when: build_composer
102+
103+
# Build a composer project if configured.
104+
- include: tasks/build-composer-project.yml
105+
when: build_composer_project
106+
94107
# Install site if configured.
95108
- include: tasks/install-site.yml
96109
when: install_site
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
- name: Generate Drupal project with composer package.
3+
command: >
4+
{{ composer_path }} create-project
5+
{{ drupal_composer_project_package }} {{ drupal_composer_install_dir }}
6+
{{ drupal_composer_project_options|default('--prefer-dist --no-interaction') }}
7+
when: not drupal_site_exists
8+
become: no
9+
10+
- name: Install dependencies with composer require.
11+
command: >
12+
{{ composer_path }} require {{ item }}
13+
chdir={{ drupal_composer_install_dir }}
14+
with_items: "{{ drupal_composer_dependencies|default([]) }}"
15+
become: no

provisioning/tasks/build-composer.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
- name: Ensure drupal_composer_install_dir directory exists.
3+
file:
4+
path: "{{ drupal_composer_install_dir }}"
5+
state: directory
6+
mode: 0775
7+
become: no
8+
when: drupal_composer_path and not drupal_site_exists
9+
10+
- name: Copy composer.json into place.
11+
copy:
12+
src: "{{ drupal_composer_path }}"
13+
dest: "{{ drupal_composer_install_dir }}/composer.json"
14+
when: drupal_composer_path and not drupal_site_exists
15+
become: no
16+
17+
- name: Run composer install.
18+
command: >
19+
composer install
20+
chdir={{ drupal_composer_install_dir }}
21+
when: not drupal_site_exists
22+
become: no
23+
24+
- name: Install dependencies with composer require.
25+
command: >
26+
{{ composer_path }} require {{ item }}
27+
chdir={{ drupal_composer_install_dir }}
28+
with_items: "{{ drupal_composer_dependencies|default([]) }}"
29+
become: no

provisioning/tasks/build-makefile.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
copy:
44
src: "{{ drush_makefile_path }}"
55
dest: /tmp/drupal.make.yml
6-
when: drupal_site.stat.exists == false
6+
when: not drupal_site_exists
77

88
- name: Ensure drupal_core_path directory exists.
99
file:
@@ -12,23 +12,23 @@
1212
recurse: yes
1313
mode: 0775
1414
become: no
15-
when: drupal_site.stat.exists == false
15+
when: not drupal_site_exists
1616

1717
- name: Generate Drupal site with drush makefile.
1818
command: >
1919
{{ drush_path }} make -y /tmp/drupal.make.yml --no-gitinfofile
2020
chdir={{ drupal_core_path }}
21-
when: drupal_site.stat.exists == false
21+
when: not drupal_site_exists
2222
become: no
2323

2424
- name: Check if a composer.json file is present.
2525
stat: "path={{ drupal_core_path }}/composer.json"
2626
register: drupal_core_composer_file
27-
when: drupal_site.stat.exists == false
27+
when: not drupal_site_exists
2828

2929
- name: Run composer install if composer.json is present.
3030
command: >
3131
composer install
3232
chdir={{ drupal_core_path }}
33-
when: drupal_site.stat.exists == false and drupal_core_composer_file.stat.exists == true
33+
when: not drupal_site_exists and drupal_core_composer_file.stat.exists
3434
become: no

tests/ubuntu-16-nginx.config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
---
22
drupalvm_webserver: nginx
3+
4+
# Test composer based installation.
5+
build_makefile: false
6+
build_composer: true
7+
drupal_core_path: "{{ drupal_composer_install_dir }}/docroot"

0 commit comments

Comments
 (0)