join(" ", $load)))); exit(1); } } /** * Implementation of hook_drush_command(). */ function provision_drush_command() { $items['provision-save'] = array( 'description' => dt('Save Drush alias'), 'arguments' => array( '@context_name' => 'Context to save', ), 'options' => array_merge(array( '--context_type' => 'server, platform, or site; default server', '--delete' => 'Remove the alias.', ), provisionContext_server::option_documentation(), provisionContext_platform::option_documentation(), provisionContext_site::option_documentation()), 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, ); $items['provision-install'] = array( 'description' => dt('Provision a new site using the provided data.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-install-backend'] = array( 'description' => dt('Provision a new site using the provided data.'), 'hidden' => TRUE, 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE ); $items['provision-import'] = array( 'arguments' => array('domain.com' => dt('The domain of the site to import.')), 'description' => dt('Turn an already running site into a provisioned site.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-backup'] = array( '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.')), 'description' => dt('Generate a back up for the site.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-enable'] = array( 'arguments' => array('domain.com' => dt('The domain of the site to enable (only if enabled).')), 'description' => 'Enable a disabled site.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-disable'] = array( 'arguments' => array('domain.com' => dt('The domain of the site to disable (only if disabled).')), 'description' => 'Disable a site.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-lock'] = array( 'description' => 'Lock a platform from having any other sites provisioned on it.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-unlock'] = array( 'description' => 'Unlock a platform so that sites can be provisioned on it.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-verify'] = array( 'arguments' => array('domain.com' => dt('The domain of the site to verify).')), 'description' => 'Verify that the provisioning framework is correctly installed.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, ); $items['provision-restore'] = array( 'description' => 'Restore the site to a previous backup. This will also generate a backup of the site as it was.', 'arguments' => array('domain.com' => dt('The domain of the site to be restored'), 'site_backup.tar.gz' => dt('The backup to restore the site to.')), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-deploy'] = array( '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.')), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-migrate'] = array( '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' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-clone'] = array( 'description' => 'Clone a site between platforms.', 'arguments' => array('domain.com' => dt('The domain to clone. Any outstanding updates will be run.'), 'new.domain.com' => dt('The new domain name to use.'), '/path/to/platform' => dt('The platform to clone the site to.')), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-delete'] = array( 'description' => 'Delete a site.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['provision-login_reset'] = array( 'description' => 'Generate a one-time login reset URL.', 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); $items['hostmaster-migrate'] = array( 'description' => dt('Migrate an instance of the Hostmaster front end to a new platform'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, 'arguments' => array( 'example.com' => dt('The name of the site to migrate'), '/path/to/platform' => dt('The platform to migrate the site to.') ), ); $items['hostmaster-install'] = array( 'description' => dt('Install and verify the Hostmaster frontend.'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, 'arguments' => array( 'example.com' => dt('The name of the site to install'), '/path/to/platform' => dt('The platform to install the site on.'), 'you@example.com' => dt('The email account to send a welcome mail to'), ), ); $items['hostmaster-pause'] = array( 'description' => dt('Prepare the site to be migrated to a new platform.'), 'arguments' => array( 'example.com' => dt('The url of the site being migrated.')), ); $items['hostmaster-resume'] = array( 'description' => dt('Complete the migration of the site to a new platform.'), 'arguments' => array( 'example.com' => dt('The url of the site being migrated.')), ); $items['provision-rename'] = array( 'description' => 'Rename a site.', 'arguments' => array('domain.com' => dt('The domain to rename. Any outstanding updates will be run.'), 'new.domain.com' => dt('The new domain name to use.')), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT ); return $items; } function drush_provision_save($alias = null) { if (drush_get_option('delete', false)) { // remove an existing alias $config = new provisionConfig_drushrc_alias($alias); $config->unlink(); } else { // trigger additional logic that should happen only on save. d($alias)->type_invoke('save'); // create or update the record d($alias)->write_alias(); } } function drush_provision_verify() { d()->command_invoke('verify'); } function _provision_default_web_group() { $info = posix_getgrgid(posix_getgid()); $common_groups = array( 'httpd', 'www-data', 'apache', 'webservd', 'nogroup', 'nobody', $info['name']); foreach ($common_groups as $group) { if (provision_posix_groupname($group)) { return $group; break; } } return null; } /** * determine the number of CPU on the machine * * This tries a best guess at the number of CPUs running on the system. This is * useful for calculating sane load threshold. * * On Linux, this parses /proc/cpuinfo and looks for lines like this: * * processor : 0 * ... * processor : 1 * processor : n * * The number of CPUs on the system is n+1, we just count the number of lines. * * Other systems remain to be implemented, and would be best implemetend * through a PECL (or similar) extension that would use the POSIX sysconf * interface, as such: * * ncpus = sysconf(_SC_NPROCESSORS_ONLN); * * If no method can be found to figure out the number of CPUs, this will return * FALSE. * * People wishing to extend this to other platforms should look at * suggestions at: * * http://groups.google.com/group/sage-devel/browse_thread/thread/d65209f7ad6057fc * * @see provision_load_critical() * @todo implement for other systems than Linux */ function provision_count_cpus() { $ncpus = FALSE; if (file_exists("/proc/cpuinfo")) { # this should work on Linux with a /proc filesystem $cpuinfo = file_get_contents("/proc/cpuinfo"); if ($cpuinfo !== FALSE) { if (preg_match_all("/^processor.*:.*[0-9]+$/m", $cpuinfo, $matches)) { $ncpus = count(array_pop($matches)); } } } return $ncpus; } /** * determine if overall load of the machine is critical * * We use the "average system load" of the system as a metric, as available * through 'uptime' or in PHP sys_getloadavg() since 5.1. The load is usually * defined as "the number of processes in the system run queue" * * It's not a really reliable metric, but it's the best shot we've got without * getting into real specific details about I/O, CPU or memory load that are * going to be even tougher to evaluate. * * We base our evaluation on the number of CPUs on the servers. If there are * more than 5 processes waiting per CPU, we abort completely. If we ignore the * number of available CPUs, we assume a critical limit is a load of 10. * * @see sys_getloadavg() */ function provision_load_critical($load = null, $threshold = null) { if (is_null($load)) { $load = sys_getloadavg(); } if (is_null($threshold)) { if ($ncpus = provision_count_cpus()) { $threshold = $ncpus * 5; } else { // can't determine the number of CPU, we hardcode at load 10 $threshold = 10; } } return ($load[0] > $threshold); }