From 05b0f6c0ae7299dc4d7c3c8fc65408796a461291 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Mon, 12 Sep 2016 17:09:22 +0200 Subject: [PATCH] Seems to be working now --- cluster_cron.pl | 131 +++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/cluster_cron.pl b/cluster_cron.pl index d17eeb3..c8c9399 100755 --- a/cluster_cron.pl +++ b/cluster_cron.pl @@ -5,6 +5,7 @@ use bignum; use Sys::Hostname; use File::Compare; use File::Copy; +use File::Temp; # We need a common directory for our nodes to write and read state from plus a couple of cronfiles my $mode = $ARGV[0]; @@ -32,7 +33,6 @@ unless ($spooldir) { my $sharedcrondir = "$shareddir/crontab"; my $cronfile = "$spooldir/$user"; my $activesharedcronfile = "$sharedcrondir/$user"; -my $activecronfile = "$sharedcrondir/$user.active"; my $passivesharedcronfile = "$sharedcrondir/$user.passive"; my $oldactivesharedcronfile = "$sharedcrondir/$user.old"; my $oldpassivesharedcronfile = "$sharedcrondir/$user.passive.old"; @@ -154,7 +154,7 @@ sub uncomment { open(INFILE,"<$infile"); my $content = ''; while(my $line = ) { - $line =~ s/^(#)([\d*])/$2/g; + $line =~ s/^(#\s?)([\d*])/$2/g; $content .= $line; } @@ -165,67 +165,30 @@ sub uncomment { } -sub compare_and_copy { - my ($shared, $old, $cron) = @_; - unless ($cron) { - $cron = $cronfile; - } - print "Comparing files\n"; - # If we don't have any cronjob on this host - unless (-e $cron) { - print "There is no cronfile on this host\e"; - # If we have a cronjob on the shared drive - if (-e $shared) { - print "We have a shared cronfile\n"; - copy($shared, $cron) or die "Copy failed: $!"; - # If we dont have an old file - unless (-e $old) { - print "We dont have an old file so creating one\n"; - copy($shared, $old) or die "Copy failed: $!"; - } - } - # Nothing to do, no cronjob on any host - return; - } +sub cron_compare { + my ($one, $two, $three) = @_; - # If there is no cronjob on the shared drive - unless (-e $shared) { - - print "There is no shared cronjobs\n"; - # But we have a cronjob on this host - if (-e $cron) { - print "We have some cronjobs\n"; - copy($cron, $shared) or die "Copy failed: $!"; - # If we dont have an old file - unless (-e $old) { - print "There is no old cron job here som fixing that\n"; - copy($cron, $old) or die "Copy failed: $!"; - } - - } - return; - } + print "Comparing files\n"; # Current cronfile and shared cronfile is not same - if(compare($cron, $shared) != 0) { + if(compare($one, $two) != 0) { - print "Current cronfile and shared is not the same\n"; + print "$one and $two is not the same\n"; - # This means that the other node has changed the cron file - if(compare($cron, $old) == 0) { + # This means that the other node has changed the cron file + if(compare($one, $three) == 0) { - print "Some othe rnode has changed the cron file\n"; - copy($shared, $cron) or die "Copy failed: $!"; - copy($shared, $old) or die "Copy failed: $!"; - # All files are now the same - # This means that my node has changed the cron file - } else { + print "Some other node has changed the file\n"; + return 1; + # This means that my node has changed the cron file + } else { - print "This node has changed the cron file\n"; - copy($cron, $shared) or die "Copy failed: $!"; - # The other node will now detect the difference and do the correct adjustment - } + print "This node has changed the cron file\n"; + return 2; + } } + + return 0; } @@ -234,32 +197,62 @@ if($mode == 0) { unless (-d $electiondir) { mkdir $electiondir; } + my $tempfile = tmpnam(); # Update timestamp write_timestamp; # See if I am the one if (is_active(get_nodes) ){ - # first we comment out my cronfile and put it at activecronfile - comment_out($cronfile, $activecronfile); - # Then we compare it to the oldactivesharedcronfile - compare_and_copy($activesharedcronfile, $oldactivesharedcronfile, $activecronfile ); - # Now we compare the result of that to passivesharedcronfile - compare_and_copy($activesharedcronfile, $passivesharedcronfile, $activecronfile); - # Lastly we activate the result of that - uncomment($activesharedcronfile, $cronfile); - + my $compare = cron_compare($cronfile, $activesharedcronfile, $oldactivesharedcronfile ); + # The other node has changed the file + if($compare == 1) { + copy($activesharedcronfile, $cronfile); + copy($activesharedcronfile, $oldactivesharedcronfile); + # This node has changed the file + } elsif ($compare == 2) { + copy($cronfile, $activesharedcronfile); + #comment_out($cronfile, $passivesharedcronfile); + } + comment_out($cronfile, $tempfile); + $compare = cron_compare($tempfile, $passivesharedcronfile, $oldpassivesharedcronfile); + # The other node has changed the file + if($compare == 1) { + uncomment($passivesharedcronfile, $cronfile); + } elsif ($compare == 2) { + copy($passivesharedcronfile, $oldpassivesharedcronfile); + comment_out($cronfile, $passivesharedcronfile); + } + # We are passive node } else { - compare_and_copy($passivesharedcronfile, $oldpassivesharedcronfile); - comment_out($passivesharedcronfile, $cronfile); - uncomment($passivesharedcronfile, $activesharedcronfile); + my $compare = cron_compare($cronfile, $passivesharedcronfile, $oldpassivesharedcronfile ); + # The other node has changed the file + if($compare == 1) { + copy($passivesharedcronfile, $cronfile); + # We have changed stuff + } elsif ($compare == 2) { + copy($passivesharedcronfile, $oldpassivesharedcronfile); + comment_out($cronfile, $passivesharedcronfile); + copy($passivesharedcronfile, $cronfile); + } } - + unlink $tempfile; } # If we are running in active/active mode else { - compare_and_copy($activesharedcronfile, $oldactivesharedcronfile); + my $compare = cron_compare($cronfile, $activesharedcronfile, $oldactivesharedcronfile ); + # The other node has changed the file + if($compare == 1) { + copy($activesharedcronfile, $cronfile); + # We have changed things + } elsif ($compare == 2) { + copy($activesharedcronfile, $oldactivesharedcronfile); + comment_out($cronfile, $activesharedcronfile); + copy($activesharedcronfile, $cronfile); + } } + exit; +