ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Restart-AlkamiServiceFabricApplications.ps1

72 lines
2.7 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Restart-AlkamiServiceFabricApplications {
<#
.SYNOPSIS
Restarts every microservice in the Service Fabric cluster.
.PARAMETER hostname
A single server hostname from the target Service Fabric cluster.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[string]$hostname = "localhost",
[Parameter(Mandatory = $false)]
[switch]$force
)
$loglead = (Get-LogLeadName);
if(!($force.IsPresent)) {
throw "$loglead Must include -Force. You wouldn't want to run this accidentally, now would you?";
return;
}
# Connect to the SF cluster.
Connect-AlkamiServiceFabricCluster -hostname $hostname | Out-Null;
Write-Host "$loglead Restarting all services on Service Fabric cluster at $hostname";
# Get all of the SF nodes for the environment.
$nodes = Get-ServiceFabricNode;
# Identify the node type (and thus the environment) of the node we are targeting.
if($hostname -eq "localhost") {
$hostname = Get-FullyQualifiedServerName;
}
$hostnameNode = $nodes | Where-Object { ($_.NodeName -like $hostname) -or ($_.IpAddressOrFQDN -like $hostname) } | Select-Object -First 1;
if($null -eq $hostnameNode) {
Write-Error "$loglead Unable to determine an environment/NodeType associated with server $hostname";
return;
}
if($hostnameNode.NodeType -eq "SeedNode") {
Write-Error "$hostname is a Seed Node. Please target a worker node to bounce a specific environment.";
return;
}
$nodeType = $hostnameNode.NodeType;
Write-Verbose "$loglead Restarting services on nodes with type $nodeType";
# Filter the nodes down to the servers in the specific environment, and nodes that are up.
$nodes = $nodes | Where-Object { ($_.NodeType -eq $nodeType) -and ($_.NodeStatus -eq "Up") };
# Get the unique applications deployed to each of these nodes.
$applications = @();
foreach($node in $nodes) {
$applications += (Get-ServiceFabricDeployedApplication -NodeName $node.NodeName);
}
$applications = $applications | Select-Object -ExpandProperty ApplicationTypeName -Unique;
if(Test-IsCollectionNullOrEmpty $applications) {
Write-Warning "$loglead Found no applications to restart. Exiting..";
return;
}
# Restart all the applications.
Invoke-Parallel -objects $applications -arguments $hostname -numThreads 16 -script {
param($applicationTypeName, $arguments)
$hostname = $arguments[0];
Restart-AlkamiServiceFabricApplication -applicationTypeName $applicationTypeName -hostname $hostname;
};
Write-Host "$loglead All services are restarted.";
}