hosting.drush.inc

Drush include for the Hosting module.

Functions

Namesort descending Description
drush_hosting_import Command to import an existing provision named context.
hosting_drush_command Implements hook_drush_command().
hosting_drush_import Imports a drush named context / "site alias" into the hostmaster frontend.

Constants

File

hosting.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush include for the Hosting module.
  5. */
  6. define('HOSTING_QUEUE_DEFAULT_LOCK_WAIT', 30);
  7. define('HOSTING_QUEUE_LOCK_TIMEOUT', 3600.0);
  8. /**
  9. * Implements hook_drush_command().
  10. */
  11. function hosting_drush_command() {
  12. $items['hosting-dispatch'] = array(
  13. 'description' => dt('Centralized command for dispatching the various queue processors (hosting, cron, backup etc.)'),
  14. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  15. 'options' => array(
  16. 'lock-wait' => dt('Time to wait to acquire a lock on dispatched queues. Defaults to @wait seconds.', array('@wait' => HOSTING_QUEUE_DEFAULT_LOCK_WAIT)),
  17. ),
  18. );
  19. $items['hosting-setup'] = array(
  20. 'description' => dt('Set up initial configuration settings such as the cron entry for the queue dispatcher and more.'),
  21. );
  22. // If we're trying to get help, then try to bootstrap as much as possible.
  23. $current_command = drush_get_command();
  24. if (isset($current_command['command']) && ($current_command['command'] == 'help')) {
  25. drush_bootstrap_max();
  26. }
  27. // If we've not bootstrapped fully, then this function may not be around.
  28. if (function_exists('hosting_get_queues')) {
  29. $queues = hosting_get_queues();
  30. foreach ($queues as $queue => $info) {
  31. $dispatch = dt("Dispatched: @items items every @frequency minutes", array('@items' => $info['items'], '@frequency' => round($info['frequency'] / 60)));
  32. $items['hosting-' . $queue] = array(
  33. 'callback' => 'hosting_run_queue',
  34. 'description' => $info['description'] . " " . $dispatch,
  35. 'queue' => $queue,
  36. 'options' => array(
  37. 'lock-wait' => dt('Time to wait to acquire a lock on the @queue queue. Defaults to @wait seconds.', array('@queue' => $queue, '@wait' => HOSTING_QUEUE_DEFAULT_LOCK_WAIT)),
  38. ),
  39. );
  40. }
  41. }
  42. $items['hosting-task'] = array(
  43. 'description' => 'execute a specific queue item',
  44. 'arguments' => array(
  45. '@context_name' => 'Context to work on',
  46. 'command' => 'provision-[command] to invoke',
  47. ),
  48. 'options' => array(
  49. 'force' => "Force the specified task to execute even if it's not queued to run.",
  50. ),
  51. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  52. );
  53. $items['hosting-import'] = array(
  54. 'description' => 'Import an existing backend context name into the front end.',
  55. 'arguments' => array(
  56. '@context_name' => 'Context to import',
  57. ),
  58. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  59. );
  60. $items['hosting-pause'] = array(
  61. 'description' => dt('Prepare the hostmaster site to be migrated to a new platform.'),
  62. 'arguments' => array(
  63. 'example.com' => dt('The url of the site being migrated.'),
  64. ),
  65. );
  66. $items['hosting-resume'] = array(
  67. 'description' => dt('Complete the migration of the hostmaster site to a new platform.'),
  68. 'arguments' => array(
  69. 'example.com' => dt('The url of the site being migrated.')),
  70. 'options' => array(
  71. 'old_platform_name' => dt('The old platform name'),
  72. 'new_platform_name' => dt('The new platform name'),
  73. ),
  74. );
  75. return $items;
  76. }
  77. /**
  78. * Command to import an existing provision named context.
  79. *
  80. * It generate nodes for it.
  81. *
  82. * @param string $alias
  83. * The name of the provision context to import.
  84. */
  85. function drush_hosting_import($alias) {
  86. if (!empty($alias)) {
  87. if (d($alias)->name) {
  88. drush_log("Importing $alias");
  89. hosting_drush_import($alias);
  90. }
  91. }
  92. }
  93. /**
  94. * Imports a drush named context / "site alias" into the hostmaster frontend.
  95. *
  96. * By creating nodes and translating the value.
  97. * This is bascally the reverse of the context_options hook.
  98. *
  99. * @param object|string $alias
  100. * The name of the provision context to import.
  101. *
  102. * @return int
  103. * The node ID associated with the alias.
  104. */
  105. function hosting_drush_import($alias) {
  106. $name = (is_object($alias)) ? $alias->name : $alias;
  107. static $known_contexts = array();
  108. // Avoid re-importing the same object twice in one execution
  109. // of the script.
  110. if (isset($known_contexts[$name])) {
  111. drush_log("Already re-imported {$name} in this process.");
  112. return $known_contexts[$name];
  113. }
  114. $context = d($name);
  115. if ($node = hosting_context_load($name)) {
  116. drush_log("Context {$name} already has an associated node. Updating {$node->type} node {$node->nid} instead.");
  117. $node->no_verify = TRUE;
  118. $known_contexts[$name] = $node->nid;
  119. }
  120. else {
  121. // First time we've seen this context.
  122. $node = new stdClass();
  123. $node->type = $context->type;
  124. // Set the hosting name too.
  125. $node->hosting_name = trim($context->name, '@');
  126. $node->status = 1;
  127. }
  128. // Iterate through all the Drush commands which may want to save this node.
  129. $modules = drush_command_implements('drush_context_import');
  130. foreach ($modules as $module) {
  131. $func = "{$module}_drush_context_import";
  132. $func($context, $node);
  133. }
  134. node_save($node);
  135. if ($node->nid) {
  136. drush_log("Context {$name} has been imported. Updated {$node->type} node {$node->nid}.");
  137. $known_contexts[$name] = $node->nid;
  138. }
  139. return $node->nid;
  140. }