Changeset 17883 for trunk/DataStoreServer/scripts/dsreg
- Timestamp:
- Jun 2, 2008, 3:03:40 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/DataStoreServer/scripts/dsreg (modified) (26 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/DataStoreServer/scripts/dsreg
r17751 r17883 1 1 #!/usr/bin/env perl 2 2 # 3 # Data Store file set registration and de-registration 3 # Data Store file set registration and de-registration program 4 4 # 5 5 … … 11 11 use Digest::MD5::File qw( file_md5_hex ); 12 12 use DBI; 13 #use DBD::mysql 4.005; # needed to set this one day on ipp000 13 14 use PS::IPP::Metadata::Config; 14 15 use PS::IPP::Metadata::Stats; … … 29 30 my $product; 30 31 my $fileset; 32 my $filelist; 31 33 32 34 my $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; 35 my ($ps0, $ps1, $ps2, $ps3, $ps4, $ps5, $ps6, $ps7); 41 36 42 37 my $linkfiles; # if set, put links to files in datapath in the Data Store 43 38 my $copyfiles; # if set, copy files from this directory to the Data Store fileset 44 39 my $datapath; # source for files required if $linkfiles or $copyfiles 40 45 41 my $dbname; # Database name 46 47 my $ds_dir; 42 my $dsroot; 48 43 49 44 my $add; … … 52 47 53 48 my $verbosity = 1; 54 55 56 49 my $no_cleanup = 1; # if something goes wrong don't delete copied or linked files for debug 57 50 58 51 # … … 65 58 'product=s' => \$product, 66 59 'fileset=s' => \$fileset, 60 'list=s' => \$filelist, 67 61 'type=s' => \$fstype, 68 62 'datapath=s' => \$datapath, … … 78 72 'ps6=s' => \$ps6, 79 73 'ps7=s' => \$ps7, 80 'dbname=s' => \$dbname, 74 'dbname=s' => \$dbname, 75 'dsroot=s' => \$dsroot, 81 76 ) or pod2usage(2); 82 77 83 78 my $err = ""; 84 79 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 86 83 87 84 # will exit if neither or both -add and -del were specified … … 90 87 91 88 if ($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; 96 91 if ($linkfiles and $copyfiles) { 97 $err .= "only one of --link and --copy allowedn";92 $err .= "only one of --link and --copy is allowed\n"; 98 93 } 99 94 if (($linkfiles or $copyfiles) and !$datapath) { 100 95 $err .= "need to specify datapath with --link or --copy\n"; 101 96 } 97 if ($linkfiles && (substr($datapath, 0, 1) ne "/")) { 98 $err .= "datapath must begin with / when using --link\n"; 99 } 102 100 } 103 101 … … 109 107 my $ipprc = PS::IPP::Config->new(); # IPP Configuration 110 108 my $siteConfig = $ipprc->{_siteConfig}; 111 if (!$ds _dir) {112 $ds _dir = metadataLookupStr($ipprc->{_siteConfig}, 'PSTAMP_DATA_STORE_ROOT');113 if (!$ds _dir) {109 if (!$dsroot) { 110 $dsroot = metadataLookupStr($ipprc->{_siteConfig}, 'DATA_STORE_ROOT'); 111 if (!$dsroot) { 114 112 die("Data Store root directory not set"); 115 113 } … … 117 115 118 116 119 if (!stat("$ds _dir/index.txt")) {120 $err .= "Data Store not found at '$ds _dir'.\n"117 if (!stat("$dsroot/index.txt")) { 118 $err .= "Data Store not found at '$dsroot'.\n" 121 119 } 122 120 … … 126 124 127 125 show_usage("Invalid product '$product'.") 128 unless defined stat("$ds _dir/$product/index.txt");126 unless defined stat("$dsroot/$product/index.txt"); 129 127 130 128 my $dbserver = metadataLookupStr($siteConfig, 'DBSERVER'); … … 138 136 my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n"; 139 137 140 141 142 my $fileset_dir = "$ds_dir/$product/$fileset"; 138 my $fileset_dir = "$dsroot/$product/$fileset"; 143 139 my $index_script_name = "$fileset_dir/index.txt"; 144 140 … … 154 150 155 151 if (!$prod_id) { 156 die("product $product not found ");152 die("product $product not found\n"); 157 153 } 158 154 $stmt = $dbh->prepare("SELECT fileset_id,fileset_name FROM dsFileset WHERE fileset_name = '$fileset'" . … … 226 222 # fileset_name = $fileset. So later we'll check again that only one fileset with the name exists 227 223 228 # Read file data from STDIN229 # XXX: Perhaps allow this to come from a file230 231 224 my $lineno = 0; 232 225 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 233 233 my @files; 234 while (< STDIN>) {234 while (<$in>) { 235 235 $lineno++; 236 236 … … 242 242 my @fields = split /\|/; 243 243 if (@fields < 2) { 244 print STDERR "Line $lineno: ignored malformed input line.\n"; 245 next; 244 die "malformed input line: $filelist line $lineno: $_\n"; 246 245 } 247 246 $filename = shift @fields; … … 250 249 # 251 250 # save any type specific fields provided in the string to be added to the 252 # VALUES list for this file251 # 253 252 my $nfields = @fields; 254 253 my $i; … … 259 258 $ts_string .= ", NULL"; 260 259 } 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 sum271 my $md5 = file_md5_hex($path);272 273 260 my $hashref = { 274 261 'file' => $filename, 275 'bytes' => $finfo[7],276 'md5sum' => $md5,277 262 'type' => $filetype, 278 'ts_string' => $ts_string263 'ts_string' => $ts_string 279 264 }; 280 265 … … 282 267 } 283 268 284 if (@files == 0) { 285 die("empty filelist"); 286 } 287 288 # 269 die("empty filelist\n") if (@files == 0); 270 289 271 # Prepare the fileset directory 290 272 if ($linkfiles or $copyfiles) { 291 ## create the fileset directory273 ## create the fileset directory 292 274 if (! -e $fileset_dir) { 293 275 if (!mkdir $fileset_dir) { 294 die("failed trying to make fileset directory $fileset_dir");276 die("failed trying to create fileset directory $fileset_dir"); 295 277 } 296 278 } … … 302 284 303 285 if ($linkfiles) { 304 if (! symlink $src, $dest) {286 if (! symlink $src, $dest) { 305 287 die("failed trying to link $src to $dest"); 306 288 } … … 315 297 print STDERR "error preparing the fileset directory: $@\n"; 316 298 317 if ( system "rm -r $fileset_dir") {299 if (!$no_cleanup && system "rm -r $fileset_dir") { 318 300 die("failed to remove $fileset_dir"); 319 301 } … … 323 305 } 324 306 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? 325 325 # 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)) { 327 327 cleanup(); 328 328 die("failed to copy index script to file set"); … … 331 331 eval { 332 332 $dbh->{RaiseError} = 1; # raise exception if error occurs 333 $dbh->{PrintError} = 0;333 $dbh->{PrintError} = 1; 334 334 $dbh->{AutoCommit} = 0; 335 335 … … 360 360 # note: ts_string has a leading comma 361 361 my $query = "INSERT INTO dsFile" . 362 363 362 " (fileset_id, file_id, file_name, bytes, md5sum, type, " . 364 363 " type_col_0, type_col_1, type_col_2, type_col_3)" . … … 376 375 377 376 $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"); 379 378 if ($count == 0E0) { 380 379 $dbh->rollback(); … … 404 403 pod2usage( 405 404 -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] 407 406 408 407 Commands: 409 408 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: 412 411 413 412 fileID|type|ts0|ts1|ts2|ts3 … … 417 416 418 417 --del Remove a fileset. 418 419 --list file containing list of files (use - for STDIN) 419 420 420 421 Options: … … 447 448 448 449 sub cleanup { 449 450 if ($linkfiles or $copyfiles) { 450 if (!$no_cleanup && ($linkfiles or $copyfiles)) { 451 451 if (system "rm -r $fileset_dir") { 452 452 print STDERR "failed to remove $fileset_dir";
Note:
See TracChangeset
for help on using the changeset viewer.
