<commit_before>###############################################################################
##
## Description:
##  Push and execute kdump_config.sh, kdump_execute.sh, kdump_result.sh in VM
##  Trigger kdump successfully and get vmcore
##
###############################################################################
##
## Revision:
## V1.0 - boyang - 01/18/2017 - Build script
## V1.1 - boyang - 02/13/2017 - Remove kdump_result.sh
## V1.2 - boyang - 02/14/2107 - Cancle trigger kdump with at command
## V1.3 - boyang - 02/22/2017 - Check vmcore function into while
## V1.4 - boyang - 02/28/2017 - Send and execute kdump_execute.sh in while
## V1.5 - boyang - 02/03-2017 - Call WaitForVMSSHReady and Remove V1.4
##
###############################################################################

<#
.Synopsis
    Trigger target VM kdump.

.Description
    Trigger target VM kdump based on different cases.

.Parameter vmName
    Name of the test VM.

.Parameter hvServer
    Name of the VIServer hosting the VM.

.Parameter testParams
    Semicolon separated list of test parameters.
#>

param([String] $vmName, [String] $hvServer, [String] $testParams)

#
# Checking the input arguments
#
if (-not $vmName)
{
    "FAIL: VM name cannot be null!"
    exit
}

if (-not $hvServer)
{
    "FAIL: hvServer cannot be null!"
    exit
}

if (-not $testParams)
{
    Throw "FAIL: No test parameters specified"
}

#
# Display the test parameters so they are captured in the log file
#
"TestParams : '${testParams}'"

#
# Parse the test parameters
#
$rootDir = $null
$sshKey = $null
$ipv4 = $null
$crashkernel = $null
$logdir = $null
$nmi = $null
$tname = $null

$params = $testParams.Split(";")
foreach ($p in $params) 
{
	$fields = $p.Split("=")
	switch ($fields[0].Trim()) 
	{
		"rootDir"		{ $rootDir = $fields[1].Trim() }
		"sshKey"		{ $sshKey = $fields[1].Trim() }
		"ipv4"			{ $ipv4 = $fields[1].Trim() }
		"crashkernel"	{ $crashkernel = $fields[1].Trim() }
		"TestLogDir"	{ $logdir = $fields[1].Trim()}
		"NMI"			{ $nmi = $fields[1].Trim()}
		"TName"			{ $tname = $fields[1].Trim()}		
		default			{}
    }
}

#
# Check all parameters are valid
#
if (-not $rootDir)
{
	"Warn : no rootdir was specified"
}
else
{
	if ( (Test-Path -Path "${rootDir}") )
	{
		cd $rootDir
	}
	else
	{
		"Warn : rootdir '${rootDir}' does not exist"
	}
}

if ($null -eq $sshKey) 
{
	"FAIL: Test parameter sshKey was not specified"
	return $False
}

if ($null -eq $ipv4) 
{
	"FAIL: Test parameter ipv4 was not specified"
	return $False
}

if ($null -eq $crashkernel)
{
	"FAIL: Test parameter crashkernel was not specified"
	return $False
}

if ($null -eq $logdir)
{
	"FAIL: Test parameter logdir was not specified"
	return $False
}

# NMI is not supports now

if ($null -eq $tname)
{
	"FAIL: Test parameter tname was not specified"
	return $False
}

#
# Source tcutils.ps1
#
. .\setupscripts\tcutils.ps1
PowerCLIImport
ConnectToVIServer $env:ENVVISIPADDR `
                  $env:ENVVISUSERNAME `
                  $env:ENVVISPASSWORD `
                  $env:ENVVISPROTOCOL

###############################################################################
#
# Main Body
#
###############################################################################

#
# SendFileToVM: send kdump_config.sh script to VM for configuring kdump.conf and grub.conf
#
Write-Output "Start to send kdump_config.sh to VM."
Write-Host -F Cyan "kdump.ps1: Start to send kdump_config.sh to VM......."
$retVal = SendFileToVM $ipv4 $sshKey ".\remote-scripts\kdump_config.sh" "/root/kdump_config.sh"
if (-not $retVal)
{
	Write-Output "FAIL: Failed to send kdump_config.sh to VM, and retVal is $retVal."
	Write-Host -F Red "kdump.ps1: FAIL: Failed to send kdump_config.sh to VM, and retVal is $retVal........"
	return $Failed
}
else
{
	Write-Output "PASS: Send kdump_config.sh to VM."
	Write-Host -F Green "kdump.ps1: PASS: Send kdump_config.sh to VM......."
	$retVal = $Passed
}

#
# SendCommandToVM: execute kdump_config.sh
#
Write-Output "Start to execute kdump_config.sh in VM."
Write-Host -F Cyan "kdump.ps1: Start to execute kdump_config.sh in VM......."
$retVal = SendCommandToVM $ipv4 $sshKey "cd /root && dos2unix kdump_config.sh && chmod u+x kdump_config.sh && ./kdump_config.sh $crashkernel"
#$retVal = .\bin\plink -i ssh\${sshKey} root@${ipv4} "cd /root && dos2unix kdump_config.sh && chmod u+x kdump_config.sh && ./kdump_config.sh $crashkernel"
if (-not $retVal)
{
	Write-Output "FAIL: Failed to execute kdump_config.sh in VM, and retVal is $retVal."
	Write-Host -F Red "kdump.ps1: FAIL: Failed to execute kdump_config.sh to VM, and retVal is $retVal......."
	
	#
	# Debug: GetFileFromVM: get kdump_config.summary to local
	#
	$retVal = GetFileFromVM $ipv4 $sshKey "/root/summary.log" "${tname}_fail_kdump_config.log"
	
	return $Failed
}
else
{
	Write-Output "PASS: Execute kdump_config.sh in VM."
	Write-Host -F Green "kdump.ps1: PASS: Execute kdump_config.sh in VM......."
	
	#
	# Debug: GetFileFromVM: get kdump_config.summary to local
	#
	GetFileFromVM $ipv4 $sshKey "/root/summary.log" "${logdir}/${tname}_pass_kdump_config.log"
	
	$retVal = $Passed
}

#<commit_msg>Remove SendCommandToVM and replace with plink<commit_after>###############################################################################
##
## Description:
##  Push and execute kdump_config.sh, kdump_execute.sh, kdump_result.sh in VM
##  Trigger kdump successfully and get vmcore
##
###############################################################################
##
## Revision:
## V1.0 - boyang - 01/18/2017 - Build script
## V1.1 - boyang - 02/13/2017 - Remove kdump_result.sh
## V1.2 - boyang - 02/14/2107 - Cancle trigger kdump with at command
## V1.3 - boyang - 02/22/2017 - Check vmcore function into while
## V1.4 - boyang - 02/28/2017 - Send and execute kdump_execute.sh in while
## V1.5 - boyang - 02/03-2017 - Call WaitForVMSSHReady and Remove V1.4
##
###############################################################################

<#
.Synopsis
    Trigger target VM kdump.

.Description
    Trigger target VM kdump based on different cases.

.Parameter vmName
    Name of the test VM.

.Parameter hvServer
    Name of the VIServer hosting the VM.

.Parameter testParams
    Semicolon separated list of test parameters.
#>

param([String] $vmName, [String] $hvServer, [String] $testParams)

#
# Checking the input arguments
#
if (-not $vmName)
{
    "FAIL: VM name cannot be null!"
    exit
}

if (-not $hvServer)
{
    "FAIL: hvServer cannot be null!"
    exit
}

if (-not $testParams)
{
    Throw "FAIL: No test parameters specified"
}

#
# Display the test parameters so they are captured in the log file
#
"TestParams : '${testParams}'"

#
# Parse the test parameters
#
$rootDir = $null
$sshKey = $null
$ipv4 = $null
$crashkernel = $null
$logdir = $null
$nmi = $null
$tname = $null

$params = $testParams.Split(";")
foreach ($p in $params) 
{
	$fields = $p.Split("=")
	switch ($fields[0].Trim()) 
	{
		"rootDir"		{ $rootDir = $fields[1].Trim() }
		"sshKey"		{ $sshKey = $fields[1].Trim() }
		"ipv4"			{ $ipv4 = $fields[1].Trim() }
		"crashkernel"	{ $crashkernel = $fields[1].Trim() }
		"TestLogDir"	{ $logdir = $fields[1].Trim()}
		"NMI"			{ $nmi = $fields[1].Trim()}
		"TName"			{ $tname = $fields[1].Trim()}		
		default			{}
    }
}

#
# Check all parameters are valid
#
if (-not $rootDir)
{
	"Warn : no rootdir was specified"
}
else
{
	if ( (Test-Path -Path "${rootDir}") )
	{
		cd $rootDir
	}
	else
	{
		"Warn : rootdir '${rootDir}' does not exist"
	}
}

if ($null -eq $sshKey) 
{
	"FAIL: Test parameter sshKey was not specified"
	return $False
}

if ($null -eq $ipv4) 
{
	"FAIL: Test parameter ipv4 was not specified"
	return $False
}

if ($null -eq $crashkernel)
{
	"FAIL: Test parameter crashkernel was not specified"
	return $False
}

if ($null -eq $logdir)
{
	"FAIL: Test parameter logdir was not specified"
	return $False
}

# NMI is not supports now

if ($null -eq $tname)
{
	"FAIL: Test parameter tname was not specified"
	return $False
}

#
# Source tcutils.ps1
#
. .\setupscripts\tcutils.ps1
PowerCLIImport
ConnectToVIServer $env:ENVVISIPADDR `
                  $env:ENVVISUSERNAME `
                  $env:ENVVISPASSWORD `
                  $env:ENVVISPROTOCOL

###############################################################################
#
# Main Body
#
###############################################################################

#
# SendFileToVM: send kdump_config.sh script to VM for configuring kdump.conf and grub.conf
#
Write-Output "Start to send kdump_config.sh to VM."
Write-Host -F Cyan "kdump.ps1: Start to send kdump_config.sh to VM......."
$retVal = SendFileToVM $ipv4 $sshKey ".\remote-scripts\kdump_config.sh" "/root/kdump_config.sh"
if (-not $retVal)
{
	Write-Output "FAIL: Failed to send kdump_config.sh to VM, and retVal is $retVal."
	Write-Host -F Red "kdump.ps1: FAIL: Failed to send kdump_config.sh to VM, and retVal is $retVal........"
	return $Failed
}
else
{
	Write-Output "PASS: Send kdump_config.sh to VM."
	Write-Host -F Green "kdump.ps1: PASS: Send kdump_config.sh to VM......."
	$retVal = $Passed
}

#
# SendCommandToVM: execute kdump_config.sh
#
Write-Output "Start to execute kdump_config.sh in VM."
Write-Host -F Cyan "kdump.ps1: Start to execute kdump_config.sh in VM......."
#$retVal = SendCommandToVM $ipv4 $sshKey "cd /root && dos2unix kdump_config.sh && chmod u+x kdump_config.sh && ./kdump_config.sh $crashkernel"
$retVal = .\bin\plink -i ssh\${sshKey} root@${ipv4} "cd /root && dos2unix kdump_config.sh && chmod u+x kdump_config.sh && ./kdump_config.sh $crashkernel"
if (-not $retVal)
{
	Write-Output "FAIL: Failed to execute kdump_config.sh in VM, and retVal is $retVal."
	Write-Host -F Red "kdump.ps1: FAIL: Failed to execute kdump_config.sh to VM, and retVal is $retVal......."
	
	#
	# Debug: GetFileFromVM: get kdump_config.summary to local
	#
	$retVal = GetFileFromVM $ipv4 $sshKey "/root/summary.log" "${tname}_fail_kdump_config.log"
	
	return $Failed
}
else
{
	Write-Output "PASS: Execute kdump_config.sh in VM."
	Write-Host -F Green "kdump.ps1: PASS: Execute kdump_config.sh in VM......."
	
	#
	# Debug: GetFileFromVM: get kdump_config.summary to local
	#
	GetFileFromVM $ipv4 $sshKey "/root/summary.log" "${logdir}/${tname}_pass_kdump_config.log"
	
	$retVal = $Passed
}

#
{'fraction_non_alphanumeric': 0.21030126336248786, 'fraction_numerical': 0.01749271137026239, 'mean_word_length': 3.746771217712177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1420812', 'n_tokens_mistral': 3832, 'n_tokens_neox': 3653, 'n_words': 1035}