| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Drush commands
|
| 4 |
*
|
| 5 |
*
|
| 6 |
* This module provides a framework for a Drupal site to manage and install new Drupal sites, using the command line
|
| 7 |
* Drush utility.
|
| 8 |
*
|
| 9 |
* It allows for pluggable 'provisioning modules' that can extend and modify the tasks that are taken during installation.
|
| 10 |
*
|
| 11 |
* Each site has the following commands that can be run on it.
|
| 12 |
*
|
| 13 |
* Implemented :
|
| 14 |
* install - Install a new Drupal site. The install command uses 3 separate hooks to do it's job,
|
| 15 |
* namely hook_provision_pre_install(), hook_provision_install() and hook_provision_post_install()
|
| 16 |
* verify - Recreate all configuration files, to be in synch with changes in the front end. And test that they are correct.
|
| 17 |
* stats - Return an associated array of site statistics. (implemented in provision_stats module, is thus optional)
|
| 18 |
* import - Import the details of an already existing site into the provisioning framework.
|
| 19 |
* This command inspects the settings.php and generates the site.php file that the framework uses for configuration.
|
| 20 |
* backup - Generates a tarball containing the sites directory, the site data configuration and the database dump.
|
| 21 |
* This allows the tarball to act as a 'site package', which can be redeployed on other installations,
|
| 22 |
* or used for an upgrade.
|
| 23 |
* disable - Disable an installed Drupal site. Changes the virtual host config file so that it redirects to provision_disabled_site_redirect_url
|
| 24 |
* enable - Re-enable a site that has already been disabled. Recreates the virtual host file.
|
| 25 |
* delete - Generates a back up of the site, and then removes all references to it.
|
| 26 |
* restore - Revert to a previous backup of the site.
|
| 27 |
*
|
| 28 |
* deploy - Accepts a site package (backup) as argument, and redeploys it, running the upgrade processes on it.
|
| 29 |
* Uses hook_provision_pre_upgrade(), hook_provision_upgrade() and hook_provision_post_upgrade() hooks,
|
| 30 |
* and allows clean roll back if any errors occur. Will include stringent checking of module versions,
|
| 31 |
* and allow unit tests to be run.
|
| 32 |
* login_reset - Generate a one-time login reset URL.
|
| 33 |
*
|
| 34 |
* Not implemented yet :
|
| 35 |
* rename - Change the url of a site. This requires moving of files, and numerous other issues.
|
| 36 |
*/
|
| 37 |
|
| 38 |
// Do not allow the program to be run as the root user. ever
|
| 39 |
$name = posix_getpwuid(posix_geteuid());
|
| 40 |
if ($name['name'] == 'root') {
|
| 41 |
return drush_set_error('PROVISION_IS_ROOT', dt('You are running the provision script as the root user. Exiting'));
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* @defgroup provisiondrush Command line interface for Provision.
|
| 46 |
* @{
|
| 47 |
*/
|
| 48 |
include_once('provision.inc');
|
| 49 |
include_once('provision.path.inc');
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Implementation of hook_drush_command().
|
| 53 |
*/
|
| 54 |
function provision_drush_command() {
|
| 55 |
|
| 56 |
$items['provision install'] = array(
|
| 57 |
'arguments' => array('domain.com' => dt('The domain of the site to install.')),
|
| 58 |
'description' => dt('Provision a new site using the provided data.'),
|
| 59 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 60 |
);
|
| 61 |
|
| 62 |
$items['provision import'] = array(
|
| 63 |
'arguments' => array('domain.com' => dt('The domain of the site to import.')),
|
| 64 |
'description' => dt('Turn an already running site into a provisioned site.'),
|
| 65 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 66 |
);
|
| 67 |
|
| 68 |
$items['provision backup'] = array(
|
| 69 |
'arguments' => array('domain.com' => dt('The domain of the site to back up.')),
|
| 70 |
'optional arguments' => array('backup-file' => dt('The file to save the backup to. This will be a gzipped tarball.')),
|
| 71 |
'description' => dt('Generate a back up for the site.'),
|
| 72 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 73 |
);
|
| 74 |
|
| 75 |
$items['provision enable'] = array(
|
| 76 |
'arguments' => array('domain.com' => dt('The domain of the site to enable (only if enabled).')),
|
| 77 |
'description' => 'Enable a disabled site.',
|
| 78 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 79 |
);
|
| 80 |
$items['provision disable'] = array(
|
| 81 |
'arguments' => array('domain.com' => dt('The domain of the site to disable (only if disabled).')),
|
| 82 |
'description' => 'Disable a site.',
|
| 83 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 84 |
);
|
| 85 |
|
| 86 |
$items['provision verify'] = array(
|
| 87 |
'arguments' => array('domain.com' => dt('The domain of the site to verify).')),
|
| 88 |
'description' => 'Verify that the provisioning framework is correctly installed.',
|
| 89 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
|
| 90 |
);
|
| 91 |
|
| 92 |
$items['provision restore'] = array(
|
| 93 |
'description' => 'Restore the site to a previous backup. This will also generate a backup of the site as it was.',
|
| 94 |
'arguments' => array('domain.com' => dt('The domain of the site to be restored'),
|
| 95 |
'site_backup.tar.gz' => dt('The backup to restore the site to.')),
|
| 96 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 97 |
);
|
| 98 |
|
| 99 |
$items['provision deploy'] = array(
|
| 100 |
'description' => 'Deploy an existing backup to a new url.',
|
| 101 |
'arguments' => array('domain.com' => dt('The domain to deploy the site package to.'),
|
| 102 |
'site_backup.tar.gz' => dt('The backup to deploy.')),
|
| 103 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 104 |
);
|
| 105 |
|
| 106 |
$items['provision migrate'] = array(
|
| 107 |
'description' => 'Migrate a site between platforms.',
|
| 108 |
'arguments' => array('domain.com' => dt('The domain to migrate. Any outstanding updates will be run.'),
|
| 109 |
'/path/to/platform' => dt('The platform to migrate the site to.')),
|
| 110 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 111 |
);
|
| 112 |
|
| 113 |
$items['provision clone'] = array(
|
| 114 |
'description' => 'Clone a site between platforms.',
|
| 115 |
'arguments' => array('domain.com' => dt('The domain to clone. Any outstanding updates will be run.'),
|
| 116 |
'new.domain.com' => dt('The new domain name to use.'),
|
| 117 |
'/path/to/platform' => dt('The platform to clone the site to.')),
|
| 118 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 119 |
);
|
| 120 |
|
| 121 |
$items['provision delete'] = array(
|
| 122 |
'description' => 'Delete a site.',
|
| 123 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 124 |
);
|
| 125 |
|
| 126 |
$items['provision login_reset'] = array(
|
| 127 |
'description' => 'Generate a one-time login reset URL.',
|
| 128 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT
|
| 129 |
);
|
| 130 |
|
| 131 |
|
| 132 |
$items['hostmaster migrate'] = array(
|
| 133 |
'description' => dt('Migrate an instance of the Hostmaster front end to a new platform'),
|
| 134 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
|
| 135 |
'arguments' => array(
|
| 136 |
'example.com' => dt('The name of the site to migrate'),
|
| 137 |
'/path/to/platform' => dt('The platform to migrate the site to.')
|
| 138 |
),
|
| 139 |
);
|
| 140 |
|
| 141 |
$items['hostmaster make'] = array(
|
| 142 |
'description' => dt('Build a platform containing the Hostmaster user interface for provision.'),
|
| 143 |
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
|
| 144 |
'arguments' => array(
|
| 145 |
'/path/to/platform' => dt('The path to create the platform in.')
|
| 146 |
),
|
| 147 |
);
|
| 148 |
|
| 149 |
|
| 150 |
$items['hostmaster park'] = array(
|
| 151 |
'description' => dt('Prepare the site to be migrated to a new platform.'),
|
| 152 |
'arguments' => array(
|
| 153 |
'example.com' => dt('The url of the site being migrated.')),
|
| 154 |
);
|
| 155 |
|
| 156 |
$items['hostmaster unpark'] = array(
|
| 157 |
'description' => dt('Complete the migration of the site to a new platform.'),
|
| 158 |
'arguments' => array(
|
| 159 |
'example.com' => dt('The url of the site being migrated.')),
|
| 160 |
);
|
| 161 |
|
| 162 |
|
| 163 |
return $items;
|
| 164 |
}
|
| 165 |
|
| 166 |
|
| 167 |
function drush_provision_hostmaster_make($platform) {
|
| 168 |
drush_backend_invoke('make', array(dirname(__FILE__) . '/aegir.make', $platform));
|
| 169 |
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Generate a provision.settings.php file to configure provision
|
| 174 |
*/
|
| 175 |
function _provision_generate_config() {
|
| 176 |
$exists = provision_path_exists(drush_get_option('docroot_path') . '/drushrc.php');
|
| 177 |
if ($exists) {
|
| 178 |
drush_log(dt("Found existing drushrc.php file"));
|
| 179 |
provision_path("chmod", drush_get_option('docroot_path') . '/drushrc.php', 0600,
|
| 180 |
dt('Changed permissions of drushrc.php to @confirm'),
|
| 181 |
dt('Could not change permissions of drushrc.php to @confirm'));
|
| 182 |
}
|
| 183 |
else {
|
| 184 |
drush_log(dt("Generating drushrc.php file"));
|
| 185 |
}
|
| 186 |
provision_save_platform_data();
|
| 187 |
provision_path("chmod", drush_get_option('docroot_path') . '/drushrc.php', 0400,
|
| 188 |
dt('Changed permissions of drushrc.php to @confirm'),
|
| 189 |
dt('Could not change permissions of drushrc.php to @confirm'));
|
| 190 |
return TRUE;
|
| 191 |
}
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
function _provision_default_restart_cmd() {
|
| 196 |
$command = '/usr/sbin/apachectl'; # a proper default for most of the world
|
| 197 |
foreach (explode(':', $_SERVER['PATH']) as $path) {
|
| 198 |
$options[] = "$path/apache2ctl";
|
| 199 |
$options[] = "$path/apachectl";
|
| 200 |
}
|
| 201 |
# try to detect the apache restart command
|
| 202 |
$options[] = '/usr/local/sbin/apachectl'; # freebsd
|
| 203 |
$options[] = '/usr/sbin/apache2ctl'; # debian + apache2
|
| 204 |
$options[] = $command;
|
| 205 |
|
| 206 |
foreach ($options as $test) {
|
| 207 |
if (is_executable($test)) {
|
| 208 |
$command = $test;
|
| 209 |
break;
|
| 210 |
}
|
| 211 |
}
|
| 212 |
|
| 213 |
return "sudo $command graceful";
|
| 214 |
}
|
| 215 |
|
| 216 |
function _provision_default_web_group() {
|
| 217 |
$info = posix_getgrgid(posix_getgid());
|
| 218 |
$common_groups = array(
|
| 219 |
'httpd',
|
| 220 |
'www-data',
|
| 221 |
'apache',
|
| 222 |
'nogroup',
|
| 223 |
'nobody',
|
| 224 |
$info['name']);
|
| 225 |
|
| 226 |
foreach ($common_groups as $group) {
|
| 227 |
if (provision_posix_groupname($group)) {
|
| 228 |
return $group;
|
| 229 |
break;
|
| 230 |
}
|
| 231 |
}
|
| 232 |
return null;
|
| 233 |
}
|
| 234 |
|