As I've been architecting Drupal solutions for almost ten years now, I've accumulated quite a bit of knowledge on devops best practices, which constitutes a sizeable amount of the consulting that I do. This includes documentation, configuration management, development processes and deployment processes. In this article, I'll be introducing Drupal Helpers, a collection of standard scripts and configurations that I use on all of my client projects (where applicable).
At the time of the writing, the repository provides support for the following operations and Drush set-up. Additional tools are always welcome.
- Refreshing a development site's database and files
- Deploying code to a development/integration site
- Deploying code to staging or production sites
- PHP configuration for Drush
- Default Drush alias configuration
- Backing up databases
- Rebuilding a site with its latest Drush makefile
- Deploying Solr onto the GlassFish application server
Refreshing a development site's database and files
The resync-drupal-db-for-dev script essentially deploys the database and files from a staging site (Staging) or production site (Prod) onto a development site, but it also does many other things that should be done as part of that process, devifying it.
It takes source and destination Drush aliases as arguments and the third one, a list of modules to disable, is optional. Here is the usage example:
- resync-drupal-db-for-dev <SOURCE_DRUSH_ALIAS> <DESTINATION_DRUSH_ALIAS> [<MODULES_DISABLE>]
It performs the following tasks:
- Saves a cache-cleared dump of the destination database (DB) as a backup.
- Overwrites the destination DB with the source's.
- Rebuilds the registry in case PHP file locations have changed.
- Updates the DB schema.
- Reverts all features to the code in Features modules.
- Reverts all views to those defined in code.
- Disables modules that shouldn't be enabled during development.
- Enables modules that are helpful for development.
- Sets the files and temporary directories to standard locations.
- Enables on-screen error reporting.
- Disables user-initiated cron runs. This should really be disabled everywhere for performance reasons.
- Clears all caches.
- Overwrites the destination's files directory with the source's.
- Runs cron.
Developers should be doing all of this every time they refresh their DBs. Because it's tricky and time-consuming to do all of these manually, some of the steps are often missed. This leads to configuration mismanagement issues between development sandboxes and other environments. I'd recommend that this script, or another one like it, be run frequently on the authoritative development/integration site (Dev) and local development sites to prevent such mishaps.
The script tries to be as versatile as possible, working in a variety of GNU/Linux environments. If it doesn't work for yours, please submit a merge request so that we can get support added.
Deploying code to a development/integration site
The deploy-drupal-code-dev script is useful for deploying the latest development code that's been merged to the development branch to Dev. As part of that process, it does everything else that's necessary after a code deployment, including clearing external Varnish caches. It doesn't produce output to the screen when running it directly, as it's assumed to be run as a cron job. All output gets redirected to a log file. A best practice is to have it run nightly so that Dev is kept up-to-date.
Deploying code to staging or production sitesThe two (2) scripts deploy-drupal-code-qa and deploy-drupal-code-prod basically follow the same idea as the Dev script above, except that:
- They require a Git release tag as an argument, as only tagged releases should be deployed to Staging and Prod.
- They produce output directly to the screen, as they are intended to be run manually.
PHP configuration for Drush
The drush.ini configuration file (see Configuring php.ini for details) adds Drush-specific configuration to PHP, differentiated by its being run from the command line; Drush's PHP doesn't go through a Web server.
As Drush is often called upon for batch processing, it requires more resources than Web-server PHP. Also, there aren't usually multiple instances of it running so we don't need be as concerned about overflowing resource limits. With Web servers, there could be a huge number of PHP processes running on public-facing sites.
The configuration file does the following:
- Increases the memory limit.
- Ensures all errors are shown.
- Sets the time zone.
- Increases the maximum execution time. You may need to do this for MySQL/variants as well.
Make sure this is set up by placing the file (or a symlink to it) in your /etc/php5/cli/conf.d/ directory (or the equivalent for different systems).
Default Drush alias configuration
The default.alias.drushrc.php Drush alias file is a standard location for storing all common Drush configuration. It needs to be set in each of your site alias' configuration stanzas to use it.
The line is:
$aliases['dev'] = array(
'parent' => '@default',
It could actually use an update now that Better handling of structure-tables and skip-tables options (including cache_* support!) is done. We no longer need to explicitly state which cache tables we need to skip during certain operations.
Backing up databases
The backup-drupal-db script will back up a Drupal DB whenever it's run. It can be run stand-alone or as a Cron job. To save storage space, cache tables will not be included in the compressed dump files. Each is timestamped, and the "LATEST" file will always be a symbolic link to the most recent backup. Old backups are automatically deleted after a set number of days; the default is 60 (~2 months).
Rebuilding a site with its latest Drush makefile
- Takes the site off-line.
- Backs up the existing database.
- Updates the Git repository with the latest code.
- Purges the old code base.
- Rebuilds it.
- Re-installs the previous sites directory (with site-specific configuration).
- Properly sets all file permissions.
- Updates the DB schema.
- Turns the site back on-line.
- Clears all caches.
Deploying Solr onto the GlassFish application server
With Solr 5, it's no longer necessary to run the search engine in an application server. It can run as a stand-alone application. See Install Apache Solr 5 and Drupal Search API on your laptop in minutes for an example of how to do this. For earlier versions though, this is helpful in getting the Java stack set up to run alongside the PHP one.