#!/usr/bin/perl
## @file appUpgrade
# @brief It upgrades applications defined in associated deployment.xml
#        file to the target LinuxBase server.
# @details All required for upgrade data is located in the deployment.xml
#          file. Successful operation returns 0, otherwise 1 will be returned.
# @param hostname - target host name to deploy (key parameter to find the data
#                   related to the deployment operation).
# @param IP - ip-address of the target host
# @author Kate Bereznyakova
# @date June 2008
# @version 1.00 - Initial version
# @version 1.01 - Two colons were removed before shared functions and variables.
# @version 1.10 - Upgrade operation status update is changed.
# @version 1.11 - Upgrade operation status update is changed (update status before each return command).
# @version 1.12 - Updated error section checking
# @version 1.13 - Updated log records with hostname and IP information.
# @version 1.14 - Get deployment report/logs step is added.
#                 "/error" and "errorMsg" replaced with variables.

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

use strict;
use warnings;
use baselog;
use base_deploy;
use common_variables;
use Getopt::Long;
use CommonInstallUtils;

## @skipline
sub appUpgrade();
exit(appUpgrade());

###############################################################################
## @fn appUpgrade()
# @brief   Main function. It checks input parameters and implements general
#          logic.
# @details
# @param   $hostname - target hostname to deploy
# @param   IP - ip-address of the target host
# @return  $OK (0)    -  deployment succeed
# @return  $ERROR (1) -  deployment failed
# @author  Kate Bereznyakova
# @date    June 2008
sub appUpgrade()
{
   my $hostname = undef;
   my $ip       = undef;

   GetOptions("hostname:s" => \$hostname,
              "IP:s"       => \$ip,);

   my $retval = $OK;

   # check input parameters section.
   if ((!defined $hostname) or ($hostname eq ''))
   {
      baselog($LOG_ERROR, 'Hostname is undefined.');
      return $ERROR;
   }

   my $cfg_file = get_deploy_cfg_path($hostname);

   if ((!defined $ip) or ($ip eq ''))
   {
      baselog($LOG_ERROR, 'TLAN is undefined.');
      update_section($cfg_file, $DEPERRORPATH, 'UNPA');
      reset_status($cfg_file);
      return $ERROR;
   } ## end if ((!defined $ip) or ...

   baselog($LOG_INFO, "$hostname, $ip. Starting upgrade operation.");

   if (!validateIP($ip))
   {
      baselog($LOG_ERROR, "Invalid TLAN IP data: $ip.");
      update_section($cfg_file, $DEPERRORPATH, 'INVIP');
      reset_status($cfg_file);
      return $ERROR;
   } ## end if (!validateIP($ip))

   # end: check input parameters section.

   my $deployMng = base_deploy::new($hostname, $ip);
   if (!defined $deployMng)
   {
      baselog($LOG_ERROR, 'Cannot create deploy object.');
      update_section($cfg_file, $DEPERRORPATH,    'PREP');
      update_section($cfg_file, $DEPERRORMSGPATH, 'Cannot create deploy object.');
      reset_status($cfg_file);
      return $ERROR;
   } ## end if (!defined $deployMng...

   #log directory exists. Need to remove old logs first.
   $deployMng->cleanup_logs_dir();

   if ($deployMng->prepare_deploy_data())
   {
      if (!$deployMng->update_deploy_status('Upgrade is in progress'))
      {
         baselog($LOG_ERROR, 'Cannot update status in the deployment.xml file.');
      }

      baselog($LOG_DEBUG, 'Calling upgrade command.');
      my $deploy_call = $deployMng->call_remote_cmd("$RMT_UPGRADE_CMD -hostname $hostname");
      if ((!defined $deploy_call) or ($deploy_call ne $OK))
      {

         # to avoid loosing error in case of local operation
         # or previous error, check the tag before updating
         # if operation local and tag is empty then it is possible
         # that corresponding script was not triggered as well.
         my $temp_error = get_error_code($deployMng->{'deploymentCfg'});
         if ((defined $temp_error) and ($temp_error eq ''))
         {
            update_section($cfg_file, $DEPERRORPATH, 'RSWF');
         }

         baselog($LOG_ERROR, 'Remote upgrade failed.');
         $retval = $ERROR;
      } ## end if ((!defined $deploy_call...

      if (!$deployMng->retrieve_deploy_xml())
      {
         update_section($cfg_file, $DEPERRORMSGPATH, 'Cannot synchronize status.');
         baselog($LOG_ERROR, 'Fail to get remote deployment.xml file.');
         $retval = $ERROR;
      } ## end if (!$deployMng->retrieve_deploy_xml...
      $deployMng->get_deploy_logs();
   } ## end if ($deployMng->prepare_deploy_data...
   else
   {
      $retval = $ERROR;
      update_section($cfg_file, $DEPERRORPATH, 'PREDEPF');
   }
   if (base_deploy::clean_up($hostname))
   {
      baselog($LOG_INFO, 'Deployment directory was cleared');
   }
   else
   {
      baselog($LOG_ERROR, 'Cannot clear deployment directory');
   }
   undef $deployMng;

   # check if operation status set correctly
   reset_status($cfg_file);

   return $retval;
} ## end sub appUpgrade()
