Advanced Drush

 

Advanced Drush
Moshe Weitzman

Drush is the Drupal shell that allows you to affect a Drupal site from command line. You can write scripts to increase:

  • Ease of installation and configuration
  • Speed of maintenance
  • Reproducability of actions

A reaction from the audience (check out the guy to the left)

Drush 4 HEAD

The last release happened at DrupalCon San Francisco, so there's a lot of new stuff in here. We've added a lot of commands around managing users:

  • user-cancel
  • user-create
  • user-add-role, remove-role
  • user-block, unblock
  • user-password
  • user-information

All these will still work amongst Drupal 5, 6 and 7.

We also have field integration, which gets rid of a bit of the tedium of setting up content types:

  • field-create
  • field-clone
  • field-update
  • field-delete
  • field-info

This is only for Drupal 7, since it uses the Field API. To create a field you pass it the name of the field, field type and widget type; it will create that field and give you the URL to go to to complete field configuration. There was no good way to programmatically fill out that information.

updatecode --lock

will pin the module to the current version; we will write a lock file in the directory where that module resides, so it gets ignored by update code later. This is great if you've hacked a module (tee hee!)

drush help --html

will give you a detailed HTML report of all commands. The --html option is a new option, you can post that HTML somewhere on your intranet, for example. Also, drush.ws is the new online drush reference.

sql-sync --sanitize

sql-sync makes a copy of a database onto another database that may or may not be on the same server. It does a mysqldump/gzip/rsync/un-gzip/mysql, but in an automated fashion. It automates the SSH connections and mysql arguments as well.

The new piece is the --sanitize option, which can call sanitize hooks that modules can use to wipe email addresses, for example, or other information that should be sanitized in their own tables. I have a hope that people will start using this and we will stop passing around sensitive information to our developers and so forth.

policy.drush.inc

To prevent developers from doing disastrous things with Drush, you can do things like prevent developers from overwriting your production database with sql-sync.

Drush 3

In Drush 3 we introduced core-cli, a customized bash shell for drush. You don't have to type 'drush' all the time, you can just type the command name. You can also use site aliases to run commands on multiple sites (e.g. @sites). Submodules are downloaded to the .drush folder. Also includes sql-sync, rsync, and site-upgrade.

You can use aliases to sync:
drush rsync @dev @stage
drush sql-sync @dev @stage

Drush Commands

drush -i ./examples mmas
-i includes the ./examples directory, and mmas is the command name. It generates an error message unless run as sudo:
sudo drush -i ./examples mmas

The first thing you do when writing drush commands is to implement hook_drush_command(), which is similar to the menu hook in Drupal. You can specify aliases, and even specify the level of bootstrapping you want, from a full Drupal bootstrap to only bootstrapping drush.

There are also validate hooks for each command of the form drush_hook_COMMAND_validate(). Here is where we can set that the command must be run by the root user (as in this example.)

The standard drush callback, in this example drush_sandwich_make_me_a_sandwich($filling = 'ascii').

Did you enjoy this post? Please spread the word.