-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask.hosting.inc
202 lines (179 loc) · 6.92 KB
/
task.hosting.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* @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.
*/
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 {
_hosting_task_log_print($entry);
}
if (drush_get_option('debug', FALSE)) {
_hosting_task_log_print($entry);
}
}
/**
* Switch between Drush 8's OO logging and the older private function.
*/
function _hosting_task_log_print($entry) {
if (function_exists('_drush_print_log')) {
// Remove after dropping Drush 6 comaptibility.
return _drush_print_log($entry);
}
else {
$logger = new Drush\Log\Logger();
return $logger->log($entry['type'], $entry['message'], $entry);
}
}
/**
* 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()
*/
function drush_hosting_task_validate($id, $type = NULL) {
drush_set_option('user', 1);
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_LOGIN);
if (is_numeric($id)) {
$task = node_load($id);
}
elseif (is_string($id) && isset($type)) {
$ref = hosting_context_load($id);
if ($ref->nid) {
// Get additional arguments to the drush command and convert them to "task_args" as expected by the task node.
$task_args = array();
// Parse task_arguments passed to drush in the format "name=value"
$arguments = func_get_args();
$drush_args = array_splice($arguments, 2);
foreach ($drush_args as $i => $arg) {
list($name, $value) = explode('=', $arg);
$task_args[$name] = $value;
}
$task = hosting_add_task($ref->nid, $type, $task_args);
}
}
if ($task->type == 'task') {
$task->ref = node_load($task->rid);
$task->options = array();
$task->context_options = array(
'context_type' => $task->ref->type,
'master_url' => url('', array('absolute' => TRUE)),
'root' => NULL,
'uri' => NULL,
);
$task->args = array();
$task->changed = REQUEST_TIME;
$task->executed = REQUEST_TIME;
/* if not already running, remove the task from the queue
* this is to avoid concurrent task runs */
if ($task->task_status == HOSTING_TASK_PROCESSING && !drush_get_option('force', FALSE)) {
return drush_set_error('HOSTING_TASK_RUNNING', dt("This task is already running, use --force"));
}
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"));
}
$task->task_status = HOSTING_TASK_PROCESSING;
$task->revision = TRUE;
node_save($task);
drush_set_context('HOSTING_TASK', $task);
drush_set_context('DRUSH_LOG_CALLBACK', '_hosting_task_log');
drush_log(dt("Task starts processing") . ': ' . $task->title, 'queue');
// 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];
}
}
else {
drush_set_error('HOSTING_INVALID_TASK', t("Could not find or create a '!type' task for hosting context '!context'.", array(
'!type' => $type,
'!context' => $id,
)));
}
}
/**
* 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()
* @see hook_hosting_TASK_OBJECT_context_options()
*/
function drush_hosting_task() {
$task = &drush_get_context('HOSTING_TASK');
$output = array();
$mode = drush_get_option('debug', FALSE) ? 'GET' : 'POST';
// Make sure argument order is correct.
ksort($task->args);
// 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
// reference and it breaks under PHP 5.3
$hook = 'hosting_' . $task->ref->type . '_context_options';
foreach (module_implements($hook) as $module) {
$function = $module . '_' . $hook;
call_user_func_array($function, array(&$task));
}
drush_invoke_process('@none', 'provision-save', array('@' . $task->ref->hosting_name), $task->context_options, array('method' => $mode, 'integrate' => TRUE));
}
if (($task->ref->type == 'site' && $task->ref->site_status == HOSTING_SITE_DELETED)
|| ($task->ref->type == 'platform' && $task->ref->platform_status == HOSTING_PLATFORM_DELETED)) {
// We're performing a task on a site that has been deleted...
// d() will not be returning a site object.
$alias = '@none';
}
else {
$alias = $task->ref->hosting_name;
}
if (!isset($task->task_command)) {
$task->task_command = 'provision-' . $task->task_type;
}
// Run the actual command. Adding alias here to work around Drush API.
$output = provision_backend_invoke($alias, $task->task_command, $task->args, $task->options, $mode);
// Pass the drush output back to the HOOK_post_hosting_TASK_task() and
// HOOK_hosting_TASK_task_rollback() hooks, as the $data argument.
drush_set_context('HOSTING_DRUSH_OUTPUT', $output);
// On succesful delete, remove the named context.
if ($task->task_type === 'delete' && !drush_get_error()) {
drush_invoke_process('@none', 'provision-save', array('@' . $task->ref->hosting_name), array('delete' => TRUE), array('method' => $mode, 'integrate' => TRUE));
}
// New revision is created at the beginning of function.
$task->revision = FALSE;
$task->delta = microtime(TRUE) - $task->executed;
node_save($task);
}
/**
* Rollback hook for the hosting-task Drush command.
*
* @see hook_hosting_TASK_TYPE_task_rollback()
*/
function drush_hosting_task_rollback() {
$task = &drush_get_context('HOSTING_TASK');
$hook = sprintf("hosting_%s_task_rollback", str_replace('-', '_', $task->task_type));
drush_log(dt('Invoking :hook hooks.', array(':hook' => $hook)));
module_invoke_all($hook, $task, drush_get_context('HOSTING_DRUSH_OUTPUT'));
}
/**
* Post completion hook for the hosting-task Drush command.
*
* @see hook_post_hosting_TASK_TYPE_task()
*/
function drush_hosting_post_hosting_task($task) {
$task = &drush_get_context('HOSTING_TASK');
$hook = sprintf("post_hosting_%s_task", str_replace('-', '_', $task->task_type));
drush_log(dt('Invoking :hook hooks.', array(':hook' => $hook)));
module_invoke_all($hook, $task, drush_get_context('HOSTING_DRUSH_OUTPUT'));
}