# online
#
# Copyright(C) 2003 Sybase Inc.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
#
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF SYBASE INC.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF SYBASE INC.
#
#

#
# make the script work under any of csh, sh or perl
#
eval '(exit $?0)' && eval 'exec /opt/VRTSvcs/bin/perl5 -S $0 ${1+"$@"}'
& eval 'exec /opt/VRTSvcs/bin/perl5 -S $0 $argv:q'
	if 0;

# This script onlines an HAase instance. If the database is already
# running, it does nothing.
#
use sybhautil; 
($res_name,$Sybase_home,$Dataserver_name,$Backupserver_name,$Monserver_name,$Textserver_name,$Secondary_companion_name,$Dataserver_login_file,$RUN_server_file,$Thorough_probe_cycle,$Thorough_probe_script,$Debug,$Log_max_size,$Failback_strategy) = @ARGV;

my($log_message);
my($ase_status);
my($group);
my($failover)=1;
my($retval);
my($ypcheck);
my($onlinetimeout);
my($auxtimeout);
my($envfile)="";

# initialize environment
init_env();

# log purpose
$log_message = sprintf "VCS:2000100:HAase:$res_name:online:call online.";
system("$HALOG -add C \"$log_message\" -msgid 2000100 -parameters $res_name");

#
# Retrieve the attribute value for OnlineTimeout
#
($retval, $onlinetimeout) = getAttrInfo($res_name, $RES_TYPE, $ONLINETIMEOUT);

#
# Check if the dataserver and its service group is on primary node 
#
($ase_status, $group) = IsASEonPrimary($res_name);

if($ase_status == 1) 
{
	$log_message = sprintf "VCS:2000101:HAase:$res_name:online:HAase service $Dataserver_name is on primary node.";
	system("$HALOG -add C \"$log_message\" -msgid 2000101 -parameters $res_name");

	# initialize RUN file for backup server or monitor server
	($retval, $RUN_srv_file, $RUN_backup_srv_file, $RUN_monitor_srv_file)=init_resource();
	if (!$retval)
	{
		$log_message = sprintf "VCS:156001:HAase:$res_name:online:HAase resource $res_name cannot be initialized, exiting.";
		system("$HALOG -add C \"$log_message\" -msgid 156001 -parameters $res_name");
		exit 1;
	}

	#
	# check if SYBASE.csh or SYBASE.sh exists
	#
	if ($use_cshell)
	{
		$envfile="$Sybase_home/SYBASE.csh";
	}
	else
	{
		$envfile="$Sybase_home/SYBASE.sh";
	}

	if (!(-e $envfile))
	{
		$log_message = sprintf "VCS:2000602:HAase:$res_name:online:$envfile doesn't exist.";
		system("$HALOG -add C \"$log_message\" -msgid 2000602 -parameters $res_name");
		exit 1;
	}

	#
	# Check if the dataserver is already running
	#
	if(IsServerPresent($res_name, $Dataserver_name) != 0) 
	{
		$log_message = sprintf "VCS:2000050:HAase:$res_name:online:HAase service $Dataserver_name is already running";
		system("$HALOG -add C \"$log_message\" -msgid 2000050 -parameters $res_name $Dataserver_name");
		exit 0;
	}

	# 
	# server is not running & has to be started
	# first, make the attribute, 'Critical' of HAase resource to be 1. 
	#
	$retval=changeCritical($res_name, 1);

	$log_message = sprintf "VCS:2000061:HAase:$res_name:online:HAase resource in $group has been set to Critical.";
	system("$HALOG -add C \"$log_message\" -msgid 2000061 -parameters $res_name");

	#
	# Start up the dataserver. If failing, immediately returns.
	# monitor entry will determine if dataserver is online or not.
	#
	($retval, $auxtimeout) = start_dataserver();
	if ($retval != 1)
	{
		exit 1;
	} 

	#
	# Start the auxiliary servers
	#
	start_auxiliary_servers($auxtimeout);

	#
	# Delay first monitor by 10 seconds
	#
	exit 10;
}
elsif ($ase_status == 0) 
{
	$log_message = sprintf "VCS:2000102:HAase:$res_name:online:ASE is on secondary node.";
	system("$HALOG -add C \"$log_message\" -msgid 2000102 -parameters $res_name");

	#
	# first, make the attribute, 'Critical' of HAase resource in the 
	# service group to be 0 so that it won't fail back automatically. 
	#
	$retval=changeCritical($res_name, 0);

	$log_message = sprintf "VCS:2000062:HAase:$res_name:online:HAase resource in $group has been set to non-Critical.";
	system("$HALOG -add C \"$log_message\" -msgid 2000062 -parameters $res_name ");

	$retval=connect_companion($companion, $failover);

	if ($retval==0)
	{
		$log_message = sprintf "VCS:2000104:HAase:$res_name:online:primary server cannot failover to companion server."; 
		system("$HALOG -add C \"$log_message\" -msgid 2000104 -parameters $res_name");
	}

	exit 0;
}
else
{
	$log_message = sprintf "VCS:2000103:HAase:$res_name:online:ASE is in transition or it is an error.";
	system("$HALOG -add C \"$log_message\" -msgid 2000103 -parameters $res_name");

	exit 0;
}

#
# Start up dataserver.
# Return 1 if success, 0 if failure.
#
sub start_dataserver
{
	my($log_message);
	my($ase_start_cmd);
	my($count) = 1;

	if ($use_nis)
	{
		if ($use_cshell)
		{
			if ($Owner ne "root")
			{
				$ase_start_cmd = "su $Owner -c \'source $Sybase_home/SYBASE.csh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &\'";
			}
			else
			{
				$ase_start_cmd = "source $Sybase_home/SYBASE.csh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &";
			}
		}
		else
		{
			if ($Owner ne "root")
			{
				$ase_start_cmd = "su $Owner -c \'. $Sybase_home/SYBASE.sh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &\'";
			}
			else
			{
				$ase_start_cmd = ". $Sybase_home/SYBASE.sh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &";
			}
		}
	}
	else
	{
		if ($use_cshell)
		{
			if ($Owner ne "root")
			{
				$ase_start_cmd = "su $Owner -c \'source $Sybase_home/SYBASE.csh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &\'";
			}
			else
			{
				$ase_start_cmd = "source $Sybase_home/SYBASE.csh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &";
			}
		}
		else
		{
			if ($Owner ne "root")
			{
				$ase_start_cmd = "su $Owner -c \'. $Sybase_home/SYBASE.sh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file & \'";
			}
			else
			{
				$ase_start_cmd = ". $Sybase_home/SYBASE.sh; $Sybase_home/$SYBASE_ASE/install/startserver -f $RUN_srv_file &";
			}
		}
	}

	$log_message = sprintf "VCS:2000309:HAase:$res_name:online:system command=$ase_start_cmd";
	system("$HALOG -add C \"$log_message\" -msgid 2000309 -parameters $res_name");

	# start up the dataserver
	system($ase_start_cmd);

	# check if dataserver is online within $onlinetimeout seconds
	while ( (IsServerPresent($res_name, $Dataserver_name) == 0) && $count < $onlinetimeout)
	{
		sleep 2;
		$count+=2;
		$log_message = sprintf "VCS:2000305:HAase:$res_name:online:not online yet. count=$count";
		system("$HALOG -add C \"$log_message\" -msgid 2000305 -parameters $res_name");
	}

	if ($count >= $onlinetimeout)
	{
		$log_message = sprintf "VCS:2000103:HAase:$res_name:online:online $Dataserver_name dataserver time out.";
		system("$HALOG -add C \"$log_message\" -msgid 2000300 -parameters $res_name");
		return (0, $count);
	}
	else
	{
		$log_message = sprintf "VCS:2000306:HAase:$res_name:online:$Dataserver_name dataserver is online.";
		system("$HALOG -add C \"$log_message\" -msgid 2000306 -parameters $res_name");
		$count = $onlinetimeout - $count; 
		return (1, $count);
	}
}

#
# Start up auxiliary servers: backup and monitor servers.
# For each auxiliary server, the timeout value for the script to wait is the 
# remaining time after dataserver and other auxiliary servers are online within
# the $onlinetimeout limit. 
# Always return 0 whether success or failure. 
#
sub start_auxiliary_servers
{
	my($timeout) = @_;
	my (@server) = ("Backup", "Monitor");
	my (@server_name) = ($Backupserver_name, $Monserver_name);
	my (@RUN_file) = ($RUN_backup_srv_file, $RUN_monitor_srv_file);
	my ($tmptimeout) = 0;
	my ($timeout_increment) = 2;
	my ($index);
	my ($aux_start_cmd);

	for $index (0 .. 1)
	{
		if ((length($RUN_file[$index]) != 0) && (-e $RUN_file[$index])) 
		{
			if(IsServerPresent($res_name,$server_name[$index])==0)
			{
				$log_message = sprintf "VCS:2000401:HAase:$res_name:online:Starting $server[$index] server $server_name[$index] using RUN file $RUN_file[$index].";
				system("$HALOG -add C \"$log_message\" -msgid 20000402 -parameters $res_name");

				#
				# Run command as owner of ASE home directory
				# 
				if ($use_nis == 1)
				{
					if ($use_cshell)
					{
						if ($Owner ne "root")
						{
							$aux_start_cmd = "su $Owner -c \'source $Sybase_home/SYBASE.csh; $RUN_file[$index] &\'";
						}
						else
						{
							$aux_start_cmd = "source $Sybase_home/SYBASE.csh; $RUN_file[$index] &";
						}
					}
					else
					{
						if ($Owner ne "root")
						{	
							$aux_start_cmd = "su $Owner -c \'. $Sybase_home/SYBASE.sh; $RUN_file[$index] & \'";
						}
						else
						{
							$aux_start_cmd = ". $Sybase_home/SYBASE.sh; $RUN_file[$index] &";
						}
					}
				}
				else
				{
					if ($use_cshell)
					{
						if ($Owner ne "root")
						{
							$aux_start_cmd = "su $Owner -c \'source $Sybase_home/SYBASE.csh; $RUN_file[$index] & \'";
						}
						else
						{
							$aux_start_cmd = "source $Sybase_home/SYBASE.csh; $RUN_file[$index] &";
						}
					}
					else
					{
						if ($Owner ne "root")
						{
							$aux_start_cmd = "su $Owner -c \'. $Sybase_home/SYBASE.sh; $RUN_file[$index] & \'";
						}
						else
						{
							$aux_start_cmd = ". $Sybase_home/SYBASE.sh; $RUN_file[$index] &";
						}
					}
				}

				# start the aux servers
				system($aux_start_cmd);

				while ((IsServerPresent($res_name,$server_name[$index])==0) && ($tmptimeout < $timeout))
				{
					sleep $timeout_increment;
					$tmptimeout=$tmptimeout + $timeout_increment;
				}

				if (( $tmptimeout >= $timeout ))
				{
					$log_message = sprintf "VCS:2000402:HAase:$res_name:online:Couldn't start $server[$index] server $server_name[$index] in $timeout seconds.";
					system("$HALOG -add C \"$log_message\" -msgid 2000402 -parameters $res_name");
					return 0;
				}
				else
				{
					$log_message = sprintf "VCS:2000403:HAase:$res_name:online: $server[$index] server $server_name[$index] started successfully.";
					system("$HALOG -add C \"$log_message\" -msgid 2000403 -parameters $res_name");
					$timeout-=$tmptimeout; 
				}
			}
			else
			{
				$log_message = sprintf "VCS:2000404:HAase:$res_name:online: $server[$index] server $server_name[$index] already running.";
				system("$HALOG -add C \"$log_message\" -msgid 2000404 -parameters $res_name");
			}
		}
		else
		{
			if (length($server_name[$index]) > 0)
			{
				$log_message = sprintf "VCS:2000405:HAase:$res_name:online: not starting $server[$index] server $server_name[$index] since RUN file not found.";
				system("$HALOG -add C \"$log_message\" -msgid 2000405 -parameters $res_name");
			}
		}
	}
	return 0;
}

