# $Header: opatch/platform_opatches/platform_crs/226/crs/crspatch.pm /st_opatch_11.2/3 2011/03/07 21:03:32 vganesan Exp $
#
# crspatch.pm
#
# Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 
#

=head1 NAME

  crspatch.pm  Oracle clusterware Patching Module/Package

=head1 DESCRIPTION

   This package contains functions required for  patching
   Oracle clusterware Software

=cut

#    MODIFIED   (MM/DD/YY)
#    ksviswan    09/23/10 - Part fix for bug 10119895
#    ksviswan    09/21/10 - Merge fix for bugs 9482228,9750739
#    dpham       06/30/10 - Add arguement to create_dirs() and set_file_perms() 
#                           functions (9850696)
#    ksviswan    08/24/09 - Fix Bug 8797450
#    dpham       07/29/09 - XbranchMerge dpham_bug-8727340 from
#                           st_has_11.2.0.1.0
#    ksviswan    07/24/09 - Install ACFS after patching
#    dpham       07/15/09 - XbranchMerge dpham_bug-8664938 from main
#    dpham       07/09/09 - wait for crs to start
#    ksviswan    04/20/09 - Creation



use strict;
use English;
use File::Spec::Functions;

use crsconfig_lib;


sub Getcrsconfig
{

}

sub Getdbconfig
{

}

sub Stopdbhomeres
{
   my $home = $_[0];
   my $sihadb = $_[1];
   my $ohown = $_[2];
   my $srvctlbin    = catfile ($home, "bin", "srvctl");
   my $nodename = $CFG->HOST; 
   my $success = SUCCESS;
   my $cmd;
   my @output;
   my $status;

   my $stfile = catfile ($home, "srvm", "admin", "stophome.txt");
   $ENV{ORACLE_HOME} = $home;

   #Bug 10226636
   if (-e $stfile) {
      unlink $stfile;
   }
   
   if ( ! $sihadb) {
      $cmd = "$srvctlbin stop home -o $home -s $stfile -n $nodename";
   } else {
      $cmd = "$srvctlbin stop home -o $home -s $stfile";
   }

   $status = run_as_user2($ohown, \@output, $cmd );

   trace("$cmd output is @output");

   if ($status != 0) {
     error("Failed to stop resources from  database home $home");
     $success = FAILED;
   } else {
     trace("Stopped resources from datbase home $home");
  }
  return $success;
}

sub Statusdbhomeres
{
   my $home = $_[0];
   my $sihadb = $_[1];
   my $nodename = $_[2];
   my $ohown = $_[3];
   my $srvctlbin    = catfile ($home, "bin", "srvctl");
   my $success;
   my $cmd;
   my @output;
   my $status;

   my $stfile = catfile ($home, "srvm", "admin", "stathome.txt");
   $ENV{ORACLE_HOME} = $home;

   #Bug 10226636
   if (-e $stfile) {
      unlink $stfile;
   }

   if ( ! $sihadb) {
      $cmd = "$srvctlbin status home -o $home -s $stfile -n $nodename";
   } else {
      $cmd = "$srvctlbin status home -o $home -s $stfile";
   }

   $status = run_as_user2($ohown, \@output, $cmd );

   trace("$cmd output is @output");
  
   unlink ($stfile);
   return ($status, @output);
}

sub Stopcrshomeres
{

}

sub Stopcrs
{

}

sub Instantiatepatchfiles
{
   #TODO - Should we just rely on crsconfig_params or
   #should we derive the critical values.
   instantiate_scripts ();

   my @crsconfig_dirs = read_file (catfile ($ORA_CRS_HOME, 'crs', 'utl', 
                                            'crsconfig_dirs'));
   create_dirs (\@crsconfig_dirs);

   copy_wrapper_scripts ();

   my @crsconfig_fileperms = read_file (catfile ($ORA_CRS_HOME, 'crs', 'utl', 
                                                  "crsconfig_fileperms"));
   set_file_perms (\@crsconfig_fileperms);

   #set the ownership/permission of olr
   if ($CFG->IS_SIHA) {
     s_set_ownergroup ($ORACLE_OWNER, $ORA_DBA_GROUP, $CFG->OLR_LOCATION);
   } else {
     s_set_ownergroup ($CFG->SUPERUSER, $ORA_DBA_GROUP, $CFG->OLR_LOCATION);
   }
   s_set_perms ("0600", $CFG->OLR_LOCATION);

   #copy init.ohad,ohasd to init/rc dirs
   s_register_service("ohasd");
}

sub StartCRS
{
   my $rc;
   # Validate system command
   ValidateCRSCTL || return $FAILED;
   trace("Starting Oracle Clusterware");
   $rc = system ("$CRSCTL start crs"); 

   if (!wait_for_stack_start(36)) { exit 1; }
}

sub StartHA
{
   my $rc;
   # Validate system command
   ValidateCRSCTL || return $FAILED;
   trace("Starting Oracle Restart");
   $rc = system ("$CRSCTL start has");

    # Check if the service/daemon has started
    trace ("Checking ohasd");
    my $ohasd_running = check_service ("ohasd", 24);

    if ($ohasd_running) {
      trace ("ohasd started successfully");
    } else {
      error ("Timed out waiting for ohasd to start.");
      exit 1;
    }
}
sub Startcrshomeres
{

}

sub Startdbhomeres
{
   my $home = $_[0];
   my $sihadb = $_[1];
   my $ohown = $_[2];

   my $srvctlbin    = catfile ($home, "bin", "srvctl");
   my $nodename = $CFG->HOST; 
   my $success = SUCCESS;
   my $cmd;
   my @output;
   my $status;

   my $stfile = catfile ($home, "srvm", "admin", "stophome.txt");

   if ( ! $sihadb ) {
      $cmd = "$srvctlbin start home -o $home -s $stfile -n $nodename";
   } else {
       $cmd = "$srvctlbin start home -o $home -s $stfile";
   }

   $ENV{ORACLE_HOME} = $home;
   $status = run_as_user2($ohown, \@output, $cmd );

   trace("$cmd output is @output");

   if ($status != 0) {
     error("Failed to start resources from  database home $home");
     $success = FAILED;
   } else {
     trace("Started resources from datbase home $home");
  }

  unlink ($stfile);  
  return $success;
}

######################################################################
#                       M A I N                                      #
######################################################################
sub CRSPatch
{
 
   my $nostrtflg = $_[0];

   trace ("Patching Oracle Clusterware");
   trace ("norestart flag is set to $nostrtflg");

   #Instantiate the patched files.
   Instantiatepatchfiles ();
  
   installUSMDriver();

   if (! $nostrtflg) {
      StartCRS();  
   }
}

sub CRSPatchhome
{
   my $destcrshome = $_[0];
   my $actcrshome  = s_get_olr_file ("crs_home");

   stopClusterware($actcrshome, "crs");


   #update olr
   s_validate_olrconfig($CFG->OLR_LOCATION, $destcrshome);
   
   CRSPatch ();
   
}

sub HAPatch
{

   my $nostrtflg = $_[0];
   trace ("Patching Oracle Restart");

   trace ("norestart flag is set to $nostrtflg");

   #Instantiate the patched files.
   Instantiatepatchfiles ();

   installUSMDriver();

   if (! $nostrtflg) {
     StartHA();
   }
}
1;