ps/Modules/Alkami.DevOps.TeamCity/Public/Stop-TeamCityAgent.ps1

79 lines
2.8 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Stop-TeamCityAgent {
<#
.SYNOPSIS
Stop the TeamCity Agent service(s) on a given host or on the host(s) in a given Agent Pool
.PARAMETER ComputerName
FQDN hostname of the TeamCity agent host where you want to stop the Agent service
.PARAMETER AgentPool
TeamCity Agent Pool name of the TeamCity agents you want to stop - this must be the EXACT NAME FROM TEAMCITY
.LINK
Get-TeamCityAgentHostnames
.LINK
Start-TeamCityAgent
.LINK
https://ci.corp.alkamitech.com/agents.html?tab=agentPools
.LINK
https://ci.corp.alkamitech.com/app/rest/agentPools
#>
[CmdletBinding()]
[OutputType([System.Void])]
param(
[Parameter(Mandatory = $true, ParameterSetName = "ComputerName")]
$ComputerName,
[Parameter(Mandatory = $true, ParameterSetName = "AgentPool")]
$AgentPool
)
# Why would you name the Keys like that, Tom?
#
# Because that's what they're named in TeamCity. Ideally, we'd be able to pull from the API
# For a lot of things eventually and I don't want to make names up.
# If you don't know the name, look it up in TeamCity. That's the name.
# Also...
# There are other pools that we don't touch the hosts of. "All" isn't really "All"
$agentPoolToServiceNameFragments = @{
"Sandbox" = @('buildSandbox', 'migrateSandbox')
"Dev/QA" = @('migrateQaDev', 'buildQaDev')
"Production/Staging" = @('buildagent', 'migrateagent')
"All" = @('buildSandbox', 'migrateSandbox', 'migrateQaDev', 'buildQaDev', 'buildagent', 'migrateagent')
}
# I hate everything about this except the last entry
# I really want to be getting these from the REST API. Or something. ANYthing else.
$agentPoolToAgentHostname = @{
"Sandbox" = @("tea316229.fh.local", "tea37021.fh.local")
"Dev/QA" = @("tea316208.fh.local", "tea370104.fh.local")
"Production/Staging" = @("tea316155.fh.local", "tea31697.fh.local")
"All" = Get-TeamCityAgentHostnames
}
if ($PSCmdlet.ParameterSetName -eq "AgentPool") {
$ComputerName = $agentPoolToAgentHostname[$AgentPool]
}
if ($PSCmdlet.ParameterSetName -eq "ComputerName") {
$AgentPool = "All"
}
$agentServiceFragmentNames = $agentPoolToServiceNameFragments[$AgentPool]
$sbStopAgent = {
param($sbServiceNameFragments)
$serviceNames = @()
foreach ($serviceName in $sbServiceNameFragments) {
$temp = (Get-ServiceInfoByCIMFragment $serviceName).name
if ($null -ne $temp) {
$serviceNames += $temp
}
}
Stop-Service $serviceNames
}
Invoke-ParallelServers -Servers $ComputerName -Script $sbStopAgent -Arguments ($agentServiceFragmentNames)
}