provision.drush.inc 8.41 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
 *   rename  - Change the url of a site. This requires moving of files, and numerous other issues.
 */

37 38 39 40 41 42
// Do not allow the program to be run as the root user. ever
$name = posix_getpwuid(posix_geteuid());
if ($name['name'] == 'root') {
  return drush_set_error(PROVISION_IS_ROOT, dt('You are running the provision script as the root user. Exiting'));
}

43 44 45 46
/**
 * @defgroup provisiondrush Command line interface for Provision.
 * @{
 */
47 48
include_once('provision.inc');
include_once('provision.path.inc');
49 50 51 52 53

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

55 56 57
  $items['provision install'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('install'),
58 59
    'arguments' => array('domain.com' => dt('The domain of the site to install.')),
    'description' => dt('Provision a new site using the provided data.'),
60
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
61 62 63 64 65
  );

  $items['provision import'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('import'),
66 67
    'arguments' => array('domain.com' => dt('The domain of the site to import.')),
    'description' => dt('Turn an already running site into a provisioned site.'),
68
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
69 70 71 72 73
  );

  $items['provision update'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('update'),
74
    'arguments' => array('domain.com' => dt('The domain of the site to update.')),
75
    'description' => dt('Run any outstanding updates on the site.'),
76
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
77 78 79 80 81
  );
  
  $items['provision backup'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('backup'),
82 83
    '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.')),
84
    'description' => dt('Generate a back up for the site.'),
85
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
86 87 88 89 90
  );
  
  $items['provision enable'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('enable'), 
91
    'arguments' => array('domain.com' => dt('The domain of the site to enable (only if enabled).')),
92
    'description' => 'Enable a disabled site.',
93
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
94 95 96 97
  );
  $items['provision disable'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('disable'), 
98
    'arguments' => array('domain.com' => dt('The domain of the site to disable (only if disabled).')),    
99
    'description' => 'Disable a site.',
100
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
101 102 103 104 105
  );

  $items['provision verify'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('verify'),
106 107
    'arguments' => array('domain.com' => dt('The domain of the site to verify).')),    
    'description' => 'Verify that the provisioning framework is correctly installed.',
108
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, 
109 110 111 112 113 114
  );
 
  $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.',
115
    'arguments' => array('domain.com' => dt('The domain of the site to be restored'),
116
      'site_backup.tar.gz' => dt('The backup to restore the site to.')),
117
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
118 119 120 121 122 123 124 125
  );

  $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.')),
126
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
127
  );
128 129 130 131 132 133 134

  $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.')),
135
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
136
  );
137 138 139 140
 
  $items['provision delete'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('delete'),
141
    'description' => 'Delete a site.',
142
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
143 144 145 146 147 148
  );

  $items['provision cron'] = array(
    'callback' => 'provision_command',
    'callback arguments' => array('cron'),
    'description' => 'Run cron process for site.',
149
    'arguments' => array('domain.com' => dt('The domain of the site to be processed')),
150
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
151 152 153 154 155 156
  );
 
  return $items;
}


157 158 159 160
/**
 * Generate a provision.settings.php file to configure provision
 */
function _provision_generate_config() {
161
  drush_log(dt("Generating drushrc.php file"));
162
  provision_path("chmod", drush_get_option('docroot_path') . '/drushrc.php', 0600,
163 164 165 166
     dt('Changed permissions of drushrc.php to @confirm'),
      dt('Could not change permissions of drushrc.php to @confirm'));

  provision_save_platform_data();
167
  provision_path("chmod", drush_get_option('docroot_path') . '/drushrc.php', 0400,
168 169 170 171 172 173 174 175 176
    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
177 178 179 180 181 182 183 184 185 186
  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) {
187 188
    if (is_executable($test)) {
      $command = $test;
189
      break;
190 191 192 193 194 195
    }
  }

  return "sudo $command graceful";
}

196
function _provision_default_web_group() {
197
  $info = posix_getgrgid(posix_getgid());
198 199 200 201 202 203
  $common_groups = array(
    'httpd',
    'www-data', 
    'apache',
    'nogroup',
    'nobody',
204
    $info['name']);
205 206 207 208 209 210 211 212 213 214

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