📄 myproxy-replicate
字号:
#!/usr/bin/perluse strict;use Getopt::Long;##use File::Find;use Fcntl ':flock'; # import LOCK_* constantsuse File::stat;##use Cwd;##use Config;use Data::Dumper;use Time::Local;use IPC::Open3;require Pod::Usage;my $MYPROXY_DEFAULT_PORT = "7512";my $MYPROXY_DEFAULT_LOCATION = "/var/myproxy";my $MYPROXY_REPLICATE_FILE = "\.myproxy_replicate";my $MYPROXY_DELETED_FILE = "\.myproxy_deleted";my $MYPROXY_PID_FILE = "\.myproxy_pid";my $SECONDS_PER_HOUR = (60 * 60);## Do a perl check for version >= 5.005.#if ( ! ( defined eval "require 5.005" ) ){ die "Requires at least Perl version 5.005";}my $gpath = $ENV{GLOBUS_LOCATION};if (!defined($gpath)){ die "GLOBUS_LOCATION needs to be set before running this script";}# process the -option optionsmy ( $repository, $verbose, $debug, $help, $usage, $config );GetOptions( 'storage|r=s' => \$repository, 'config|c=s' => \$config, 'verbose|v' => \$verbose, 'debug|d' => \$debug, 'usage|u' => \$usage, 'help|h' => \$help) or Pod::Usage::pod2usage(0);Pod::Usage::pod2usage(0) if $help;Pod::Usage::pod2usage(0) if $usage;my $dbglvl = 0;$dbglvl += 1 if( $verbose );$dbglvl += 2 if( $debug );my $globus_dir = $ENV{GLOBUS_LOCATION};#### Find the MyProxy Repository. If one is not given check the default## locations.##if( !defined($repository) ){ print "Checking for $MYPROXY_DEFAULT_LOCATION\n" if( $dbglvl > 0 ); if( !(-d $MYPROXY_DEFAULT_LOCATION) ) { print "Checking for $globus_dir$MYPROXY_DEFAULT_LOCATION\n" if( $dbglvl > 0 ); if( !(-d "$globus_dir$MYPROXY_DEFAULT_LOCATION") ) { die "Could not find MyProxy repository in any of the default " . "locations.\nDefault: $MYPROXY_DEFAULT_LOCATION or " . "\$GLOBUS_LOCATION$MYPROXY_DEFAULT_LOCATION.\n"; } $repository = "$globus_dir$MYPROXY_DEFAULT_LOCATION"; } else { $repository = $MYPROXY_DEFAULT_LOCATION; } print "Setting repository to $repository\n" if( $dbglvl > 1 ); }#### Check to see if myproxy-store is found and executable##my $myproxy_store;chomp($myproxy_store = `which myproxy-store 2>/dev/null`);die "myproxy-store not in PATH, stopped" if (!(-x $myproxy_store));#### Check to see if myproxy-destroy is found and executable##my $myproxy_destroy;chomp($myproxy_destroy = `which myproxy-destroy 2>/dev/null`);die "myproxy-destroy not in PATH, stopped" if (!(-x $myproxy_destroy));#### Check for a server configuration file. If one is not given, check the## default locations.##if( !defined($config) ){ if( !(-e "/etc/myproxy-server.config") ) { if( !(-e "$globus_dir/etc/myproxy-server.config") ) { die "Could not find MyProxy configuration file in any of the " . "default locations.\nDefault: /etc/myproxy-server.config or " . "\$GLOBUS_LOCATION/etc/myproxy-server.config.\n"; } $config = "$globus_dir/etc/myproxy-server.config"; } else { $config = "/etc/myproxy-server.config"; }}print "Using server config file: $config\n" if( $dbglvl > 0 );my $lst_rep_time;my $new_rep_time = undef;my @file_list;my $retval = main();exit( $retval );############################################################################# Do everything###########################################################################sub main{ ## ## Make sure no other instance of myproxy-replicate can run until ## current one finishes. ## open PID, ">$repository/$MYPROXY_PID_FILE"; flock( PID, LOCK_EX ); print PID "$$\n"; my $rep_file = "$repository/$MYPROXY_REPLICATE_FILE"; ## ## Get the timestamp of the last replication. ## if( -e $rep_file ) { $lst_rep_time = get_last_replicate_time( $rep_file ); } else { $lst_rep_time = 0; } $new_rep_time = timelocal(localtime); ## ## Retrieve all of the slave MyProxy servers from config file. ## my $slave_servers = get_slaves( $config ); print "Slave Servers:\n" . Dumper $slave_servers if( $dbglvl > 1 ); ## ## Read the repository and find the files that have changed since the ## last replication. ## my $files = read_dir( $repository, "\.creds" ); print Dumper $files if( $dbglvl > 1 ); ## ## send the files to the slave servers. ## my $ret = replicate_files( $repository, $files, $slave_servers ); if( $ret == 0 ) { $ret = delete_files( $repository, $files, $slave_servers ); } ## ## Check to see if we had a problem with either replicating or ## deleting. If there was a problem don't update .myproxy_replicate ## or .myproxy_delete. ## if( !$ret ) { print "Replication complete: ", localtime() . "\n"; finish_up( $repository ); } else { print STDERR "Replication Failed\n"; }##sleep( 50 ); flock( PID, LOCK_UN ); close PID; unlink( "$repository/$MYPROXY_PID_FILE" ); return( $ret );}############################################################################# Functions############################################################################### get_last_replicate_time( file )#### Read the replication timestamp.##sub get_last_replicate_time{ my $filename = shift; open LSTREP, $filename; $lst_rep_time = <LSTREP>; close LSTREP; return $lst_rep_time;}#### get_slaves( file )#### Get the list of slave servers from the configuration file. ##sub get_slaves{ my $config = shift; my $slist; my $junk; my @slave_list; open CFG, $config; for (<CFG>) { next if( !($_ =~ /^slave_servers/) ); ($junk, $slist) = split /slave_servers/, $_; my @slaves = split /;/, $slist; for my $s (@slaves) { my ($server, $port) = split /:/, $s; chomp($server); chomp($port); my $ops = "-s $server "; $ops .= "-p $port " if( length($port) > 0 ); push @slave_list, $ops; } } close CFG; return \@slave_list;}#### read_dir( directory, expression )#### Read the MyProxy repository and find files that match expression. ##sub read_dir{ my $directory = shift; my $expression = shift; my $files; my $stuff = undef; opendir(DIR, $directory) or die print "ERROR: directory \"$directory\" could not be opened!\n"; @file_list = map { $_->[0] } # Form a list of names without paths. grep { $_->[0] =~ /$expression/ } # extract the files. map { [ $_, "$directory/$_" ] } # form anonymous array [name, pathname] # because readdir strips the path from the bname grep { ! /^\.\.?$/ } # remove the current directory and its parent readdir(DIR); #read all of the filenames in the directory for my $f (@file_list) { my $sb = stat("$directory/$f"); my $mod_time = localtime $sb->mtime; printf "File is %s, mtime %s\n", $f, $mod_time if( $dbglvl > 1 ); if( $sb->mtime >= $lst_rep_time ) { push @{$files}, $f; } } $stuff->{'files'} = $files; closedir(DIR); $stuff->{'del'} = missing_files( $directory, @file_list ); return $stuff;}#### replicate_files( reposityr, files, slaves )#### Replicate all of the files listed to all of the slaves listed. ##sub replicate_files{ my $rep = shift; my $files = shift; my $slaves = shift; my ($exitstatus, $output); my $ret = 0; for my $f (@{$files->{'files'}}) { my $data = $f; $data =~ s/creds/data/; my $options = parse_datafile( $rep, $data ); print "File not found: $rep/$data\n File $rep/$f not replicated.\n\n" if( !defined($options) ); next if( !defined($options) ); $options .= "-c $rep/$f -y $rep/$f ";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -