#!/home/product/code/webui-perl/bin/perl
#SUDOER_USERS: nobody
#SUDOER_RUNAS_USERS: root, mta
#SUDOER_TAGS: NOPASSWD, EXEC
use strict;
use warnings;
use Barracuda::Sudo::Lib;

=pod

=head1 NAME

cloud_restart

=head1 DESCRIPTION

The script implements sudo calls for Barracuda/Cluster.pm, invoked as part of cluster reset action.
The function Barracuda::Sudo::cluster_restart accepts one input, which determines whether a full restart will be performed.
Barracuda::Sudo::cluster_restart(int):
    1 - Full restart. The sshd will be droped and the services ssh-master and quar_helper will be restarted.
    0 - Partial restart. The services ssh-master and quar_helper will be restarted.


=cut

my ($full) = get_argv(sub {
    $_[0] == 1  || $_[0] == 0});


if ($full == 1)
{
    my $fh;
    my $undef;
    open $fh, "-|", "/usr/bin/readlink /home/product/code/firmware/current" or failwith(250, "Failed to find product path");

    my $sshd_path = <$fh>;
    failwith(250, "Failed to find product path") unless defined $sshd_path;
    chomp($sshd_path);
    close $fh;
    system_succeed("/usr/bin/killall", $sshd_path . "/sbin/sshd");
}

system_succeed("/home/product/code/firmware/current/sbin/service_manager.pl", "ssh-master", "restart");
system_succeed("/home/product/code/firmware/current/sbin/service_manager.pl", "quar_helper", "restart"); 


exit 0;
