provision.inc 27.1 KB
Newer Older
1 2 3 4 5
<?php
/**
 * @file
 * The provisioning framework API.
 *
6 7
 * API functions that are used by the provisioning framework to provide
 * structure to the provisioning modules.
8 9
 */

10 11
drush_errors_on();

12 13 14 15 16 17 18 19 20
/**
 * Return an instance of the provision autoloader.
 *
 * This will instiatate an instance if it needs to.
 */
function provision_autoload() {
  static $instance = NULL;

  if (is_null($instance)) {
21
    require_once(dirname(__FILE__) . '/Symfony/Component/ClassLoader/UniversalClassLoader.php');
22
    $instance = new UniversalClassLoader();
23 24 25 26 27 28 29 30 31 32 33 34 35 36
    // Activate the autoloader.
    $instance->register();
  }

  return $instance;
}

/**
 * Register a PECL style prefix with the provision autoloader.
 *
 * @param $prefix
 *   The class prefix to register.
 * @param $dir
 *   The directory to search for the classes in.
37 38 39
 * @param $prepend
 *   If the directory should be searched first for the classes in the given
 *   prefix, set this to TRUE, otherwise, the default, FALSE, is fine.
40
 */
41
function provision_autoload_register_prefix($prefix, $dir, $prepend = FALSE) {
42

43 44 45 46 47 48 49 50
  // Get any current directories set for this prefix.
  $current_prefixes = provision_autoload()->getPrefixes();
  if (isset($current_prefixes[$prefix])) {
    $dirs = $current_prefixes[$prefix];
  }
  else {
    $dirs = array();
  }
51

52 53 54 55 56 57 58
  // Now add the new one.
  if ($prepend) {
    array_unshift($dirs, $dir);
  }
  else {
    array_push($dirs, $dir);
  }
59

60 61
  // Set the prefixes.
  provision_autoload()->registerPrefix($prefix, $dirs);
62 63 64
}

// Add our prefix to the autoloader.
65
provision_autoload_register_prefix('Provision_', dirname(__FILE__));
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/**
 * Return the directory containing the file a class is defined in.
 *
 * @param $class_name
 *   The class name to search for.
 * @return
 *   A directory if the class can be found or an empty string if not.
 */
function provision_class_directory($class_name) {
  return dirname(provision_class_file($class_name));
}

/**
 * Return the file a class is defined in.
 *
 * @param $class_name
 *   The class name to search for.
 * @return
 *   A file if the class can be found or an empty string if not.
 */
function provision_class_file($class_name) {
  if (class_exists($class_name)) {
    $reflect = new reflectionClass($class_name);
    return $reflect->getFilename();
  }
  return '';
}
94

95 96 97 98
/**
 * @defgroup sitedata Site data management utility functions.
 * @{
 * The provision framework maintains a site.php file in the sites directory, to maintain additional
99
 * information from the front end, as well as providing a change history of setting changes.
100 101 102 103 104
 *
 * These functions load, save and manage changes made to the site data. This data has diagnostic and infrastructure
 * values, that allow sites to be more easily moved between different provisioned platforms.
 */

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/**
 * Make a determination whether or not the given
 * host is local or not.
 *
 * We needed to fork this from drush core to handle the case sensitivity in host names.
 *
 * @param host
 *   A hostname, 'localhost' or '127.0.0.1'.
 * @return
 *   True if the host is local.
 */
function provision_is_local_host($host) {
  $host = strtolower($host);
  // In order for this to work right, you must use 'localhost' or '127.0.0.1'
  // or the machine returned by 'uname -n' for your 'remote-host' entry in
  // your site alias.  Note that sometimes 'uname -n' does not return the
  // correct value.  To fix it, put the correct hostname in /etc/hostname
  // and then run 'hostname -F /etc/hostname'.
123
  return ($host == 'localhost') ||
124 125 126 127 128 129
    ($host == '127.0.0.1') ||
    (gethostbyname($host) == '127.0.0.1') ||
    (gethostbyname($host) == '127.0.1.1') || // common setting on
                                             // ubuntu and friends
    ($host == strtolower(php_uname('n'))) ||
    ($host == provision_fqdn());
130 131 132 133 134 135 136
}

/**
 * return the FQDN of the machine or provided host
 *
 * this replicates hostname -f, which is not portable
 */
137
function provision_fqdn($host = NULL) {
138 139 140 141
  if (is_null($host)) {
    $host = php_uname('n');
  }
  return strtolower(gethostbyaddr(gethostbyname($host)));
142
}
143

144 145 146 147 148 149 150 151
/**
 * Retrieve a base_url for the currently active site.
 *
 * TODO: when we actually support HTTPS, do this correctly.
 */
function provision_get_base_url() {
  $base_url = 'http://' . d()->uri;

152
  $http_port = d()->server->http_port;
153 154 155 156 157 158
  if (!is_null($http_port) && ($http_port != 80)) {
    $base_url .= ':' . $http_port;
  }
  return $base_url;
}

159
/**
160
 * Save modified options to the drushrc.php file
161
 */
162
function provision_save_server_data() {
163
  if (!drush_get_error()) {
164
    $config = new Provision_Config_Drushrc_Server(d()->name);
165
    $config->write();
166
  }
167 168
}

169 170 171

function provision_save_site_data() {
  if (!drush_get_error()) {
172
    $config = new Provision_Config_Drushrc_Site(d()->name);
173
    $config->write();
174
    provision_drupal_sync_site();
175 176 177 178 179 180 181 182 183
  }
}


/**
 * Save modified options to the drushrc.php file
 */
function provision_save_platform_data() {
  if (!drush_get_error()) {
184
    $config = new Provision_Config_Drushrc_Platform(d()->name);
185
    $config->write();
186
    provision_drupal_sync_site();
187 188 189 190
  }
}


191 192 193 194
/**
 * @} End of "defgroup sitedata".
 */

195 196 197
/**
 * Remove files or directories, recursively
 *
198
 * This was taken from Drupal 7's file.inc, with slight modifications:
anarcat's avatar
anarcat committed
199 200 201
 *  * carry error codes along the way (returns TRUE only if all operations return TRUE)
 *  * remove any type of files encountered (not files and directories)
 *  * do not follow symlink directories
202 203
 *
 * @see file_unmanaged_delete_recursive()
204 205
 */
function _provision_recursive_delete($path) {
206
  $ret = 1;
207 208
  // is_dir() follows symlinks, so it can return true on a symlink
  if (is_dir($path) && !is_link($path)) {
209
    $d = dir($path);
Steven Jones's avatar
Steven Jones committed
210
    while (($entry = $d->read()) !== FALSE) {
211 212 213
      if ($entry == '.' || $entry == '..') {
        continue;
      }
214
      $entry_path = $path . '/' . $entry;
215
      $ret &= _provision_recursive_delete($entry_path);
216
    }
217
    $d->close();
218

219
    $rm = provision_file()->rmdir($path)
220 221 222
      ->succeed('Deleting @path directory successful.')
      ->fail('Deleting @path directory failed.')
      ->status();
223 224 225 226

    $ret = $ret && $rm;
  }
  else {
227
    $rm = provision_file()->unlink($path)
228 229
      ->fail('Deleting @path file failed.')
      ->status();
230
    $ret = $ret && $rm;
231 232 233 234
  }
  return $ret;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/**
 * Convenience copy of Drupal 6's file_check_location()
 *
 * Check if a file is really located inside $directory. Should be used to make
 * sure a file specified is really located within the directory to prevent
 * exploits.
 *
 * @code
 *   // Returns FALSE:
 *   file_check_location('/www/example.com/files/../../../etc/passwd', '/www/example.com/files');
 * @endcode
 *
 * @param $source A string set to the file to check.
 * @param $directory A string where the file should be located.
 * @return 0 for invalid path or the real path of the source.
 *
 * @see file_check_location()
 */
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
function _provision_file_check_location($source, $directory = '') {
  $check = realpath($source);
  if ($check) {
    $source = $check;
  }
  else {
    // This file does not yet exist
    $source = realpath(dirname($source)) .'/'. basename($source);
  }
  $directory = realpath($directory);
  if ($directory && strpos($source, $directory) !== 0) {
    return 0;
  }
  return $source;
}

269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/**
 * Find the username of the current running procses
 *
 * This will return the username of the current running user (as seen
 * from posix_geteuid()) and should be used instead of
 * get_current_user() (which looks at the file owner instead).
 *
 * @see get_current_user()
 * @see posix_geteuid()
 *
 * @return
 *   String. The username.
 */
function provision_current_user() {
  return provision_posix_username(posix_geteuid());
}

287
/**
288
 * Check whether a user is a member of a group.
289
 *
290 291 292 293 294 295
 * @param user
 *   username or user id of user.
 * @param group
 *   groupname or group id of group.
 *
 * @return
296
 *   Boolean. True if user does belong to group,
297
 *   and FALSE if the user does not belong to the group, or either the user or group do not exist.
298
 */
299 300 301 302 303 304 305 306
function provision_user_in_group($user, $group) {
  // TODO: make these singletons with static variables for caching.
  $user = provision_posix_username($user);
  $group = provision_posix_groupname($group);
  if ($user && $group) {
    $info = posix_getgrnam($group);
    if (in_array($user, $info['members'])) {
      return TRUE;
307 308
    }
  }
309
  return FALSE;
310 311 312 313 314 315
}

/**
 * Return the valid system username for $user.
 *
 * @return
316
 *   Returns the username if found, otherwise returns FALSE
317 318 319
 */
function provision_posix_username($user) {
  // TODO: make these singletons with static variables for caching.
320
  // we do this both ways, so that the function returns NULL if no such user was found.
321 322 323 324
  if (is_numeric($user)) {
    $info = posix_getpwuid($user);
    $user = $info['name'];
  }
325
  else {
326 327 328 329 330 331 332 333 334 335
    $info = posix_getpwnam($user);
    $user = $info['name'];
  }
  return $user;
}

/**
 * Return the valid system groupname for $group.
 *
 * @return
336
 *   Returns the groupname if found, otherwise returns FALSE
337 338 339
 */
function provision_posix_groupname($group) {
  // TODO: make these singletons with static variables for caching.
340
  // we do this both ways, so that the function returns NULL if no such user was found.
341
  if (is_numeric($group)) {
342 343 344 345 346 347
    $info = posix_getgrgid($group);
    $group = $info['name'];
  }
  else {
    $info = posix_getgrnam($group);
    $group = $info['name'];
348
  }
349
  return $group;
350
}
351

anarcat's avatar
anarcat committed
352 353 354 355 356 357 358 359 360
/**
 * Generate a random alphanumeric password.
 *
 * This is a copy of Drupal core's user_password() function. We keep it
 * here in case we need this and don't have a bootstrapped Drupal
 * around.
 *
 * @see user_password()
 */
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
function provision_password($length = 10) {
  // This variable contains the list of allowable characters for the
  // password. Note that the number 0 and the letter 'O' have been
  // removed to avoid confusion between the two. The same is true
  // of 'I', 1, and 'l'.
  $allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';

  // Zero-based count of characters in the allowable list:
  $len = strlen($allowable_characters) - 1;

  // Declare the password as a blank string.
  $pass = '';

  // Loop the number of times specified by $length.
  for ($i = 0; $i < $length; $i++) {

    // Each iteration, pick a random character from the
    // allowable string and append it to the password:
    $pass .= $allowable_characters[mt_rand(0, $len)];
  }

  return $pass;
}
384

385 386 387 388 389 390 391 392 393 394 395 396 397
/**
 * This is a helper function which changes deeply nested objects into arrays
 *
 * This helps get past the face that objects are not simple to work with, or
 * save in context files.
 *
 * This function 'misuses' a side effect of the json_decode function's second
 * parameter. As this is done in C, and the structures we are manipulating
 * aren't that large, it should be performant enough.
 */
function _scrub_object($input) {
  return json_decode(json_encode($input), TRUE);
}
398 399 400

/**
 * Execute a command against a specific context object.
401
 *
402 403 404 405 406 407 408 409 410 411
 * @param $target
 *   the context to operate on, @ prefix is optional.
 * @param $command
 *   drush command passed to drush_invoke_process().
 * @param $arguments
 *   drush arguments passed to drush_invoke_process().
 * @param $data
 *   drush data passed to drush_invoke_process().
 * @param $mode
 *   drush IPC mode (GET/POST) passed to drush_invoke_process().
412
 *
413
 * @see drush_invoke_process()
414
 */
415
function provision_backend_invoke($target, $command, $arguments = array(), $data = array(), $mode = 'GET') {
416
  $context = '@' . ltrim($target, '@');
417
  return drush_invoke_process($context, $command, $arguments, $data, array('method' => $mode, 'integrate' => TRUE, 'dispatch-using-alias' => TRUE));
418
}
Steven Jones's avatar
Steven Jones committed
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
/**
 * Invoke a command in a new process, targeting the site specified by
 * the provided site alias record.
 *
 * Use this function instead of drush_backend_invoke_sitealias,
 * drush_backend_invoke_args, or drush_backend_invoke_command
 * (all obsolete in drush 5).
 *
 * @param array $site_alias_record
 *  The site record to execute the command on.  Use '@self' to run on the current site.
 * @param string $command_name
 *  The command to invoke.
 * @param array $commandline_args
 *  The arguments to pass to the command.
 * @param array $commandline_options
 *  The options (e.g. --select) to provide to the command.
 * @param $backend_options
Steven Jones's avatar
Steven Jones committed
437 438 439
 * - TRUE - integrate errors
 * - FALSE - do not integrate errors
 * - array - @see drush_backend_invoke_concurrent
440 441
 *     There are also several options that _only_ work when set in
 *     this parameter.  They include:
Steven Jones's avatar
Steven Jones committed
442
 *     - 'invoke-multiple'
443 444 445
 *        If $site_alias_record represents a single site, then 'invoke-multiple'
 *        will cause the _same_ command with the _same_ arguments and options
 *        to be invoked concurrently (e.g. for running concurrent batch processes).
Steven Jones's avatar
Steven Jones committed
446
 *     - 'concurrency'
447 448
 *        Limits the number of concurrent processes that will run at the same time.
 *        Defaults to '4'.
Steven Jones's avatar
Steven Jones committed
449
 *     - 'override-simulated'
450 451 452
 *        Forces the command to run, even in 'simulated' mode. Useful for
 *        commands that do not change any state on the machine, e.g. to fetch
 *        database information for sql-sync via sql-conf.
Steven Jones's avatar
Steven Jones committed
453
 *     - 'interactive'
454
 *        Overrides the backend invoke process to run commands interactively.
Steven Jones's avatar
Steven Jones committed
455
 *     - 'fork'
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
 *        Overrides the backend invoke process to run non blocking commands in 
 *        the background. Forks a new process by adding a '&' at the end of the 
 *        command. The calling process does not receive any output from the child 
 *        process. The fork option is used to spawn a process that outlives its
 *        parent.
 *
 * @return
 *   If the command could not be completed successfully, FALSE.
 *   If the command was completed, this will return an associative
 *   array containing the results of the API call.
 *   @see drush_backend_get_result()
 *
 * Do not change the signature of this function!  drush_invoke_process
 * is one of the key Drush APIs.  See http://drupal.org/node/1152908
 */
function _provision__drush_invoke_process($site_alias_record, $command_name, $commandline_args = array(), $commandline_options = array(), $backend_options = FALSE) {
  if (is_array($site_alias_record) && array_key_exists('site-list', $site_alias_record)) {
    $site_alias_records = drush_sitealias_resolve_sitespecs($site_alias_record['site-list']);
    $site_alias_records = drush_sitealias_simplify_names($site_alias_records);
    foreach ($site_alias_records as $alias_name => $alias_record) {
      $invocations[] = array(
        'site' => $alias_record,
        'command' => $command_name,
        'args' => $commandline_args,
      );
    }
  }
  else {
    $invocations[] = array(
      'site' => $site_alias_record,
      'command' => $command_name,
      'args' => $commandline_args);
    $invoke_multiple = drush_get_option_override($backend_options, 'invoke-multiple', 0);
    if ($invoke_multiple) {
      $invocations = array_fill(0, $invoke_multiple, $invocations[0]);
    }
  }
  return _provison__drush_backend_invoke_concurrent($invocations, $commandline_options, $backend_options);
}

/**
 * Execute a new local or remote command in a new process.
 *
 * n.b. Prefer drush_invoke_process() to this function.
 *
 * @param invocations
 *   An array of command records to exacute. Each record should contain:
Steven Jones's avatar
Steven Jones committed
503
 *   - 'site':
504
 *       An array containing information used to generate the command.
Steven Jones's avatar
Steven Jones committed
505
 *       - 'remote-host'
506
 *            Optional. A remote host to execute the drush command on.
Steven Jones's avatar
Steven Jones committed
507
 *       - 'remote-user'
508
 *            Optional. Defaults to the current user. If you specify this, you can choose which module to send.
Steven Jones's avatar
Steven Jones committed
509
 *       - 'ssh-options'
510
 *            Optional.  Defaults to "-o PasswordAuthentication=no"
Steven Jones's avatar
Steven Jones committed
511
 *       - 'path-aliases'
512
 *            Optional; contains paths to folders and executables useful to the command.
Steven Jones's avatar
Steven Jones committed
513
 *       - '%drush-script'
514 515 516 517 518
 *            Optional. Defaults to the current drush.php file on the local machine, and
 *            to simply 'drush' (the drush script in the current PATH) on remote servers.
 *            You may also specify a different drush.php script explicitly.  You will need
 *            to set this when calling drush on a remote server if 'drush' is not in the
 *            PATH on that machine.
Steven Jones's avatar
Steven Jones committed
519
 *   - 'command':
520
 *       A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.
Steven Jones's avatar
Steven Jones committed
521
 *   - 'args':
522
 *       An array of arguments for the command.
Steven Jones's avatar
Steven Jones committed
523
 *   - 'options'
524 525 526
 *       Optional. An array containing options to pass to the remote script.
 *       Array items with a numeric key are treated as optional arguments to the
 *       command.
Steven Jones's avatar
Steven Jones committed
527
 *   - 'backend-options':
528
 *       Optional. Additional parameters that control the operation of the invoke.
Steven Jones's avatar
Steven Jones committed
529
 *       - 'method'
530 531 532 533 534 535 536
 *            Optional. Defaults to 'GET'.
 *            If this parameter is set to 'POST', the $data array will be passed
 *            to the script being called as a JSON encoded string over the STDIN
 *            pipe of that process. This is preferable if you have to pass
 *            sensitive data such as passwords and the like.
 *            For any other value, the $data array will be collapsed down into a
 *            set of command line options to the script.
Steven Jones's avatar
Steven Jones committed
537
 *       - 'integrate'
538 539 540 541
 *            Optional. Defaults to TRUE.
 *            If TRUE, any error statuses will be integrated into the current
 *            process. This might not be what you want, if you are writing a
 *            command that operates on multiple sites.
Steven Jones's avatar
Steven Jones committed
542
 *       - 'log'
543 544 545
 *            Optional. Defaults to TRUE.
 *            If TRUE, any log messages will be integrated into the current
 *            process.
Steven Jones's avatar
Steven Jones committed
546
 *       - 'output'
547 548 549
 *            Optional. Defaults to TRUE.
 *            If TRUE, output from the command will be synchronously printed to
 *            stdout.
Steven Jones's avatar
Steven Jones committed
550
 *       - 'drush-script'
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
 *            Optional. Defaults to the current drush.php file on the local
 *            machine, and to simply 'drush' (the drush script in the current
 *            PATH) on remote servers.  You may also specify a different drush.php
 *            script explicitly.  You will need to set this when calling drush on
 *            a remote server if 'drush' is not in the PATH on that machine.
 * @param common_options
 *    Optional. Merged in with the options for each invocation.
 * @param backend_options
 *    Optional. Merged in with the backend options for each invocation.
 * @param default_command
 *    Optional. Used as the 'command' for any invocation that does not
 *    define a command explicitly.
 * @param default_site
 *    Optional. Used as the 'site' for any invocation that does not
 *    define a site explicitly.
 * @param context
 *    Optional. Passed in to proc_open if provided.
 *
 * @return
 *   If the command could not be completed successfully, FALSE.
 *   If the command was completed, this will return an associative array containing the data from drush_backend_output().
 */
function _provison__drush_backend_invoke_concurrent($invocations, $common_options = array(), $common_backend_options = array(), $default_command = NULL, $default_site = NULL, $context = NULL) {
  $index = 0;

  // Slice and dice our options in preparation to build a command string
  $invocation_options = array();
  foreach ($invocations as $invocation)  {
    $site_record = isset($invocation['site']) ? $invocation['site'] : $default_site;
    // NULL is a synonym to '@self', although the latter is preferred.
    if (!isset($site_record)) {
      $site_record = '@self';
    }
    // If the first parameter is not a site alias record,
    // then presume it is an alias name, and try to look up
    // the alias record.
    if (!is_array($site_record)) {
      $site_record = drush_sitealias_get_record($site_record);
    }
    $command = isset($invocation['command']) ? $invocation['command'] : $default_command;
    $args = isset($invocation['args']) ? $invocation['args'] : array();
    $command_options = isset($invocation['options']) ? $invocation['options'] : array();
    $backend_options = isset($invocation['backend-options']) ? $invocation['backend-options'] : array();
    // If $backend_options is passed in as a bool, interpret that as the value for 'integrate'
    if (!is_array($common_backend_options)) {
      $integrate = (bool)$common_backend_options;
      $common_backend_options = array('integrate' => $integrate);
    }

    $command_options += $common_options;
    $backend_options += $common_backend_options;

    $backend_options = _drush_backend_adjust_options($site_record, $command, $command_options, $backend_options);

    // Insure that contexts such as DRUSH_SIMULATE and NO_COLOR are included.
    $command_options += _drush_backend_get_global_contexts($site_record);

    if (isset($site_record['#name'])) {
      list($post_options, $commandline_options, $drush_global_options) = _drush_backend_classify_options(array(), $command_options, $backend_options);
      $command = '@' . ltrim($site_record['#name'], '@') . ' ' . $command;
    }
    else {
      list($post_options, $commandline_options, $drush_global_options) = _drush_backend_classify_options($site_record, $command_options, $backend_options);
    }
    $site_record += array('path-aliases' => array());
    $site_record['path-aliases'] += array(
      '%drush-script' => NULL,
    );

    $site = (array_key_exists('#name', $site_record) && !array_key_exists($site_record['#name'], $invocation_options)) ? $site_record['#name'] : $index++;
    $invocation_options[$site] = array(
      'site-record' => $site_record,
      'command' => $command,
      'args' => $args,
      'post-options' => $post_options,
      'drush-global-options' => $drush_global_options,
      'commandline-options' => $commandline_options,
      'command-options' => $command_options,
      'backend-options' => $backend_options,
    );
  }

  // Calculate the length of the longest output label
  $max_name_length = 0;
  $label_separator = '';
  if (!array_key_exists('no-label', $common_options) && (count($invocation_options) > 1)) {
    $label_separator = array_key_exists('#label-separator', $common_options) ? $common_options['#label-separator'] : ' >> ';
    foreach ($invocation_options as $site => $item) {
      $backend_options = $item['backend-options'];
      if (!array_key_exists('#output-label', $backend_options)) {
        if (is_numeric($site)) {
          $backend_options['#output-label'] = ' * [@self.' . $site;
          $label_separator = '] ';
        }
        else {
          $backend_options['#output-label'] = $site;
        }
        $invocation_options[$site]['backend-options']['#output-label'] = $backend_options['#output-label'];
      }
      $name_len = strlen($backend_options['#output-label']);
      if ($name_len > $max_name_length) {
        $max_name_length = $name_len;
      }
      if (array_key_exists('#label-separator', $backend_options)) {
        $label_separator = $backend_options['#label-separator'];
      }
    }
  }
  // Now pad out the output labels and add the label separator.
  $reserve_margin = $max_name_length + strlen($label_separator);
  foreach ($invocation_options as $site => $item) {
    $backend_options = $item['backend-options'] + array('#output-label' => '');
    $invocation_options[$site]['backend-options']['#output-label'] = str_pad($backend_options['#output-label'], $max_name_length, " ") . $label_separator;
    if ($reserve_margin) {
      $invocation_options[$site]['drush-global-options']['reserve-margin'] = $reserve_margin;
    }
  }

  // Now take our prepared options and generate the command strings
  $cmds = array();
  foreach ($invocation_options as $site => $item) {
    $site_record = $item['site-record'];
    $command = $item['command'];
    $args = $item['args'];
    $post_options = $item['post-options'];
    $commandline_options = $item['commandline-options'];
    $command_options = $item['command-options'];
    $drush_global_options = $item['drush-global-options'];
    $backend_options = $item['backend-options'];
    $os = drush_os($site_record);
    // If the caller did not pass in a specific path to drush, then we will
    // use a default value.  For commands that are being executed on the same
    // machine, we will use DRUSH_COMMAND, which is the path to the drush.php
    // that is running right now.  For remote commands, we will run a wrapper
    // script instead of drush.php -- drush.bat on Windows, or drush on Linux.
    $drush_path = $site_record['path-aliases']['%drush-script'];
    $drush_command_path = drush_build_drush_command($drush_path, array_key_exists('php', $command_options) ? $command_options['php'] : NULL, $os, array_key_exists('remote-host', $site_record));
    $cmd = _drush_backend_generate_command($site_record, $drush_command_path . " " . _drush_backend_argument_string($drush_global_options, $os) . " " .  $command, $args, $commandline_options, $backend_options) . ' 2>&1';
    $cmds[$site] = array(
      'cmd' => $cmd,
      'post-options' => $post_options,
      'backend-options' => $backend_options,
    );
  }

  return _drush_backend_invoke($cmds, $common_backend_options, $context);
697
}
698

699
/**
700 701 702 703 704 705 706 707
 * the aegir version of the backend
 *
 * @return string
 *  the aegir version as stored in the .info file, potentially
 *  including the 6.x- prefix. to get a cleaned up version, use
 *  provision_version_parts()
 *
 * @see provision_version_parts()
708 709 710 711 712
 */
function provision_version() {
  $ini = parse_ini_file(dirname(__FILE__) . '/provision.info');
  return $ini['version'];
}
713

714 715 716 717 718 719
/**
 * Aegir API implemented by this backend
 *
 * This is the major release number, the first part of the version
 * stored in the info file
 *
720
 * @return int
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
 *   a number greater than zero, 1 for 1.0 or 1.0-rc2, 2 for 2.0, etc.
 *
 * @see provision_version_parts()
 */
function provision_api_version() {
  $parts = provision_version_parts();
  return $parts[0];
}

/**
 * The different parts of the version number
 *
 * This cleans up the version number by removing the Drupal version
 * (6.x-...) and splits the remaining version on dots.
 *
736
 * @return array
737 738 739 740 741 742 743 744
 *   the major and minor version numbers, e.g. array(1, 0-rc3) for
 *   1.0-rc3 or array(1, 2) for 1.2
 */
function provision_version_parts() {
  $version = preg_replace('/^[^-]*-/', '', provision_version()); // remove "6.x-"
  return explode('.', $version);
}

745 746 747 748 749 750 751 752 753 754 755 756 757
/**
 * Normalise a context name, ensuring that it starts with one '@'.
 *
 * @param $name
 *   The context name to normalise.
 *
 * @return
 *   The normalised context name.
 */
function provision_normalise_context_name($name) {
  return '@' . ltrim($name, '@');
}

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
// Base class for provision exceptions.
class provisionException extends Exception {

}

/**
 * Signal for parent to continue processing.
 *
 * The primary use for this class is for the config
 * classes to be able to signal to it's caller, that
 * the configuration file was not needed, and to
 * continue on.
 */
class provisionException_continue extends provisionException {

}

775 776 777 778 779 780 781 782
/**
 * Provision class.
 *
 * This is just a container for some useful static methods.
 */
class provision {
  /**
   * The actual body of the method_invoke function.
783
   *
784 785 786 787 788 789 790 791 792 793
   * This is a static method so it can be re-used by some other classes
   * that aren't contexts. (notably services and configs).
   */
  static function method_invoke($object, $func, $args = array()) {
    if (method_exists($object, $func)) {
      return call_user_func_array(array($object, $func), $args);
    }
  }
}

794
include_once('provision.context.inc');
795 796
include_once('provision.service.inc');
include_once('provision.file.inc');