-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboa.module
596 lines (514 loc) · 16.4 KB
/
boa.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
function boa_variable_get($var) {
$defaults = array(
'hadoop_path' => '/home/hadoop/hadoop-current',
'compiler_path' => '/home/boa/compiler',
'output_path' => '/home/boa/www/boa/output',
'output_path_url' => 'boa/output',
);
return variable_get('boa_' . $var, $defaults[$var]);
}
function boa_path_get($var) {
$path = boa_variable_get($var);
if (substr($path, -1) != '/') $path.= '/';
return realpath($path);
}
function hadoop_exec_path() {
return boa_path_get('hadoop_path') . '/bin/hadoop';
}
function boa_config_form($form, &$form_state) {
$form['#tree'] = TRUE;
$form['boa_hadoop_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to Hadoop install'),
'#default_value' => boa_variable_get('hadoop_path'),
'#size' => 50,
'#maxlength' => 200,
'#description' => t('The path to the Hadoop installation. This should contain a bin/ directory underneath it.'),
'#required' => TRUE,
);
$form['boa_compiler_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to Boa compilers'),
'#default_value' => boa_variable_get('compiler_path'),
'#size' => 50,
'#maxlength' => 200,
'#description' => t('The path to the directory that contains all Boa compilers for each dataset.'),
'#required' => TRUE,
);
$form['boa_output_path'] = array(
'#type' => 'textfield',
'#title' => t('Path to Boa output'),
'#default_value' => boa_variable_get('output_path'),
'#size' => 50,
'#maxlength' => 200,
'#description' => t('The path where Boa output files are written to, so users can download them.'),
'#required' => TRUE,
);
$form['boa_output_path_url'] = array(
'#type' => 'textfield',
'#title' => t('URL to Boa output'),
'#default_value' => boa_variable_get('output_path_url'),
'#size' => 50,
'#maxlength' => 200,
'#description' => t('The URL where Boa output files are (relative to the root), so users can download them.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
function boa_config_form_validate($form, &$form_state) {
if (realpath($form_state['values']['boa_hadoop_path']) === false)
form_set_error('boa_hadoop_path', t('Path does not exist.'));
if (realpath($form_state['values']['boa_compiler_path']) === false)
form_set_error('boa_compiler_path', t('Path does not exist.'));
if (realpath(realpath($form_state['values']['boa_hadoop_path']) . '/bin/hadoop') === false)
form_set_error('boa_hadoop_path', t('Could not find \'bin/hadoop\' executable at this path.'));
if (is_executable(realpath(realpath($form_state['values']['boa_hadoop_path']) . '/bin/hadoop')) === false)
form_set_error('boa_hadoop_path', t('The file \'bin/hadoop\' is not executable under this path.'));
if (realpath($form_state['values']['boa_output_path']) === false)
form_set_error('boa_output_path', t('Path does not exist.'));
if (is_writable(realpath($form_state['values']['boa_output_path'])) === false)
form_set_error('boa_output_path', t('The output directory is not writable.'));
}
function is_boa_admin() {
global $user;
return in_array('administrator', $user->roles) || in_array('boa_admin', $user->roles);
}
function is_boa_demo() {
global $user;
return in_array('boa_demo', $user->roles);
}
function is_boa_user() {
global $user;
return in_array('boa_user', $user->roles);
}
function is_boa_throttled() {
global $user;
return in_array('boa_throttled', $user->roles);
}
function get_input_query() {
$query = db_select('boa_input', 'r');
$query->fields('r', array('id'));
if (!is_boa_admin()) {
$query->condition('id', 1, '>');
$query->condition('enabled', 1);
}
$query->isNotNull('path');
$query->isNotNull('compiler_path');
return $query;
}
function is_valid_input($id) {
$query = get_input_query();
$query->condition('id', $id);
$result = $query->execute();
return $result->rowCount() == 1;
}
function invalid_input() {
drupal_set_message(t('Invalid input: input ID does not exist or is not enabled for your user.'), 'error');
drupal_exit();
}
function job_run_count($uid = null) {
if ($uid !== null && !is_boa_throttled())
return 0;
$query = db_select('boa_jobs', 'j');
$query->condition('hadoop_status', '1');
if ($uid !== null)
$query->condition('uid', $uid);
$query->addExpression('COUNT(id)', 'running');
$result = $query->execute();
$record = $result->fetchObject();
return $record->running;
}
function boa_date($timestamp) {
return format_date($timestamp, 'custom', 'r');
}
function formatBytes($size) {
if ($size == 0)
return '0 bytes';
$suffixes = array(' bytes', 'k', 'M', 'G');
$base = log($size) / log(1024);
return round(pow(1024, $base - floor($base)), 2) . $suffixes[floor($base)];
}
function fakeRunning($record) {
if (is_boa_admin())
return;
global $user;
if ($user !== null && property_exists($user, 'name') && $user->name == "paclab")
return;
if ($record->compiler_status == 2 && $record->compile_end > time()) {
$record->compiler_status = 1;
$record->hadoop_status = 0;
}
if ($record->hadoop_status == 2 && $record->hadoop_end > time())
$record->hadoop_status = 1;
}
function boa_duration($length) {
$s = "";
if ($length > 3600) {
$s .= (floor($length / 3600)) . "h ";
$length = $length % 3600;
}
if (strlen($s) > 0 || $length > 60) {
$s .= (floor($length / 60)) . "m ";
$length = $length % 60;
}
return $s . "{$length}s";
}
function isValidJob($jobId) {
global $user;
if ($user === null) return false;
$query = db_select('boa_jobs', 'r');
$query->fields('r', array('id'));
if (!is_boa_admin()) {
$query->condition(db_or()
->condition('uid', $user->uid)
->condition('public', true)
);
$query->condition('deleted', 0);
}
$query->condition('id', $jobId);
$result = $query->execute();
return $result->rowCount() == 1;
}
function invalidJob($jobId) {
drupal_set_message(t('Invalid job :jobid or this job does not belong to you.', array(':jobid' => $jobId)), 'error');
drupal_goto('boa/jobs');
drupal_exit();
}
function showStatus($status) {
switch ($status) {
default:
case 0: return t('Waiting');
case 1: return t('Running');
case 2: return t('Finished');
case 3: return t('Stopping');
case -1: return t('Error');
case -2: return t('Killed');
}
}
function boa_job_access($jobId, $jobId2 = null) {
if ($jobId2 !== null && !boa_job_access($jobId2)) return false;
if (isValidJob($jobId)) return true;
return boa_public_access($jobId);
}
function boa_public_access($jobId) {
if (is_boa_admin())
return true;
$query = db_select('boa_jobs', 'r');
$query->fields('r', array('id'));
$query->condition('public', 1);
$query->condition('deleted', 0);
$query->condition('id', $jobId);
$result = $query->execute();
return $result->rowCount() == 1;
}
function boa_status_access($jobId) {
$query = db_select('boa_jobs', 'r');
$query->fields('r', array('id'));
$query->condition('hadoop_status', 1);
$query->condition('id', $jobId);
$result = $query->execute();
return $result->rowCount() == 1;
}
function boa_output_access_rpc($jobId) {
$query = db_select('boa_jobs', 'r');
$query->fields('r', array('hadoop_status', 'compiler_status'));
$query->addExpression('unix_timestamp(compile_end)', 'compile_end');
$query->addExpression('unix_timestamp(hadoop_end)', 'hadoop_end');
$query->condition('r.id', $jobId);
$result = $query->execute();
if ($result->rowCount() == 0)
return false;
$record = $result->fetchObject();
fakeRunning($record);
return $record->hadoop_status == 2;
}
function boa_output_access($jobId) {
$query = db_select('boa_jobs', 'r');
$query->join('boa_output', 'o', 'r.id = o.id');
$query->fields('r', array('hadoop_status', 'compiler_status'));
$query->addExpression('unix_timestamp(compile_end)', 'compile_end');
$query->addExpression('unix_timestamp(hadoop_end)', 'hadoop_end');
$query->addExpression('COALESCE(o.length, 0)', 'length');
$query->condition('r.id', $jobId);
$result = $query->execute();
if ($result->rowCount() == 0)
return false;
$record = $result->fetchObject();
fakeRunning($record);
return $record->hadoop_status == 2 && $record->length > 0;
}
function boa_job_not_deleted_access($jobId) {
$query = db_select('boa_jobs', 'r');
$query->fields('r', array('id'));
$query->condition('deleted', 0);
$query->condition('id', $jobId);
$result = $query->execute();
return $result->rowCount() == 1;
}
function boa_job_stopped_access($jobId) {
return !boa_job_running_access($jobId);
}
function boa_job_resubmit_access($jobId) {
$query = db_select('boa_jobs', 'j');
$query->join('boa_input', 'i', 'j.input = i.id');
$query->fields('i', array('enabled'));
$query->isNotNull('path');
$query->isNotNull('compiler_path');
$query->condition('j.id', $jobId);
$result = $query->execute();
if ($result->rowCount() == 0) return false;
$record = $result->fetchObject();
if (!$record->enabled && !is_boa_admin())
return false;
return !boa_job_running_access($jobId);
}
function boa_job_running_access($jobId) {
$query = db_select('boa_jobs', 'r');
$query->fields('r', array('hadoop_status', 'compiler_status'));
$query->addExpression('unix_timestamp(compile_end)', 'compile_end');
$query->addExpression('unix_timestamp(hadoop_end)', 'hadoop_end');
$query->condition('id', $jobId);
$result = $query->execute();
if ($result->rowCount() == 0)
return false;
$record = $result->fetchObject();
fakeRunning($record);
return $record->compiler_status == 0 || $record->compiler_status == 1 ||
($record->compiler_status == 2 && ($record->hadoop_status == 0 || $record->hadoop_status == 1));
}
function boa_user_access($jobId = null) {
if (is_boa_user() || is_boa_demo() || is_boa_admin())
return true;
if ($jobId !== null && boa_public_access($jobId)) {
drupal_goto("boa/job/public/$jobId");
drupal_exit();
}
return false;
}
/**
* Implements hook_menu().
*/
function boa_menu() {
$items['boa/run'] = array(
'title' => t('Run Examples'),
'description' => t('Run Boa examples.'),
'weight' => -1,
'menu_name' => 'main-menu',
'page callback' => 'drupal_get_form',
'page arguments' => array('boa_run_form'),
'access callback' => 'boa_user_access',
'file' => 'run.inc',
);
$items['boa/jobs'] = array(
'title' => t('Job List'),
'description' => t('View all jobs you have submitted.'),
'menu_name' => 'main-menu',
'page callback' => 'boa_jobs',
'access callback' => 'boa_user_access',
'file' => 'jobs.inc',
);
$items['boa/running'] = array(
'title' => t('Running Job List'),
'description' => t('View all currently running jobs.'),
'menu_name' => 'main-menu',
'page callback' => 'boa_running',
'access callback' => 'is_boa_admin',
'file' => 'running.inc',
);
$items['boa/search/%'] = array(
'title' => t('Search Results'),
'description' => t('Find jobs you have submitted by keyword.'),
'page callback' => 'boa_search_results',
'page arguments' => array(2),
'access callback' => 'boa_user_access',
'file' => 'search.inc',
);
$items['boa/search'] = array(
'title' => t('Job Search'),
'description' => t('Find jobs you have submitted by keyword.'),
'menu_name' => 'main-menu',
'page callback' => 'boa_search',
'access callback' => 'boa_user_access',
'file' => 'search.inc',
);
$items['boa/job/%'] = array(
'title' => t('Job Status'),
'page callback' => 'boa_job',
'page arguments' => array(2),
'access callback' => 'boa_user_access',
'access arguments' => array(2),
'type' => MENU_CALLBACK,
'file' => 'job.inc',
);
$items['boa/job/%/view'] = array(
'title' => t('Job Status'),
'page callback' => 'boa_job',
'page arguments' => array(2),
'access callback' => 'boa_user_access',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'job.inc',
);
$items['boa/job/%/output'] = array(
'title' => t('View Job Output'),
'weight' => 1,
'page callback' => 'boa_job_output',
'page arguments' => array(2, false),
'access callback' => 'boa_output_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'file' => 'job.output.inc',
);
$items['boa/job/%/download'] = array(
'title' => t('Download Job Output'),
'weight' => 5,
'page callback' => 'boa_job_download',
'page arguments' => array(2, true),
'access callback' => 'boa_output_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'file' => 'job.download.inc',
);
$items['boa/job/%/download-src'] = array(
'title' => t('Download Source Code'),
'weight' => 5,
'page callback' => 'boa_job_download_src',
'page arguments' => array(2),
'access callback' => 'boa_user_access',
'file' => 'job.download.src.inc',
);
$items['boa/job/%/stop'] = array(
'title' => t('Stop Job'),
'weight' => -5,
'page callback' => 'drupal_get_form',
'page arguments' => array('boa_job_stop', 2),
'access callback' => 'boa_job_running_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_ACTION,
'file' => 'job.stop.inc',
);
$items['boa/job/%/delete'] = array(
'title' => t('Delete Job'),
'weight' => 1,
'page callback' => 'drupal_get_form',
'page arguments' => array('boa_job_delete', 2),
'access callback' => 'boa_job_not_deleted_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_ACTION,
'file' => 'job.delete.inc',
);
$items['boa/job/%/resubmit'] = array(
'title' => t('Resubmit Job'),
'weight' => -3,
'page callback' => 'boa_job_resubmit',
'page arguments' => array(2),
'access callback' => 'boa_job_resubmit_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_ACTION,
'file' => 'job.resubmit.inc',
);
$items['boa/job/%/edit'] = array(
'title' => t('Edit Source Code'),
'weight' => -1,
'page callback' => 'boa_job_edit',
'page arguments' => array(2),
'access callback' => 'boa_user_access',
'file' => 'job.edit.inc',
);
$items['boa/job/public/%'] = array(
'title' => t('Boa Job Status'),
'page callback' => 'boa_public_view',
'page arguments' => array(3),
'access callback' => 'boa_public_access',
'access arguments' => array(3),
'type' => MENU_CALLBACK,
'file' => 'public.view.inc',
);
$items['boa/job/public/%/download'] = array(
'title' => t('Download Job Output'),
'weight' => 5,
'page callback' => 'boa_public_download',
'page arguments' => array(3),
'access callback' => 'boa_public_access',
'access arguments' => array(3),
'type' => MENU_LOCAL_TASK,
'file' => 'public.download.inc',
);
$items['boa/job/%/public'] = array(
'title' => t('Make Job Public'),
'weight' => -1,
'page callback' => 'boa_job_public',
'page arguments' => array(2),
'access callback' => 'boa_job_not_deleted_access',
'access arguments' => array(2),
'file' => 'job.public.inc',
);
$items['boa/job/%/private'] = array(
'title' => t('Make Job Private'),
'weight' => -1,
'page callback' => 'boa_job_private',
'page arguments' => array(2),
'access callback' => 'boa_job_not_deleted_access',
'access arguments' => array(2),
'file' => 'job.public.inc',
);
$items['boa/publicjobs'] = array(
'title' => t('List Public Jobs'),
'weight' => -1,
'page callback' => 'boa_job_list',
'access callback' => 'is_boa_admin',
'file' => 'public.list.inc',
);
$items['boa/job/%/status'] = array(
'title' => t('Execution Status'),
'weight' => 5,
'page callback' => 'boa_job_status',
'page arguments' => array(2),
'access callback' => 'boa_status_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'file' => 'job.status.inc',
);
$items['boa/job/%/diff/%'] = array(
'title' => t('Diff'),
'weight' => 5,
'page callback' => 'boa_job_diff',
'page arguments' => array(2, 4),
'access callback' => 'boa_job_access',
'access arguments' => array(2, 4),
'file' => 'job.diff.inc',
);
$items['boa/datasets/add'] = array(
'title' => t('Add Boa Dataset'),
'weight' => -1,
'page callback' => 'drupal_get_form',
'page arguments' => array('boa_dataset_add_form'),
'access callback' => 'is_boa_admin',
'file' => 'datasets.inc',
);
$items['boa/datasets/%/edit'] = array(
'title' => t('Edit Boa Dataset'),
'weight' => -1,
'page callback' => 'drupal_get_form',
'page arguments' => array('boa_dataset_edit_form', 2),
'access callback' => 'is_boa_admin',
'file' => 'datasets.inc',
);
$items['boa/datasets'] = array(
'title' => t('Dataset List'),
'description' => t('View all input datasets.'),
'weight' => -1,
'page callback' => 'boa_dataset_list',
'access callback' => 'is_boa_admin',
'file' => 'datasets.inc',
);
$items['admin/config/boa'] = array(
'title' => 'Boa Settings',
'description' => 'Configuration for Boa module',
'page callback' => 'drupal_get_form',
'page arguments' => array('boa_config_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}