I attended Opscode Chef training a couple weeks ago and got super-psyched about it. I’m working on a project for Jackson River, and utilizing Chef is a big component. While writing the cookbook for this project, I found that I needed more access to Drush.

At first, I used the execute resource, but I couldn’t reliably make the commands idempotent. Furthermore, using execute felt like a hack, and it made my recipes look less elegant. I decided to write custom LWRPs to extend the existing Drush cookbook.

Here’s the result of that effort, my fork of the Drush cookbook: https://github.com/benclark/chef-drush

I’m openly soliciting feedback here. I’m a PHP developer, and this is the first Ruby code I’ve ever written. I cribbed from other cookbooks that attempt to do the same thing (borrowing quite a bit from the Jenkins cookbook providers), and I did my best to be consistent and logical.


drush Lightweight Resources

The drush cookbook is used to interact with Drupal via Drush. It contains the following lightweight resources:

There is also a helper library.

drush_cmd

The drush_cmd lightweight resource provides direct access to Drush. This is the backbone upon which the other resources interact with Drush.

Actions

This lightweight resource provider has the following actions:

Action Description
:execute Default. Indicates that the Drush command should be executed.

Attributes

This lightweight resource provider has the following attributes:

Attribute Kind Description
command String Required. The command sent to Drush. Examples: pm-enable, pm-disable, site-install.
arguments String, Array The arguments to go with the command. Examples: for pm-enable command, devel as argument.
options String, Array The options to go with the command. Examples: for vset command, --exact option.
drupal_root String Required. Equivalent to drush -r <path>, --root=<path>.
drupal_uri String Equivalent to drush -l <http://example.com:8888>, --uri=<http://example.com:8888>.
assume_yes Boolean Equivalent to drush -y, --yes. Defaults to true because chef-client can’t accept user input.
assume_no Boolean Equivalent to drush -n, --no. Defaults to false.
backend Boolean Equivalent to drush --backend. Defaults to false.
shell_input String Optionally pipe a string into the Drush command.
shell_timeout Integer Set a timeout for the shell operation. Defaults to 900 (15 minutes).
shell_user User Run the Drush command as a specific user.
shell_group Group Run the Drush command as a member of a specific group.
block Proc Ruby block to act on the stdout from the Drush command.

Examples

To run drush site-install for example.com:

drush_cmd "site-install" do
  arguments "standard"
  options [ "--account-mail=example@example.com", "--account-name=admin" ]
  drupal_root "/var/www/html"
  drupal_uri "http://www.example.com"
  shell_user "www-data"
  shell_group "www-data"
end

drush_variable

The drush_variable lightweight resource provides the ability to set and delete variables in a given Drupal install.

Actions

This lightweight resource provider has the following actions:

Action Description
:set Default. Set a value for a given variable.
:delete Delete a variable. (@todo)

Attributes

This lightweight resource provider has the following attributes:

Attribute Kind Description
name String Required. The name of the variable. Examples: preprocess_css, site_offline.
value String, Integer, Hash, TrueClass, FalseClass Required. The variable’s value.
drupal_root String Required. Equivalent to drush -r <path>, --root=<path>.
drupal_uri String Equivalent to drush -l <http://example.com:8888>, --uri=<http://example.com:8888>.
shell_timeout Integer Set a timeout for the shell operation. Defaults to 900 (15 minutes).
shell_user User Run the Drush command as a specific user.
shell_group Group Run the Drush command as a member of a specific group.

Examples

Enable CSS preprocessing:

drush_variable "preprocess_css" do
  value 1
  drupal_root "/var/www/html"
  drupal_uri "http://www.example.com"
  shell_user "www-data"
  shell_group "www-data"
end

drush_php_eval

The drush_php_eval lightweight resource executes a block of PHP code.

Actions

This lightweight resource provider has the following actions:

Action Description
:execute Default. Execute the PHP code.

Attributes

This lightweight resource provider has the following attributes:

Attribute Kind Description
php String Required. The PHP code. Examples: variable_set('site_offline', 0).
drupal_root String Required. Equivalent to drush -r <path>, --root=<path>.
drupal_uri String Equivalent to drush -l <http://example.com:8888>, --uri=<http://example.com:8888>.
format String One of the following output formats: var_export, csv, json, list, string, table, yaml. Defaults to var_export.
shell_timeout Integer Set a timeout for the shell operation. Defaults to 900 (15 minutes).
shell_user User Run the Drush command as a specific user.
shell_group Group Run the Drush command as a member of a specific group.
block Proc Ruby block to act on the stdout from the Drush command.

Examples

Generate the $drupal_hash_salt value, and save it into a file:

drupal_hash_salt_path = "/var/www/private/drupal_hash_salt.txt"
drush_php_eval "generate_drupal_hash_salt" do
  php "echo drupal_hash_base64(drupal_random_bytes(55))"
  drupal_root "/var/www/html"
  block do |stdout|
    ::File.open(drupal_hash_salt_path, 'w') { |f| f.write stdout }
  end
  not_if { ::File.exists?(drupal_hash_salt_path) }
end

drush_make

The drush_make lightweight resource turns a given makefile into a Drupal codebase.

Actions

This lightweight resource provider has the following actions:

Action Description
:install Default. Build the makefile.

Attributes

This lightweight resource provider has the following attributes:

Attribute Kind Description
build_path String Required. The path at which to build the makefile.
makefile String Required. Filename or URI of the makefile to use for this build.
shell_timeout Integer Set a timeout for the shell operation. Defaults to 900 (15 minutes).
shell_user User Run the Drush command as a specific user.
shell_group Group Run the Drush command as a member of a specific group.

Examples

Build the example.make makefile:

drush_make "/var/www/html" do
  makefile "http://example.com/examples/example.make"
  shell_user "www-data"
  shell_group "www-data"
end

drush_site_install

The drush_site_install lightweight resource installs Drupal along with modules/themes/configuration using the specified install profile.

Note: this provider assumes that settings.php already has the database credentials provided for the given drupal_uri. You can use a template resource to define settings.php.

Actions

This lightweight resource provider has the following actions:

Action Description
:install Default. Install Drupal from the specified install profile.

Attributes

This lightweight resource provider has the following attributes:

Attribute Kind Description
profile String Required. The install profile to use.
force Boolean Force the installation. Defaults to false.
site_name String Optionally provide the site name.
drupal_root String Required. Equivalent to drush -r <path>, --root=<path>.
drupal_uri String Equivalent to drush -l <http://example.com:8888>, --uri=<http://example.com:8888>.
shell_timeout Integer Set a timeout for the shell operation. Defaults to 900 (15 minutes).
shell_user User Run the Drush command as a specific user.
shell_group Group Run the Drush command as a member of a specific group.

Examples

Install Drupal using the standard profile:

drush_site_install "standard" do
  drupal_root "/var/www/html"
  drupal_uri "http://www.example.com"
  site_name "Example"
end

Helper library

There is a helper library to assist with some of the idempotence.

drupal_present?(path)

This verifies that Drupal core is present at path, but isn’t necessarily bootstrapped via drush status.

drupal_installed?(path, uri = ‘http://default’)

This verifies that Drupal successfully bootstraps at path and uri (via drush status).

drush_vget_json(path, name, uri = ‘http://default’, exact_match = true)

This returns a JSON Object with the results of drush variable-get.

drush_vget_match?(path, name, value, uri = ‘http://default’)

This verifies that a Drupal variable name matches the given value.