-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeploy.module
504 lines (465 loc) · 12.9 KB
/
deploy.module
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
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
503
504
<?php
/**
* @file
* Deploy module functions.
*/
/**
* Include core implementations and Deploy's own implementations.
*
* @todo
* Do this properly with hook_hook_info().
*/
module_load_include('inc', 'deploy', 'deploy.manager');
module_load_include('inc', 'deploy', 'deploy.core');
module_load_include('inc', 'deploy', 'deploy.deploy');
/**
* Deployment statuses.
*/
define('DEPLOY_STATUS_FAILED', 0);
define('DEPLOY_STATUS_STARTED', 1);
define('DEPLOY_STATUS_PROCESSING', 2);
define('DEPLOY_STATUS_DEPLOYED', 3);
define('DEPLOY_STATUS_PUBLISHED', 4);
/**
* Implements hook_permission().
*/
function deploy_permission() {
return array(
'administer deployments' => array(
'title' => t('Administer deployments'),
'description' => t('Perform administrative tasks around deployments, like plans and endpoints.'),
),
);
}
/**
* Implements hook_ctools_plugin_type().
*/
function deploy_ctools_plugin_type() {
return array(
'authenticators' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array('handler'),
),
'services' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array('handler'),
),
'aggregators' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array('handler'),
),
'processors' => array(
'cache' => TRUE,
'use hooks' => TRUE,
'classes' => array('handler'),
),
);
}
/**
* Get all aggregator plugins.
*/
function deploy_get_aggregator_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'aggregators');
}
/**
* Get one aggregator plugin.
*/
function deploy_get_aggregator_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'aggregators', $name);
}
/**
* Get all processor plugins.
*/
function deploy_get_processor_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'processors');
}
/**
* Get one processor plugin.
*/
function deploy_get_processor_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'processors', $name);
}
/**
* Get all authenticator plugins.
*/
function deploy_get_authenticator_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'authenticators');
}
/**
* Get one authenticator plugin.
*/
function deploy_get_authenticator_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'authenticators', $name);
}
/**
* Get all service plugins.
*/
function deploy_get_service_plugins() {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'services');
}
/**
* Get one service plugin.
*/
function deploy_get_service_plugin($name) {
ctools_include('plugins');
return ctools_get_plugins('deploy', 'services', $name);
}
/**
* Object factory for a deployment plan.
*/
function deploy_plan_create($schema, $item) {
$plan = new DeployPlan;
// Unserialize our serialized params.
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['serialize'])) {
$plan->{$field} = unserialize($item->{$field});
}
else {
$plan->{$field} = $item->{$field};
}
}
return $plan;
}
/**
* Loader callback for a deployment plan.
*/
function deploy_plan_load($name) {
$plans = deploy_plan_load_all();
if (isset($plans[$name])) {
drupal_alter("deploy_plan_load", $plans[$name]);
return $plans[$name];
}
return FALSE;
}
/**
* Loader callback for all deployment plans.
*/
function deploy_plan_load_all($args = array()) {
$type = !empty($args) ? 'conditions' : 'all';
ctools_include('export');
$plans = ctools_export_load_object('deploy_plans', $type, $args);
if (isset($plans)) {
// Load each plan's configuration into itself.
foreach ($plans as &$plan) {
$plan->load();
}
return $plans;
}
}
/**
* Loader callback for all enabled deployment plans.
*/
function deploy_plan_load_all_enabled($args = array()) {
$type = !empty($args) ? 'conditions' : 'all';
ctools_include('export');
$enabled_plans = array();
$all_plans = ctools_export_load_object('deploy_plans', $type, $args);
if (isset($all_plans)) {
// Load each plan's configuration into itself.
foreach ($all_plans as $plan) {
if (!empty($plan->disabled)) {
continue;
}
$plan->load();
$enabled_plans[$plan->name] = $plan;
}
return $enabled_plans;
}
}
/**
* Options callback for the deploy_plan data type.
*/
function deploy_plan_get_options($args = array()) {
$plans = deploy_plan_load_all($args);
$options = array();
foreach ($plans as $plan_name => $info) {
$options[$plan_name] = $info->title;
}
return $options;
}
/**
* Helper function to recieve the status of a plan.
*/
function deploy_plan_get_status($name) {
$query = db_select('deploy_deployments', 'dd');
$query->join('deploy_log', 'dl', 'dd.lid = dl.lid');
$status = $query
->fields('dl', array('status'))
->condition('dd.plan_name', $name)
->orderBy('dl.timestamp', 'DESC')
->range(0, 1)
->execute()
->fetchField();
return $status;
}
/**
* Object factory for a deployment endpoint.
*/
function deploy_endpoint_create($schema, $item) {
$endpoint = new DeployEndpoint;
// Unserialize our serialized params.
foreach ($schema['fields'] as $field => $info) {
if (!empty($info['serialize'])) {
$endpoint->{$field} = unserialize($item->{$field});
}
else {
$endpoint->{$field} = $item->{$field};
}
}
return $endpoint;
}
/**
* Loader callback for a deployment endpoint.
*/
function deploy_endpoint_load($name) {
$endpoints = deploy_endpoint_load_all();
if (isset($endpoints[$name])) {
return $endpoints[$name];
}
return FALSE;
}
/**
* Loader callback for a deployment endpoint.
*/
function deploy_endpoint_load_all() {
ctools_include('export');
$endpoints = ctools_export_load_object('deploy_endpoints', 'all');
if (isset($endpoints)) {
return $endpoints;
}
}
/**
* Writes a deployment plan.
*/
function deploy_plan_save($data) {
$plan = new DeployPlan();
foreach ($data as $key => $value) {
$plan->{$key} = $value;
}
$returned = ctools_export_crud_save('deploy_plans', $plan);
if (SAVED_NEW !== $returned && SAVED_UPDATED !== $returned) {
throw new DeployPlanException('Failed to create plan.');
}
return $plan;
}
/**
* Implements hook_entity_dependency_iterator().
*/
function deploy_entity_dependency_iterator() {
$plugins = array();
$plugins['deploy_iterator'] = array(
'title' => t('Deploy Iterator'),
'description' => t('Default iterator for Deploy to return UUID entities and invoke Deploy-specific hooks.'),
'handler' => 'DeployIterator',
'file' => 'includes/DeployIterator.inc',
);
return $plugins;
}
/**
* Constructs a deployment iterator, which is the core of the dependency
* framework.
*/
function deploy_iterator($entities, $plan = NULL) {
if (is_null($plan) || empty($plan->dependency_plugin)) {
$iterator = new DeployIterator($entities);
return new EntityDependencyIteratorIterator($iterator);
}
elseif (!empty($plan->dependency_plugin)) {
$class_name = ctools_plugin_load_class('entity_dependency', 'iterator', $plan->dependency_plugin, 'handler');
$iterator = new $class_name($entities);
return new EntityDependencyIteratorIterator($iterator);
}
}
/**
* Helper function to get operation info.
*/
function deploy_get_operation_info($event_name = NULL) {
$cache = &drupal_static(__FUNCTION__);
if (empty($cache)) {
$operations = module_invoke_all('deploy_operation_info');
foreach (array('preprocess', 'postprocess') as $event) {
// Avoid empty keys.
if (!isset($operations[$event])) {
$operations[$event] = array();
}
}
$cache = $operations;
}
if (!empty($event_name)) {
return $cache[$event_name];
}
return $cache;
}
/**
* Implements hook_cron_queue_info().
*/
function deploy_cron_queue_info() {
return array(
'deploy_deploy' => array(
'worker callback' => 'deploy_queue_worker_deploy',
'time' => 60,
),
'deploy_publish' => array(
'worker callback' => 'deploy_queue_worker_publish',
'time' => 60,
),
);
}
/**
* Processes a single queued item for deployment.
*/
function deploy_queue_worker_deploy($entity, &$context = NULL) {
$endpoint = deploy_endpoint_load($entity->__metadata['endpoint_name']);
$plan = deploy_plan_load($entity->__metadata['plan_name']);
if ($plan && $endpoint) {
$entities = array(array('type' => $entity->__metadata['type'], 'id' => $entity->__metadata['id']));
$iterator = deploy_iterator($entities, $plan);
$endpoint->deploy($entity->__metadata['deployment_key'], $iterator, $entity->__metadata['lock_name']);
}
}
/**
* Processes a single queued item for publishing.
*/
function deploy_queue_worker_publish($entity, &$context = NULL) {
$endpoint = deploy_endpoint_load($entity->__metadata['endpoint_name']);
$plan = deploy_plan_load($entity->__metadata['plan_name']);
if ($plan && $endpoint) {
$entities = array(array('type' => $entity->__metadata['type'], 'id' => $entity->__metadata['id']));
$iterator = deploy_iterator($entities, $plan);
$endpoint->publish($entity->__metadata['deployment_key'], $iterator, $entity->__metadata['lock_name']);
$context['results'][$entity->__metadata['endpoint_name']] = $entity->__metadata['plan_name'];
}
}
/**
* Batch API 'finished' callback.
*/
function deploy_batch_finished_operation($success, $results, $operations) {
if ($success) {
foreach ($results as $endpoint_name => $plan_name) {
$endpoint = deploy_endpoint_load($endpoint_name);
$plan = deploy_plan_load($plan_name);
drupal_set_message(t('Plan %plan has been deployed and published to %endpoint.', array('%plan' => $plan->title, '%endpoint' => $endpoint->title)));
}
}
}
/**
* Helper function to retrieve relevant information about a deployment status.
*/
function deploy_status_info($status = NULL, $key = NULL) {
$info = array(
DEPLOY_STATUS_FAILED => array(
'title' => t('Failed'),
'keyed message' => 'Deployment %key failed.',
'watchdog' => WATCHDOG_ERROR,
'class' => 'error',
),
DEPLOY_STATUS_STARTED => array(
'title' => t('Started'),
'keyed message' => 'Deployment of %key started.',
'watchdog' => WATCHDOG_INFO,
'class' => 'warning',
),
DEPLOY_STATUS_PROCESSING => array(
'title' => t('Processing'),
'keyed message' => 'Deployment %key is processing.',
'watchdog' => WATCHDOG_INFO,
'class' => 'warning',
),
DEPLOY_STATUS_DEPLOYED => array(
'title' => t('Deployed'),
'watchdog' => WATCHDOG_INFO,
'keyed message' => 'Deployment %key was deployed.',
'class' => 'warning',
),
DEPLOY_STATUS_PUBLISHED => array(
'title' => t('Published'),
'keyed message' => 'Deployment %key was published.',
'watchdog' => WATCHDOG_INFO,
'class' => 'status',
),
);
if ($status === NULL && $key === NULL) {
return $info;
}
elseif ($status !== NULL && $status !== FALSE && $key === NULL) {
if (isset($info[$status])) {
return $info[$status];
}
}
elseif ($status !== NULL && $status !== FALSE && $key !== NULL && $key !== FALSE) {
if (isset($info[$status][$key])) {
return $info[$status][$key];
}
}
return FALSE;
}
/**
* Helper function to log deployments.
*
* This function also logs to the watchdog log for more visibility.
*
* @param mixed $key
* Can be either an existing key (UUID) or a string of a plan name indicating
* that a new deployment is started and a key needs to be generated.
* @param integer $status
* Status code.
* @param string $message
* A message to log.
* @param array $variables
* Placeholder variables to be used in $message.
*/
function deploy_log($key, $status, $message = '', $variables = array(), $timestamp = NULL) {
// Log to watchdog for more visibility.
$info = deploy_status_info($status);
if ($status == DEPLOY_STATUS_FAILED) {
watchdog_exception('deploy', $message);
}
else {
watchdog('deploy', $info['keyed message'], array('%key' => $key), $info['watchdog']);
}
if ($timestamp === NULL) {
$timestamp = time();
}
// If the key isn't a UUID, then it's a plan name indicating that a new
// deployment has started.
if ($key && !uuid_is_valid($key)) {
$plan_name = $key;
$key = uuid_generate();
db_insert('deploy_deployments')
->fields(array(
'plan_name' => $plan_name,
'uuid' => $key,
))
->execute();
}
if (uuid_is_valid($key)) {
// Add the log entry for this deployment.
$lid = db_insert('deploy_log')
->fields(array(
'uuid' => $key,
'status' => $status,
'message' => $message,
'timestamp' => $timestamp,
))
->execute();
// Update the log pointer for this deployment.
db_update('deploy_deployments')
->fields(array(
'lid' => $lid,
))
->condition('uuid', $key)
->execute();
return $key;
}
return FALSE;
}