provision.drush.inc 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php
/**
 * Drush commands
 *
 *
 * This module provides a framework for a Drupal site to manage and install new Drupal sites, using the command line
 * Drush utility. 
 *
 * It allows for pluggable 'provisioning modules' that can extend and modify the tasks that are taken during installation.
 *
 * Each site has the following commands that can be run on it.
 *
 * Implemented : 
 *   install - Install a new Drupal site. The install command uses 3 separate hooks to do it's job, 
 *              namely hook_provision_pre_install(), hook_provision_install() and hook_provision_post_install()
 *   verify  - Recreate all configuration files, to be in synch with changes in the front end. And test that they are correct.
 *   stats   - Return an associated array of site statistics. (implemented in provision_stats module, is thus optional)
 *   import  - Import the details of an already existing site into the provisioning framework.
 *             This command inspects the settings.php and generates the site.php file that the framework uses for configuration.
 *   backup  - Generates a tarball containing the sites directory, the site data configuration and the database dump.
 *             This allows the tarball to act as a 'site package', which can be redeployed on other installations,
 *             or used for an upgrade.
 *   disable - Disable an installed Drupal site. Changes the virtual host config file so that it redirects to provision_disabled_site_redirect_url
 *   enable  - Re-enable a site that has already been disabled. Recreates the virtual host file.
 *   delete -  Generates a back up of the site, and then removes all references to it.
 *   restore - Revert to a previous backup of the site.
 *   cron - Run cron process for a site
 *
 *   upgrade - Accepts a site package (backup) as argument, and redeploys it, running the upgrade processes on it.
 *             Uses hook_provision_pre_upgrade(), hook_provision_upgrade() and hook_provision_post_upgrade() hooks,
 *             and allows clean roll back if any errors occur. Will include stringent checking of module versions,
 *             and allow unit tests to be run.
33
 * Not implemented yet :
34 35 36 37 38 39 40
 *   rename  - Change the url of a site. This requires moving of files, and numerous other issues.
 */

/**
 * @defgroup provisiondrush Command line interface for Provision.
 * @{
 */
41 42 43 44 45 46 47 48 49 50 51 52 53 54
include_once('provision.inc');
include_once('provision.path.inc');
provision_init();
/** 
 * Implementation of hook_init
 *
 * Provide defines for all the major paths and settings.
 * These are settings that must not be modified during the running of the
 * program, but are configurable.
 */
function provision_init() {
  // Do not allow the program to be run as the root user. ever
  $name = posix_getpwuid(posix_geteuid());
  if ($name['name'] == 'root') {
55
    return drush_set_error(PROVISION_IS_ROOT, dt('You are running the provision script as the root user. Exiting'));
56 57 58
  }

  // Set up defines for platform
59
  $docroot = drush_get_option(array("r", "root"), $_SERVER['PWD']);
60 61 62 63 64 65 66 67 68 69 70
  
  define('PROVISION_DOMAIN', $_SERVER['HTTP_HOST']);
  // Paths
  $path =  ($docroot) ? $docroot : $_SERVER['DOCUMENT_ROOT'];
  define('PROVISION_DOCROOT_PATH', rtrim($path, '/'));
  define('PROVISION_SITES_PATH', rtrim($path, '/') .'/sites');
  define('PROVISION_DRUSH_PATH', './drush.php');
  $parts = explode("/", rtrim($path, '/'));
  array_pop($parts);
  define('PROVISION_PARENT_PATH', rtrim(implode("/" , $parts), '/'));
  define('PROVISION_BACKUP_PATH', 
71
    drush_get_option('backup_path', PROVISION_PARENT_PATH . '/backups'));
72
  define('PROVISION_CONFIG_PATH', 
73
    drush_get_option('config_path', PROVISION_PARENT_PATH .'/config'));
74 75 76 77
  define('PROVISION_VHOST_PATH', PROVISION_CONFIG_PATH .'/vhost.d');

  // Commands
  define('PROVISION_RESTART_CMD',
78
    drush_get_option('restart_cmd', _provision_default_restart_cmd()));
79 80 81 82

  // System account
  $info = posix_getgrgid(posix_getgid());
  define('PROVISION_WEB_GROUP', 
83
    drush_get_option('web_group', $info['name'] ));
84
  define('PROVISION_SCRIPT_USER', 
85
    drush_get_option('script_user', get_current_user() ));
86 87 88

  // Redirection urls
  define('PROVISION_MASTER_URL', 
89
    drush_get_option('master_url',  $GLOBALS['base_url']));
90 91 92 93 94
  define('PROVISION_WEB_DISABLE_URL', PROVISION_MASTER_URL .'/provision/disabled');
  define('PROVISION_WEB_MAINTENENCE_URL', PROVISION_MASTER_URL .'/provision/maintenance');

  // Database
  define('PROVISION_MASTER_DB', 
95
    drush_get_option('master_db', $GLOBALS['db_url']));
96 97 98 99 100
  $db = parse_url(PROVISION_MASTER_DB);
  define('PROVISION_DB_USER', $db['user']);
  define('PROVISION_DB_PASSWD', $db['pass']);
  define('PROVISION_DB_HOST', $db['host']);
 
101
  define('PROVISION_DB_TYPE', $db['scheme']);
102 103
}

104 105 106 107 108

/**
 * Implementation of hook_drush_command().
 */
function provision_drush_command() {
109

110 111 112
  $items['provision install'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('install'),
113 114
    'arguments' => array('domain.com' => dt('The domain of the site to install.')),
    'description' => dt('Provision a new site using the provided data.'),
115
    'bootstrap' => -1
116 117 118 119 120
  );

  $items['provision import'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('import'),
121 122
    'arguments' => array('domain.com' => dt('The domain of the site to import.')),
    'description' => dt('Turn an already running site into a provisioned site.'),
123
    'bootstrap' => -1
124 125 126 127 128
  );

  $items['provision update'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('update'),
129
    'arguments' => array('domain.com' => dt('The domain of the site to update.')),
130 131
    'description' => dt('Run any outstanding updates on the site.'),
    'bootstrap' => -1
132 133 134 135 136
  );
  
  $items['provision backup'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('backup'),
137 138
    'arguments' => array('domain.com' => dt('The domain of the site to back up.')),
    'optional arguments' => array('backup-file' => dt('The file to save the backup to. This will be a gzipped tarball.')),
139 140
    'description' => dt('Generate a back up for the site.'),
    'bootstrap' => -1
141 142 143 144 145
  );
  
  $items['provision enable'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('enable'), 
146
    'arguments' => array('domain.com' => dt('The domain of the site to enable (only if enabled).')),
147 148
    'description' => 'Enable a disabled site.',
    'bootstrap' => -1
149 150 151 152
  );
  $items['provision disable'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('disable'), 
153
    'arguments' => array('domain.com' => dt('The domain of the site to disable (only if disabled).')),    
154 155
    'description' => 'Disable a site.',
    'bootstrap' => -1
156 157 158 159 160
  );

  $items['provision verify'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('verify'),
161 162
    'arguments' => array('domain.com' => dt('The domain of the site to verify).')),    
    'description' => 'Verify that the provisioning framework is correctly installed.',
163
    'bootstrap' => -1
164 165 166 167 168 169
  );
 
  $items['provision restore'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('restore'),
    'description' => 'Restore the site to a previous backup. This will also generate a backup of the site as it was.',
170
    'arguments' => array('domain.com' => dt('The domain of the site to be restored'),
171 172 173 174 175 176 177 178 179 180
      'site_backup.tar.gz' => dt('The backup to restore the site to.')),
    'bootstrap' => -1
  );

  $items['provision deploy'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('deploy'),
    'description' => 'Deploy an existing backup to a new url.',
    'arguments' => array('domain.com' => dt('The domain to deploy the site package to.'),
      'site_backup.tar.gz' => dt('The backup to deploy.')),
181
    'bootstrap' => -1
182
  );
183 184 185 186 187 188 189 190 191

  $items['provision migrate'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('migrate'),
    'description' => 'Migrate a site between platforms.',
    'arguments' => array('domain.com' => dt('The domain to migrate. Any outstanding updates will be run.'),
      '/path/to/platform' => dt('The platform to migrate the site to.')),
    'bootstrap' => -1
  );
192 193 194 195
 
  $items['provision delete'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('delete'),
196 197
    'description' => 'Delete a site.',
    'bootstrap' => -1
198 199 200 201 202 203
  );

  $items['provision cron'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('cron'),
    'description' => 'Run cron process for site.',
204 205
    'arguments' => array('domain.com' => dt('The domain of the site to be processed')),
    'bootstrap' => -1
206 207 208 209 210 211
  );
 
  return $items;
}


212 213 214 215
/**
 * Generate a provision.settings.php file to configure provision
 */
function _provision_generate_config() {
216
  drush_log(dt("Generating drushrc.php file"));
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  provision_path("chmod", PROVISION_DOCROOT_PATH . '/drushrc.php', 0600,
     dt('Changed permissions of drushrc.php to @confirm'),
      dt('Could not change permissions of drushrc.php to @confirm'));

  provision_save_platform_data();
  provision_path("chmod", PROVISION_DOCROOT_PATH . '/drushrc.php', 0400,
    dt('Changed permissions of drushrc.php to @confirm'),
    dt('Could not change permissions of drushrc.php to @confirm'));
  return TRUE;
}



function _provision_default_restart_cmd() {
  $command = '/usr/sbin/apachectl'; # a proper default for most of the world
232 233 234 235 236 237 238 239 240 241
  foreach (explode(':', $_SERVER['PATH']) as $path) {
    $options[] = "$path/apache2ctl";
    $options[] = "$path/apachectl";
  }
  # try to detect the apache restart command
  $options[] = '/usr/local/sbin/apachectl'; # freebsd
  $options[] = '/usr/sbin/apache2ctl'; # debian + apache2
  $options[] = $command;

  foreach ($options as $test) {
242 243
    if (is_executable($test)) {
      $command = $test;
244
      break;
245 246 247 248 249 250
    }
  }

  return "sudo $command graceful";
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
function _provision_default_web_group() {
  $common_groups = array(
    'httpd',
    'www-data', 
    'apache',
    'nogroup',
    'nobody',
    PROVISION_WEB_GROUP);

  foreach ($common_groups as $group) {
    if (provision_posix_groupname($group)) {
      return $group;
      break;
    }
  }
  return null;
}