#!/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 
#####################################################################
#                                                                   #
# Command: sample_showattrs                                         #
#                                                                   #
#-------------------------------------------------------------------#
#                                                                   #
#      This sample CSM script demonstrates how to read node         #
#      attributes from the config_info file.  These node attributes #
#      match the attributes of the ManagedNode objects.  The script #
#      stores the attributes in global variables with the same      #
#      names as the attributes (i.e. $::ManagementServer,           #
#      $::InstallCSMVersion, $::InstallDistributionName, etc.)      #
#                                                                   #
#      This sample script also demonstrates the use of a data file  #
#      stored in /csminstal/csm/scripts/data.                       #
#                                                                   #
#      This script can be copied to any of the customization script #
#      directories, and will be run during the appropriate phase of #
#      installation.  The customization script directories are:     #
#         /csminstall/csm/scripts/installprereboot                  #
#         /csminstall/csm/scripts/installpostreboot                 #
#         /csminstall/csm/scripts/update                            #
#                                                                   #
#      See the Installation and Planning Guide for details on how   #
#      to use customization scripts.                                #
#                                                                   #
# Exit codes:                                                       #
#     0 - All was successful.                                       #
#     1 - An error occured.                                         #
#                                                                   #
#                                                                   #
#####################################################################

use strict;

#-------------------------------------------------------------------#
#   MAIN Main main section of code
#-------------------------------------------------------------------#
#-------------------------------------------------------------------#
# Global Variables                                                  #
#-------------------------------------------------------------------#
$::GLOBAL_EXIT = 0;
$::NOK         = 1;
$::OK          = 0;

# Directory names used by CSM

# The CSMMOUNTPOINT environment variable is set by the CSM script
#    that calls this script
#
$::CSMMNTDIR = $ENV{'CSMMOUNTPOINT'};

$::CSMCFGDIR      = $::CSMMNTDIR . "/csm/config";         # config_info files
$::SCRIPTSDIR     = $::CSMMNTDIR . "/csm/scripts";        # top level script dir
$::SCRIPTDATADIR  = $::SCRIPTSDIR . "/data";              # script data files
$::PREINSTALLDIR  = $::SCRIPTSDIR . "/installprereboot";  # prereboot scripts
$::POSTINSTALLDIR = $::SCRIPTSDIR . "/installpostreboot"; # postreboot scripts
$::UPDATEDIR      = $::SCRIPTSDIR . "/update";            # updatenode scripts

$::map_file = "$::CSMCFGDIR/nodemap";    # Nodename mapping file.
                                         # Maps node's hostname to node name
                                         # as known on the management server.
my $akb_hostname = "";                   # Name of this node as known by
                                         # the management server

#
# Determine the hostname of the node as it is known by the management server.
# This may be different than the local hostname of the node.
# This sets $::akb_hostname which is used by get_config_info().
#
if (&get_akb_hostname != 0)
{
	print
	  "Error:  Cannot determine this node's hostname as known by the management server.\n";
	$::GLOBAL_EXIT = 1;
	exit;
}
$akb_hostname = $::akb_hostname;

#
# Get some attributes about this node from the config_info file
#
if (&get_config_info($akb_hostname) != 0)
{
	print
	  "Error:  Could not get node attribute values from the node config_info file.\n";
	$::GLOBAL_EXIT = 1;
	exit;
}

#
# Display all the attributes that we know about
#
print "ManagementServer           = $::ManagementServer\n";
print "Hostname                   = $::Hostname\n";
print "InstallCSMVersion          = $::InstallCSMVersion\n";
print "InstallOSName              = $::InstallOSName\n";
print "InstallDistributionName    = $::InstallDistributionName\n";
print "InstallDistributionVersion = $::InstallDistributionVersion\n";
print "InstallPkgArchitecture     = $::InstallPkgArchitecture\n";
print "ConsoleSerialDevice        = $::ConsoleSerialDevice\n";
print "RemoteShell                = $::RemoteShell\n";
print "SetupRemoteShell           = $::SetupRemoteShell\n";
print "InstallServer              = $::InstallServer\n";

#
# Now read some data from the scripts/data directory
#
my $datafile = "$::SCRIPTDATADIR/mydata";  # /csminstall/csm/scripts/data/mydata
if (-f $datafile)
{
	print "\nReading $datafile:\n";
	unless (open(DATAFILE, "<$datafile"))
	{

		# Couldn't open $datafile
		print "Error:  Cannot open file $datafile for reading.\n";
		$::GLOBAL_EXIT = 1;
		exit;
	}
	while (<DATAFILE>)
	{
		print "$_";
	}
}

#
# finish up and exit
#
END
{

	#Determine exit code
	if ($::GLOBAL_EXIT > $?)
	{
		$? = $::GLOBAL_EXIT;
	}
}

exit;    # end of Main

#-------------------------------------------------------------------------#
#   get_akb_hostname - Get the hostname of this node as it is known by    #
#                       the management server. Do this by checking the    #
#                       nodemap file which was created in the             #
#                       /csminstall/csm/config directory.                 #
#                                                                         #
#                       Each line of the file is in the format:           #
#                           csm_hostname<space>node_hostname              #
#                                                                         #
#   Return Codes: 0 - All was successful.                                 #
#                 1 - An error occured.                                   #
#--------------------------------------#----------------------------------#

sub get_akb_hostname
{

	my ($akb_hn, $my_hn);
	my $platform = `uname`;
	my $hostname;

	chomp $platform;

	# get our hostname
	if ($platform eq "AIX")
	{
		$hostname = `hostname`;
	}
	elsif ($platform eq "Linux")
	{
		$hostname = `hostname -f`;
	}
	else
	{
		return $::NOK;
	}
	chomp $hostname;

	my $match = 0;

	# check the mapfile for the hostname as known by the management server
	if (-f $::map_file)
	{
		unless (open(MAPFILE, "<$::map_file"))
		{

			# print error message
			print "Error:  Cannot read file $::map_file\n";
			return $::NOK;
		}

		while (<MAPFILE>)
		{
			($akb_hn, $my_hn) = split(' ');
			if ($my_hn eq $hostname)
			{
				$::akb_hostname = $akb_hn;
				chomp $::akb_hostname;
				$match++;
				last;
			}
		}
		if (!$match)
		{

			# Could not find hostname in nodemap file.
			print "Error:  Could not find $hostname in $::map_file\n";
			return $::NOK;
		}
		close(MAPFILE);
	}
	else
	{
		print "Error:  Cannot read file $::map_file\n";
		return $::NOK;
	}
	return $::OK;
}

#-------------------------------------------------------------------------#
#   get_config_info - Get required information out of the config_info     #
#                     file. This file contains attributes and values for  #
#                     the node we are running on.                         #
#                                                                         #
#   Return Codes: 0 - All was successful.                                 #
#                 1 - An error occured.                                   #
#--------------------------------------#----------------------------------#

sub get_config_info
{

	my ($akb_hostname) = @_;
	my ($attr, $value);
	my $cfg_file = "$::CSMCFGDIR/$akb_hostname.config_info";

	# /csminstall/csm/config/<nodename>.config_info

	chomp $cfg_file;

	my $match_svr  = 0;
	my $match_node = 0;
	my $match_vers = 0;

	# get attribute values from the config_info file
	if (-f $cfg_file)
	{
		unless (open(CFGINFO, "<$cfg_file"))
		{

			# Couldn't open $cfg_file
			print "Error:  Cannot open file $cfg_file for reading.\n";
			return $::NOK;
		}

		while (<CFGINFO>)
		{
			($attr, $value) = split('=');

			if ($attr eq "ManagementServer")
			{
				$match_svr++;
				$::ManagementServer = $value;
				chomp $::ManagementServer;
			}
			if ($attr eq "Hostname")
			{    # Hostname as known on the MS
				$match_node++;
				$::Hostname = $value;
				chomp $::Hostname;
			}
			if ($attr eq "InstallCSMVersion")
			{
				$match_vers++;
				$::InstallCSMVersion = $value;
				chomp $::InstallCSMVersion;
			}
			if ($attr eq "InstallOSName")
			{
				$::InstallOSName = $value;
				chomp $::InstallOSName;
			}
			if ($attr eq "InstallDistributionName")
			{
				$::InstallDistributionName = $value;
				chomp $::InstallDistributionName;
			}
			if ($attr eq "InstallDistributionVersion")
			{
				$::InstallDistributionVersion = $value;
				chomp $::InstallDistributionVersion;
			}
			if ($attr eq "InstallPkgArchitecture")
			{
				$::InstallPkgArchitecture = $value;
				chomp $::InstallPkgArchitecture;
			}
			if ($attr eq "ConsoleSerialDevice")
			{
				$::ConsoleSerialDevice = $value;
				chomp $::ConsoleSerialDevice;
			}
			if ($attr eq "RemoteShell")
			{
				$::RemoteShell = $value;
				chomp $::RemoteShell;
			}
			if ($attr eq "SetupRemoteShell")
			{
				$::SetupRemoteShell = $value;
				chomp $::SetupRemoteShell;
			}
			if ($attr eq "InstallServer")
			{
				$::InstallServer = $value;
				chomp $::InstallServer;
			}
		}

		# Check a few attributes to make sure we got something
		if (!$match_svr || !$match_vers || !$match_node)
		{

			# Could not find attribute values in config_info file.
			print "Error:  Could not find attribute values in $cfg_file\n";
			return $::NOK;
		}
		close(CFGINFO);
	}
	else
	{

		# $cfg_file does not exist.
		print "Error:  $cfg_file does not exist\n";
		return $::NOK;
	}
	return $::OK;
}
