ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Wait-AlkamiServiceFabricNodeStatus.ps1
2023-05-30 22:51:22 -07:00

102 lines
3.6 KiB
PowerShell

function Wait-AlkamiServiceFabricNodeStatus {
<#
.SYNOPSIS
Polls the status of a Service Fabric host until either a timeout occurs or the node reaches the
specified status.
.PARAMETER DesiredStatus
[string] The target status of the Service Fabric node. Must be either 'Disabled' or 'Up'.
.PARAMETER Hostname
[string] The host name of the server in the Service Fabric cluster to poll. If not provided, defaults to localhost.
.PARAMETER TimeoutMinutes
[byte] The length of time in minutes to poll the cluster for a healthy status before declaring that an error
has occurred. Defaults to 30 minutes.
.PARAMETER SleepIntervalSeconds
[byte] The length of time in seconds to pause between cluster status queries. Defaults to 15 seconds.
.PARAMETER SkipHealthCheck
[switch] Flag indicating to skip validation that the node health is 'Ok' in addition to the node status.
.EXAMPLE
Wait-AlkamiServiceFabricNodeStatus -DesiredStatus 'Up' -Hostname 'fab197478'
WARNING: [Find-CertificateByName] Could not find certificate with Common Name staging-fabricadmin.alkamitech.com
[Connect-AlkamiServiceFabricCluster] Connected to Service Fabric cluster at endpoint fab197478:19000
[Wait-AlkamiServiceFabricNodeStatus] : Waiting for Service Fabric node FAB197478 to have status of Up.
[Wait-AlkamiServiceFabricNodeStatus] : Service Fabric node FAB197478 has status of Up after 00:00:00.0607888.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet('Disabled', 'Up')]
[string]$DesiredStatus,
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$Hostname = 'localhost',
[Parameter(Mandatory=$false)]
[byte]$TimeoutMinutes = 30,
[Parameter(Mandatory=$false)]
[ValidateRange(1, 120)]
[byte]$SleepIntervalSeconds = 15,
[Parameter(Mandatory=$false)]
[switch]$SkipHealthCheck
)
$loglead = (Get-LogLeadName)
Connect-AlkamiServiceFabricCluster -Hostname $Hostname
# Resolve the hostname into a service fabric node name (case-sensitive)
if ( $Hostname -eq 'localhost' ) {
$actualHostname = "$env:COMPUTERNAME"
} else {
$actualHostname = $Hostname
}
$curNode = Get-ServiceFabricNode | Where-Object { $_.NodeName -eq $actualHostname } | Select-Object -First 1
if ( ! $curNode ) {
Write-Error "$loglead : Could not retrieve the Service Fabric node for $actualHostname."
return
} else {
$curNodeName = $curNode.NodeName
}
Write-Host "$loglead : Waiting for Service Fabric node $curNodeName to have status of $DesiredStatus."
# Keep looping until the Service Fabric node has the desired status or we timeout.
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
while($true) {
$curNode = Get-ServiceFabricNode -NodeName $curNodeName
if (( $curNode.NodeStatus -eq $DesiredStatus ) -and ( $SkipHealthCheck -or ( 'Ok' -eq $curNode.HealthState ))) {
$stopwatch.Stop()
Write-Host "$logLead : Service Fabric node $curNodeName has status of $DesiredStatus after $($stopwatch.Elapsed.ToString())."
break
} elseif ( $stopwatch.Elapsed.TotalMinutes -gt $timeoutMinutes ) {
$stopwatch.Stop()
Write-Error "$logLead : Timed out waiting for Service Fabric node $curNodeName to be in state $DesiredStatus after $($stopwatch.Elapsed.ToString())."
break
} else {
Start-Sleep -Seconds $sleepIntervalSeconds
}
}
}