#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)98   1.9.1.8   src/csm/install/nodestatus.perl, setup, csm_rfish, rfishs001b 11/10/05 03:39:21
#------------------------------------------------------------------------------------
# This script is basically invoked by DMSRM action 'NodeInstallMessage" during
# a node's install process. At this point this scipt supports three different operations
# that are required by node(s) during install process.
# The three keys that corresponds to each operations are
# 1 -  change boot order to hard disk for the given node
# 2 -  write iinstall status message to the corresponding node's status file  ex /csminstall/csm/status/node2.clusters.com
# 3 -  minimally managed node install completion.
#--------------------------Include Libraries------------------------------------------
use strict;
BEGIN { $::csmpm = $ENV{'CSM_PM'} ? $ENV{'CSM_PM'} : '/opt/csm/pm'; }
use lib $::csmpm;
use locale;
use Getopt::Std;
use CSMDefs;
use ServerUtils;
use NodesetUtils;
use NodeUtils;
use NetworkUtils;
use Fcntl qw(:DEFAULT :flock);

# set local scope because we only want classes on the mgmt svr
$ENV{'CT_MANAGEMENT_SCOPE'} = 1;

# todo: remove when lsrsrc-api converts to above
$ENV{'CT_SESSION_SCOPE'} = 1;

$::MSGCAT     = 'csmInstall.cat';
$::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : '/opt/csm/msgmaps';
$::MSGSET     = 'csminstall';
$::HOST_RECORD_FILE = "nodemsg_requests";

# get definitions based on the attributes of the management server
%::msOSDefs = ServerUtils->get_OSDefs();

my $key;
my $hostname;
my $messageString;

#-------------------------Start Of Subroutines-----------------------------------------

sub usage
{
	print <<ENDOFUSAGETEXT;
Usage:  nodestatus [-h]
	nodestatus [-v | -V] { -p  <parameter> } 
ENDOFUSAGETEXT
	exit 1;
}

sub getArgs
{

	#if (! getopts('hvV') ) { &usage; }
	my @commandArgs = @ARGV;
	print("Command args:@commandArgs\n");
	$key           = $commandArgs[0];
	$hostname      = $commandArgs[1];
	$messageString = $commandArgs[2];
	my $lastIndex = $#commandArgs;
	for (my $index = 3 ; $index <= $lastIndex ; $index++)
	{
		$messageString .= " " . $commandArgs[$index];
	}
}

#--------------------------------Start Of Main---------------------------------------------

#key options
# 1 -  change boot order
# 2 -  write status
# 3 -  minimally managed node install completion.
# 4 -  hw maintenance message

# start logging
MessageUtils->append_logging($::INSTALLNODE_LOG);

&getArgs();

if ($key eq "1")
{
	my $recordFile = $::CSMSTATUSDIR . "/" . $::HOST_RECORD_FILE;
	open(RECORD_FILE, ">>$recordFile")
	  or die
      MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 
                                   'csminstall',     'E1', 
                                   'EMsgCANT_WRITE_FILE', $recordFile
                                  ); 
	flock(RECORD_FILE, LOCK_EX) or die
    MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 
                                 'csminstall',     'E1', 
                                 'EMsgCANT_LOCK_FILE', $recordFile
                                 ); 
	print RECORD_FILE "$hostname\n";
	close(RECORD_FILE);

}
elsif ($key eq "2")
{
	if ($messageString)
	{

		#my $command = "/csminstall/csm/write_status -n $hostname $messageString" ;
		my ($statusFlag, $msg) = split(':', $messageString);
		my $command =
		  "/csminstall/csm/write_status -n $hostname $statusFlag $msg";
		my $rc = system("$command") >> 8;

		if ($rc != 0)
		{
			MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
								 'csminstall', "E", "EMsgNO_STATUS", $hostname);
		}
		else
		{
			MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
					'csminstall', "I", "IMsgWROTE_STATUS_MSG", $msg, $hostname);
		}
	}
}
elsif ($key eq "3")
{

	#handle minimally managed node install completion
	NodeUtils->runcmd("/opt/csm/csmbin/gatherSSHHostkeys $hostname", 0);
	NodeUtils->SetMode($hostname, $::CSMLITE);

	#Now set the AllowManageRequest back to zero
	my %nodehash;
	$nodehash{$hostname} = 1;
	NodeUtils->SetAllowRequest(\%nodehash, '0');
	MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH, 'csminstall',
								 "I", "IMsgMINMANAGED_INSTALLED", $hostname);
}

# stop logging
MessageUtils->stop_logging();

#-------------------------------End Of Main------------------------------------------

