IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 19, 2014, 4:05:27 PM (12 years ago)
Author:
eugene
Message:

merge changes from trunk

Location:
branches/eam_branches/ps2-tc3-20130727
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ps2-tc3-20130727

  • branches/eam_branches/ps2-tc3-20130727/ippScripts/scripts/sc_remote_exec.pl

    r36680 r37403  
    2424my $ssh        = can_run('ssh')  or (warn "Can't find ssh" and $missing_tools = 1);
    2525my $scp        = can_run('scp')  or (warn "Can't find scp" and $missing_tools = 1);
    26 my $remotetool = can_run('echo') or (warn "Can't find remotetool" and $missing_tools = 1);
     26my $remotetool = can_run('remotetool') or (warn "Can't find remotetool" and $missing_tools = 1);
    2727
    2828if ($missing_tools) {
     
    3131}
    3232
    33 
    3433# Options
    35 my ($remote_id,$path_base,$policy,$poll,$job_id,$dbname,$verbose,$no_update,$camera);
    36 
     34my ($remote_id,$path_base,$policy,$poll,$force,$job_id,$dbname,$verbose,$no_update,$camera);
     35$force = 0;
     36$verbose = 0;
    3737GetOptions(
    3838    'remote_id=s'   => \$remote_id,
     
    4141    'policy=s'      => \$policy,
    4242    'poll'          => \$poll,
     43    'force-poll'    => \$force,
    4344    'camera=s'      => \$camera,
    4445    'dbname=s'      => \$dbname,
     
    5354
    5455my $ipprc = PS::IPP::Config->new( $camera ) or my_die( "Unable to set up", $remote_id);
    55 
    5656my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files                                   
    5757
     
    6161
    6262print "passed authentication challenge.\n";
     63
     64# Phase 1.5: Grab the information about this run;
     65my $rt_cmd = "$remotetool -listrun -remote_id $remote_id ";
     66$rt_cmd   .= " -dbname $dbname " if defined($dbname);
     67my ( $rt_success, $rt_error_code, $rt_full_buf, $rt_stdout_buf, $rt_stderr_buf ) =
     68    run(command => $rt_cmd, verbose => $verbose);
     69unless ($rt_success) {
     70    $rt_error_code = (($rt_error_code >> 8) or $PS_EXIT_PROG_ERROR);
     71    &my_die("UNable to run remotetool to determine remote run status",
     72            $remote_id,$rt_error_code);
     73}
     74my $rrData = $mdcParser->parse(join "", @$rt_stdout_buf) or
     75    &my_die("UNable to run remotetool to determine remote run status",
     76            $remote_id,$rt_error_code);
     77my $rrData2 = parse_md_list($rrData);
     78my $runData = ${ $rrData2 }[0]; # There should be only one
    6379
    6480# These are operations we only need to do on the first call, not on subsequent poll operations.
     
    7288    my $uri_check   = $path_base . ".check";
    7389    my $uri_config  = $path_base . ".config";
     90    my $uri_generate= $path_base . ".generate";
     91    my $uri_return  = $path_base . ".return";
    7492
    7593    my $disk_command = $ipprc->file_resolve($uri_command);
     
    7795    my $disk_check   = $ipprc->file_resolve($uri_check);
    7896    my $disk_config  = $ipprc->file_resolve($uri_config);
     97    my $disk_generate= $ipprc->file_resolve($uri_generate);
     98    my $disk_return  = $ipprc->file_resolve($uri_return);
    7999   
    80100    scp_put($disk_command,uri_local_to_remote($uri_command));
     
    82102    scp_put($disk_check,uri_local_to_remote($uri_check));
    83103    scp_put($disk_config,uri_local_to_remote($uri_config));
     104    scp_put($disk_generate,uri_local_to_remote($uri_generate));
     105    scp_put($disk_return, uri_local_to_remote($uri_return));
    84106   
    85107# Run check command
    86108    my (undef,$remote_check) = uri_convert($uri_check);
    87     my $ssh_check_stdout = ssh_exec_command("${remote_root}/sc_validate_files.pl --check_list $remote_check");
    88    
    89 # Parse stdout for files to transfer.
    90     if ($#{ $ssh_check_stdout } != -1) { # There were files not found.
    91         # Someone was missing.  Use the check and transfer lists to identify the local version, and send it.
    92         # What was missing?
    93         my %missing_files = ();
    94         foreach my $l (split /\n/, (join '', @$ssh_check_stdout)) {
    95             print "Missing!: $l\n";
    96             $missing_files{$l} = 1;
    97         }
    98        
    99         # Do we have that file?
    100         my @check_files = ();
    101         open(CHECK,$disk_check);
    102         while(<CHECK>) {
    103             chomp;
    104             push @check_files, $_;
    105         }
    106         close(CHECK);
    107        
    108         my @trans_files = ();
    109         open(TRANS,$disk_transfer);
    110         while(<TRANS>) {
    111             chomp;
    112             push @trans_files, $_;
    113         }
    114         close(TRANS);
    115        
    116         if ($#trans_files != $#check_files) { &my_die("The transfer file list and check file list are different sizes",
    117                                                       $remote_id,$PS_EXIT_DATA_ERROR); }
    118        
    119         for (my $i = 0; $i <= $#check_files; $i++) {
    120             if (exists($missing_files{$check_files[$i]})) {
    121                 if ($trans_files[$i] ne "REMOTE_ONLY") {
    122                     scp_put($trans_files[$i],"$check_files[$i]");
    123                 }
    124                 else {
    125                     &my_die("There is a file that should be there, but the file isn't there, and I don't have a copy of that file here.  The file is: $check_files[$i]",
    126                         $remote_id,$PS_EXIT_DATA_ERROR); # This seems a bit harsh.
    127                 }
    128             }
    129         }
    130     }
    131 
     109    my (undef,$remote_transfer)= uri_convert($uri_transfer);
     110
     111    my $ssh_check_stdout = ssh_exec_command("${remote_root}/sc_transfer_tool.pl --input $remote_check --fetch $remote_transfer");
     112    # We no longer need to parse this output, as it retrieves files itself.
     113    foreach my $l (split /\n/, (join '', @$ssh_check_stdout)) {
     114        print "$l\n";
     115    }
    132116
    133117# Run real command
     
    147131    my $command = "$remotetool -updaterun -remote_id $remote_id ";
    148132    $command .= " -set_state run ";
    149     $command .= " -set_job_id $job_id ";
     133    $command .= " -job_id $job_id ";
    150134    $command .= " -dbname $dbname " if defined $dbname;
    151135   
    152136    system($command);
    153137   
    154 }
     138} # End of exec phase
    155139
    156140# Check that we have a valid job_id, either from the command line, or from the msub command.
     
    162146# Poll the job status?
    163147my $poll_count = 0;
    164 my $poll_max   = 5;
     148my $poll_max   = 1; # If it isn't finished now, it's unlikely to magically finish in the next five minutes.
    165149my $poll_sleep = 60;
    166150my $poll_response = 0;
    167 while (($poll_count < $poll_max)&&($poll_response != 1)&&($poll_response != -1)) {
     151my $poll_word  = "nothing";
     152
     153if ($force) {
     154    $poll_response = 1;
     155    $poll_word     = "Completed";
     156}
     157
     158while (($poll_count < $poll_max)&&($poll_response != 1)&&($poll_response != -1)&&($force != 1)) {
    168159    if ($verbose) {
    169160        print "Polling code.  Sleeping.  $poll_count of $poll_max $poll_response\n";
     
    172163        sleep($poll_sleep);
    173164    }
    174     $poll_response = poll_job($job_id);
     165    ($poll_response,$poll_word) = poll_job($job_id);
    175166
    176167    if ($poll_response == 1) { # This job has completed
     
    178169    }
    179170    elsif ($poll_response == -1) { # This job has an error
    180         &my_die("The job exited incorrectly.",$remote_id, $PS_EXIT_PROG_ERROR);
     171        warn("The job exited incorrectly: $remote_id $job_id $poll_word.  Attempting to recover what we can.");
     172        $poll_response = 1; # To fool the check later into proceding
    181173    }
    182174    $poll_count++;
     
    185177    print "Stopped Polling code.  $poll_count of $poll_max $poll_response\n";
    186178}
    187 # Retrieve validation summary
    188 my $fault = 0;
    189 
    190 exit(1);
    191 # Read this as a metadata object
    192 my $ssh_exec_stdout = ssh_exec_command("sc_validate_processing.pl ${path_base}.out");
    193 my $retrieveData = $mdcParser->parse(join "", @$ssh_exec_stdout);
    194 my $retrieveData2= parse_md_list($retrieveData);
    195 
    196 foreach my $retrieveEntry (@$retrieveData2) {
    197     my $process_fault = $retrieveEntry->{fault};
    198     my $dbcommand = $retrieveEntry->{dbcommand};
    199     my $filelist  = $retrieveEntry->{filelist};
    200 
    201     scp_get("${filelist}",${filelist});
    202     open(FL,${filelist});
    203     while(<FL>) {
    204         chomp;
    205         my $f = $_;
    206         scp_get("${f}",$f);
    207     }
    208     close(FL);
    209 
     179if ($poll_response != 1) {
     180    &my_die("Job hasn't completed.  Ending polling.", $remote_id, 0, "run", $job_id);
     181}
     182
     183
     184# Initialize the remote side to sync things
     185
     186# Once sync completes, execute all the database commands
     187# Grab the return list
     188my $uri_return  = $path_base . ".return";
     189my ($ipp_return,$remote_return) = uri_convert($uri_return);
     190# We shouldn't need to get this file.  We made that file.
     191#scp_get($remote_return,$ipp_return);
     192
     193# Construct list of dbinfo files we'll need to execute.
     194my @returned_files = ();
     195my @dbinfo_files   = ();
     196my @dbindex        = ();
     197my $iter           = 0;
     198open(RETURN,   "$ipp_return") || &my_die("Couldn't open file? $ipp_return", $remote_id,$PS_EXIT_SYS_ERROR);
     199while (<RETURN>) {
     200    chomp;
     201    push @returned_files, $_;
     202    if ($_ =~ /dbinfo/) {
     203        push @dbinfo_files, $_;
     204        push @dbindex, $iter;
     205    }
     206    $iter++;
     207
     208}
     209close(RETURN);
     210print STDERR "Expect $#dbinfo_files dbinfo files\n";
     211
     212my $first = 0;
     213my $last  = $#dbinfo_files;
     214my $mid   = 0;
     215my $range = 20;  # How wide of a chunk to look through to catch threading drops
     216my $offset = 0;  # Offset to start from.
     217
     218# Feed the return list into the transfer tool
     219# If this fails, we should go to an auth wait point.
     220if ($poll_word eq 'Completed') { 
     221    # We're complete.  All files should exist on disk
     222    my $iter = 0;
     223    my $transfer_success = 1;
     224    do {
     225        $transfer_success = 1;
     226        $iter++;
     227        check_ssh_connection();
     228        my $return_push_output = ssh_exec_command("${remote_root}/sc_transfer_tool.pl --input $remote_return --offset $offset");
     229        my $return_push_output_print = join "\n", @{ $return_push_output };
     230        if ($return_push_output_print =~ /exited with value 255/) { $transfer_success = 0; }
     231        print "$return_push_output_print\n";
     232    } while (($transfer_success != 1) &&  ($iter < 10));  # Try harder;
     233}
     234else {
     235    # Just try and fetch things, because we didn't complete correctly, it seems.
     236    my $return_push_output = ssh_exec_command("${remote_root}/sc_transfer_tool.pl --input $remote_return --offset $offset");
     237    my $return_push_output_print = join "\n", @{ $return_push_output };
     238    print "$return_push_output_print\n";
     239}
     240
     241foreach my $file (@dbinfo_files) {
     242    open(DBF,"$file") || warn "Missing file $file\n";
     243    my $cmd = <DBF>;
     244    close(DBF);
     245    chomp($cmd);
     246#    print STDERR "$file $cmd\n";
     247    $cmd =~ s/-/ -/g; # This is just a safety check for missing space.
    210248    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    211         run(command => $dbcommand, verbose => $verbose);
     249        run(command => $cmd, verbose => $verbose);
    212250    unless ($success) {
     251
     252        # This shouldn't fail, but we also can't suddenly abort if something does fail. 
     253        # Now we're going to drop the component that owns this dbinfo, which should force a retry.
    213254        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    214         &my_die("The database command returned did not execute correctly. $dbcommand $process_fault",
    215                 $remote_id,$error_code);
    216     }
    217 
    218     if ($process_fault != 0) {
    219         $fault = 1;
    220     }
    221 }
    222 
    223 if (($policy ne 'ignore')&&($fault != 0)) {
    224     &my_die("This job exited without all components finishing successfully.  Halting.",
    225             $remote_id,$fault,"halt");
    226 }
    227 else {
    228     print "This job exited without all components finishing successfully. $fault\n" if $fault;
    229     &my_die("This job finished.  Setting remote job to full.",
    230             $remote_id,0,"full");
    231 }
    232 
    233 # Promote to finished.
    234 &my_die(); # Not really, but I don't have the tool interface finished, so these are all just placeholders.
    235 # Quick review:
    236 # new -> pending -> run -> full
    237 # auth
     255        warn("The command that shouldn't fail has failed: $file $cmd $remote_id $error_code");
     256        if ($poll_word ne 'Completed') {  # If the job claims it completed, then we're fighting the network.
     257            drop_component($remote_id,$cmd,$file);
     258        }
     259    }
     260
     261}
     262
     263# We're done, so set the state and exit.
     264&my_die("Finished",$remote_id,0,"full");
     265
    238266
    239267# END PROGRAM
     
    248276        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    249277        &my_die("Authorization check failed.",$remote_id,0,'auth');
    250 
    251 #       my $update_command = "$remotetool -updaterun -remote_id $remote_id ";
    252 #       $update_command .= " -set_state auth ";
    253 #       $update_command .= " -dbname $dbname " if defined $dbname;
    254 #       system($update_command);
    255 #       exit(0);  # This isn't an error.  It's a state we monitor and move to automatically.
    256278    }
    257279}
     
    289311sub ssh_exec_command {
    290312    my $cmd = shift;
    291     $cmd = "$ssh $DMZ_HOST ssh ${SEC_HOST} $cmd";
     313    $cmd = "$ssh  $DMZ_HOST ssh  ${SEC_HOST} $cmd";
     314    print "EXEC: $cmd\n";
    292315    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    293316        run(command => $cmd, verbose => $verbose);
    294317    unless ($success) {
    295318        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    296         &my_die();
     319        # If we failed, see if we failed due to authorization.
     320        check_ssh_connection();
     321        &my_die("Failed to execute command: >>$cmd<<");
    297322    }
    298323    return ($stdout_buf);
     
    305330   
    306331    if ($response =~ /State: Running/) {
    307         return(0);
     332        return(0,"Running");
    308333    }
    309334    elsif ($response =~ /State: Completed/) {
    310         return(1);
     335        return(1,"Completed");
    311336    }
    312337    elsif ($response =~ /State: Idle/) {
    313         return(-2);
     338        return(-2,"Idle");
    314339    }
    315340    else {
    316         return(-1);
     341        my $response_word = $response;
     342        $response_word =~ s/.*State: //;
     343        $response_word =~ s/\n.*//;
     344        return(-1,$response_word);
    317345    }
    318346}
     
    363391    return($local_uri);
    364392}
     393
     394sub drop_component {
     395    my $remote_id = shift;
     396    my $cmd = shift;
     397    my $file = shift;
     398
     399    if ($cmd eq '') {
     400        # Zero byte file.  This dbinfo was never constructed properly.
     401       
     402        # Steal the stage_id from the filename.  This isn't something I'm happy about.
     403        my $stage = $runData->{stage};
     404        my $stage_id = $file;
     405        if ($stage eq 'chip') {
     406            $stage_id =~ s/^.*ch\.//;
     407        }
     408        elsif ($stage eq 'camera') {
     409            $stage_id =~ s/^.*cm\.//;
     410        }
     411        elsif ($stage eq 'warp') {
     412            $stage_id =~ s/^.*wrp\.//;
     413        }
     414        elsif ($stage eq 'stack') {
     415            $stage_id =~ s/^.*stk\.//;
     416        }
     417        $stage_id =~ s/\..*$//;
     418       
     419        # Drop this from the database
     420        my $drop_cmd = "$remotetool -dropcomponent -remote_id $remote_id -stage_id $stage_id ";
     421        $drop_cmd   .= " -dbname $dbname " if defined($dbname);
     422        system($drop_cmd);
     423    }
     424
     425}
     426
    365427sub my_die {
    366428    my $msg = shift;
     
    368430    my $exit_code = shift;
    369431    my $exit_state = shift;
    370 
     432    my $jobid = shift;
    371433    $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
    372434
     
    374436   
    375437    if (defined $id and not $no_update) {
    376         my $command = "$remotetool -updaterun -stage_id $id";
     438        my $command = "$remotetool -updaterun -remote_id $id";
    377439        $command .= " -fault $exit_code " if defined $exit_code;
     440        $command .= " -job_id $job_id " if defined $jobid;
    378441        $command .= " -set_state $exit_state " if defined $exit_state;
    379442        $command .= " -dbname $dbname " if defined $dbname;
     
    384447    exit($exit_code);
    385448}
     449
     450# Quick review:
     451# new -> pending -> run -> full
     452# auth
Note: See TracChangeset for help on using the changeset viewer.