Best method to enable/disable modules depending on the environment What would be the best solution to enable/disable modules depending on the site environment (dev, stage or production)?


Created a database dump containing the system table for each installation. Then when deploying I use mysql -uuser -ppass database &lt; dump-&lt;type&gt;.sql where the type reflects the kind of environment.
Created a shell script for each environment, that uses drush to enable or disable needed modules and set specific site settings.
Wrote a small deploy script for each site that looks something like this:

// change working directory to drupal installation path
chdir('public_html');

// define root, include bootstrap.inc and start drupal
define('DRUPAL_ROOT', getcwd());
require_once(DRUPAL_ROOT . '/includes/bootstrap.inc');
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// modules that should be disabled/enabled on stage site
$modules = array(
   'field_ui' =&gt; 1,
   'toolbar' =&gt; 1,
   'update' =&gt; 1,
   'overlay' =&gt; 0,
   'feeds_import' =&gt; 0,
   'feeds_ui' =&gt; 0,
   'feeds' =&gt; 0,
   'menu' =&gt; 1,
   'advanced_help' =&gt; 1,
   'job_scheduler' =&gt; 1,
   'wysiwyg' =&gt; 1,
   'views_ui' =&gt; 1,
   'dblog' =&gt; 1,
);      

// enable/disable modules and set site specific variables
foreach ($modules as $module =&gt; $state):
  if ($state == 1):
    if (!module_exists($module)):
      module_enable(array($module), FALSE);
    endif;      
  else:
    if (module_exists($module)):
      module_disable(array($module), FALSE);
    endif;
  endif;
endforeach;     

variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL);
variable_set('preprocess_css', 0);
variable_set('preprocess_js', 0);
variable_set('cache', 1);
variable_set('page_compression', 0);
variable_set('block_cache', 0);



Does anyone have a better solution?
One possible answer is the Environment module (the 7.x dev release works just fine). It allows you to define arbitrary environments (comes configured with "Development" and "Production", but you can have more if necessary). It then provides drush environment-switch to switch between environments. You can then implement hook_environment_switch() to do anything you want when the environment is switched. There's no built-in support for enabling and disabling modules, but that's easy enough to do in code, as you demonstrate above. It's also convenient for doing things like changing variable values and blocking development users. 

You then put something like drush environment-switch production in your deploy steps.

This also has the benefit of being able to switch back to Development easily. For instance, I have views_ui disabled in Production, but enabled in Development. If I need to urgently check a view in the ui on the production site, I can switch the environment to Development, do what I need, and switch back.

It's also nice that since it's implemented in hooks, you can keep different types of functionality in different modules, and you can keep these things in the code base and under version control.