IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38475


Ignore:
Timestamp:
Jun 15, 2015, 2:33:16 PM (11 years ago)
Author:
watersc1
Message:

Working version of p2 code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/stsci_disks/p2_check.pl

    r38281 r38475  
    33use DBI;
    44use Nebulous::Client;
     5use Nebulous::Server::SQL;
     6#use Nebulous::Util;
     7use Nebulous::Key qw( parse_neb_key parse_neb_volume );
     8
     9use SQL::Interp qw( sql_interp );
     10
    511
    612use IPC::Cmd 0.36 qw( can_run run );
     
    2026my $difftool    = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
    2127my $stacktool   = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
     28my $staticskytool = can_run('staticskytool') or (warn "Can't find staticskytool" and $missing_tools = 1);
    2229
    2330if ($missing_tools) {
     
    2633}
    2734
     35my $verbose = 0;
     36my $debug   = 0;
    2837my ($stage,$stage_id,$dbname);
    2938GetOptions(
     
    3443
    3544
     45my $camera = 'GPC1';
     46my $ipprc = PS::IPP::Config->new( $camera ) or my_die("Unable to set_up", $stage, $stage_id);
     47my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     48
     49# Grab a volume to use for this pass.
     50my @volumes = ();
     51for (my $i = 0; $i <= 19; $i++) {
     52    for (my $j = 0; $j < 3; $j++) {
     53        push @volumes, sprintf("stsci%02d.%1d",$i,$j);
     54    }
     55}
     56my $rand_vol = $volumes[rand @volumes];
     57
     58# Set up the logs
     59my $time = time();
     60my $logDest = "neb://any/stsci_shuffle/${stage}/${stage}.${stage_id}.log.${time}";
     61
     62unless ($debug) {
     63    $ipprc->redirect_output($logDest) or &my_die("Could not redirect output to logfile ${logDest}", $stage,$stage_id);
     64}
     65
     66my $manifest_dir = "/data/${rand_vol}/MANIFEST/${stage}";
     67system("mkdir -p ${manifest_dir}");
     68open(MANIFEST,">${manifest_dir}/${stage}.${stage_id}.MANIFEST") || &my_die( "Could not open manifest file", $stage,$stage_id);
     69
     70# Set up the nebulous database connection.
     71my (    $db, $dbhost, $dbpass, $dbuser);
     72my $conffile = "/home/panstarrs/ipp/ippconfig/nebulous.config";
     73if (-e $conffile) {
     74#    printf STDERR "found config\n";
     75    open(IN,$conffile);
     76    while(<IN>) {
     77        chomp;
     78        if ($_ =~ /NEB_DB\s+/) {
     79            $db = (split /\s+/,$_)[2];
     80        }
     81        elsif ($_ =~ /NEB_DBHOST/) {
     82            $dbhost = (split /\s+/,$_)[2];
     83        }
     84        elsif ($_ =~ /NEB_USER/) {
     85            $dbuser = (split /\s+/,$_)[2];
     86        }
     87        elsif ($_ =~ /NEB_PASS/) {
     88            $dbpass = (split /\s+/,$_)[2];
     89        }
     90    }
     91    close(IN);
     92#    print STDERR  "$db $dbhost $dbuser $dbpass\n";
     93}
     94
     95pod2usage( -msg => "Cannot configure nebulous database", -exitval => 2) unless
     96    defined $db && defined $dbhost && defined $dbuser && defined $dbpass;
     97
     98my $dbh = DBI->connect(
     99    "DBI:mysql:database=$db:host=$dbhost",
     100    $dbuser,
     101    $dbpass,
     102    {
     103        RaiseError => 1,
     104        PrintError => 0,
     105        AutoCommit => 0,
     106    },
     107    );
     108my $sql = Nebulous::Server::SQL->new();
     109
    36110my $server = $ENV{'NEB_SERVER'};
    37111my $neb = Nebulous::Client->new(
     
    39113    );
    40114
    41 my $camera = 'GPC1';
    42 my $ipprc = PS::IPP::Config->new( $camera ) or my_die("Unable to set_up", $stage, $stage_id);
    43 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    44 
    45 my $time = time();
    46 my $logDest = "neb://any/stsci_shuffle/${stage}/${stage}.${stage_id}.log.${time}";
    47 my $ipprc = PS::IPP::Config->new( "GPC1" ) or &my_die( "Could not create config object.", $stage,$stage_id);
    48 $ipprc->redirect_output($logDest) or &my_die("Could not redirect output to logfile ${logDest}", $stage,$stage_id);
    49 
    50 my $manifest_file = $ipprc->file_resolve("neb://any/stsci_shuffle/${stage}/${stage}.${stage_id}.MANIFEST",1);
    51 open(MANIFEST,">${manifest_file}") || &my_die( "Could not open manifest file", $stage,$stage_id);
    52115
    53116# Define which things we want to look at, and where and how many of each.
     
    102165$time = time();
    103166my $dtime = $time - $otime;
    104 print "LOG BEGIN $stage $stage_id $time $dtime\n";
     167log_print ("LOG BEGIN $stage $stage_id $rand_vol");
    105168
    106169foreach my $stageEntry ( @{ $stageData } ) {
     
    109172
    110173    my $path_base = $stageEntry->{path_base};
    111    
     174
     175    my %replicates;
     176    my %culls;
     177    my %md5sums_for_keys;
     178    my %canonical_files;
    112179    foreach my $component (keys %components) {
    113180        my $key;
     
    131198        }
    132199
     200        # I hate you, nebulous.
     201        eval {
     202            $key = parse_neb_key($key);
     203            $key = $key->path;
     204        };
     205       
     206
     207
    133208        # Stealing most of neb-stat here
    134209        my %md5sum_uniq;
     
    136211        my @files = map {URI->new($_)->file if $_} @$instances;
    137212        my @stsci_copies = ();
     213        my @stsci_volumes = ();
    138214        my @local_copies = ();
     215        my @local_volumes = ();
    139216        my @existance;
    140217        my @md5sums;
     
    149226                $md5sum_uniq{$md5sums[$i]} = 1;
    150227                if ($volume =~ /stsci/) {
     228                    unless (defined($canonical_files{$key})) {
     229                        $canonical_files{$key} = $files[$i];
     230                    }
    151231                    push @stsci_copies, $files[$i];
     232                    push @stsci_volumes, $volume;
    152233                }
    153234                elsif ($volume =~ /ipp/) {
    154235                    push @local_copies, $files[$i];
     236                    push @local_volumes, $volume;
    155237                }
    156238                else { &my_die("You can't exist somewhere not ipp and not stsci",$stage,$stage_id,$files[$i]); }
     
    162244            &my_die("Key $key has inconsistent md5sum values", $stage,$stage_id);
    163245        }
    164         $md5sum{$key} = (keys %md5sum_uniq)[0];
     246        $md5sums_for_keys{$key} = (keys %md5sum_uniq)[0];
    165247
    166248        # Do the logic at what we found
    167249        if ($#stsci_copies + 1 < $components{$component}{stsci}) {
    168250            # We need to make an stsci copy.
    169             my $vol = choose_random_stsci();
    170             push @{ $replicates{$key} }, $vol;
     251            push @{ $replicates{$key} }, $rand_vol;
    171252        }
    172253        if ($#local_copies + 1 < $components{$component}{local}) {
    173254            my $vol = get_volume($key);
    174             while ($vol =~ /stsci/) {
    175                 $vol = get_volume($key);
    176             }
    177255            push @{ $replicates{$key} }, $vol;
    178256        }
    179257        if ($#stsci_copies + 1 > $components{$component}{stsci}) {
    180             for (my $i = $components{$component}{stsci} + 1; $i <= $#stsci_copies; $i++) {
    181                 push @{ $culls{$key} }, $stsci_copies[$i];
    182             }
    183         }
    184         if ($#local_copies + 1 > $components{$component}{stsci}) {
    185             for (my $i = $components{$component}{local} + 1; $i <= $#local_copies; $i++) {
    186                 push @{ $culls{$key} }, $local_copies[$i];
    187             }
    188         }
    189     }
    190     log_print("SCAN $stage $stage_id $path_base");
    191 
     258            for (my $i = $components{$component}{stsci}; $i <= $#stsci_copies; $i++) {
     259                push @{ $culls{$key} }, $stsci_volumes[$i]; # $stsci_copies[$i];
     260            }
     261        }
     262        if ($#local_copies + 1 > $components{$component}{local}) {
     263            for (my $i = $components{$component}{local}; $i <= $#local_copies; $i++) {
     264                push @{ $culls{$key} }, $local_volumes[$i]; # $local_copies[$i];
     265            }
     266        }
     267
     268        my $copy_check = sprintf("ST: %d %d IPP: %d %d",
     269                                 $#stsci_copies +1,  $components{$component}{stsci},
     270                                 $#local_copies + 1, $components{$component}{local});
     271        log_print("SCAN $stage $stage_id $path_base $component $copy_check");
     272    }
     273
     274
     275    if ($debug) {
     276        foreach my $key (keys %replicates) {
     277            foreach my $vol (@{ $replicates{$key} }) {
     278                log_print("REP: $key to $vol");
     279            }
     280        }
     281        foreach my $key (keys %culls) {
     282            foreach my $vol (@{ $culls{$key} }) {
     283                log_print("CULL: $key from $vol");
     284            }
     285        }
     286        die;
     287    }
     288   
    192289    foreach my $key (keys %replicates) {
    193290        foreach my $vol (@{ $replicates{$key} }) {
    194             $neb->replicate($key,$vol) or &my_die "failed to replicate a copy of $key to $vol";
     291            log_print("REP: $key to $vol");
     292            $neb->replicate($key,$vol) or &my_die( "failed to replicate a copy of $key to $vol");
    195293            if ($@) { die $@; }
    196294        }
     
    203301            my $uris = $neb->find_instances($key,$vol);
    204302            @$uris = map {URI->new($_)->file if $_} @$uris;
    205             $uri = ${ $uris }[0];
    206             my $tmpmd5 = local_md5sum($uri);
    207             if ($tmpmd5 ne $md5sums{$key}) {
    208                 &my_die "Post-replication md5sum does not match for $key on $vol! $tmpmd5 != $md5sums{$key}";
    209             }
    210         }
    211     }
    212     log_print("CHECK $stage $stage_id $path_base");
     303            my $uri = ${ $uris }[0];
     304            my ($tmpmd5,undef) = local_md5sum($uri);
     305            if ($tmpmd5 ne $md5sums_for_keys{$key}) {
     306                &my_die("Post-replication md5sum does not match for $key on $vol! $tmpmd5 != $md5sums_for_keys{$key}");
     307            }
     308            if ($vol =~ /stsci/) {
     309                $canonical_files{$key} = $uri;
     310            }
     311        }
     312
     313    }
     314
     315
     316    foreach my $key (keys %canonical_files) {
     317        print MANIFEST "$key $canonical_files{$key} $md5sums_for_keys{$key}\n";
     318    }
     319
     320    log_print("CHCK $stage $stage_id $path_base");
    213321    foreach my $key (keys %culls) {
    214322        foreach my $vol (@{ $culls{$key} }) {
     323            log_print("CULL $key from $vol");
    215324            $neb->cull($key,$vol);
    216325        }
    217326    }
    218     log_print("CULL $stage $stage_id $path_base");
    219 }
     327    log_print("STOP $stage $stage_id $path_base");
     328}
     329close(MANIFEST);   
    220330   
    221    
    222 
     331
     332sub my_die {
     333    my $message = shift;
     334    carp($message);
     335    my $exit_code = $PS_EXIT_PROG_ERROR;
     336    close(MANIFEST);
     337    unlink("${manifest_dir}/${stage}.${stage_id}.MANIFEST");
     338
     339    exit($exit_code);
     340}
    223341       
    224342sub log_print {
    225343    my $msg = shift;
    226     $otime = $time;
     344
    227345    $time = time();
    228346    $dtime = $time - $otime;
    229     print "LOG $msg $time $dtime\n";
     347    print "LOG $time $dtime $msg\n";
    230348}
    231349
    232350sub get_volume {
    233351    my $key = shift;
    234 
    235 
     352   
    236353    my ($vol_id, $vol_host, $vol_path, $xattr, $forbidden_cabinet);   
    237354    my $max_used_space = 0.98;
     
    278395        $query->finish;
    279396    };
    280 
    281     return($vol_host);
     397    &my_die($@) if $@;
     398
     399    if (defined($vol_host)) {
     400        if ($vol_host eq 'ipp066') {
     401            $vol_host = get_volume($key);
     402            if ($vol_host eq 'ipp066') {
     403                # Really?
     404                $vol_host = get_volume($key);
     405#               if ($vol_host eq 'ipp066') {}  #fine.  Be that way.  Jerk.
     406#                 
     407            }
     408        }
     409    }
     410
     411    return($vol_host . ".0");
    282412}
    283413   
    284 
    285414sub local_md5sum {
    286415    my $filename = shift;
     
    289418    $host =~ s/\.\d//;
    290419#    print "$filename $host $volume\n";
    291     my $response = `ssh $host md5sum $filename`;
     420    my $response = `ssh -x $host md5sum $filename`;
    292421    chomp($response);
    293422    my ($sum, undef) = split /\s+/, $response;
Note: See TracChangeset for help on using the changeset viewer.