task.hosting.inc

Drush include for the Hosting module's hosting task command.

Functions

Namesort descending Description
drush_hosting_post_hosting_task Post completion hook for the hosting-task Drush command.
drush_hosting_task Drush hosting task command.
drush_hosting_task_rollback Rollback hook for the hosting-task Drush command.
drush_hosting_task_validate Validate hook for the hosting-task Drush command.
_hosting_task_log Log a message to the current task's node if possible, the screen otherwise.

File

task.hosting.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Drush include for the Hosting module's hosting task command.
  5. */
  6. /**
  7. * Log a message to the current task's node if possible, the screen otherwise.
  8. */
  9. function _hosting_task_log($entry) {
  10. $task = drush_get_context('HOSTING_TASK');
  11. if ($task->vid) {
  12. hosting_task_log($task->vid, $entry['type'], $entry['message'], $entry['error'], $entry['timestamp']);
  13. }
  14. else {
  15. return _drush_print_log($entry);
  16. }
  17. if (drush_get_option('debug', FALSE)) {
  18. return _drush_print_log($entry);
  19. }
  20. }
  21. /**
  22. * Validate hook for the hosting-task Drush command.
  23. *
  24. * We do some magic in this command to allow the user to run either a specifc
  25. * task by specifying a node id or chosen task type by specifying the type of
  26. * task, e.g. 'verify' or 'migrate'.
  27. *
  28. * @see drush_hosting_task()
  29. */
  30. function drush_hosting_task_validate($id, $type = NULL) {
  31. drush_set_option('user', 1);
  32. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_LOGIN);
  33. if (is_numeric($id)) {
  34. $task = node_load($id);
  35. }
  36. elseif (is_string($id) && isset($type)) {
  37. $ref = hosting_context_load($id);
  38. if ($ref->nid) {
  39. $task = hosting_add_task($ref->nid, $type);
  40. }
  41. }
  42. if ($task->type == 'task') {
  43. $task->ref = node_load($task->rid);
  44. $task->options = array();
  45. $task->context_options = array(
  46. 'context_type' => $task->ref->type,
  47. 'master_url' => url('', array('absolute' => TRUE)),
  48. 'root' => NULL,
  49. 'uri' => NULL,
  50. );
  51. $task->args = array();
  52. $task->changed = REQUEST_TIME;
  53. $task->executed = REQUEST_TIME;
  54. /* if not already running, remove the task from the queue
  55. * this is to avoid concurrent task runs */
  56. if ($task->task_status == HOSTING_TASK_PROCESSING && !drush_get_option('force', FALSE)) {
  57. return drush_set_error('HOSTING_TASK_RUNNING', dt("This task is already running, use --force"));
  58. }
  59. if ($task->task_status != HOSTING_TASK_QUEUED && !drush_get_option('force', FALSE)) {
  60. return drush_set_error('HOSTING_TASK_NOT_QUEUED', dt("This task is not queued, use --force"));
  61. }
  62. $task->task_status = HOSTING_TASK_PROCESSING;
  63. $task->revision = TRUE;
  64. node_save($task);
  65. drush_set_context('HOSTING_TASK', $task);
  66. drush_set_context('DRUSH_LOG_CALLBACK', '_hosting_task_log');
  67. drush_log(dt("Task starts processing") . ': ' . $task->title, 'queue');
  68. // Load Task Info.
  69. $tasks_info = hosting_available_tasks($task->ref->type);
  70. // Find task type and pass through if it needs provision_save.
  71. if (isset($tasks_info[$task->task_type])) {
  72. $task->task_info = $tasks_info[$task->task_type];
  73. }
  74. }
  75. else {
  76. drush_set_error('HOSTING_INVALID_TASK', t("This task is not valid"));
  77. }
  78. }
  79. /**
  80. * Drush hosting task command.
  81. *
  82. * This is the main way that the frontend communicates with the backend. Tasks
  83. * correspond to backend drush commands, and the results and log of the command
  84. * are attached to the task for reference.
  85. *
  86. * @see drush_hosting_task_validate()
  87. * @see hook_hosting_TASK_OBJECT_context_options()
  88. */
  89. function drush_hosting_task() {
  90. $task = &drush_get_context('HOSTING_TASK');
  91. $output = array();
  92. $mode = drush_get_option('debug', FALSE) ? 'GET' : 'POST';
  93. // Make sure argument order is correct.
  94. ksort($task->args);
  95. // If this task type needs it, run provision-save to create the named context.
  96. if (!empty($task->task_info['provision_save'])) {
  97. // Invoke hook_hosting_TASK_OBJECT_context_options()
  98. // We copy module_invoke_all() here because it doesn't pass by
  99. // reference and it breaks under PHP 5.3
  100. $hook = 'hosting_' . $task->ref->type . '_context_options';
  101. foreach (module_implements($hook) as $module) {
  102. $function = $module . '_' . $hook;
  103. call_user_func($function, $task);
  104. }
  105. drush_invoke_process('@none', 'provision-save', array('@' . $task->ref->hosting_name), $task->context_options, array('method' => $mode, 'integrate' => TRUE));
  106. }
  107. if (($task->ref->type == 'site' && $task->ref->site_status == HOSTING_SITE_DELETED)
  108. || ($task->ref->type == 'platform' && $task->ref->platform_status == HOSTING_PLATFORM_DELETED)) {
  109. // We're performing a task on a site that has been deleted...
  110. // d() will not be returning a site object.
  111. $alias = '@none';
  112. }
  113. else {
  114. $alias = $task->ref->hosting_name;
  115. }
  116. // Run the actual command. Adding alias here to work around Drush API.
  117. $output = provision_backend_invoke($alias, 'provision-' . $task->task_type, $task->args, $task->options, $mode);
  118. // Pass the drush output back to the HOOK_post_hosting_TASK_task() and
  119. // HOOK_hosting_TASK_task_rollback() hooks, as the $data argument.
  120. drush_set_context('HOSTING_DRUSH_OUTPUT', $output);
  121. // On succesful delete, remove the named context.
  122. if ($task->task_type === 'delete' && !drush_get_error()) {
  123. drush_invoke_process('@none', 'provision-save', array('@' . $task->ref->hosting_name), array('delete' => TRUE), array('method' => $mode, 'integrate' => TRUE));
  124. }
  125. // New revision is created at the beginning of function.
  126. $task->revision = FALSE;
  127. $task->delta = microtime(TRUE) - $task->executed;
  128. node_save($task);
  129. }
  130. /**
  131. * Rollback hook for the hosting-task Drush command.
  132. *
  133. * @see hook_hosting_TASK_TYPE_task_rollback()
  134. */
  135. function drush_hosting_task_rollback() {
  136. $task = &drush_get_context('HOSTING_TASK');
  137. $hook = sprintf("hosting_%s_task_rollback", str_replace('-', '_', $task->task_type));
  138. drush_log(dt('Invoking :hook hooks.', array(':hook' => $hook)));
  139. module_invoke_all($hook, $task, drush_get_context('HOSTING_DRUSH_OUTPUT'));
  140. }
  141. /**
  142. * Post completion hook for the hosting-task Drush command.
  143. *
  144. * @see hook_post_hosting_TASK_TYPE_task()
  145. */
  146. function drush_hosting_post_hosting_task($task) {
  147. $task = &drush_get_context('HOSTING_TASK');
  148. $hook = sprintf("post_hosting_%s_task", str_replace('-', '_', $task->task_type));
  149. drush_log(dt('Invoking :hook hooks.', array(':hook' => $hook)));
  150. module_invoke_all($hook, $task, drush_get_context('HOSTING_DRUSH_OUTPUT'));
  151. }