How to use drush to set configuration variables when we set up development environments

Zyxware default image3

I was using Domain Access and Domain Variable modules in my project. I have a lot of domain specific variables which have different values on production, staging and development environments. It takes me a lot of time to set these variables with the corresponding staging and development values each time when I reset these environments. So I was thinking of creating a drush command to update all these configuration variables in single step and I did it.

Its a two-step process. First we need to implement the hook hook_drush_command and then to define its callback. I have created a drush command file config_variables.drush.inc and created a drush command up-config-variables to update all configuration variables. I put it in my .drush folder. It had created in such a way that, it can be used to update variables of projects which uses simple variables system of drupal and also the projects using Domain Access and Domain Variable systems. The following are my code in the drush command file.

/**
 * Implements hook_drush_command().
 */
function config_variables_drush_command() {
  $items = array();

  $items['up-config-variables'] = array(
    'description' => 'Update configuration variables.',
    'arguments' => array(
      'config_file_path' => 'Full path to the configuration file, which defines the variables in JSON format',
    ),
    'examples' => array(
      'drush upconfig /home/variables.conf' => 'Update configuration specified in the file.',
    ),
    'aliases' => array('upconfig'),
  );

  return $items;
}

/**
 * Update configuration variables.
 * 
 * The JSON format should be,
 *  {
 *    "default": {
 *      "variable_name1": "value1",
 *      "variable_name2": "value2"
 *    }
 *  }
 * If the variable is a domain variable, then specify the domain id
 * instead of "default" as,
 *  {
 *    "1": {
 *      "variable_name1": "value1",
 *      "variable_name2": "value2"
 *    },
 *    "2": {
 *      "variable_name1": "value1",
 *      "variable_name2": "value2"
 *    }
 *  }
 * 
 */
function drush_config_variables_up_config_variables($config_file_path) {
  $conf = file_get_contents($config_file_path);
  if ($conf) {
    $conf_variables = json_decode($conf);
    if ($conf_variables) {
      foreach ($conf_variables as $key => $variables) {
        foreach ($variables as $name => $value) {
          if ($key == 'default') {
            variable_set($name, $value);
            drush_print(dt($name . ' updated on ' . $key));
          }
          elseif ((is_numeric($key))
            && module_exists('domain')
            && module_exists('variable_store')) {
            $realm_key = domain_load_machine_name($key);
            variable_store_set('domain', $realm_key, $name, $value);
            drush_print(dt($name . ' updated on ' . $realm_key));
          }
          else {
            drush_print(dt('Error: ' . $name . ' is not updated on ' . $realm_key));
          }
        }
      }
    }
  }
}

All configuration variables should be defined in the JSON format in a file. The above drush command will accept a parameter to get the full path to this configuration file and will set those variables with the values specified in that file.

Its quite easy to write a drush command like this and it saved my valuable time a lot. If you face any doubts while configuring, please let us know. Also, you may look into other solved issues related to drush over here.