IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 2, 2008, 3:03:40 PM (18 years ago)
Author:
bills
Message:

Lots of changes. Allow filelist to be specified on command line instead of
requiring from stdin. Sanity checking. Improvements in --link and
--copy operation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DataStoreServer/scripts/dsreg

    r17751 r17883  
    11#!/usr/bin/env perl
    22#
    3 # Data Store file set registration and de-registration
     3# Data Store file set registration and de-registration program
    44#
    55
     
    1111use Digest::MD5::File qw( file_md5_hex );
    1212use DBI;
     13#use DBD::mysql 4.005;  # needed to set this one day on ipp000
    1314use PS::IPP::Metadata::Config;
    1415use PS::IPP::Metadata::Stats;
     
    2930my $product;
    3031my $fileset;
     32my $filelist;
    3133
    3234my $fstype;
    33 my $ps0;
    34 my $ps1;
    35 my $ps2;
    36 my $ps3;
    37 my $ps4;
    38 my $ps5;
    39 my $ps6;
    40 my $ps7;
     35my ($ps0, $ps1, $ps2, $ps3, $ps4, $ps5, $ps6, $ps7);
    4136
    4237my $linkfiles;      # if set, put links to files in datapath in the Data Store
    4338my $copyfiles;      # if set, copy files from this directory to the Data Store fileset
    4439my $datapath;       # source for files required if $linkfiles or $copyfiles
     40
    4541my $dbname;                     # Database name
    46 
    47 my $ds_dir;
     42my $dsroot;
    4843
    4944my $add;
     
    5247
    5348my $verbosity = 1;
    54 
    55 
    56 
     49my $no_cleanup = 1; # if something goes wrong don't delete copied or linked files for debug
    5750
    5851#
     
    6558        'product=s'     =>      \$product,
    6659        'fileset=s'     =>      \$fileset,
     60        'list=s'        =>      \$filelist,
    6761        'type=s'        =>      \$fstype,
    6862        'datapath=s'    =>      \$datapath,
     
    7872        'ps6=s'         =>      \$ps6,
    7973        'ps7=s'         =>      \$ps7,
    80            'dbname=s'   =>      \$dbname,
     74        'dbname=s'      =>      \$dbname,
     75        'dsroot=s'      =>      \$dsroot,
    8176) or pod2usage(2);
    8277
    8378my $err = "";
    8479
    85 $err .= "product and fileset are required\n" unless defined $product and $fileset;
     80$err .= "--product is required\n" unless defined $product;
     81$err .= "--fileset is required\n" unless defined $fileset;
     82
    8683
    8784# will exit if neither or both -add and -del were specified
     
    9087
    9188if ($add) {
    92     if (!$fstype) {
    93         $err .= "need to specify fileset type to add\n";
    94     }
    95 
     89    $err .= "--type is required\n" unless defined $fstype;
     90    $err .= "--list is required\n" unless defined $filelist;
    9691    if ($linkfiles and $copyfiles) {
    97         $err .= "only one of --link and --copy allowedn";
     92        $err .= "only one of --link and --copy is allowed\n";
    9893    }
    9994    if (($linkfiles or $copyfiles) and !$datapath) {
    10095        $err .= "need to specify datapath with --link or --copy\n";
    10196    }
     97    if ($linkfiles && (substr($datapath, 0, 1) ne "/")) {
     98        $err .= "datapath must begin with / when using --link\n";
     99    }
    102100}
    103101
     
    109107my $ipprc =  PS::IPP::Config->new(); # IPP Configuration
    110108my $siteConfig = $ipprc->{_siteConfig};
    111 if (!$ds_dir) {
    112     $ds_dir = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_DATA_STORE_ROOT');
    113     if (!$ds_dir) {
     109if (!$dsroot) {
     110    $dsroot = metadataLookupStr($ipprc->{_siteConfig}, 'DATA_STORE_ROOT');
     111    if (!$dsroot) {
    114112        die("Data Store root directory not set");
    115113    }
     
    117115
    118116
    119 if (!stat("$ds_dir/index.txt")) {
    120     $err .= "Data Store not found at '$ds_dir'.\n"
     117if (!stat("$dsroot/index.txt")) {
     118    $err .= "Data Store not found at '$dsroot'.\n"
    121119}
    122120
     
    126124
    127125show_usage("Invalid product '$product'.")
    128     unless defined stat("$ds_dir/$product/index.txt");
     126    unless defined stat("$dsroot/$product/index.txt");
    129127
    130128my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER');
     
    138136my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n";
    139137
    140 
    141 
    142 my $fileset_dir = "$ds_dir/$product/$fileset";
     138my $fileset_dir = "$dsroot/$product/$fileset";
    143139my $index_script_name = "$fileset_dir/index.txt";
    144140
     
    154150
    155151    if (!$prod_id) {
    156         die("product $product not found");
     152        die("product $product not found\n");
    157153    }
    158154    $stmt = $dbh->prepare("SELECT fileset_id,fileset_name FROM dsFileset WHERE fileset_name = '$fileset'" .
     
    226222    # fileset_name = $fileset. So later we'll check again that only one fileset with the name exists
    227223
    228     # Read file data from STDIN
    229     # XXX: Perhaps allow this to come from a file
    230 
    231224    my $lineno = 0;
    232225
     226    my $in;
     227    if ($filelist eq "-") {
     228        $in = *STDIN;
     229    } else {
     230        open $in, "<$filelist" or die  "cannot open filelist file $filelist\n";
     231    }
     232
    233233    my @files;
    234     while (<STDIN>) {
     234    while (<$in>) {
    235235        $lineno++;
    236236
     
    242242        my @fields = split /\|/;
    243243        if (@fields < 2) {
    244                 print STDERR "Line $lineno: ignored malformed input line.\n";
    245                 next;
     244            die "malformed input line: $filelist line $lineno: $_\n";
    246245        }
    247246        $filename = shift @fields;
     
    250249        #
    251250        # save any type specific fields provided in the string to be added to the
    252         # VALUES list for this file
     251        #
    253252        my $nfields = @fields;
    254253        my $i;
     
    259258            $ts_string .= ", NULL";
    260259        }
    261 
    262         my $path = "$datapath/$filename";
    263         my @finfo = stat("$path");
    264 
    265         unless (@finfo) {
    266             die ("Line $lineno: referenced file "
    267                 ."($path does not exist.\n");
    268         }
    269 
    270         # Get MD5 sum
    271         my $md5 = file_md5_hex($path);
    272 
    273260        my $hashref = {
    274261            'file'      => $filename,
    275             'bytes'     => $finfo[7],
    276             'md5sum'    => $md5,
    277262            'type'      => $filetype,
    278             'ts_string'  => $ts_string
     263            'ts_string' => $ts_string
    279264        };
    280265
     
    282267    }
    283268
    284     if (@files == 0) {
    285         die("empty filelist");
    286     }
    287 
    288     #
     269    die("empty filelist\n") if (@files == 0);
     270
    289271    # Prepare the fileset directory
    290272    if ($linkfiles or $copyfiles) {
    291             ## create the fileset directory
     273        ## create the fileset directory
    292274        if (! -e $fileset_dir) {
    293275            if (!mkdir $fileset_dir) {
    294                 die("failed trying to make fileset directory $fileset_dir");
     276                die("failed trying to create fileset directory $fileset_dir");
    295277            }
    296278        }
     
    302284
    303285                if ($linkfiles) {
    304                     if (!  symlink $src, $dest) {
     286                    if (! symlink $src, $dest) {
    305287                        die("failed trying to link $src to $dest");
    306288                    }
     
    315297            print STDERR "error preparing the fileset directory: $@\n";
    316298
    317             if (system "rm -r $fileset_dir") {
     299            if (!$no_cleanup && system "rm -r $fileset_dir") {
    318300                die("failed to remove $fileset_dir");
    319301            }
     
    323305    }
    324306
     307    # now calculate the md5sum and file size
     308    foreach my $file (@files) {
     309
     310        my $path = "$fileset_dir/$file->{file}";
     311        my @finfo = stat("$path");
     312
     313        unless (@finfo) {
     314            die ("Line $lineno: referenced file "
     315                ."($path does not exist.\n");
     316        }
     317        $file->{bytes}  = $finfo[7];
     318
     319        # Get MD5 sum
     320        $file->{md5sum} = file_md5_hex($path);
     321    }
     322
     323
     324    # TODO: why do I do this before the database insert is complete?
    325325    # create the cgi script index.txt by making a copy of its parent's
    326     if (!copy("$ds_dir/$product/index.txt", $index_script_name)) {
     326    if (!copy("$dsroot/$product/index.txt", $index_script_name)) {
    327327        cleanup();
    328328        die("failed to copy index script to file set");
     
    331331    eval {
    332332        $dbh->{RaiseError} = 1; # raise exception if error occurs
    333         $dbh->{PrintError} = 0;
     333        $dbh->{PrintError} = 1;
    334334        $dbh->{AutoCommit} = 0;
    335335
     
    360360            # note: ts_string has a leading comma
    361361            my $query = "INSERT INTO dsFile" .
    362 
    363362                    " (fileset_id, file_id, file_name, bytes, md5sum, type, " .
    364363                    " type_col_0, type_col_1, type_col_2, type_col_3)" .
     
    376375
    377376        $count = $dbh->do("UPDATE dsProduct SET last_update = UTC_TIMESTAMP(), last_fs = \'$fileset\'"
    378             . " WHERE prod_id = $prod_id");
     377                                . " WHERE prod_id = $prod_id");
    379378        if ($count == 0E0) {
    380379            $dbh->rollback();
     
    404403    pod2usage(
    405404        -msg =>
    406 "usage: $tmp[$#tmp] [--add|--del] --product prod_name --fileset fs_name --type fs_type [options]
     405"usage: $tmp[$#tmp] [--add|--del] --product prod_name --fileset fs_name --type fs_type --list filelist [options]
    407406
    408407Commands:
    409408
    410     --add        Add a new fileset with the given metadata options.
    411                 File information will be read per-line from STDIN, in the form:
     409    --add       Add a new fileset with the given metadata options.
     410                File information will be read per-line from the file specifed, in the form:
    412411
    413412                    fileID|type|ts0|ts1|ts2|ts3
     
    417416
    418417    --del        Remove a fileset.
     418
     419    --list      file containing list of files (use - for STDIN)
    419420
    420421Options:
     
    447448
    448449sub cleanup {
    449 
    450     if ($linkfiles or $copyfiles) {
     450    if (!$no_cleanup && ($linkfiles or $copyfiles)) {
    451451        if (system "rm -r $fileset_dir") {
    452452            print STDERR "failed to remove $fileset_dir";
Note: See TracChangeset for help on using the changeset viewer.