#!/usr/bin/perl
## @file spins
# @brief This is the command to place a Service Pack (bundle of patches and SUs) into service.
# @details
# @parm -r determines prompt user for input or perform actions by default without any questions.
# @parm -sp ignores any reboots.
# @param --help      print this help
#
# @author Dave Cairns
# @version 1.00 - Initial version

=pod

=head1 NAME

B<spins> - Put a Service Pack into service.

=head1 SYNOPSIS

B<spins> [B<--help>]

=head1 DESCRIPTION

Put Service Pack content (set of Patches and SUs) into service.

=head1 OPTIONS

=over 4

=item B<--help>

Print this help message and exit.

=back

=head1 EXAMPLES

=over 4

=item spins 

$ B<spins>
  All SUs and patches within the SP Nortel_Service_Pack_Linux_6.00_08.ntl will be in service.    
  This command may cause service interruption.

          The following patches will not be put into service since they were not loaded by spload:  nortel-cs1000-nrsm-6.00.08.03-00.i386.000.ntl, p28066_1.ntl . 
  Please refer to the spload output for more details.

  All Nortel and Jboss-Quantum applications will be stopped.
  All Nortel and Jboss-Quantum applications will be started.
  Do you want to proceed? (Y/N) [N]? Y
  Current configuration does not have failsafe GW enabled, use EM to configure if desired.

  ...

  Patch handle: 1

  Performing the installation:
  Installing: nortel-cs1000-tps-6.00.08.05-00.i386
  Performing SU installation (Nortel RPM)...
  Preparing...                ########################################### [100%]
     1:nortel-cs1000-tps      ########################################### [100%]

  Done.
  The SU installation has completed.
  The patch 1 has been installed successfully.
  Patch handle: 2 

  ...

  spins command for SP Nortel_Service_Pack_Linux_6.00_08.ntl completed successfully.

=back 

=head1 CAVEATS

spload must have been successfully run previously for this command to work

=head1 SEE ALSO

B<spout>, B<spload>

=cut

use lib "/opt/nortel/base/lib/";

use warnings;
use strict;

use common_variables;
use common_functions;
use baselog;
use Getopt::Long;

use patchutil::patching;
use base_semctl;
use patchutil::patches_target;

my $ssh_hang_on_exit_bug_flag ;         # NEEDED BY END FUNCTION

sub spins_main();

exit(spins_main());
## @fn sub spins_main ()
# @brief Main function of the script
# @details This function is an entry point of spins command.
# @param
#   - -help - shows usage
#   - -r - interactive flag to allow prompts for user (optional).
#   - -sp - ignores any reboots (optional).
# @return
# One of the return codes defined in target_common.pm:
#    $OK - spins command run successfully
#    $ERROR - spins command failed.
# @author Dave Cairns
# @date Oct 2008
# @note
sub spins_main()
{
   my $helpflag;
   my $remoteflag;
   my $spflag;
   my $ret    = $OK;
   my %params = ();
   $params{'interact'} = $TRUE;
   $params{'sp'}       = $FALSE;

   if (warning_if_not_nortel_grp())
   {
      return $ERROR;
   }
   set_subsystem('Base_Patch');

   # get parameters entered
   my $res = GetOptions(
              "help"   => \$helpflag,
              "remote" => \$remoteflag,
              "sp"     => \$spflag,
              "die"    => \$ssh_hang_on_exit_bug_flag
             );

   if (!$res)
   {
      print "Incorrect options provided\n";
      print_pod_usage();
      return $ERROR;
   } ## end if (!$res)

   if ($helpflag)
   {
      print_pod_usage();
      return $OK;
   }

## ARGV[0] only has data if there were uncaught parameters.
   if (defined($ARGV[0]))
   {
      baselog($LOG_ERROR, "Invalid parameters entered\n", $TRUE);
      print_pod_usage();
      return $ERROR;
   } ## end if (defined($ARGV[0]))

   # check input parameters
   #validate option -r
   if ($remoteflag)
   {
      $params{'interact'} = $FALSE;
   }

   # validate option -sp
   if ($spflag)
   {
      $params{'sp'} = $TRUE;
   }

   # if SPLOAD_DIR and SPINS_DIR exists, the SPINS dir belong to a previous SP, and a spins command will be allow to put in service the new SP currently present at the SPLOAD_DIR.
   if ((-d $SPINS_DIR) && (!-d $SPLOAD_DIR))
   {
      baselog($LOG_INFO, "There is already an SP in service.\n", $TRUE);
      return $OK;
   }
   if (!-d $SPLOAD_DIR)
   {
      baselog($LOG_INFO, "There is no SP in loaded state.\n", $TRUE);
      return $OK;
   }
   ::signals_ignore();
   my $semmngr = base_semctl::new("PATCH");
   if (!defined($semmngr))
   {
      my $error_descr = $!;
      baselog($LOG_ERROR, "Cannot create semaphore manager object ($error_descr).\n", $TRUE);
      return $ERROR;
   } ## end if (!defined($semmngr)...
   my @semaphores = ($SEM_APPSTART, $SEM_HARD);

   if (!$semmngr->sem_take($SEM_PATCH, IPC_NOWAIT))
   {
      baselog($LOG_ERROR, "Cannot take semaphores. Patching operation aborted.\n", $TRUE);
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   if (!$semmngr->sem_take(\@semaphores, $WAIT_FLAG, $PINS_SEM_WAITTIME))
   {
      baselog($LOG_ERROR, "Cannot take semaphores. Patching operation aborted. Retry command.\n", $TRUE);
      if (!$semmngr->sem_give($SEM_PATCH))
      {
         baselog($LOG_ERROR, "Cannot release semaphores.");
      }
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_take...

   my $patching = patchutil::patching::new();
   if (not defined($patching))
   {
      baselog($LOG_ERROR, "Cannot create patching object\n", $TRUE);
      $ret = $ERROR;
   }
   else
   {
      if (not $patching->spins(\%params))
      {
         baselog($LOG_ERROR, "Failed to put Service Pack In Service.\n", $TRUE);
         $ret = $ERROR;
      }
      $patching = undef;
   } ## end else [ if (not defined($patching...
   if (!$semmngr->sem_give(\@semaphores))
   {
      baselog($LOG_ERROR, "Cannot release semaphores.\n", $TRUE);
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_give...
   if (!$semmngr->sem_give($SEM_PATCH))
   {
      baselog($LOG_ERROR, "Cannot release semaphores.");
      undef $semmngr;
      return $ERROR;
   } ## end if (!$semmngr->sem_give...
   undef $semmngr;

   return $ret;
} ## end sub spins_main()


END
{   # WORKAROUND - YUK!
    #
    #   Due to a OpenSSH hang-on-exit issue, if we are being called remotely we
    #   will create and call a script to kill our parent "sshd: nortel@notty"
    #   to terminate the ssh session on this side of a remote ssh reguest
    #   session
    #
    if ($ssh_hang_on_exit_bug_flag)
    {
        # Save original return value
        my$retVal = $? ;

        if( ssh_hang_on_exit_bug() == $ERROR )
        {
           return $ERROR;
        }

        baselog($LOG_INFO,"I AM OUTA HERE");

        # Return the original return value since "ssh_hang_on_exit_bug" would
        # have changed the result.
        return $retVal;
    }
}   # END
 
