Defining Servers and Websites

Page

A prerequisite to the following is having a .drush directory set up and stored in a repository; see the previous section for details.

Defining a Default Drush Aliases File

The .drush/defaults.aliases.drushrc.php file contains an alias called prod which will be the parent of all the servers we will define. Its contents:

<?php
$aliases['prod'] = array(
  'path-aliases' => array(
    '%dump' => '/tmp/dump-'.time().'prod.sql',
    '%files' => 'files',
   ),
);

$aliases['localdev'] = array(
  'path-aliases' => array(
    '%dump' => '/tmp/dump-'.time().'localdev.sql',
    '%files' => 'files',
   ),
);

Telling Drush About Your Servers

Creating an alias (nickname) for each of your servers allows drush to associate websites with servers. Here is an example .drush/servers.aliases.drushrc.php that defines several servers: a testing server, a preview (staging) server, and two production servers for the basketweaving and berrypicking departments.

<?php
$aliases['testserver'] = array(
  'parent' => '@defaults.prod',
  'remote-host' => 'test.biology-it.iastate.edu',
);
$aliases['previewserver'] = array(
    'parent' => '@defaults.prod',
    'remote-host' => 'preview.biology-it.iastate.edu',
);
$aliases['basketweavingserver'] = array(
  'parent' => '@defaults.prod',
  'remote-host' => 'www.basketweaving.iastate.edu',
);
$aliases['berrypickingserver'] = array(
  'parent' => '@defaults.prod',
  'remote-host' => 'www.berrypicking.iastate.edu',
);

Notice that we are creating a tree. Each server has @default.prod (created in the Defining a Default Drush Aliases File, above) as its parent. You guessed it: next we will create aliases for websites and put them into the tree under each server.

Telling Drush About Your Websites

Look at the following file, which lives in .drush/luggage_doc.aliases.drushrc.php carefully. It is the drush alias file for this website, the one you are reading. First we define some constants which might be helpful in deployment scripts. There is nothing magic about 'luggage_docconstants'; we just decided that the alias for a website plus the string 'constants' was a unique string that we could use in scripts to get values. Thus any property we want to define about this website will go in the keyed array under luggage_docconstants. We just have the repo for this website but we could have defined the netid of the site owner, an emergency contact number, or whatever.

We then define aliases for each environment. This allows us to refer to a site on a certain environment using an alias name like @luggage_doc.preview for this site on the staging server or @luggage_doc.live for this site on the website you are viewing. There is also a local environment, so I can refer to @luggage_doc.local if I am working on it locally. For example,

drush @luggage_doc.local cc all

will clear all the caches on a local version of this website. You guessed it; I can also clear the caches on the live site from the command line on my development box with

drush @luggage_doc.live cc all

Note that in order to do that, I need a few more things in place, but that is the end goal of all this: intuitive, simple interactions with multiple websites in multiple environments.

Here is .drush/luggage_doc.aliases.drushrc.php. It is one of many such files in .drush; we use one file per website to keep things sane.

<?php
$aliases['luggage_docconstants'] = array(
  'repo' => 'git@github.com:isubit/luggage_docs.git',
);
$aliases['live'] = array(
  'parent' => '@basketweavingserver',
  'root' => '/var/www/html/www.biology-it.iastate.edu/luggage_doc',
  'uri' => 'www.biology-it.iastate.edu/luggage_doc',
);
$aliases['preview'] = array(
  'parent' => '@previewserver',
  'root' => '/var/www/html/luggage_docs',
  'uri' => 'preview.biology-it.iastate.edu/luggage_doc',
);
$aliases['test'] = array(
  'parent' => '@testserver',
  'root' => '/var/www/html/luggage_doc',
  'uri' => 'test.biology-it.iastate.edu/luggage_doc',
);
$aliases['local'] = array(
  'parent' => '@defaults.localdev',
  'root' => getenv('WEBROOT').'luggage_doc',
  'uri' => getenv('URI').'luggage_doc',
);

Checking That Everything is Working

You know that you have things defined correctly when you can ask drush about a site alias and get a response. Here we ask drush about @luggage_doc:

drush sa @luggage_doc

We receive this output:

$aliases['luggage_doc.live'] = array (
  'path-aliases' => 
  array (
    '%dump' => '/tmp/dump-1408243783prod.sql',
    '%files' => 'files',
  ),
  '#file' => '/Users/john/.drush/luggage_doc.aliases.drushrc.php',
  'remote-host' => 'www.basketweaving.iastate.edu',
  'root' => '/var/www/html/www.biology-it.iastate.edu/luggage_doc',
  'uri' => 'www.biology-it.iastate.edu/luggage_doc',
);
...