Repository navigation
Expand file tree
/
Copy pathutil.pm
More file actions
executable file
·659 lines (542 loc) · 14.5 KB
/
Copy pathutil.pm
File metadata and controls
executable file
·659 lines (542 loc) · 14.5 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/home/jsoc/bin/linux_x86_64/activeperl
package WriteLog;
use strict;
use warnings;
use Data::Dumper;
use POSIX qw(strftime);
sub new
{
my($clname) = shift;
my($fname) = shift;
my($append) = shift;
my($mode);
my($self) =
{
_fname => undef,
_fh => undef
};
bless($self, $clname);
if (defined($fname) && length($fname) > 0 && -e $fname)
{
my($fh);
if (defined($append) && $append != 0)
{
$mode = ">>";
}
else
{
$mode = ">";
}
if (defined(open($fh, ">$fname")))
{
$self->{_fname} = $fname;
$self->{_fh} = $fh;
}
else
{
print STDERR "Invalid file path.\n";
$self = undef;
}
}
else
{
print STDERR "Invalid file path.\n";
$self = undef;
}
return $self;
}
sub DESTROY
{
my($self) = shift;
$self->close();
}
sub Write
{
my($self) = shift;
my($msg) = shift;
my($nodate) = shift;
my($now);
my($timestr);
if (!defined($nodate) || $nodate == 0)
{
# Start the line with a timestamp
$now = localtime();
$timestr = POSIX::strftime("%B %d, %Y [%H:%M:%S %z]", localtime());
print $self->{_fh} "$timestr"; # Does not end in newline.
if (defined($msg) && length($msg) > 0)
{
print $self->{_fh} " - ";
}
}
if (defined($msg && length($msg) > 0)
{
print $self->{_fh} "$msg\n";
}
else
{
print $self->{_fh} "\n";
}
}
sub Close
{
my($self) = shift;
if (defined($self->{_fh}))
{
$self->{_fh}->close();
$self->{_fh} = undef;
}
}
package Cfg;
use strict;
use warnings;
sub new
{
my($clname, $src, $log) = @_;
my($self) =
{
_hash => {},
_log => undef
};
bless($self, $clname);
if (-e $src)
{
# config source is a file
my($cfg);
my($cfgH) = {};
if ($self->ParseCFile($src, $cfgH))
{
if (defined($log))
{
$log->Write("Unable to open configuration file $src.");
}
else
{
print STDERR "Unable to open configuration file $src.\n";
}
$self = undef;
}
else
{
$self->{_hash} = $cfgH;
}
}
else
{
# config source is a hash array
$self->{_hash} = $src;
}
$self->{_log} = $log;
return $self;
}
sub DESTROY
{
}
sub Get
{
my($self) = shift;
my($name) = shift;
if (exists($self->{_hash}->{$name}))
{
return $self->{_hash}->{$name};
}
else
{
if (defined($self->{_log}))
{
$self->{_log}->Write("Unknown configuration argument '$name'.");
}
else
{
print STDERR "Unknown configuration argument '$name'.\n";
}
return undef;
}
}
sub ParseCFile
{
my($self, $conf, $hashref) = @_;
my(@cfgdata);
my(%cfgraw);
my(%cfg);
my($rv);
$rv = 0;
if (open(CNFFILE, "<$conf"))
{
@cfgdata = <CNFFILE>;
# Make key-value pairs of all non-ccomment lines in configuration file.
%cfgraw = map {
chomp;
my($key, $val) = m/^\s*(\w+)\s*=\s*(.*)/;
defined($key) && defined($val) ? ($key, $val) : ();
} grep {/=/ and !/^#/} @cfgdata;
close(CNFFILE);
# Expand in-line variables in arguments
%cfg = map {
my($val) = $cfgraw{$_};
my($var);
my($key);
my($sub);
while ($val =~ /(\${.+?})/)
{
$var = $1;
$key = ($var =~ /\${(.+)}/)[0];
$sub = $cfgraw{$key};
if (defined($var) && defined($sub))
{
$var = '\${' . $key . '}';
$val =~ s/$var/$sub/g;
}
}
($_, $val);
} keys(%cfgraw);
}
else
{
if (defined($self->{_log}))
{
$self->{_log}->Write("Unable to open configuration file '$conf'.");
}
else
{
print STDERR "Unable to open configuration file '$conf'.\n";
}
$rv = 1;
}
if ($rv == 0)
{
# everything is AOK - copy %cfg to referenced hash array
my($hkey);
my($hval);
foreach $hkey (keys(%cfg))
{
$hval = $cfg{$hkey};
$hashref->{$hkey} = $hval;
}
}
return $rv;
}
package Gate;
use strict;
use warnings;
use constant kStateFNextUpdate => "nextupdate";
use constant kStateFUpdateDelta => "updatedelta";
use constant kStateFType => "type";
use constant kStateFActionTask => "actiontask";
use constant kStateFGateStatus => "gatestatus";
use constant kStateFProduct => "product";
use constant kStateFStatusTask => "statustask";
use constant kStateFLow => "low";
use constant kStateFHigh => "high";
sub new
{
my($clname, $gatesdir, $gatesubdir, $log) = @_;
my($self) =
{
_name => undef,
_gatedir => undef,
_log => undef,
_nextupdate => undef,
_updatedelta => undef,
_type => undef,
_actiontask => undef,
_gatestatus => undef,
_product => undef,
_statustask => undef,
_low => undef,
_high => undef,
_orig => undef, # The original state that existed when the object was created.
_tickets => undef
};
my($fh);
bless($self, $clname);
if (defined($log))
{
$self->{_log} = $log;
}
if (defined($gatedir) && -d $gatedir)
{
# Gate Identification
$self->{_name} = $gatesubdir;
$self->{_gatedir} = "$gatesdir/$gatesubdir";
# Read the contents of all state files.
$self->LoadState();
# There are additional state files, but they are not accessed by the Gatekeeper (coverage_args, gate_name, key, project, sequence_number).
}
else
{
$self->WriteLog("Invalid gate directory $gatedir.");
$self = undef;
}
return $self;
}
sub DESTROY
{
my($self) = @_;
my(@tickets);
# Must flush out to state files, if attributes have changed.
if ($self->{_nextupdate} ne $self->{_orig}->{_nextupdate})
{
# NextUpdate is a time string.
$self->WriteContent(&kStateFNextUpdate, $self->{_nextupdate});
}
if ($self->{_updatedelta} != $self->{_orig}->{_updatedelta})
{
# UpdateDelta is a number of seconds.
$self->WriteContent(&kStateFUpdateDelta, $self->{_updatedelta});
}
if ($self->{_type} ne $self->{_orig}->{_type})
{
# Type is a string (the value could be numeric, but it could also be a string).
$self->WriteContent(&kStateFType, $self->{_type});
}
if ($self->{_actiontask} ne $self->{_orig}->{_actiontask})
{
# ActionTask is the name of a script/program, which is a string.
$self->WriteContent(&kStateFActionTask, $self->{_actiontask});
}
if ($self->{_gatestatus} ne $self->{_orig}->{_gatestatus})
{
# GateStatus is a string.
$self->WriteContent(&kStateFGateStatus, $self->{_gatestatus});
}
if ($self->{_product} ne $self->{_orig}->{_product})
{
# Product is a DRMS data series, which is a string.
$self->WriteContent(&kStateFProduct, $self->{_product});
}
if ($self->{_statustask} ne $self->{_orig}->{_statustask})
{
# StatusTask is the name of a script/program, which is a string.
$self->WriteContent(&kStateFStatusTask, $self->{_statustask});
}
# Low/High - In general, these state files are updated by the status scripts. But the Gatekeeper
# itself will set these values on ocassion. The Gatekeeper and the status scripts will read
# both of these too. So, we better make sure that there is no possibility that the Gatekeeper
# does ANYTHING while the status scripts are running. Otherwise, there could be a race condition.
if ($self->{_low} ne $self->{_orig}->{_low})
{
# StatusTask is the name of a script/program, which is a string.
$self->WriteContent(&kStateFLow, $self->{_low});
}
if ($self->{_high} ne $self->{_orig}->{_high})
{
# StatusTask is the name of a script/program, which is a string.
$self->WriteContent(&kStateFHigh, $self->{_high});
}
# Flush tickets back to disk.
@tickets = keys(%{$self->{_tickets}});
foreach my $ticketname (@tickets)
{
$ticketO = $self->{_tickets}->{$ticketname};
$ticket->Flush();
}
$self->{_tickets} = undef;
if (defined($self->{_log}))
{
$self->{_log} = undef;
}
}
# Class methods
# Member methods
sub LoadState
{
my($self, $attrib) = @_;
if (defined($attrib) && length($attrib) > 0)
{
$self->{"_" . $attrib} = self->ReadContent($attrib);
# Save the original state so we can check for changes when flushing to disk.
$self->{_orig}->{"_" . $attrib} = $self->{"_" . $attrib};
}
else
{
$self->{_nextupdate} = $self->ReadContent(&kStateFNextUpdate);
$self->{_updatedelta} = $self->ReadContent(&kStateFUpdateDelta);
$self->{_type} = $self->ReadContent(&kStateFType);
$self->{_actiontask} = $self->ReadContent(&kStateFActionTask);
$self->{_gatestatus} = $self->ReadContent(&kStateFGateStatus);
$self->{_product} = $self->ReadContent(&kStateFProduct);
$self->{_statustask} = $self->ReadContent(&kStateFStatusTask);
$self->{_low} = $self->ReadContent(&kStateFLow);
$self->{_high} = $self->ReadContent(&kStateFHigh);
# Save the original state so we can check for changes when flushing to disk.
$self->{_orig} = {};
$self->{_orig}->{_nextupdate} = $self->{_nextupdate};
$self->{_orig}->{_updatedelta} = $self->{_updatedelta};
$self->{_orig}->{_type} = $self->{_type};
$self->{_orig}->{_actiontask} = $self->{_actiontask};
$self->{_orig}->{_gatestatus} = $self->{_gatestatus};
$self->{_orig}->{_product} = $self->{_product};
$self->{_orig}->{_statustask} = $self->{_statustask};
$self->{_orig}->{_low} = $self->{_low};
$self->{_orig}->{_high} = $self->{_high};
}
}
sub Reload
{
my($self, $attrib) = @_;
$self->LoadState($attrib);
}
sub WriteLog
{
my($self, $msg) = @_;
if (defined($self->{_log}))
{
$self->{_log}->Write($msg);
}
else
{
print STDERR "$msg\n";
}
}
sub ReadContent
{
my($self, $statefID) = @_;
my($sfile) = $self->{_gatedir} . "/" . $statefID;
my($fh);
my($content);
if (defined(open($fh, "<$sfile")))
{
$content = <$fh>;
$fh->close();
}
else
{
$self->WriteLog("Unable to open state file $sfile for reading.");
}
return $content;
}
sub WriteContent
{
my($self, $statefID, $content) = @_;
my($sfile) = $self->{_gatedir} . "/" . $statefID;
my($fh);
if (defined(open($fh, ">$sfile")))
{
print $fh $content;
$fh->close();
}
else
{
$self->WriteLog("Unable to open state file $sfile for writing.");
}
}
sub OnHold
{
my($self) = @_;
return ($self->{_gatestatus} =~ /^\s*hold\s*$/i);
}
sub Name
{
my($self) = @_;
return $self->{_name};
}
sub Get
{
my($self, $attrib) = @_;
return $self->{"_$attrib"};
}
sub Set
{
my($self, $attrib, $val) = @_;
if (exists($self->{"_$attrib"}))
{
$self->{"_$attrib"} = $val;
}
else
{
$self->WriteLog("Unknown gate attribute $attrib.");
}
}
sub ProcessTickets
{
my($self) = @_;
my($ticketO);
# Load ticket state. First I have to figure out what state is needed by the Gatekeeper
# Create the ticket objects
foreach my $ticketname (@tickets)
{
$ticketO = new Ticket();
$task = $ticketO->DoTask();
if (!defined($task))
{
# Fatal error.
}
$self->{_tickets} = $ticketO;
}
}
package JSOCTime;
use strict;
use warnings;
use DateTime qw(compare);
use DateTime::Format::Strptime;
# Static funcctions
sub Compare
{
my($t1) = shift;
my($t2) = shift;
return DateTime->compare($t1, $t2);
}
# Non-static member functions
sub new
{
my($clname) = shift;
my($str) = shift;
my($pattern) = shift;
my($self) =
{
_dt => undef # a DateTime object
};
bless($self, $clname);
my($strp) = new DateTime::Format::Strptime(pattern => '$pattern', locale => 'en_US', time_zone => 'UTC');
if (defined($strp))
{
my($dt) = $strp->parse_datetime($str);
if (defined($dt))
{
$self->{_dt} = $dt;
}
else
{
$self = undef;
}
}
else
{
$self = undef;
}
return $self;
}
sub Copy
{
my($self) = @_;
my($classname);
my($new) =
{
_dt => DateTime->from_object(object => $self->{_dt});
};
$classname = ref($self);
bless($new, $classname);
return $new;
}
sub Cmp
{
my($self, $other) = @_;
return DateTime->compare($self->{_dt}, $other->{_dt});
}
sub GT
{
my($self, $other) = @_;
my($cmp) = $self->Cmp($other);
return ($cmp == 1);
}
sub Add
{
my($self, $seconds) = @_;
$self->{_dt}->add(DateTime::Duration->new(seconds => $seconds));
}
# Returns the time-string representation of the member DateTime object.
sub TimeString
{
my($self) = @_;
return $self->{_dt}->strftime("%Y.%m.%d_%H:%M:%S_%Z");
}