-
Notifications
You must be signed in to change notification settings - Fork 4
/
ddb_cp.module
executable file
·576 lines (499 loc) · 20.4 KB
/
ddb_cp.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<?php
/**
* @file
* Control Panel for the DDB CMS system.
*
* DDB CMS is a Drupal based frontend in use by Danish public libraries.
*/
// Standard error message when the webservice does not respond.
define('DDB_CP_MSG_ERROR_NO_WEB_SERVICE', 'Cannot communicate with the DDB Webservice.');
/**
* Returns an instance of the ddbService class.
*
* It utilizing Drupal's static cache to implement a singleton pattern.
*/
function ddb_cp_ws() {
$result = &drupal_static(__FUNCTION__);
if (!isset($result)) {
$result = new DdbService();
}
return $result;
}
/**
* Implements hook_menu().
*/
function ddb_cp_menu() {
$items = array();
$path = drupal_get_path('module', 'ddb_cp');
$items['admin/ddb_cp'] = array(
'title' => 'DDB',
'description' => 'Configure and manage the DDB installation.',
'page callback' => 'ddb_cp_menu_content',
'access arguments' => array('administer ddb'),
'file' => 'ddb_cp.admin.inc',
'file path' => $path . '/includes/',
);
$items['admin/ddb_cp/config'] = array(
'title' => 'Configuration',
'description' => 'Configure this DDB instance.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ddb_cp_menu_configuration'),
'access arguments' => array('administer ddb_cp'),
'file' => 'ddb_cp.admin.inc',
'file path' => $path . '/includes/',
);
if (ddb_cp_ws()->model() == 'webmaster') {
$items['admin/ddb_cp/development'] = array(
'title' => 'Development',
'description' => 'Manage development of this DDB instance.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ddb_cp_menu_development'),
'access arguments' => array('administer ddb_cp'),
'file' => 'ddb_cp.admin.inc',
'file path' => $path . '/includes/',
);
}
$items['admin/ddb_cp/test/xml'] = array(
'page callback' => 'ddb_cp_get_test_result',
'type' => MENU_CALLBACK,
'access arguments' => array('administer ddb_cp''),
);
$items['ajax/ddb_cp/status'] = array(
'page callback' => 'ddb_cp_ajax_status',
'type' => MENU_CALLBACK,
'access arguments' => array('administer ddb_cp''),
);
$items['ajax/ddb_cp/test/execute'] = array(
'page callback' => 'ddb_cp_ajax_test_execute',
'type' => MENU_CALLBACK,
'access arguments' => array('administer ddb_cp''),
);
$items['ajax/ddb_cp/recreate'] = array(
'page callback' => 'ddb_cp_ajax_recreate',
'type' => MENU_CALLBACK,
'access arguments' => array('administer ddb_cp''),
);
$items['ajax/ddb_cp/test/result'] = array(
'page callback' => 'ddb_cp_ajax_test_result',
'type' => MENU_CALLBACK,
'access arguments' => array('administer ddb_cp''),
);
return $items;
}
/**
* Implements hook_permission().
*/
function ddb_cp_permission() {
return array(
'administer ddb_cp' => array(
'title' => t('Administer this DDB instance'),
),
);
}
/**
* Function to return raw Jenkins XML test report or 'Page not found'.
*
* @param string $site
* 'dev' or 'stg' to select the site.
* @param null $number
* Optional number to select a specific test run - defaults to latest.
*
* @return mixed
* XML data or 'Page not found'.
*/
function ddb_cp_get_test_result($site = NULL, $number = NULL) {
$site_types = array('dev', 'stg');
if ($site && in_array($site, $site_types)) {
_ddb_cp_save_test_result($site, $number);
}
else {
return MENU_NOT_FOUND;
}
// @TODO: This function never returns any XML data only page not found error code?
}
/**
* Form submit routine to create a new developer site.
*/
function ddb_cp_create_dev_submit($form, &$form_state) {
if (ddb_cp_ws()->execute('Create developer site', array('SITE' => ddb_cp_ws()->names['prod'], 'TEMPLATE' => $form_state['input']['platform']))) {
drupal_set_message(t('A job to create a new developer site has been scheduled.'));
}
else {
drupal_set_message(t(DDB_CP_MSG_ERROR_NO_WEB_SERVICE), 'error');
}
drupal_rebuild_form('ddb_cp_menu_development', $form_state);
}
/**
* Form submit routine to create a new staging site.
*
* @param array $form
* The form that have been submitted.
* @param array $form_state
* The forms state information.
*/
function ddb_cp_create_stg_submit($form, &$form_state) {
if (ddb_cp_ws()->execute('Create staging site', array('SITE' => ddb_cp_ws()->names['prod']))) {
drupal_set_message(t('A job to create a new staging site has been scheduled.'));
}
else {
drupal_set_message(t(DDB_CP_MSG_ERROR_NO_WEB_SERVICE), 'error');
}
drupal_rebuild_form('ddb_cp_menu_development', $form_state);
}
/**
* Form submit to update production site to current staging site platform.
*
* @param array $form
* The form that have been submitted.
* @param array $form_state
* The forms state information.
*/
function ddb_cp_update_prod_submit($form, &$form_state) {
if (ddb_cp_ws()->execute('Update production site', array('SITE' => ddb_cp_ws()->names['prod']))) {
drupal_set_message(t('A job to update the production site has been scheduled.'));
}
else {
drupal_set_message(t(DDB_CP_MSG_ERROR_NO_WEB_SERVICE), 'error');
}
drupal_rebuild_form('ddb_cp_menu_development', $form_state);
}
/**
* Function to recreate a developer and/or staging site.
*
* @param string $site
* 'dev' or 'stg' to indicate the site type to recreate.
*/
function ddb_cp_ajax_recreate($site) {
if (isset($site)) {
switch ($site) {
case 'dev':
$status = ddb_cp_ws()->status()['stg'];
if ($status['status'] == 'running' || $status['status'] == 'building' || $status['status'] == 'disabled') {
ddb_cp_ws()->execute('Delete staging site', array('SITE' => ddb_cp_ws()->names['prod']));
}
ddb_cp_ws()->execute('Delete developer site', array('SITE' => ddb_cp_ws()->names['prod']));
break;
case 'stg':
ddb_cp_ws()->execute('Recreate staging site', array('SITE' => ddb_cp_ws()->names['prod']));
break;
}
}
}
/**
* Ajax callback to fetch site status from webservice.
*/
function ddb_cp_ajax_status() {
$result = array();
if (ddb_cp_ws()->status()) {
foreach (ddb_cp_ws()->status() as $site => $status) {
$result['#' . $site . '-site-status'] = _ddb_cp_get_site_status_message($site, $status);
if ($site == 'dev' || $site == 'stg') {
$result['#' . $site . '-test-status'] = _ddb_cp_get_test_status_message($site, $status);
}
}
}
drupal_json_output($result);
}
/**
* Ajax callback to initiate a site test.
*
* @param string $site
* Full DNS name of site to test.
*/
function ddb_cp_ajax_test_execute($site) {
if (isset($site)) {
ddb_cp_ws()->execute(ddb_cp_ws()->names[$site] . ' test');
}
}
/**
* Ajax callback to fetch test result from Jenkins server.
*
* @param string $site
* 'dev' or 'stg' to select test result.
*
* @return mixed
* JSON encoded HTML to display test result or 'Page not found'.
*/
function ddb_cp_ajax_test_result($site = NULL) {
$site_types = array('dev', 'stg');
if ($site && in_array($site, $site_types)) {
drupal_json_output(_ddb_cp_get_test_result($site));
}
else {
return MENU_NOT_FOUND;
}
// @todo: Missing return statement it will note return any thing if the if statement is hit?
}
/**
* Function to return raw XML result file from Jenkins server.
*
* @param string $site
* 'dev' or 'stg' to select the site.
* @param null $number
* Optional number to select a specific test run - defaults to latest.
*/
function _ddb_cp_save_test_result($site = NULL, $number = NULL) {
if (isset($site)) {
if ($xml = ddb_cp_ws()->testReport(ddb_cp_ws()->names[$site], $number)) {
drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
if (isset($number)) {
drupal_add_http_header('Content-Disposition', 'attachment; filename="Testreport ' . ddb_cp_ws()->names[$site] . ' build ' . $number . '.xml"');
}
else {
drupal_add_http_header('Content-Disposition', 'attachment; filename="Testreport ' . ddb_cp_ws()->names[$site] . '.xml"');
}
print '<?xml version="1.0" encoding="UTF-8"?>';
print $xml;
drupal_exit();
}
else {
drupal_set_message(t('Error fetching testreport'), 'error');
}
}
}
/**
* Returns an HTML formatted site status message string.
*
* @param string $site
* 'prod', 'dev', or 'stg' site to format message for.
* @param mixed $status
* Site-specific status record as selected by ddb_cp_ajax_status()
*
* @todo: All the HTML in this would be nice to have into tpl's and theme functions.
*
* @return string
* An HTML formatted site status string.
*/
function _ddb_cp_get_site_status_message($site, $status) {
$prefix = '<div id="' . $site . '-site-status" class="site-status-unknown">';
$message = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/> ' . t('The site status is unknown.');
$platform = '';
$timestamp = '';
switch ($status['status']) {
case 'unknown':
$prefix = '<div id="' . $site . '-site-status" class="site-status-unknown">';
$message = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/> ' . t('The site status is unknown.');
break;
case 'running':
$prefix = '<div id="' . $site . '-site-status" class="site-status-running">';
$message = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/green.gif"/> ' . t('The site is up and running.');
$timestamp = '<tr><th>' . t('Creation time') . ':</th><td>' . format_date($status['timestamp'], 'short') . '</td></tr>';
$platform = '<tr><th>' . t('Platform') . ':</th><td>' . $status['platform']['name'] . '</td></tr>';
break;
case 'building':
$prefix = '<div id="' . $site . '-site-status" class="site-status-building">';
$message = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/green_anime.gif"/> ' . t('The site is being created.');
break;
case 'disabled':
$prefix = '<div id="' . $site . '-site-status" class="site-status-disabled">';
$message = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/yellow.gif"/> ' . t('The site is disabled.');
$timestamp = '<tr><th>' . t('Creation time') . ':</th><td>' . format_date($status['timestamp'], 'short') . '</td></tr>';
$platform = '<tr><th>' . t('Platform') . ':</th><td>' . $status['platform']['name'] . '</td></tr>';
break;
case 'not_exist':
$prefix = '<div id="' . $site . '-site-status" class="site-status-not-exist">';
$message = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/> ' . t('The site does not exist.');
break;
}
return $prefix . $message . '<table>' . $timestamp . $platform . '</table></div>';
}
/**
* Retrieves and formats a full test report from a Jenkins server.
*
* @param string $site
* 'dev' or 'stg' site to get test report for.
*
* @todo: All the HTML in this would be nice to have into tpl's and theme functions.
*
* @return string
* An HTML formatted <div/> showing Jenkins test report.
*/
function _ddb_cp_get_test_result($site = NULL) {
$result = '<div>Test result not available.</div>';
if (isset($site)) {
$buildnumber = NULL;
$buildurl = NULL;
if ($data = ddb_cp_ws()->testResult(ddb_cp_ws()->names[$site])) {
$xml = simplexml_load_string($data);
$node = $xml->xpath('/freeStyleBuild/number');
if (isset($node)) {
$buildnumber = $node[0]->__toString();
}
$node = $xml->xpath('/freeStyleBuild/url');
if (isset($node)) {
// @todo: This variable is only being set but never begin used?
$buildurl = $node[0]->__toString();
}
}
$result = '<div id="' . $site . '-test-buttons"><img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/16x16/reload.png" alt="Passed"/> <a id="reload-' . $site . '-test-report" href="#">Reload</a> <img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/16x16/disk.png" alt="Passed"/> <a href="/admin/ddb_cp/test/xml/' . $site . '/' . $buildnumber . '">Save</a></div>';
$result .= '<p>' . _ddb_cp_get_test_status_summary(ddb_cp_ws()->status()[$site]) . '</p>';
$test = array();
if ($data = ddb_cp_ws()->testReport(ddb_cp_ws()->names[$site])) {
$xml = simplexml_load_string($data);
foreach ($xml->xpath('/testResult/suite/case') as $node) {
$scratch = explode('.', $node->className->__toString());
$test[$scratch[0]][$scratch[1]][$node->name->__toString()] = array('status' => $node->status->__toString(), 'lines' => $node->stdout->__toString());
if (isset($node->errorStackTrace) && $node->errorStackTrace != '') {
$test[$scratch[0]][$scratch[1]][$node->name->__toString()]['lines'] = $node->errorDetails->__toString() . ': ' . $node->errorStackTrace->__toString();
}
}
}
$failed = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/16x16/failed.png" alt="' . t('Failed') . '"/>';
$passed = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/16x16/passed.png" alt="' . t('Passed') . '"/>';
foreach ($test as $key => $suite) {
$failures = 0;
foreach ($suite as $case) {
foreach ($case as $step) {
if ($step['status'] == 'FAILED' || $step['status'] == 'REGRESSION') {
$failures++;
}
}
}
if ($failures > 0) {
$result .= '<div class="' . $site . '-test-suite collapsed">' . $failed . ' <a href="#">' . $key . '</a>';
}
else {
$result .= '<div class="' . $site . '-test-suite collapsed">' . $passed . ' <a href="#">' . $key . '</a>';
}
foreach ($suite as $suite_k => $case) {
$failures = 0;
foreach ($case as $step) {
if ($step['status'] == 'FAILED' || $step['status'] == 'REGRESSION') {
$failures++;
}
}
if ($failures > 0) {
$result .= '<div class="' . $site . '-test-case collapsed">' . $failed . ' <a href="#">' . $suite_k . '</a>';
}
else {
$result .= '<div class="' . $site . '-test-case collapsed">' . $passed . ' <a href="#">' . $suite_k . '</a>';
}
foreach ($case as $case_k => $step) {
if ($step['status'] == 'FAILED' || $step['status'] == 'REGRESSION') {
$result .= '<div class="' . $site . '-test-step collapsed">' . $failed . ' <a href="#">' . $case_k . '</a>';
}
else {
$result .= '<div class="' . $site . '-test-step collapsed">' . $passed . ' <a href="#">' . $case_k . '</a>';
}
$result .= '<div class="' . $site . '-stdout"><pre>' . $step['lines'] . '</pre></div></div>';
}
$result .= '</div>';
}
$result .= '</div>';
}
}
return $result;
}
/**
* Returns an HTML formatted test status message string.
*
* @param string $site
* 'dev' or 'stg' site to format message for.
* @param mixed $status
* Site-specific status record as selected by ddb_cp_ajax_status()
*
* @todo: All the HTML in this would be nice to have into tpl's and theme functions.
*
* @return string
* An HTML formatted test status string.
*/
function _ddb_cp_get_test_status_message($site, $status) {
$prefix = '<div id="' . $site . '-test-status" class="test-status-unknown">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/>';
$timestamp = '';
switch ($status['test']['status']) {
case 'unknown':
$prefix = '<div id="' . $site . '-test-status" class="test-status-unknown">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/>';
break;
case 'blue':
$prefix = '<div id="' . $site . '-test-status" class="test-status-ok test-ready">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/green.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'blue_anime':
$prefix = '<div id="' . $site . '-test-status" class="test-status-ok test-running">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/green_anime.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'yellow':
$prefix = '<div id="' . $site . '-test-status" class="test-status-warn test-ready">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/yellow.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'yellow_anime':
$prefix = '<div id="' . $site . '-test-status" class="test-status-warn test-running">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/yellow_anime.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'red':
$prefix = '<div id="' . $site . '-test-status" class="test-status-fail test-ready">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/red.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'red_anime':
$prefix = '<div id="' . $site . '-test-status" class="test-status-fail test-running">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/red_anime.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'notbuilt':
$prefix = '<div id="' . $site . '-test-status" class="test-status-notbuild test-ready">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/>';
break;
case 'notbuilt_anime':
$prefix = '<div id="' . $site . '-test-status" class="test-status-notbuilt test-running">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey_anime.gif"/>';
break;
case 'aborted':
$prefix = '<div id="' . $site . '-test-status" class="test-status-aborted test-ready">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
case 'aborted_anime':
$prefix = '<div id="' . $site . '-test-status" class="test-status-aborted test-running">';
$ball = '<img src="/' . drupal_get_path('module', 'ddb_cp') . '/images/32x32/grey_anime.gif"/>';
if ($status['test']['timestamp']) {
$timestamp = '<table><tr><th>' . t('Test time') . ':</th><td>' . format_date($status['test']['timestamp'], 'short') . '</td></tr></table>';
}
break;
}
return $prefix . $ball . ' Last test: ' . _ddb_cp_get_test_status_summary($status) . $timestamp . '</div>';
}
/**
* Function to format test summary description.
*
* @param mixed $status
* Site-specific status record as selected by ddb_cp_ajax_status()
*
* @return string
* A formatted test summary string.
*/
function _ddb_cp_get_test_status_summary($status) {
$result = t('Test summary not available.');
if (isset($status['test']) && isset($status['test']['last'])) {
if ($status['test']['last']['status'] == 'ABORTED') {
$result = 'Build ' . $status['test']['last']['number'] . ' - Cancelled.';
}
elseif ($status['test']['last']['status'] == 'FAILURE') {
$result = 'Build ' . $status['test']['last']['number'] . ' - Failed.';
}
elseif ((!$status['test'] == NULL) && (!$status['test']['last'] == NULL) && (!$status['test']['last']['status'] == NULL)) {
$result = 'Build ' . $status['test']['last']['number'] . ' - ' . $status['test']['last']['total'] . ' total, ' . $status['test']['last']['failed'] . ' failed, ' . $status['test']['last']['skipped'] . ' skipped.';
}
}
return $result;
}