HomeDevOpsDrupal Settings Per Environment

Drupal Settings Per Environment

We have a need at NEWMEDIA for certain Drupal settings and configurations to be present in production, in staging, and while on a development machine. I will be outlining the problem and a solution we use here at NEWMEDIA.

Problem

We develop and release sites at NEWMEDIA in phased releases. During each iteration new functionality is added to complement the existing functionality. Sound familiar? During integration testing we want to be able to use the live database as the starting point to ensure all of our update hooks are going to work appropriately and to run automated tests to ensure the functionality is working.

However, on the live site there are many Drupal configurations that we would not want to be enabled on our dev or staging server. Some of these are:

commerce payment processor settings (we would want our dev payment credentials used)
securepages (we would want this either off or reconfigured)
devel (we want this enabled on dev)
maillog (we want this enabled only on dev)
mandrill (we want this disabled on dev)
cron (we want certain cronjobs disabled on dev)

Solution

Enter the Environment Module. The environment module lets you specify your environment either via drush or in your settings.php file.

  1. Enable environment and environment_force modules.
  2. In your settings.php file set $conf[‘environment_override’] = ‘development’;
  3. In a helper module use hook_environment_switch. Below is an example:
HOOK_environment_switch($target_env, $current_env, $workflow = NULL) {
  // Declare each optional development-related module
  $devel_modules = array(
    'devel',
    'diff',
    'maillog',
  );

  $prod_modules = array(
    'securepages',
    'mandrill',
  );

  switch ($target_env) {
    case 'production':
      module_disable($devel_modules);
      module_enable($prod_modules);

      // enable crons
      elysia_cron_set_job_disabled('commerce_recurring_cron', FALSE);
      drupal_set_message('Disabled development modules');
      break;
    case 'development':
      module_disable($prod_modules);
      module_enable($devel_modules);

      // disable cron
      elysia_cron_set_job_disabled('commerce_recurring_cron', TRUE);

      // set maillog to not send email
      variable_set('maillog_send', FALSE);

      // give authenticated user devel access
      user_role_grant_permissions(2, array('access devel information'));

    // give another role devel access
      $role2 = user_role_load_by_name('role2');
      user_role_grant_permissions($role2->rid, array('access devel information'));

      drupal_set_message('Enabled development modules');
      break;
  }
}