#!/usr/bin/perl

=head1 NAME

Script to put this node into maintenance state

=head1 SYNOPSIS

  When called with none of the options, this will output the current state of the maintenance mode on this node.

  Options :
   --activate : Activate the maintenance mode on this node
   --deactivate : Deactivate the maintenance mode on this node

=head1 DESCRIPTION

Script to put this node into maintenance state. Will ensure that MariaDB will not re-elect itself as master if its unable to join an existing quorum

=cut

use strict;
use warnings;

use lib '/usr/local/pf/lib';

use pf::log;
BEGIN {
  use Log::Log4perl qw(get_logger);
  my $log_conf = q(
  log4perl.rootLogger              = INFO, SCREEN
  log4perl.appender.SCREEN         = Log::Log4perl::Appender::Screen
  log4perl.appender.SCREEN.stderr  = 0
  log4perl.appender.SCREEN.layout  = Log::Log4perl::Layout::PatternLayout
  log4perl.appender.SCREEN.layout.ConversionPattern = %p : %m %n
  );
  Log::Log4perl::init(\$log_conf);
}

use pf::cluster;
use Getopt::Long;

my ($activate, $deactivate, $help);
GetOptions (
    "activate" => \$activate,
    "deactivate" => \$deactivate,
    "h" => \$help,
);

if($help){
  pod2usage( -verbose => 1 );
}

my $maintenance_file = "/usr/local/pf/var/maintenance-mode";

# Show status when there is no parameter
if(!$activate && !$deactivate) {
    my $msg = pf::cluster::is_in_maintenance() ? "Maintenance mode is activated" : "Maintenance mode is deactivated";
    print $msg . "\n";
}
elsif($activate) {
    pf::cluster::activate_maintenance();
    if(system("systemctl cat monit > /dev/null 2>&1") == 0) {
        print "==========\nStopping and disabling Monit...\n";
        do_command("systemctl stop monit");
        do_command("systemctl disable monit");
    }

    print "==========\nStopping PacketFence...\n";
    do_command("/usr/local/pf/bin/pfcmd service pf stop");
    
    print "==========\nStopping PacketFence MariaDB...\n";
    do_command("systemctl stop packetfence-mariadb");

    print "==========\nSetting default target to multi-user\n";
    do_command("systemctl set-default multi-user.target");

    print "==========\nNode has been put into maintenance mode...\n";

    exit 0;
}
elsif($deactivate) {
    pf::cluster::deactivate_maintenance();
    
    print "==========\nStarting PacketFence Redis Cache...\n";
    do_command("systemctl start packetfence-redis-cache");

    print "==========\nStarting PacketFence MariaDB...\n";
    do_command("systemctl start packetfence-mariadb");

    print "==========\nStarting PacketFence...\n";
    do_command("/usr/local/pf/bin/pfcmd service pf start");
    
    if(system("systemctl cat monit > /dev/null 2>&1") == 0) {
        print "==========\nStarting and enabling Monit...\n";
        do_command("systemctl start monit");
        do_command("systemctl enable monit");
    }

    print "==========\nSetting default target to packetfence-cluster\n";
    do_command("systemctl set-default packetfence-cluster.target");

    print "==========\nNode has been put out of the maintenance mode...\n";

}

=head2 do_command

Perform a command and output an error as well as exit if it fails

=cut

sub do_command {
    my ($command) = @_;
    my $ret = system($command);

    if($ret != 0) {
        print "=========\nFailed to execute $command\n==========\n";
        exit 1;
    }
    
    return 1;
}
