At our university we have dev/test/live versions of our sites at urls like
- dev.example.foo.edu
- test.example.foo.edu
- live.example.foo.edu (the real live url is example.foo.edu)
There are certain Drupal site variables (values in the variables table) that need to change depending on the environment (dev, test, live) where the site is running.
The first time the site is migrated to the live environment certain site variables need to magically change from their "dev" values to their "live" values.
After that initial migration we will periodically load the live database into the dev and test sites to update their content. When this happens I want certain site variables to change to their "dev" values.
I don't want the site builder to have to do anything. I want the variables to simply change to the correct values.
It's okay if the site admin wants to manually override these variables. They shouldn't have to, but there could be a need for this...
I mainly need a Drupal 7 solution--bonus if it works with Drupal 6.
Possible solutions:
Does a module exist that will do this?
I haven't found one. I've looked at
- http://drupal.org/project/tadaa (incomplete)
- http://drupal.org/project/context_domain (provides a url "reaction" but context can't provide the kind of variable condition I need. Looked at http://drupal.org/project/context_var (provides a Reaction not a Condition)).
- http://drupal.org/project/strongarm (can set variables...often used with Features...)
Use hook_update_N (http://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_update_N/7)
- This doesn't meet the requirement that the site builder doesn't have to do anything. They would have to copy paste a update_71234() in some install file. They would also have to remember to run update.php in various situations.
Write a custom module?
This works:
/**
* Implementation of hook_cron().
*/
function ucb_varenv_cron() {
global $base_url;
//if (variable_get('ucb_varenv', FALSE) === FALSE) {
$parts = explode('.', $base_url);
$vars = ucb_varenv_vars();
$p1 = str_replace('http://', '', $parts[0]);
while (list($k, $v) = each($vars[$p1])) {
variable_set($k, $v);
}
//variable_set('ucb_varenv', TRUE);
//}
}
function ucb_varenv_vars() {
$vars = array(
'dev' => array(
'cas_server' => "TEST2.example.com",
),
'live' => array(
'cas_server' => "auth.example.edu",
),
);
return $vars;
}
Hook cron seems like a clumsy approach. The commented ucb_varenv code was from and experiment where this would run one time only at hook_boot()--but that seemed like a bad idea.
The more I think about this, I wonder if there is some way to leverage strongarm to do what I need.