#!/usr/bin/perl
## @file appDeploy
# @brief It deploys applications defined in associated deployment.xml
#        file to the target LinuxBase server.
# @details All required for deployment 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 March 2008
# @version 1.00 - Initial version
# @version 1.01 - Two colons were removed before shared functions and variables.
# @version 1.10 - Deploy operation status update is changed.
# @version 1.11 - Deploy operation status update is changed (update status before each return command).
# @version 1.12 - update_section function call is updated to keep old data as well.
# @version 1.13 - Updated error section checking
# @version 1.14 - Updated log records with hostname and IP information.
# @version 1.15 - Get deployment report/logs step is added.
#                 "/error" and "errorMsg" replaced with variables.
# @note none

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 appDeploy();
exit(appDeploy());

###############################################################################
## @fn appDeploy()
# @brief   Main function. It checks input parameters and implements general
#          logic.
# @details None.
# @param   $hostname - target hostname to deploy
# @param   IP - ip-address of the target host
# @param   $restore - restore is an optional parameter that defined whether to
#                     use restore data left from system upgrade.
# @return  $OK (0)    -  deployment succeed
# @return  $ERROR (1) -  deployment failed
# @author  Kate Bereznyakova
# @date    March 2008
# @note    none
###############################################################################
sub appDeploy()
{
   my $hostname = undef;
   my $ip       = undef;
   my $restore  = undef;

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

   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 deploy 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('Installation is in progress'))
      {
         baselog($LOG_ERROR, 'Cannot update status in the deployment.xml file.');
      }
      my $deploy_cmd = "$RMT_DEPLOY_CMD -hostname $hostname";
      if ($restore)
      {
         $deploy_cmd .= " -restore";
      }

      baselog($LOG_DEBUG, "Remote command is '$deploy_cmd'.");
      my $deploy_call = $deployMng->call_remote_cmd($deploy_cmd);
      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, 'Deployment failed.');
         $retval = $ERROR;
      } ## end if ((!defined $deploy_call...

      if (!$deployMng->retrieve_deploy_xml())
      {
         baselog($LOG_ERROR, 'Fail to get remote deployment.xml file.');
         update_section($cfg_file, $DEPERRORMSGPATH, 'Cannot synchronize status.', $KEEPOLDCNT);
         $retval = $ERROR;
      } ## end if (!$deployMng->retrieve_deploy_xml...
      $deployMng->get_deploy_logs();
   } ## end if ($deployMng->prepare_deploy_data...
   else
   {
      update_section($cfg_file, $DEPERRORPATH, 'PREDEPF');
      $retval = $ERROR;
   }
   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 appDeploy()

