|
|
@ -1,16 +1,22 @@
|
|
|
|
#!/usr/bin/env perl
|
|
|
|
#!/usr/bin/env perl
|
|
|
|
use warnings;
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use bignum;
|
|
|
|
use Sys::Hostname;
|
|
|
|
use Sys::Hostname;
|
|
|
|
use File::Compare;
|
|
|
|
use File::Compare;
|
|
|
|
use File::Copy;
|
|
|
|
use File::Copy;
|
|
|
|
|
|
|
|
|
|
|
|
# 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, $user, $shareddir, $spooldir) = @_;
|
|
|
|
my $mode = $ARGV[0];
|
|
|
|
|
|
|
|
my $user = $ARGV[1];
|
|
|
|
|
|
|
|
my $shareddir = $ARGV[2];
|
|
|
|
|
|
|
|
my $spooldir = $ARGV[3];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print "Mode: $mode, user: $user, shareddir: $shareddir and spooldir: $spooldir\n";
|
|
|
|
# Set some defaults if we didn't get them
|
|
|
|
# Set some defaults if we didn't get them
|
|
|
|
unless ($mode) {
|
|
|
|
unless ($mode == 0) {
|
|
|
|
# mode 0 = active/passive, mode 1 = active/active
|
|
|
|
# mode 0 = active/passive, mode 1 = active/active
|
|
|
|
|
|
|
|
print "Mode not set on command line, going to active/active\n";
|
|
|
|
$mode = 1;
|
|
|
|
$mode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unless ($user) {
|
|
|
|
unless ($user) {
|
|
|
@ -25,12 +31,18 @@ unless ($spooldir) {
|
|
|
|
|
|
|
|
|
|
|
|
my $sharedcrondir = "$shareddir/crontab";
|
|
|
|
my $sharedcrondir = "$shareddir/crontab";
|
|
|
|
my $cronfile = "$spooldir/$user";
|
|
|
|
my $cronfile = "$spooldir/$user";
|
|
|
|
my $sharedcronfile = "$sharedcrondir/$user";
|
|
|
|
my $activesharedcronfile = "$sharedcrondir/$user";
|
|
|
|
my $oldsharedcronfile = "$sharedcrondir/$user.old";
|
|
|
|
my $activecronfile = "$sharedcrondir/$user.active";
|
|
|
|
|
|
|
|
my $passivesharedcronfile = "$sharedcrondir/$user.passive";
|
|
|
|
|
|
|
|
my $oldactivesharedcronfile = "$sharedcrondir/$user.old";
|
|
|
|
|
|
|
|
my $oldpassivesharedcronfile = "$sharedcrondir/$user.passive.old";
|
|
|
|
my $electiondir = "$sharedcrondir/election";
|
|
|
|
my $electiondir = "$sharedcrondir/election";
|
|
|
|
my $hostname = hostname;
|
|
|
|
my $hostname = hostname;
|
|
|
|
my $time = time;
|
|
|
|
my $time = time;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print "Host: $hostname started run at: $time\n";
|
|
|
|
|
|
|
|
|
|
|
|
# Create directories
|
|
|
|
# Create directories
|
|
|
|
unless (-d $sharedcrondir) {
|
|
|
|
unless (-d $sharedcrondir) {
|
|
|
|
mkdir $sharedcrondir or die "Can not create shared directory: $!";
|
|
|
|
mkdir $sharedcrondir or die "Can not create shared directory: $!";
|
|
|
@ -45,16 +57,21 @@ sub get_nodes {
|
|
|
|
my @nodes = ();
|
|
|
|
my @nodes = ();
|
|
|
|
my @files = <$electiondir/*>;
|
|
|
|
my @files = <$electiondir/*>;
|
|
|
|
foreach my $filename (@files) {
|
|
|
|
foreach my $filename (@files) {
|
|
|
|
my $node = {};
|
|
|
|
chomp $filename;
|
|
|
|
open(FILE, "<$filename") or die "Could not open file: $!";
|
|
|
|
my $nodename = $filename;
|
|
|
|
$filename =~ s/^$electiondir\///;
|
|
|
|
$nodename =~ s/^$electiondir\///;
|
|
|
|
$node->{'name'} = $filename;
|
|
|
|
# Don't add this host
|
|
|
|
my $content = <FILE>;
|
|
|
|
unless ($nodename eq $hostname) {
|
|
|
|
chomp $content;
|
|
|
|
my $node = {};
|
|
|
|
$node->{'time'} = $content;
|
|
|
|
open(FILE, "<$filename") or die "Could not open file: $!";
|
|
|
|
$node->{'numeric'} = get_numeric($filename);
|
|
|
|
$node->{'name'} = $nodename;
|
|
|
|
close FILE;
|
|
|
|
my $content = <FILE>;
|
|
|
|
push @nodes, $node;
|
|
|
|
chomp $content;
|
|
|
|
|
|
|
|
$node->{'time'} = $content;
|
|
|
|
|
|
|
|
$node->{'numeric'} = get_numeric($nodename);
|
|
|
|
|
|
|
|
close FILE;
|
|
|
|
|
|
|
|
push @nodes, $node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return @nodes;
|
|
|
|
return @nodes;
|
|
|
|
|
|
|
|
|
|
|
@ -79,16 +96,23 @@ sub is_active {
|
|
|
|
my $state = 1;
|
|
|
|
my $state = 1;
|
|
|
|
# Loop all nodes
|
|
|
|
# Loop all nodes
|
|
|
|
foreach my $node (@nodes) {
|
|
|
|
foreach my $node (@nodes) {
|
|
|
|
# Dont do anything if this is the node
|
|
|
|
# If it has been active in the last 120 seconds it is eligable
|
|
|
|
unless ($node eq $node->{'name'}) {
|
|
|
|
my $diff = $time - $node->{'time'};
|
|
|
|
# If it has been active in the last 120 seconds it is eligable
|
|
|
|
if( $diff < 120 ) {
|
|
|
|
my $diff = $time - $node->{'time'};
|
|
|
|
print "Node: $node->{'name'} was active last 120 sec\n";
|
|
|
|
if( $diff < 120 ) {
|
|
|
|
# Go to passive mode if the numeric of that host is less than that of this host
|
|
|
|
# Go to passive mode if the numeric of that host is less than that of this host
|
|
|
|
my $this = get_numeric($hostname);
|
|
|
|
if( get_numeric($hostname) > $node->{'numeric'} ) {
|
|
|
|
my $that = $node->{'numeric'};
|
|
|
|
$state = 0 ;
|
|
|
|
print "Numeric for $hostname is $this and for $node->{'name'} is $that\n";
|
|
|
|
}
|
|
|
|
if( $this > $that ) {
|
|
|
|
}
|
|
|
|
$state = 0 ;
|
|
|
|
|
|
|
|
print "I am not active\n";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
print "I am active\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
print "Node: $node->{'name'} was not active last 120 sec\n";
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $state;
|
|
|
|
return $state;
|
|
|
@ -108,6 +132,7 @@ sub get_numeric {
|
|
|
|
# But a # infront of any actime crontab entry
|
|
|
|
# But a # infront of any actime crontab entry
|
|
|
|
sub comment_out {
|
|
|
|
sub comment_out {
|
|
|
|
my ($infile, $outfile) = @_;
|
|
|
|
my ($infile, $outfile) = @_;
|
|
|
|
|
|
|
|
print "Commenting out, infile is $infile and outfile is $outfile\n";
|
|
|
|
open(INFILE,"<$infile");
|
|
|
|
open(INFILE,"<$infile");
|
|
|
|
my $content = '';
|
|
|
|
my $content = '';
|
|
|
|
while(my $line = <INFILE>) {
|
|
|
|
while(my $line = <INFILE>) {
|
|
|
@ -125,6 +150,7 @@ sub comment_out {
|
|
|
|
# Remove a # from any crontab entry
|
|
|
|
# Remove a # from any crontab entry
|
|
|
|
sub uncomment {
|
|
|
|
sub uncomment {
|
|
|
|
my ($infile, $outfile) = @_;
|
|
|
|
my ($infile, $outfile) = @_;
|
|
|
|
|
|
|
|
print "Unommenting, infile is $infile and outfile is $outfile\n";
|
|
|
|
open(INFILE,"<$infile");
|
|
|
|
open(INFILE,"<$infile");
|
|
|
|
my $content = '';
|
|
|
|
my $content = '';
|
|
|
|
while(my $line = <INFILE>) {
|
|
|
|
while(my $line = <INFILE>) {
|
|
|
@ -139,8 +165,72 @@ sub uncomment {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# If we are running in active/active mode
|
|
|
|
sub compare_and_copy {
|
|
|
|
if($mode) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Current cronfile and shared cronfile is not same
|
|
|
|
|
|
|
|
if(compare($cron, $shared) != 0) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print "Current cronfile and shared is not the same\n";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This means that the other node has changed the cron file
|
|
|
|
|
|
|
|
if(compare($cron, $old) == 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 "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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If we are running in active/passive mode
|
|
|
|
|
|
|
|
if($mode == 0) {
|
|
|
|
unless (-d $electiondir) {
|
|
|
|
unless (-d $electiondir) {
|
|
|
|
mkdir $electiondir;
|
|
|
|
mkdir $electiondir;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -150,53 +240,26 @@ if($mode) {
|
|
|
|
|
|
|
|
|
|
|
|
# See if I am the one
|
|
|
|
# See if I am the one
|
|
|
|
if (is_active(get_nodes) ){
|
|
|
|
if (is_active(get_nodes) ){
|
|
|
|
uncomment $sharedcronfile, $cronfile;
|
|
|
|
# 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);
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
comment_out $cronfile, $sharedcronfile;
|
|
|
|
compare_and_copy($passivesharedcronfile, $oldpassivesharedcronfile);
|
|
|
|
|
|
|
|
comment_out($passivesharedcronfile, $cronfile);
|
|
|
|
|
|
|
|
uncomment($passivesharedcronfile, $activesharedcronfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# If we are running in actie/passive mode
|
|
|
|
# If we are running in active/active mode
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
# If we don't have any cronjob on this host
|
|
|
|
compare_and_copy($activesharedcronfile, $oldactivesharedcronfile);
|
|
|
|
unless (-e $cronfile) {
|
|
|
|
|
|
|
|
# If we have a cronjob on the shared drive
|
|
|
|
|
|
|
|
if (-e $sharedcronfile) {
|
|
|
|
|
|
|
|
copy($sharedcronfile, $cronfile) or die "Copy failed: $!";
|
|
|
|
|
|
|
|
copy($sharedcronfile, $oldsharedcronfile) or die "Copy failed: $!";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Nothing to do, no cronjob on any host
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# If there is no cronjob on the shared drive
|
|
|
|
|
|
|
|
unless (-e $sharedcronfile) {
|
|
|
|
|
|
|
|
# But we have a cronjob on this host
|
|
|
|
|
|
|
|
if (-e $cronfile) {
|
|
|
|
|
|
|
|
copy($cronfile, $sharedcronfile) or die "Copy failed: $!";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Current cronfile and shared cronfile is not same
|
|
|
|
|
|
|
|
if(compare($cronfile, $sharedcronfile) != 0) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This means that the other node has changed the cron file
|
|
|
|
|
|
|
|
if(compare($cronfile, $oldsharedcronfile) == 0) {
|
|
|
|
|
|
|
|
copy($sharedcronfile, $cronfile) or die "Copy failed: $!";
|
|
|
|
|
|
|
|
copy($sharedcronfile, $oldsharedcronfile) or die "Copy failed: $!";
|
|
|
|
|
|
|
|
# All files are now the same
|
|
|
|
|
|
|
|
# This means that my node has changed the cron file
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
copy($cronfile, $sharedcronfile) or die "Copy failed: $!";
|
|
|
|
|
|
|
|
# The other node will now detect the difference and do the correct adjustment
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exit;
|
|
|
|
exit;
|
|
|
|