|
|
|
@ -2,20 +2,41 @@
|
|
|
|
|
use warnings;
|
|
|
|
|
use strict;
|
|
|
|
|
use Sys::Hostname;
|
|
|
|
|
use File::Compare;
|
|
|
|
|
use File::Copy;
|
|
|
|
|
|
|
|
|
|
# We need a common directory for our nodes to write and read state from plus a couple of cronfiles
|
|
|
|
|
my ($electiondir, $cronfile, $sharedcronfile) = @_;
|
|
|
|
|
my ($mode, $user, $shareddir, $spooldir) = @_;
|
|
|
|
|
|
|
|
|
|
unless (-d $electiondir) {
|
|
|
|
|
mkdir $electiondir;
|
|
|
|
|
# Set some defaults if we didn't get them
|
|
|
|
|
unless ($mode) {
|
|
|
|
|
# mode 0 = active/passive, mode 1 = active/active
|
|
|
|
|
$mode = 1;
|
|
|
|
|
}
|
|
|
|
|
unless ($user) {
|
|
|
|
|
$user = "www-data";
|
|
|
|
|
}
|
|
|
|
|
unless ($shareddir) {
|
|
|
|
|
$shareddir = "/var/lib/thruk";
|
|
|
|
|
}
|
|
|
|
|
unless ($spooldir) {
|
|
|
|
|
$spooldir = "/var/spool/cron/crontabs";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# We need our name
|
|
|
|
|
my $sharedcrondir = "$shareddir/crontab";
|
|
|
|
|
my $cronfile = "$spooldir/$user";
|
|
|
|
|
my $sharedcronfile = "$sharedcrondir/$user";
|
|
|
|
|
my $oldsharedcronfile = "$sharedcrondir/$user.old";
|
|
|
|
|
my $electiondir = "$sharedcrondir/election";
|
|
|
|
|
my $hostname = hostname;
|
|
|
|
|
|
|
|
|
|
# We also need to know the time
|
|
|
|
|
my $time = time;
|
|
|
|
|
|
|
|
|
|
# Create directories
|
|
|
|
|
unless (-d $sharedcrondir) {
|
|
|
|
|
mkdir $sharedcrondir or die "Can not create shared directory: $!";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Get the nodes in the cluster
|
|
|
|
|
sub get_nodes {
|
|
|
|
@ -118,13 +139,64 @@ sub uncomment {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Update timestamp
|
|
|
|
|
write_timestamp;
|
|
|
|
|
# If we are running in active/active mode
|
|
|
|
|
if($mode) {
|
|
|
|
|
unless (-d $electiondir) {
|
|
|
|
|
mkdir $electiondir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Update timestamp
|
|
|
|
|
write_timestamp;
|
|
|
|
|
|
|
|
|
|
# See if I am the one
|
|
|
|
|
if (is_active(get_nodes) ){
|
|
|
|
|
# See if I am the one
|
|
|
|
|
if (is_active(get_nodes) ){
|
|
|
|
|
uncomment $sharedcronfile, $cronfile;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
} else {
|
|
|
|
|
comment_out $cronfile, $sharedcronfile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# If we are running in actie/passive mode
|
|
|
|
|
else {
|
|
|
|
|
# If we don't have any cronjob on this host
|
|
|
|
|
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;
|
|
|
|
|