Seems to be working now

master
Micke Nordin 8 years ago
parent 73d41f0aca
commit 05b0f6c0ae

@ -5,6 +5,7 @@ use bignum;
use Sys::Hostname; use Sys::Hostname;
use File::Compare; use File::Compare;
use File::Copy; 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 # We need a common directory for our nodes to write and read state from plus a couple of cronfiles
my $mode = $ARGV[0]; my $mode = $ARGV[0];
@ -32,7 +33,6 @@ unless ($spooldir) {
my $sharedcrondir = "$shareddir/crontab"; my $sharedcrondir = "$shareddir/crontab";
my $cronfile = "$spooldir/$user"; my $cronfile = "$spooldir/$user";
my $activesharedcronfile = "$sharedcrondir/$user"; my $activesharedcronfile = "$sharedcrondir/$user";
my $activecronfile = "$sharedcrondir/$user.active";
my $passivesharedcronfile = "$sharedcrondir/$user.passive"; my $passivesharedcronfile = "$sharedcrondir/$user.passive";
my $oldactivesharedcronfile = "$sharedcrondir/$user.old"; my $oldactivesharedcronfile = "$sharedcrondir/$user.old";
my $oldpassivesharedcronfile = "$sharedcrondir/$user.passive.old"; my $oldpassivesharedcronfile = "$sharedcrondir/$user.passive.old";
@ -154,7 +154,7 @@ sub uncomment {
open(INFILE,"<$infile"); open(INFILE,"<$infile");
my $content = ''; my $content = '';
while(my $line = <INFILE>) { while(my $line = <INFILE>) {
$line =~ s/^(#)([\d*])/$2/g; $line =~ s/^(#\s?)([\d*])/$2/g;
$content .= $line; $content .= $line;
} }
@ -165,68 +165,31 @@ sub uncomment {
} }
sub compare_and_copy { sub cron_compare {
my ($shared, $old, $cron) = @_; my ($one, $two, $three) = @_;
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;
}
# If there is no cronjob on the shared drive print "Comparing files\n";
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;
}
# Current cronfile and shared cronfile is not same # 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 # This means that the other node has changed the cron file
if(compare($cron, $old) == 0) { if(compare($one, $three) == 0) {
print "Some othe rnode has changed the cron file\n"; print "Some other node has changed the file\n";
copy($shared, $cron) or die "Copy failed: $!"; return 1;
copy($shared, $old) or die "Copy failed: $!";
# All files are now the same
# This means that my node has changed the cron file # This means that my node has changed the cron file
} else { } else {
print "This node has changed the cron file\n"; print "This node has changed the cron file\n";
copy($cron, $shared) or die "Copy failed: $!"; return 2;
# The other node will now detect the difference and do the correct adjustment
} }
} }
return 0;
} }
# If we are running in active/passive mode # If we are running in active/passive mode
@ -234,32 +197,62 @@ if($mode == 0) {
unless (-d $electiondir) { unless (-d $electiondir) {
mkdir $electiondir; mkdir $electiondir;
} }
my $tempfile = tmpnam();
# Update timestamp # Update timestamp
write_timestamp; write_timestamp;
# See if I am the one # See if I am the one
if (is_active(get_nodes) ){ if (is_active(get_nodes) ){
# first we comment out my cronfile and put it at activecronfile my $compare = cron_compare($cronfile, $activesharedcronfile, $oldactivesharedcronfile );
comment_out($cronfile, $activecronfile); # The other node has changed the file
# Then we compare it to the oldactivesharedcronfile if($compare == 1) {
compare_and_copy($activesharedcronfile, $oldactivesharedcronfile, $activecronfile ); copy($activesharedcronfile, $cronfile);
# Now we compare the result of that to passivesharedcronfile copy($activesharedcronfile, $oldactivesharedcronfile);
compare_and_copy($activesharedcronfile, $passivesharedcronfile, $activecronfile); # This node has changed the file
# Lastly we activate the result of that } elsif ($compare == 2) {
uncomment($activesharedcronfile, $cronfile); 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 { } else {
compare_and_copy($passivesharedcronfile, $oldpassivesharedcronfile); my $compare = cron_compare($cronfile, $passivesharedcronfile, $oldpassivesharedcronfile );
comment_out($passivesharedcronfile, $cronfile); # The other node has changed the file
uncomment($passivesharedcronfile, $activesharedcronfile); 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 # If we are running in active/active mode
else { 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; exit;

Loading…
Cancel
Save