#!/usr/bin/perl

=head1 NAME

Script to manage the nodes of the cluster

=head1 SYNOPSIS

  This allows to enable/disable nodes in the cluster configuration of the current server
  It doesn't attempt to sync the cluster and only performs the changes on the local server

  To disable a node in the cluster:
   node <node-hostname> disable

  To enable a node in the cluster:
   node <node-hostname> enable

  To see the state of a node in the cluster
   node <node-hostname>

  To show the enabled/disabled state of all nodes
   node

=head1 DESCRIPTION

Script to manage the nodes of the cluster

=cut

use strict;
use warnings;

use constant INSTALL_DIR => '/usr/local/pf';
use lib INSTALL_DIR . "/lib";

use pf::constants qw($TRUE $FALSE);
use pf::cluster;
use pf::util;
use Pod::Usage;

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);
}

my $node_hostname = $ARGV[0];

sub print_node_state {
    my ($node_hostname) = @_;
    if(-f pf::cluster::node_disabled_file($node_hostname)) {
        print "Node $node_hostname is currently disabled \n";
    }
    else {
        print "Node $node_hostname is currently enabled \n";
    }
}

unless(defined($node_hostname)) {
    for my $node_hostname (pf::cluster::all_hosts) {
        print_node_state($node_hostname);
    }
    exit;
}

unless(pf::cluster::all_find_server_by_hostname($node_hostname)) {
    print STDERR "Node $node_hostname doesn't exist in the cluster configuration \n\n";
    pod2usage(1);
}

my $enable;
if(defined($ARGV[1])) {
    if($ARGV[1] eq "enable") {
        $enable = $TRUE;
    }
    elsif($ARGV[1] eq "disable") {
        $enable = $FALSE;
    }
    else {
        print STDERR "Invalid command. Should be enable|disable \n\n";
        pod2usage(1);
    }
}
else {
    print_node_state($node_hostname);
    exit;
}

if($enable) {
    unlink pf::cluster::node_disabled_file($node_hostname);
}
else {
    touch_file(pf::cluster::node_disabled_file($node_hostname));
}

=back

=head1 AUTHOR

Inverse inc. <info@inverse.ca>

=head1 COPYRIGHT

Copyright (C) 2005-2020 Inverse inc.

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.

=cut

1;

# vim: set shiftwidth=4:
# vim: set expandtab:
# vim: set backspace=indent,eol,start:


