task.hosting.inc 5.38 KB
Newer Older
1 2
<?php

3 4 5 6 7 8 9 10
/**
 * @file
 *   Drush include for the Hosting module's hosting task command.
 */

/**
 * Log a message to the current task's node if possible, the screen otherwise.
 */
11 12 13 14 15 16
function _hosting_task_log($entry) {
  $task = drush_get_context('HOSTING_TASK');
  if ($task->vid) {
    hosting_task_log($task->vid, $entry['type'], $entry['message'], $entry['error'], $entry['timestamp']);
  }
  else {
17 18 19 20
    return _drush_print_log($entry);
  }
  if (drush_get_option('debug', FALSE)) {
    return _drush_print_log($entry);
21 22 23
  }
}

24 25 26 27 28 29 30 31 32
/**
 * Validate hook for the hosting-task Drush command.
 *
 * We do some magic in this command to allow the user to run either a specifc
 * task by specifying a node id or chosen task type by specifying the type of
 * task, e.g. 'verify' or 'migrate'.
 * 
 * @see drush_hosting_task()
 */
33
function drush_hosting_task_validate($id, $type = null) {
Adrian Rossouw's avatar
Typo  
Adrian Rossouw committed
34
  drush_set_option('user', 1);
35
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_LOGIN);
36 37
  if (is_numeric($id)) {
    $task = node_load($id);
38
  }
39 40
  elseif (is_string($id) && isset($type)) {
    $ref = hosting_context_load($id);
41 42 43 44
    if ($ref->nid) {
      if (!($task = hosting_get_most_recent_task($ref->nid, $type))) {
        $task = hosting_add_task($ref->nid, $type); 
      }
45 46 47
    }
  }

48 49
  if ($task->type == 'task') {
    $task->ref = node_load($task->rid);
50
    $task->options = array();
drumm's avatar
drumm committed
51 52 53
    $task->context_options = array(
      'context_type' => $task->ref->type,
      'master_url' => url('', array('absolute' => TRUE)),
54
      'root' => null,
55
      'uri' => null,
56
    );
57
    $task->args = array();
58 59
    $task->changed = time();
    $task->executed = time();
60 61 62 63 64
    /* if not already running, remove the task from the queue
     * this is to avoid concurrent task runs */
    if ($task->task_status == HOSTING_TASK_PROCESSING) {
      return drush_set_error('HOSTING_TASK_RUNNING', dt("This task is already running"));
    }
65 66 67
    if ($task->task_status != HOSTING_TASK_QUEUED && !drush_get_option('force', false)) {
      return drush_set_error('HOSTING_TASK_NOT_QUEUED', dt("This task is not queued, use --force"));
    }
68
    $task->task_status = HOSTING_TASK_PROCESSING;
69
    $task->revision = TRUE;
70
    node_save($task);
71 72 73 74 75 76 77
    drush_set_context('HOSTING_TASK', $task);
    drush_set_context('DRUSH_LOG_CALLBACK', '_hosting_task_log');
    drush_log(dt("Task starts processing"), 'queue');
  }
  else {
    drush_set_error('HOSTING_INVALID_TASK', t("This task is not valid"));
  }
78 79 80 81 82 83 84 85

  // Load Task Info
  $tasks_info = hosting_available_tasks($task->ref->type);

  // Find task type and pass through if it needs provision_save
  if (isset($tasks_info[$task->task_type])){
    $task->task_info = $tasks_info[$task->task_type];
  }
86 87
}

88 89 90 91 92 93 94 95
/**
 * Drush hosting task command.
 *
 * This is the main way that the frontend communicates with the backend. Tasks
 * correspond to backend drush commands, and the results and log of the command
 * are attached to the task for reference.
 *
 * @see drush_hosting_task_validate()
96
 * @see hook_hosting_TASK_OBJECT_context_options()
97
 */
98
function drush_hosting_task() {
99
  $task =& drush_get_context('HOSTING_TASK');
drumm's avatar
drumm committed
100 101
  $output = array();
  $mode = drush_get_option('debug', FALSE) ? 'GET' : 'POST';
102

103 104 105
  // Make sure argument order is correct
  ksort($task->args);

106 107 108 109
  // If this task type needs it, run provision-save to create the named context.
  if (!empty($task->task_info['provision_save'])) {
    // Invoke hook_hosting_TASK_OBJECT_context_options()
    // We copy module_invoke_all() here because it doesn't pass by
110 111 112 113
    // reference and it breaks under PHP 5.3
    $hook = 'hosting_' . $task->ref->type . '_context_options';
    foreach (module_implements($hook) as $module) {
      $function = $module . '_' . $hook;
114
      call_user_func($function, $task);
115 116
    }

Steven Jones's avatar
Steven Jones committed
117
    $output = drush_invoke_process('@none', 'provision-save', array('@' . $task->ref->hosting_name), $task->context_options, array('method' => $mode));
drumm's avatar
drumm committed
118
  }
119
 
drumm's avatar
drumm committed
120
  // Run the actual command. Adding alias here to work aorund Drush API.
121
  $output = provision_backend_invoke($task->ref->hosting_name, 'provision-' . $task->task_type, $task->args, $task->options, $mode);
122 123
  drush_set_context('HOSTING_DRUSH_OUTPUT', $output);
  $code = drush_get_error();
124
 
125 126 127
  // We return 0 on success, so anything else is an error.
  $task->task_status = ($code) ? HOSTING_TASK_ERROR : HOSTING_TASK_SUCCESS;

drumm's avatar
drumm committed
128 129
  // On succesful delete, remove the named context.
  if ($task->task_type === 'delete' && $task->task_status === HOSTING_TASK_SUCCESS) {
Steven Jones's avatar
Steven Jones committed
130
    $output = drush_invoke_process('@none', 'provision-save', array('@' . $task->ref->hosting_name), array('delete' => TRUE), array('method' => $mode));
drumm's avatar
drumm committed
131 132
  }

133 134
  // New revision is created at the beginning of function.
  $task->revision = FALSE;
135
  $task->delta = time() - $task->executed;
136 137 138
  node_save($task);
}

139 140 141 142 143
/**
 * Rollback hook for the hosting-task Drush command.
 *
 * @see hook_hosting_TASK_TYPE_task_rollback()
 */
144
function drush_hosting_task_rollback() {
145
  $task =& drush_get_context('HOSTING_TASK');
146
  module_invoke_all(sprintf("hosting_%s_task_rollback", str_replace('-', '_', $task->task_type)), $task, drush_get_context('HOSTING_DRUSH_OUTPUT'));
147 148
}

149 150 151 152 153
/**
 * Post completion hook for the hosting-task Drush command.
 *
 * @see hook_post_hosting_TASK_TYPE_task()
 */
154
function drush_hosting_post_hosting_task($task) {
155
  $task =& drush_get_context('HOSTING_TASK');
drumm's avatar
drumm committed
156

157
  module_invoke_all(sprintf("post_hosting_%s_task", str_replace('-', '_', $task->task_type)), $task, drush_get_context('HOSTING_DRUSH_OUTPUT'));
158
}