/usr/share/perl5/Globus/GRAM/JobManager.pm is in globus-gram-job-manager-scripts 6.7-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 | #
# Globus::GRAM::JobManager
#
# CVS Information:
# $Source$
# $Date$
# $Revision$
# $Author$
#
use Globus::GRAM::Error;
use Globus::GRAM::JobState;
use Globus::GRAM::JobSignal;
use Globus::Core::Paths;
use Globus::GRAM::JobDescription;
use POSIX;
use Errno;
use File::Path;
use File::Copy;
use strict;
package Globus::GRAM::JobManager;
local %ENV;
$ENV{PATH} = "$ENV{PATH}:${Globus::Core::Paths::bindir}";
my $cache_pgm = "globus-gass-cache-util";
my $url_copy_pgm = "globus-url-copy";
my $info_pgm = "grid-proxy-info";
=head1 NAME
Globus::GRAM::JobManager - Base class for all Job Manager scripts
=head1 SYNOPSIS
$manager = new Globus::GRAM::JobManager($job_description);
$manager->log("Starting new operation");
$manager->nfssync($fileobj,$createflag);
$manager->respond($hashref);
$hashref = $manager->submit();
$hashref = $manager->poll();
$hashref = $manager->cancel();
$hashref = $manager->signal();
$hashref = $manager->make_scratchdir();
$hashref = $manager->remove_scratchdir();
$hashref = $manager->rewrite_urls();
$hashref = $manager->stage_in();
$hashref = $manager->stage_out();
$hashref = $manager->cache_cleanup();
$hashref = $manager->remote_io_file_create();
$hashref = $manager->proxy_relocate();
$hashref = $manager->proxy_update();
$scalar = $manager->pipe_out_cmd(@arglist);
($stderr, $rc) = $manager->pipe_err_cmd(@arglist);
$status = $manager->fork_and_exec_cmd(@arglist);
$manager->append_path($hash, $variable, $path);
$scalar = $manager->setup_softenv();
=head1 DESCRIPTION
The Globus::GRAM::JobManager module implements the base behavior
for a Job Manager script interface. Scheduler-specific job manager
scripts must inherit from this module in order to be used by the job
manager.
=head2 Methods
=over 4
=item $manager = Globus::GRAM::JobManager->new($JobDescription)
Each Globus::GRAM::JobManager object is created by calling the constructor
with a single argument, a Globus::GRAM::JobDescription object containing
the information about the job request which the script will be modifying.
Modules which subclass Globus::GRAM::JobManager MUST call the super-class's
constructor, as in this code fragment:
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = $class->SUPER::new(@_);
bless $self, $class;
=cut
sub new
{
my $class = shift;
my $self = {};
my $description = shift;
my $save;
my $savedest;
$self->{JobDescription} = $description;
bless $self, $class;
eval { File::Path::mkpath($self->job_dir(), 0, 0700); };
if ($@) {
$self->log("Couldn't create job dir");
}
$save = $description->save_job_description();
$self->log("Checking to see if we'll save the job description: $save");
if ($save eq 'yes')
{
$savedest = "$ENV{HOME}/gram_".$description->uniq_id().".pl";
$self->log("Saving job description to $savedest");
$description->save($savedest);
}
$self->rewrite_urls();
return $self;
}
sub getenv
{
my $self = shift;
my $varname = shift;
my $description = $self->{JobDescription};
my @result;
my @environment = $description->environment();
@result = grep { (ref($_) eq 'ARRAY') && ($_->[0] eq $varname) } @environment;
if (exists($result[0]))
{
return $result[0]->[1];
}
return undef;
}
=item $manager->log($string)
Log a message to the job manager log file. The message is preceded by
a timestamp.
=cut
sub log
{
my $self = shift;
my $msg = join("", @_);
$msg =~ s/\\/\\\\/g;
$msg =~ s/\n/\\n/g;
$msg =~ s/\"/\\\"/g;
$self->respond({LOG => "msg=\"$msg\""});
return;
}
=item $manager->nfssync($object,$create)
Send an NFS update by touching the file (or directory) in question. If the
$create is true, a file will be created. If it is false, the $object will
not be created.
=cut
sub nfssync
{
my $self = shift;
my $object = shift;
my $create_p = shift;
my $now = time();
unless ( utime( $now, $now, $object ) )
{
$self->log( "NFS sync for $object failed (may be harmless): $!" );
# object did not exist
if ( $create_p )
{
local(*TEMP);
if ( open( TEMP, ">$object" ) ) {
close(TEMP);
$self->log( "NFS sync created $object" );
utime($now, $now, $object) ||
$self->log( "NFS sync still unable to access $object" );
} else {
$self->log( "NFS sync could not create $object: $!" );
}
}
}
$self->log( "Sent NFS sync for $object" );
}
=item $manager->respond($message)
Send a response to the job manager program. The response may either be
a hash reference consisting of a hash of (variable, value) pairs, which will
be returned to the job manager, or an already formatted string.
This only needs to be directly called by a job manager implementation
when the script wants to send a partial response while processing one of
the scheduler interface methods (for example,
to indicate that a file has been staged).
The valid keys for a response are defined in the RESPONSES section.
=cut
sub respond
{
my $self = shift;
my $result = shift;
my $var;
if(!ref($result))
{
print $result;
}
else
{
foreach (keys %{$result})
{
$var = uc($_);
print "GRAM_SCRIPT_$var:" . $result->{$_} . "\n";
}
}
}
=item $manager->submit()
Submit a job request to the scheduler. The default implementation returns
with the Globus::GRAM::Error::UNIMPLEMENTED error. Scheduler specific
subclasses should reimplement this method to submit the job to the
scheduler.
A scheduler which implements this method should return a hash reference
containing a scheduler-specific job identifier as the value of the hash's
JOB_ID key, and optionally, the a GRAM job state as the value of the hash's
JOB_STATE key if the job submission was successful;
otherwise a Globus::GRAM::Error value should be returned.
The job state values are defined in the Globus::GRAM::JobState module. The
job parameters (as found in the job rsl) are defined in
Globus::GRAM::Jobdescription object in $self->{JobDescription}.
For example:
return {JOB_STATE => Globus::GRAM::JobState::PENDING,
JOB_ID => $job_id};
=cut
sub submit
{
my $self = shift;
$self->log("Job Manager module does not implement 'submit'\n");
return Globus::GRAM::Error::UNIMPLEMENTED;
}
=item $manager->poll()
Poll a job's status. The default implementation returns
with the Globus::GRAM::Error::UNIMPLEMENTED error. Scheduler specific
subclasses should reimplement this method to poll the
scheduler.
A scheduler which implements this method should return a hash reference
containing the JOB_STATE value. The job's ID can be accessed by calling the
$self->{JobDescription}->jobid() method.
=cut
sub poll
{
my $self = shift;
$self->log("Job Manager module Script does not implement 'poll'\n");
return Globus::GRAM::Error::UNIMPLEMENTED;
}
=item $manager->cancel()
Cancel a job. The default implementation returns
with the Globus::GRAM::Error::UNIMPLEMENTED error. Scheduler specific
subclasses should reimplement this method to remove the job
from the scheduler.
A scheduler which implements this method should return a hash reference
containing the JOB_STATE value. The job's ID can be accessed by calling the
$self->{JobDescription}->jobid() method.
=cut
sub cancel
{
my $self = shift;
$self->log("Job Manager Script does not implement 'cancel'\n");
return Globus::GRAM::Error::UNIMPLEMENTED;
}
=item $manager->signal()
Signal a job. The default implementation returns
with the Globus::GRAM::Error::UNIMPLEMENTED error. Scheduler specific
subclasses should reimplement this method to remove the job
from the scheduler. The JobManager module can determine the job's ID,
the signal number, and the (optional) signal arguments from the
Job Description by calling it's job_id(), signal(), and and signal_arg()
methods, respectively.
Depending on the signal, it may be appropriate for the JobManager object
to return a hash reference containing a JOB_STATE update.
=cut
sub signal
{
my $self = shift;
$self->log("Job Manager Script does not implement 'signal'\n");
return Globus::GRAM::Error::UNIMPLEMENTED;
}
=item $manager->make_scratchdir()
Create a scratch directory for a job. The scratch directory location
is based on the JobDescription's scratch_dir_base() and scratch_dir() methods.
If the scratch_dir() value is a relative path, then a directory will be
created as a subdirectory of scratch_dir_base()/scratch_dir(), otherwise,
it will be created as a subdirectory of scratch_dir(). This method will
return a hash reference containing mapping SCRATCH_DIR to the absolute
path of newly created scratch directory if successful.
=cut
sub make_scratchdir
{
my $self = shift;
my $description = $self->{JobDescription};
my $created = 0;
my $tmpnam;
my $dirname;
my $scratch_prefix;
my $scratch_suffix;
my @acceptable=split(//, "abcdefghijklmnopqrstuvwxyz".
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
"0123456789");
srand();
$self->log(
"Entering Job Manager default implementation of make_scratchdir");
$scratch_prefix = $description->scratch_dir_base();
$scratch_suffix = $description->scratch_dir();
if($scratch_suffix =~ m,^/,,)
{
$scratch_prefix = $scratch_suffix;
}
elsif ($scratch_suffix !~ m,/$,,)
{
$scratch_prefix .= "/$scratch_suffix";
}
else
{
$scratch_prefix .= $scratch_suffix;
}
if(! -w $scratch_prefix)
{
return Globus::GRAM::Error::INVALID_SCRATCH;
}
my $Loops = 0;
while( (!$created) && ($Loops++ < 100) )
{
# Files with names comprised of Ascii values 48-122 should be
# relatively easy to remove from the shell if things go bad.
my $tmpname = 'gram_scratch_' .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable] .
$acceptable[rand() * $#acceptable];
$dirname = "$scratch_prefix/$tmpname";
$self->log(
"Trying to create directory named $dirname");
$created = mkdir($dirname, 0700);
if($created)
{
$self->nfssync( $dirname, 0 );
$self->log("I think it was made.... verifying");
if (-l $dirname || ! -d $dirname || ! -o $dirname)
{
$self->log("nope, somebody's messing with us.");
$created = 0;
}
}
elsif( $!{EEXIST} )
{
$self->log("Already exist; trying again");
}
else
{
last;
}
}
# We give up
if (!$created)
{
return Globus::GRAM::Error::INVALID_SCRATCH;
}
$self->log("Using $dirname as the scratch directory for this job.");
return {SCRATCH_DIR => $dirname};
}
=item $manager->make_scratchdir()
Delete a job's scratch directory. All files and subdirectories of the
JobDescription's scratch_directory() will be deleted.
=cut
sub remove_scratchdir
{
my $self = shift;
my $description = $self->{JobDescription};
my $scratch_directory;
my $count;
$scratch_directory = $description->scratch_directory();
$self->log(
"Entering Job Manager default implementation of remove_scratchdir");
if (! defined $scratch_directory )
{
$self->log("Scratch directory not defined");
return {};
}
$self->log("Removing $scratch_directory");
chdir("/");
$count = File::Path::rmtree($scratch_directory);
$self->log("Removed $count files");
return {};
}
=item $manager->make_scratchdir()
Delete some job-related files. All files listed in the JobDescription's
file_cleanup() array will be deleted.
=cut
sub file_cleanup
{
my $self = shift;
my $description = $self->{JobDescription};
my $count;
$self->log(
"Entering Job Manager default implementation of file_cleanup");
foreach ($description->file_cleanup())
{
if(!defined($_))
{
next;
}
if(ref($_))
{
return Globus::GRAM::Error::RSL_FILE_CLEANUP();
}
$self->log("Removing $_");
unlink($_);
}
return {};
}
=item $manager->rewrite_urls()
Looks up URLs listed in the JobDescription's stdin() and executable(), and
replaces them with paths to locally cached copies.
=cut
sub rewrite_urls
{
my $self = shift;
my $description = $self->{JobDescription};
my $tag = $description->cache_tag() || $ENV{'GLOBUS_GRAM_JOB_CONTACT'};
my $cache_location = $self->getenv('GLOBUS_GASS_CACHE_DEFAULT');
my $url;
my $filename;
my $filestreamout = [];
local %ENV = %ENV;
$ENV{GLOBUS_GASS_CACHE_DEFAULT} = $cache_location if ($cache_location);
foreach ('stdin', 'executable')
{
chomp($url = $description->$_());
if($url =~ m|^[a-zA-Z]+://|)
{
my @arg = ($cache_pgm, '-query', '-t', $tag, $url);
$filename = $self->pipe_out_cmd(@arg);
if($filename ne '')
{
$description->add($_, $filename);
}
}
}
foreach my $which ('stdout', 'stderr')
{
my @destinations = $description->$which();
my $first_destination = $destinations[0];
my $first_tag = undef;
my $cached_destination = $self->job_dir() . "/$which";
if (ref($first_destination))
{
if (scalar(@{$first_destination}) == 2)
{
$first_tag = $first_destination->[1];
$first_destination = $first_destination->[0];
$self->log("destination = $first_destination, tag is $first_tag");
}
else
{
$first_destination = $first_destination->[0];
$self->log("destination = $first_destination, tag is not present");
}
}
elsif (scalar(@destinations) == 2)
{
$first_tag = $destinations[1];
$self->log("destination is $first_destination, tag is $first_tag");
@destinations = ($first_destination);
}
if (scalar(@destinations) == 1 && $first_destination !~ m|://|)
{
$description->add($which, $first_destination);
next;
}
elsif (scalar(@destinations) == 1 &&
$first_destination =~ m|x-gass-cache://|)
{
my @arg = ($cache_pgm, '-add', '-t', $tag, '-n',
$first_destination, 'file:///dev/null');
if (!defined($first_tag))
{
$self->log("$which goes to $first_destination in cache");
$self->log("command is " . join(" ", @arg));
$filename = $self->pipe_out_cmd(@arg);
if ($! != 0)
{
$self->log("Error adding to cache $!");
}
$first_tag = $tag;
}
@arg = ($cache_pgm, '-query', '-t', $first_tag, $first_destination);
$filename = $self->pipe_out_cmd(@arg);
if($filename ne '')
{
$self->log("$first_destination in cache resolves to $filename");
$description->add($which, $filename);
}
else
{
$self->log("$first_destination in cache did not resolve!");
}
}
else
{
$description->add($which, $cached_destination);
}
}
return {};
}
=item $manager->stage_in()
Stage input files need for the job from remote storage. The files to
be staged are defined by the array of [URL, path] pairs in
the job description's file_stage_in() and file_stage_in_shared() methods.
The Globus::GRAM::JobManager module provides an implementation of this
functionality using the globus-url-copy and globus-gass-cache programs.
Files which are staged in are not automatically removed when the job
terminates.
This function returns intermediate responses using the
Globus::GRAM::JobManager::response() method to let the job manager know when
each individual file has been staged.
=cut
sub stage_in
{
my $self = shift;
my $description = $self->{JobDescription};
my $tag = $description->cache_tag() || $ENV{'GLOBUS_GRAM_JOB_CONTACT'};
my $cache_location = $self->getenv('GLOBUS_GASS_CACHE_DEFAULT');
my ($remote, $local, $local_resolved, $cached, $stderr, $rc, @arg);
$self->log("stage_in(enter)");
local %ENV = %ENV;
$ENV{GLOBUS_GASS_CACHE_DEFAULT} = $cache_location if ($cache_location);
if($description->executable() =~ m|^[a-zA-Z]+://|)
{
@arg = ($cache_pgm, '-add', '-t', $tag, $description->executable());
($stderr, $rc) = $self->pipe_out_cmd(@arg);
if ($rc != 0) {
$self->log("executable staging failed with $stderr");
$self->respond( {
'GT3_FAILURE_TYPE' => 'executable',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $description->executable()
});
return Globus::GRAM::Error::STAGING_EXECUTABLE;
}
$local = $self->pipe_out_cmd($cache_pgm, '-q', '-t', $tag,
$description->executable());
if ($local eq '') {
$self->respond( {
'GT3_FAILURE_TYPE' => 'executable',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $description->executable()
});
return Globus::GRAM::Error::STAGING_EXECUTABLE;
}
$self->nfssync($local, 0);
}
if($description->stdin() =~ m|^[a-zA-Z]+://|)
{
@arg = ($cache_pgm, '-add', '-t', $tag, $description->stdin());
($stderr, $rc) = $self->pipe_err_cmd(@arg);
if ($rc != 0) {
$self->log("stdin staging failed with $stderr");
$self->respond( {
'GT3_FAILURE_TYPE' => 'stdin',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $description->stdin()
});
return Globus::GRAM::Error::STAGING_STDIN
}
$local = $self->pipe_out_cmd($cache_pgm, '-q', '-t', $tag,
$description->stdin());
if ($local eq '') {
$self->respond( {
'GT3_FAILURE_TYPE' => 'stdin',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $description->stdin()
});
return Globus::GRAM::Error::STAGING_STDIN;
}
$self->nfssync($local, 0);
}
foreach ($description->file_stage_in())
{
next unless defined $_;
($remote, $local) = ($_->[0], $_->[1]);
if($local !~ m|^/|)
{
$local_resolved = $description->directory() . '/' . $local;
}
else
{
$local_resolved = $local;
}
@arg = ($url_copy_pgm, $remote, 'file://' . $local_resolved);
($stderr, $rc) = $self->pipe_err_cmd(@arg);
if($rc != 0) {
$self->log("filestagein staging failed with $stderr");
$self->respond( {
'GT3_FAILURE_TYPE' => 'filestagein',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $remote,
'GT3_FAILURE_DESTINATION' => $local
});
return Globus::GRAM::Error::STAGE_IN_FAILED
}
$self->nfssync($local_resolved, 0);
$self->respond({'STAGED_IN' => "$remote $local"});
}
foreach($description->file_stage_in_shared())
{
next unless defined $_;
($remote, $local) = ($_->[0], $_->[1]);
if($local !~ m|^/|)
{
$local_resolved = $description->directory() . '/' . $local;
}
else
{
$local_resolved = $local;
}
@arg = ($cache_pgm, '-add', '-t', $tag, $remote);
($stderr, $rc) = $self->pipe_err_cmd(@arg);
if($rc != 0) {
$self->log("filestagein staging failed with $stderr");
$self->respond( {
'GT3_FAILURE_TYPE' => 'filestagein',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $remote,
'GT3_FAILURE_DESTINATION' => $local
});
return Globus::GRAM::Error::STAGE_IN_FAILED
}
@arg = ($cache_pgm, '-query', '-t', $tag, $remote);
$cached = $self->pipe_out_cmd(@arg);
return Globus::GRAM::Error::STAGE_IN_FAILED
if($cached eq '');
symlink($cached, $local_resolved);
$self->respond({'STAGED_IN_SHARED' => "$remote $local"});
$self->log( "local=$local" );
$self->log( "local_resolved=$local_resolved" );
$self->nfssync( $local_resolved, 0 );
}
$self->log("stage_in(exit)");
return {};
}
=item $manager->stage_out()
Stage output files generated by this job to remote storage. The files to
be staged are defined by the array of [URL, destination] pairs in
the job description's file_stage_out() method. The Globus::GRAM::JobManager
module provides an implementation of this functionality using the
globus-url-copy program. Files which are staged out are not removed by this
method.
=cut
sub stage_out
{
my $self = shift;
my $description = $self->{JobDescription};
my $url_copy = "$Globus::Core::Paths::bindir/globus-url-copy";
my $tag = $description->cache_tag() || $ENV{'GLOBUS_GRAM_JOB_CONTACT'};
my $cache_location = $self->getenv('GLOBUS_GASS_CACHE_DEFAULT');
my ($local, $remote);
my ($local_path, $remote_path);
my ($stderr, $rc);
my @arg;
$self->log("stage_out(enter)");
local %ENV = %ENV;
$ENV{GLOBUS_GASS_CACHE_DEFAULT} = $cache_location if ($cache_location);
if (exists($self->{STDIO_MERGER}) && ref($self->{STDIO_MERGER}))
{
my $merger = $self->{STDIO_MERGER};
$merger->poll(1);
}
$self->nfssync( $description->stdout(), 0 )
if defined $description->stdout();
$self->nfssync( $description->stderr(), 0 )
if defined $description->stderr();
foreach ($description->file_stream_out())
{
next unless defined $_;
($local, $remote) = ($_->[0], $_->[1]);
$self->log("Staging $local to $remote");
# handle a couple of types of URLs for local files
$local_path = $local;
if($local_path =~ m|^x-gass-cache://|)
{
@arg = ($cache_pgm, '-query', '-t', $tag, $local_path);
$local_path = $self->pipe_out_cmd(@arg);
return Globus::GRAM::Error::STAGE_OUT_FAILED
if($local_path eq '');
}
elsif($local_path =~ m|^file:/|)
{
$local_path =~ s|^file:/+|/|;
}
if($local_path !~ m|^/|)
{
$local_path = $description->directory() . '/' . $local;
}
$self->nfssync($local_path, 0);
$remote_path = $remote;
if ($remote_path =~ m|^x-gass-cache://|)
{
my $msg;
@arg = ($cache_pgm, '-add',
'-n', $remote_path,
'-t', $tag,
'file:///dev/null');
$msg = $self->pipe_out_cmd(@arg);
if ($? != 0)
{
$self->log("Failed creating cache entry for $remote_path $msg");
return Globus::GRAM::Error::STAGE_OUT_FAILED;
}
@arg = ($cache_pgm, '-query', '-t', $tag, $remote_path);
$remote_path = $self->pipe_out_cmd(@arg);
if ($? != 0)
{
$self->log("Query of cache URL path returned $? -- {$remote_path}");
}
return Globus::GRAM::Error::STAGE_OUT_FAILED
if ($remote_path eq '');
$remote_path = "file://$remote_path";
}
@arg = ($url_copy_pgm, 'file://' . $local_path, $remote_path);
$self->log("Staging $local to $remote");
$self->log("Concretely, staging $local_path to $remote_path");
$self->log("with command " . join(" ", @arg));
($stderr, $rc) = $self->pipe_err_cmd(@arg);
if($rc != 0) {
$self->log("filestageout staging failed with $stderr");
$self->respond( {
'GT3_FAILURE_TYPE' => 'filestageout',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $local,
'GT3_FAILURE_DESTINATION' => $remote
});
return Globus::GRAM::Error::STAGE_OUT_FAILED
}
$self->respond({'STAGED_STREAM' => "$local $remote"});
}
foreach ($description->file_stage_out())
{
next unless defined $_;
($local, $remote) = ($_->[0], $_->[1]);
# handle a couple of types of URLs for local files
$local_path = $local;
if($local_path =~ m|^x-gass-cache://|)
{
@arg = ($cache_pgm, '-query', '-t', $tag, $local_path);
$local_path = $self->pipe_out_cmd(@arg);
return Globus::GRAM::Error::STAGE_OUT_FAILED
if($local_path eq '');
}
elsif($local_path =~ m|^file:/|)
{
$local_path =~ s|^file:/+|/|;
}
if($local_path !~ m|^/|)
{
$local_path = $description->directory() . '/' . $local;
}
$self->nfssync($local_path, 0);
@arg = ($url_copy_pgm, 'file://' . $local_path, $remote);
($stderr, $rc) = $self->pipe_err_cmd(@arg);
if($rc != 0) {
$self->log("filestageout staging failed with $stderr");
$self->respond( {
'GT3_FAILURE_TYPE' => 'filestageout',
'GT3_FAILURE_MESSAGE' => $stderr,
'GT3_FAILURE_SOURCE' => $local,
'GT3_FAILURE_DESTINATION' => $remote
});
return Globus::GRAM::Error::STAGE_OUT_FAILED
}
$self->respond({'STAGED_OUT' => "$local $remote"});
}
$self->log("stage_out(exit)");
return {};
}
=item $manager->cache_cleanup()
Clean up cache references in the GASS which match this job's cache tag .
=cut
sub cache_cleanup
{
my $self = shift;
my $description = $self->{JobDescription};
my $cache_location = $self->getenv('GLOBUS_GASS_CACHE_DEFAULT');
my $tag = $description->cache_tag() || $ENV{'GLOBUS_GRAM_JOB_CONTACT'};
my $job_path = $self->job_dir();
my ($stderr, $rc);
$self->log("cache_cleanup(enter)");
local %ENV = %ENV;
$ENV{GLOBUS_GASS_CACHE_DEFAULT} = $cache_location if ($cache_location);
if ( defined $tag )
{
($stderr, $rc) = $self->pipe_err_cmd($cache_pgm,
'-cleanup-tag', '-t', $tag);
}
$self->log("Cleaning files in job dir $job_path");
chdir("/");
my $count = File::Path::rmtree($job_path);
$self->log("Removed $count files from $job_path");
if ($rc != 0) {
$self->log("cache cleanup failed with $stderr");
}
$self->log("cache_cleanup(exit)");
return {};
}
=item $manager->remote_io_file_create()
Create the remote I/O file in the job dir which will contain the
remote_io_url RSL attribute's value.
=cut
sub remote_io_file_create
{
my $self = shift;
my $description = $self->{JobDescription};
my $tag = $description->cache_tag() || $ENV{'GLOBUS_GRAM_JOB_CONTACT'};
my $job_path = $self->job_dir();
my $filename = "$job_path/remote_io_url";
$self->log("remote_io_file_create(enter)");
local(*FH);
open(FH, ">$filename");
print FH $description->remote_io_url . "\n";
close(FH);
$self->nfssync($filename, 0);
$self->log("remote_io_file_create(exit)");
return { REMOTE_IO_FILE => $filename };
}
=item $manager->proxy_relocate()
Relocate the delegated proxy for job execution. Job Managers need to
override the default if they intend to relocate the proxy into some
common file system other than the cache. The job manager program does
not depend on the new location of the proxy. Job Manager modules must
not remove the default proxy.
=cut
sub proxy_relocate
{
my $self = shift;
my $description = $self->{JobDescription};
my $proxy_filename;
my $proxy_data;
my $new_proxy;
$self->log("proxy_relocate(enter)");
$proxy_filename = $self->pipe_out_cmd($info_pgm, '-path');
return Globus::GRAM::Error::OPENING_USER_PROXY
if ( $? != 0 || $proxy_filename eq '' );
return { X509_USER_PROXY => $proxy_filename }
}
=item $hashref = $manager->proxy_update();
=cut
sub proxy_update
{
return {};
}
=item $manager->append_path($ref, $var, $path)
Append $path to the value of $ref->{$var}, dealing with the case where
$ref->{$var} is not yet defined.
=cut
sub append_path
{
my $self = shift;
my $ref = shift;
my $var = shift;
my $path = shift;
if(exists($ref->{$var}))
{
$ref->{$var} .= ":$path";
}
else
{
$ref->{$var} = "$path";
}
}
=item $manager->pipe_out_cmd(@arg)
Create a new process to run the first argument application with the
remaining arguments (which may be empty). No shell metacharacter will
be evaluated, avoiding a shell invocation. Stderr is redirected to
/dev/null and stdout is being captured by the parent process, which
is also the result returned. In list mode, all lines are
returned, in scalar mode, only the first line is being returned. The
line termination character is already cut off. Use this function as
more efficient backticks, if you do not need shell metacharacter
evaluation.
Caution: This function deviates in two manners from regular backticks.
Firstly, it chomps the line terminator from the output. Secondly, it
returns only the first line in scalar context instead of a multiline
concatinated string. As with regular backticks, the result may be
undefined in scalar context, if no result exists.
A child error code with an exit code of 127 indicates that the application
could not be run. The scalar result returned by this function is usually
undef'ed in this case.
=cut
sub pipe_out_cmd
{
my $self = shift;
my @result;
local(*READ);
my $pid = open( READ, "-|" );
return undef unless defined $pid;
if ( $pid )
{
# parent
chomp(@result = <READ>);
close(READ);
} else {
# child
open( STDERR, '>>/dev/null' );
select(STDERR); $|=1;
select(STDOUT); $|=1;
if (! exec { $_[0] } @_ )
{
exit(127);
}
}
wantarray ? @result : $result[0];
}
=item ($stder, $rc) = $manager->pipe_err_cmd(@arg)
Create a new process to run the first argument application with the
remaining arguments (which may be empty). No shell metacharacter will
be evaluated, avoiding a shell invocation.
This method returns a list of two items, the standard error of the program, and
the exit code of the program. If the error code is 127, then the application
could not be run. Standard output is discarded.
=cut
sub pipe_err_cmd
{
my $self = shift;
my $result;
local(*READ);
my $pid = open( READ, "-|" );
return ("Error " . $! . " forking sub-process", -1) unless defined($pid);
if ( $pid )
{
# parent
chomp($result = scalar <READ>);
close(READ);
} else {
# child
open( STDERR, '>&STDOUT');
open( STDOUT, '>>/dev/null' );
select(STDERR); $|=1;
select(STDOUT); $|=1;
if (! exec { $_[0] } @_ )
{
exit(127);
}
}
($result, $?);
}
=item $manager->fork_and_exec_cmd(@arg)
Fork off a child to run the first argument in the list. Remaining arguments
will be passed, but shell interpolation is avoided. Signals SIGINT and
SIGQUIT are ignored in the child process. Stdout is appended to /dev/null,
and stderr is dup2 from stdout. The parent waits for the child to finish,
and returns the value for the CHILD_ERROR variable as result. Use this
function as more efficient system() call, if you can do not need shell
metacharacter evaluation.
Note that the inability to execute the program will result in a status code
of 127.
=cut
sub fork_and_exec_cmd
{
my $self = shift;
my $pid = fork();
return undef unless defined $pid;
if ( $pid == 0 )
{
# child
$SIG{INT} = 'IGNORE';
$SIG{QUIT} = 'IGNORE';
# FIXME: what about blocking SIGCHLD?
open STDOUT, '>>/dev/null';
open STDERR, '>&STDOUT'; # dup2()
exec { $_[0] } @_;
exit 127;
}
# parent
waitpid($pid,0); # FIXME: deal with EINTR and EAGAIN
$?;
}
=item $manager->job_dir()
Return the temporary directory to store job-related files, which have no
need for file caching.
=cut
sub job_dir {
my $self = shift;
my $description = $self->{JobDescription};
my $posix_hostname;
my $job_dir = $description->job_dir();
if ($job_dir ne '') {
$self->log("Using jm supplied job dir: $job_dir");
return $job_dir;
} elsif (exists $ENV{GLOBUS_HOSTNAME}) {
$posix_hostname = $ENV{GLOBUS_HOSTNAME};
} else {
$posix_hostname = (POSIX::uname)[1];
if ($posix_hostname !~ m/\./) {
my $aliases = join(' ',(gethostbyname($posix_hostname))[0,1]);
for my $alias (split(/\s+/, $aliases)) {
if ($alias =~ m/\./) {
$posix_hostname = $alias;
last;
}
}
}
}
$job_dir = $ENV{HOME}."/.globus/job/$posix_hostname/".$description->uniq_id();
$self->log("making my own job dir @ $job_dir");
return $job_dir;
}
=item $manager->setup_softenv()
Either add a line to the specified command script file handle to load the user's
default SoftEnv configuration, or create a custom SoftEnv script and
add commands to the specified command script file handle to load it.
=cut
sub setup_softenv
{
my $self = shift;
my $softenv_script_name = shift;
my $soft_msc = shift;
my $softenv_load = shift;
my $job_script_fh = shift;
my $rc;
my $description = $self->{JobDescription};
my @softenv = $description->softenv();
my $enable_default_software_environment
= $description->enable_default_software_environment();
if ((not @softenv) && (not $enable_default_software_environment))
{
return 0;
}
if ((not @softenv) && $enable_default_software_environment)
{
$self->log("default software environment requested");
#load default software environment
$rc = print $job_script_fh ". $softenv_load\n";
if (!$rc)
{
return "print failed: $job_script_fh: $!";
}
}
else
{
$self->log("custom software environment requested");
local(*SOFTENV);
$rc = open(SOFTENV, '>' . $softenv_script_name);
if (!$rc)
{
return "open failed: $softenv_script_name: $!";
}
foreach my $softenv (@softenv)
{
print SOFTENV $softenv . "\n";
}
close(SOFTENV);
$rc = print $job_script_fh "$soft_msc $softenv_script_name\n"
. ". $softenv_script_name.cache.sh\n"
. "rm $softenv_script_name"
. " $softenv_script_name.cache.sh\n";
if (!$rc)
{
return "print failed: $job_script_fh: $!";
}
}
return 0;
}
# Transforms a string so that shell metacharacters are escaped and
# suitable for use in double quotes
sub shell_escape($)
{
my $self = shift;
my $string = shift;
$string =~ s/\\/\\\\/g;
$string =~ s/\$/\\\$/g;
$string =~ s/"/\\\"/g;
$string =~ s/`/\\\`/g;
return $string;
}
sub job_description_class
{
return 'Globus::GRAM::JobDescription';
}
1;
=back
=head1 RESPONSES
When returning from a job interface method, or when sending an intermediate
response via the I<response>() method, the following hash keys are valid:
=over 4
=item * JOB_STATE
An integer job state value. These are enumerated in the Globus::GRAM::JobState
module.
=item * ERROR
An integer error code. These are enumerated in the Globus::GRAM::Error module.
=item * JOB_ID
A string containing a job identifier, which can be used to poll, cancel, or
signal a job in progress. This response should only be returned by the
I<submit> method.
=item * SCRATCH_DIR
A string containing the path to a newly-created scratch directory. This
response should only be returned by the I<make_scratchdir> method.
=item * STAGED_IN
A string containing the (URL, path) pair for a file which has now been staged
in. This response should only be returned by the I<stage_in> method.
=item * STAGED_IN_SHARED
A string containing the (URL, path) pair for a file which has now been staged
in and symlinked from the cache. This response should only be returned by the
I<stage_in_shared> method.
=item * STAGED_OUT
A string containing the (path, URL) pair for a file which has now been staged
out by the script. This response should only be returned by the
I<stage_out> method.
=back
=cut
# vim: filetype=perl :
|