Debian Codegraph @

myproxy-replicate

raw
1 #!/usr/bin/perl
2 use strict;
3 use Getopt::Long;
4 ##use File::Find;
5 use Fcntl ':flock'; # import LOCK_* constants
6 use File::stat;
7 ##use Cwd;
8 ##use Config;
9 use Data::Dumper;
10 use Time::Local;
11 use IPC::Open3;
12 require Pod::Usage;
13
14 my $MYPROXY_DEFAULT_PORT = "7512";
15 my $MYPROXY_DEFAULT_LOCATION = "/var/lib/myproxy";
16 my $MYPROXY_REPLICATE_FILE = "\.myproxy_replicate";
17 my $MYPROXY_DELETED_FILE = "\.myproxy_deleted";
18 my $MYPROXY_PID_FILE = "\.myproxy_pid";
19 my $SECONDS_PER_HOUR = (60 * 60);
20
21 #
22 # Do a perl check for version >= 5.005.
23 #
24 if ( ! ( defined eval "require 5.005" ) )
25 {
26 die "Requires at least Perl version 5.005";
27 }
28
29 my $gpath = $ENV{GLOBUS_LOCATION};
30
31 if (!defined($gpath))
32 {
33 print "GLOBUS_LOCATION not defined in environment. Defaulting to /usr\n";
34 $ENV{GLOBUS_LOCATION} = '/usr';
35 $gpath = $ENV{GLOBUS_LOCATION};
36 }
37
38 # process the -option options
39 my ( $repository, $verbose, $debug, $help, $usage, $config );
40
41
42 GetOptions( 'storage|r=s' => \$repository,
43 'config|c=s' => \$config,
44 'verbose|v' => \$verbose,
45 'debug|d' => \$debug,
46 'usage|u' => \$usage,
47 'help|h' => \$help)
48 or Pod::Usage::pod2usage(0);
49
50 Pod::Usage::pod2usage(0) if $help;
51 Pod::Usage::pod2usage(0) if $usage;
52
53 my $dbglvl = 0;
54
55 $dbglvl += 1 if( $verbose );
56 $dbglvl += 2 if( $debug );
57
58
59 my $globus_dir = $ENV{GLOBUS_LOCATION};
60
61 ##
62 ## Find the MyProxy Repository. If one is not given check the default
63 ## locations.
64 ##
65 if( !defined($repository) )
66 {
67 print "Checking for $MYPROXY_DEFAULT_LOCATION\n" if( $dbglvl > 0 );
68
69 if( !(-d $MYPROXY_DEFAULT_LOCATION) )
70 {
71 print "Checking for $globus_dir$MYPROXY_DEFAULT_LOCATION\n"
72 if( $dbglvl > 0 );
73
74 if( !(-d "$globus_dir$MYPROXY_DEFAULT_LOCATION") )
75 {
76 die "Could not find MyProxy repository in any of the default " .
77 "locations.\nDefault: $MYPROXY_DEFAULT_LOCATION or " .
78 "\$GLOBUS_LOCATION$MYPROXY_DEFAULT_LOCATION.\n";
79 }
80
81 $repository = "$globus_dir$MYPROXY_DEFAULT_LOCATION";
82 }
83 else
84 {
85 $repository = $MYPROXY_DEFAULT_LOCATION;
86 }
87
88 print "Setting repository to $repository\n" if( $dbglvl > 1 );
89 }
90
91 ##
92 ## Check to see if myproxy-store is found and executable
93 ##
94 my $myproxy_store;
95 chomp($myproxy_store = `which myproxy-store 2>/dev/null`);
96 die "myproxy-store not in PATH, stopped" if (!(-x $myproxy_store));
97
98 ##
99 ## Check to see if myproxy-destroy is found and executable
100 ##
101 my $myproxy_destroy;
102 chomp($myproxy_destroy = `which myproxy-destroy 2>/dev/null`);
103 die "myproxy-destroy not in PATH, stopped" if (!(-x $myproxy_destroy));
104
105 ##
106 ## Check for a server configuration file. If one is not given, check the
107 ## default locations.
108 ##
109 if( !defined($config) )
110 {
111 if( !(-e "/etc/myproxy-server.config") )
112 {
113 if( !(-e "$globus_dir/etc/myproxy-server.config") )
114 {
115 die "Could not find MyProxy configuration file in any of the " .
116 "default locations.\nDefault: /etc/myproxy-server.config or " .
117 "\$GLOBUS_LOCATION/etc/myproxy-server.config.\n";
118 }
119
120 $config = "$globus_dir/etc/myproxy-server.config";
121 }
122 else
123 {
124 $config = "/etc/myproxy-server.config";
125 }
126 }
127
128 print "Using server config file: $config\n" if( $dbglvl > 0 );
129
130 my $lst_rep_time;
131
132 my $new_rep_time = undef;
133 my @file_list;
134
135 my $retval = main();
136 exit( $retval );
137
138
139 ###########################################################################
140 ## Do everything
141 ###########################################################################
142
143 sub main
144 {
145
146 ##
147 ## Make sure no other instance of myproxy-replicate can run until
148 ## current one finishes.
149 ##
150 open PID, ">$repository/$MYPROXY_PID_FILE";
151 flock( PID, LOCK_EX );
152 print PID "$$\n";
153
154 my $rep_file = "$repository/$MYPROXY_REPLICATE_FILE";
155
156 ##
157 ## Get the timestamp of the last replication.
158 ##
159 if( -e $rep_file )
160 {
161 $lst_rep_time = get_last_replicate_time( $rep_file );
162 }
163 else
164 {
165 $lst_rep_time = 0;
166 }
167
168 $new_rep_time = timelocal(localtime);
169
170 ##
171 ## Retrieve all of the slave MyProxy servers from config file.
172 ##
173 my $slave_servers = get_slaves( $config );
174
175 print "Slave Servers:\n" . Dumper $slave_servers if( $dbglvl > 1 );
176
177 ##
178 ## Read the repository and find the files that have changed since the
179 ## last replication.
180 ##
181 my $files = read_dir( $repository, "\.creds" );
182 print Dumper $files if( $dbglvl > 1 );
183
184 ##
185 ## send the files to the slave servers.
186 ##
187 my $ret = replicate_files( $repository, $files, $slave_servers );
188
189 if( $ret == 0 )
190 {
191 $ret = delete_files( $repository, $files, $slave_servers );
192 }
193
194 ##
195 ## Check to see if we had a problem with either replicating or
196 ## deleting. If there was a problem don't update .myproxy_replicate
197 ## or .myproxy_delete.
198 ##
199 if( !$ret )
200 {
201 print "Replication complete: ", localtime() . "\n";
202 finish_up( $repository );
203 }
204 else
205 {
206 print STDERR "Replication Failed\n";
207 }
208
209
210 ##sleep( 50 );
211 flock( PID, LOCK_UN );
212 close PID;
213 unlink( "$repository/$MYPROXY_PID_FILE" );
214
215 return( $ret );
216 }
217
218
219 ###########################################################################
220 ## Functions
221 ###########################################################################
222
223 ##
224 ## get_last_replicate_time( file )
225 ##
226 ## Read the replication timestamp.
227 ##
228 sub get_last_replicate_time
229 {
230 my $filename = shift;
231
232 open LSTREP, $filename;
233 $lst_rep_time = <LSTREP>;
234 close LSTREP;
235
236 return $lst_rep_time;
237 }
238
239 ##
240 ## get_slaves( file )
241 ##
242 ## Get the list of slave servers from the configuration file.
243 ##
244 sub get_slaves
245 {
246 my $config = shift;
247
248 my $slist;
249 my $junk;
250 my @slave_list;
251
252 open CFG, $config;
253
254 for (<CFG>)
255 {
256 next if( !($_ =~ /^slave_servers/) );
257
258 ($junk, $slist) = split /slave_servers/, $_;
259 my @slaves = split /;/, $slist;
260
261 for my $s (@slaves)
262 {
263 my ($server, $port) = split /:/, $s;
264
265 chomp($server);
266 chomp($port);
267
268 my $ops = "-s $server ";
269 $ops .= "-p $port " if( length($port) > 0 );
270
271 push @slave_list, $ops;
272 }
273 }
274
275 close CFG;
276
277 return \@slave_list;
278 }
279
280 ##
281 ## read_dir( directory, expression )
282 ##
283 ## Read the MyProxy repository and find files that match expression.
284 ##
285 sub read_dir
286 {
287 my $directory = shift;
288 my $expression = shift;
289
290 my $files;
291
292 my $stuff = undef;
293
294 opendir(DIR, $directory) or die
295 print "ERROR: directory \"$directory\" could not be opened!\n";
296
297 @file_list =
298 map { $_->[0] } # Form a list of names without paths.
299 grep { $_->[0] =~ /$expression/ } # extract the files.
300 map { [ $_, "$directory/$_" ] } # form anonymous array [name, pathname]
301 # because readdir strips the path from the bname
302 grep { ! /^\.\.?$/ } # remove the current directory and its parent
303 readdir(DIR); #read all of the filenames in the directory
304
305 for my $f (@file_list)
306 {
307 my $sb = stat("$directory/$f");
308 my $mod_time = localtime $sb->mtime;
309
310 printf "File is %s, mtime %s\n", $f, $mod_time if( $dbglvl > 1 );
311
312 if( $sb->mtime >= $lst_rep_time )
313 {
314 push @{$files}, $f;
315 }
316 }
317
318 $stuff->{'files'} = $files;
319
320 closedir(DIR);
321
322 $stuff->{'del'} = missing_files( $directory, @file_list );
323
324 return $stuff;
325 }
326
327 ##
328 ## replicate_files( reposityr, files, slaves )
329 ##
330 ## Replicate all of the files listed to all of the slaves listed.
331 ##
332 sub replicate_files
333 {
334 my $rep = shift;
335 my $files = shift;
336 my $slaves = shift;
337
338 my ($exitstatus, $output);
339 my $ret = 0;
340
341 for my $f (@{$files->{'files'}})
342 {
343 my $data = $f;
344 $data =~ s/creds/data/;
345
346 my $options = parse_datafile( $rep, $data );
347
348 print "File not found: $rep/$data\n File $rep/$f not replicated.\n\n"
349 if( !defined($options) );
350 next if( !defined($options) );
351
352 $options .= "-c \"$rep/$f\" -y \"$rep/$f\" ";
353
354 for my $s (@{$slaves})
355 {
356 print "OPTIONS: $myproxy_store\n$options $s\n\n" if( $dbglvl > 2 );
357
358 ($exitstatus, $output) = runcmd( "$myproxy_store $options $s" );
359
360 if( $exitstatus != 0 )
361 {
362 $ret = 1;
363 }
364
365 print STDERR "STATUS: $exitstatus\n $output\n" if( $dbglvl > 0 );
366 }
367 $options = undef;
368 }
369
370 return $ret;
371 }
372
373 ##
374 ## parse_datafile( repository, datafile )
375 ##
376 ## Read the data file and use the information in it to create the option
377 ## list for replication.
378 ##
379 sub parse_datafile
380 {
381 my $rep = shift;
382 my $fname = shift;
383
384 my $options = undef;
385
386 open( FN, "$rep/$fname" ) or return undef;
387
388 for my $value (<FN>)
389 {
390 my( $tag, $val ) = split( /\=/, $value );
391 # OWNER creds->owner_name
392 # where does this come from? It seems like it is something that is passed
393 # but not flaged. If this is true, how do we get it to replicate?
394 if( $value =~ /OWNER=(.*)/ )
395 {
396 }
397 # LIFETIME -t
398 elsif( $value =~ /LIFETIME=(.*)/ )
399 {
400 $options .= "-t " . $1 / $SECONDS_PER_HOUR . " ";
401 }
402 # NAME -k creds->credname
403 elsif( $value =~ /^NAME=(.*)/ )
404 {
405 $options .= "-k \"$1\" ";
406 }
407 # USERNAME -l
408 elsif( $value =~ /^USERNAME=(.*)/ )
409 {
410 $options .= "-l \"$1\" ";
411 }
412 # DESCRIPTION -K creds->creddesc
413 elsif( $value =~ /DESCRIPTION=(.*)/ )
414 {
415 $options .= "-K \"$1\" ";
416 }
417 # RETRIEVERS -r creds->retrievers
418 # what about anonymous retrievers?
419 elsif( $value =~ /RETRIEVERS=(.*)/ )
420 {
421 $options .= "-x -r \"$1\" ";
422 }
423 # RENEWERS -R creds->renewers
424 # what about anonymous renewers?
425 elsif( $value =~ /RENEWERS=(.*)/ )
426 {
427 $options .= "-x -R \"$1\" ";
428 }
429 # KEYRETRIEVERS -E creds->keyretrieve
430 elsif( $value =~ /KEYRETRIEVERS=(.*)/ )
431 {
432 $options .= "-x -E \"$1\" ";
433 }
434 # END_OPTIONS
435 elsif( $tag eq "END_OPTIONS" )
436 {
437 }
438 }
439 close FN;
440
441 return $options;
442 }
443
444 ##
445 ## missing_files( repository, files )
446 ##
447 ## Look at the last snapshot of the repository on the master. Compare it
448 ## to the current list of files in the repository. If any are missing
449 ## from the repository, they must have been deleted, so we need to delete
450 ## them.
451 ##
452 sub missing_files
453 {
454 my $rep = shift;
455 my @files = @_;
456
457 my @delfiles;
458
459 open FD, "$rep/$MYPROXY_DELETED_FILE" or return undef;
460
461 my @fd = <FD>;
462
463 for my $r (@fd)
464 {
465 my $fnd = 0;
466
467 chomp($r);
468
469 for my $f (@files)
470 {
471 chomp($f);
472 $fnd = 1 if( $f eq $r );
473 }
474
475 if( !$fnd )
476 {
477 push @delfiles, $r;
478 }
479 }
480
481 return \@delfiles
482 }
483
484 ##
485 ## delete_files( repostiroy, files, slaves )
486 ##
487 ## Using the list of files that are to be deleted, send a destroy command
488 ## to each of the listed slaves.
489 ##
490 sub delete_files
491 {
492 my $rep = shift;
493 my $files = shift;
494 my $slaves = shift;
495
496 my ($exitstatus, $output);
497
498 my $ret = 0;
499
500 for my $f (@{$files->{'del'}})
501 {
502 my ($name, $ext) = split /\./, $f;
503 my ($uname, $oname ) = split /-/, $name;
504
505 my $options = "-l \"$uname\" ";
506 $options .= "-k \"$oname\" " if( length($oname) > 0 );
507
508 for my $s (@{$slaves})
509 {
510 print "OPTIONS: myproxy_destroy\n$options $s -v\n\n" if( $dbglvl > 2 );
511
512 ($exitstatus, $output) = runcmd( "$myproxy_destroy $options $s -v" );
513
514 if( ($exitstatus != 0) &&
515 !($output =~ /No such file or directory/ ||
516 $output =~ /do not exist/) )
517 {
518 print "Bad delete\n";
519 $ret = 1;
520 # For now do nothing about this. myproxy-server neeeds to be modified
521 # to return more error information. There is no way to tell why this
522 # failed.
523 #
524 # There are several problems with the current retry scheme. If we do
525 # not update the date and directory snap shot. The next time around
526 # we are going to have problems. Resending myproxy-destroy commands
527 # to servers where the cred has already been destroied returns an
528 # error message. This will just cause an infinate cycle of failures.
529 #
530 # If we log the deletes and then rerun them we can run into problems
531 # with missing creds. If we have a case where a cred is stored but
532 # the store fails and then before the next replicate, the user
533 # destories that cred we will have a problem with the destory. The
534 # server will have no idea what we are trying to destroy and return
535 # an error. Again, we end up in an infinate cycle of destories.
536 #
537 # If we log both the stores and destories we still have problems. Is
538 # we do a store and it fails and we log it, then the user destories
539 # the cred before the next replicate. We try and do a store on a
540 # cred that is not there. This causes an error. Then the destory
541 # runs and we end up with another error. Now we have two infinate
542 # cycles going.
543 #
544 # We need a better solution! I still like the idea of just taring
545 # the directory and coping it over to each slave.
546 #
547 }
548 print STDERR "STATUS: $exitstatus\n $output\n" if( $dbglvl > 0 );
549 }
550 $options = undef;
551 }
552
553 return $ret;
554 }
555
556 ##
557 ## runcmd( command )
558 ##
559 ## Run a MyProxy command and capture the exit value and the output
560 ##
561 sub runcmd
562 {
563 my ($command) = @_;
564
565 my $pid = open3(*Writer, *Reader, 0, "exec $command") ||
566 die "failed to run $command";
567
568 close(Writer);
569
570 my @output = <Reader>;
571 close(Reader);
572
573 waitpid($pid, 0);
574
575 my $exitstatus = $?;
576 my $output = join('', @output);
577
578 return ($exitstatus, $output);
579 }
580
581 sub write_timestamp
582 {
583 my $rep = shift;
584
585 open FD, ">$rep/$MYPROXY_REPLICATE_FILE";
586 print FD $new_rep_time;
587 close FD;
588 }
589
590 sub write_delete_file
591 {
592 my $rep = shift;
593
594 open FD, ">$rep/$MYPROXY_DELETED_FILE" or die
595 print "ERROR: $rep/$MYPROXY_DELETED_FILE could not be opened!\n";
596
597 for (@file_list)
598 {
599 print FD "$_\n";
600 }
601
602 close FD;
603 }
604
605 ##
606 ## findish_up( repository )
607 ##
608 ## Update the .myproxy_replicate with the current timesamp. Update
609 ## .myproxy_delete with the current snapshot of the repository.
610 ##
611 sub finish_up
612 {
613 my $rep = shift;
614
615 open FD, ">$rep/$MYPROXY_REPLICATE_FILE";
616 print FD $new_rep_time;
617 close FD;
618
619 open FD, ">$rep/$MYPROXY_DELETED_FILE" or die
620 print "ERROR: $rep/$MYPROXY_DELETED_FILE could not be opened!\n";
621
622 for (@file_list)
623 {
624 print FD "$_\n";
625 }
626
627 close FD;
628 }
629
630 __END__
631
632
633 =head1 NAME
634
635 B<myproxy-replicate> - Stores data from the MyProxy master repository to all
636 the slave servers.
637
638 =head1 SYNOPSIS
639
640 B<myproxy-replicate> [options] ...
641
642 Options:
643 [-verbose|-v] Print copious output
644 [-help|-h] Print usage
645 [-storage|-r]=<path to repository> Directory of the MyProxy repository.
646 [-config|-c]=<path to config file> Directory of the MyProxy Server
647 configuration file.
648 [-debug|-d] Run in debug mode
649
650 =head1 DESCRIPTION
651
652 B<myproxy-replicate> Replicates data.
653
654 This utility will read a specified MyProxy repository and send any new or
655 changed data to a slave MyProxy server. The slave servers are specified
656 in the B<myproxy-server.config(5)> file.
657
658 This utility will need to run at some specified interval in order to keep
659 the slave repositories semi current with the Master repository. This can
660 best be accomplished using cron, or some similar mechanism.
661
662 =head1 OPTIONS
663
664 =over 8
665
666 =item B<-v>, B<-verbose>
667
668 Enables verbose debugging output to the terminal.
669
670 =item B<-h>, B<-help>
671
672 Displays command usage text and exits.
673
674 =item B<-u>, B<-usage>
675
676 Displays command usage text and exits.
677
678 =item B<-r> I<dir>, B<-storage> I<dir>
679
680 Specifies the location of the credential storage directory.
681 The directory must be accessible only by the user running the
682 B<myproxy-server> process for security reasons.
683 Default: /var/lib/myproxy or $GLOBUS_LOCATION/var/myproxy
684
685 =item B<-c> I<file>, B<-config> I<file>
686
687 Specifies the location of the myproxy-server configuration file.
688 Default: /etc/myproxy-server.config or
689 $GLOBUS_LOCA-TION/etc/myproxy-server.config
690
691 =back
692
693 =head1 SEE ALSO
694
695 myproxy-init(1) myproxy-store(1) myproxy-retrieve(1) myproxy-delegate(1)
696 myproxy-server(8) myproxy-server.config(5)
697
698 =head1 AUTHOR
699
700 =cut
701