Skip to content

Commit

Permalink
Fix up worker pool handling (#255)
Browse files Browse the repository at this point in the history
We were treating workers as deployment targets, and trying to retrieve them from the /api/machines endpoint. They actually live under the /api/workers endpoint.
This also forces the Get-WorkerPoolMembership function to return an array, in an attempt to fix #245.

Maybe fixes #245
  • Loading branch information
matt-richardson authored Apr 18, 2020
1 parent 04fe91b commit 807dbac
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 39 deletions.
122 changes: 83 additions & 39 deletions OctopusDSC/DSCResources/cTentacleAgent/cTentacleAgent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ function Get-MachineFromOctopusServer {
return $machine
}

function Get-WorkerFromOctopusServer
{
param (
[Parameter(Mandatory=$true)]
[String]
$ServerUrl,
[Parameter(Mandatory=$true)]
[System.String]
$APIKey,
[Parameter(Mandatory=$true)]
[System.String]
$Instance,
[Parameter(Mandatory=$true)]
[AllowNull()]
[AllowEmptyString()]
[System.String]
$SpaceId
)
$apiUrl = "/workers/all"
if (![String]::IsNullOrEmpty($SpaceId)) {
$apiUrl = "/$SpaceId" + $apiUrl
}

$workers = Get-APIResult -ServerUrl $ServerUrl -APIKey $APIKey -API $apiUrl
$thumbprint = Get-TentacleThumbprint -Instance $Instance
$worker = $workers | Where-Object {$_.Thumbprint -eq $thumbprint}

return $worker
}

function Get-TentacleThumbprint {
param (
[Parameter(Mandatory=$true)]
Expand All @@ -90,6 +120,7 @@ function Get-TentacleThumbprint {
}

function Get-WorkerPoolMembership {
[OutputType([object[]])]
param (
[Parameter(Mandatory=$true)]
[System.String]
Expand Down Expand Up @@ -129,7 +160,7 @@ function Get-WorkerPoolMembership {
$workerPoolMembership += $octoWorkerPool
}
}
return $workerPoolMembership
return ,$workerPoolMembership
}

function Test-ParameterSet {
Expand Down Expand Up @@ -590,56 +621,69 @@ function Test-TargetResource {
}
}

$machine = Get-MachineFromOctopusServer -ServerUrl $OctopusServerUrl -APIKey $ApiKey -Instance $Name -SpaceId $spaceRef.Id
if ($null -ne $WorkerPools -and $WorkerPools.Length -gt 0) {
$worker = Get-WorkerFromOctopusServer -ServerUrl $OctopusServerUrl -APIKey $ApiKey -Instance $Name -SpaceId $spaceRef.Id

if ($null -ne $machine) {
if ($Environments.Count -ne $machine.EnvironmentIds.Count) {
Write-Verbose "Environments: [$Environments] vs [$machine.EnvironmentIds]: $false"
return $false
} else {
foreach ($environmentId in $machine.EnvironmentIds) {
$environmentUrl = "/environments/$environmentId"
if ($null -ne $spaceRef) {
$environmentUrl = "/$($spaceRef.Id)" + $environmentUrl
}
$environment = Get-APIResult -ServerUrl $OctopusServerUrl -ApiKey $ApiKey -API $environmentUrl
if ($Environments -notcontains $environment.Name) {
Write-Verbose "Environments: Machine currently has environment $($environment.Name), which is not listed in the passed in list [$Environments]. Machine is not in desired state."
return $false
if ($null -ne $worker) {
$workerPoolMembership = Get-WorkerPoolMembership -ServerUrl $OctopusServerUrl -ApiKey $ApiKey -Thumbprint $worker.Endpoint.Thumbprint -SpaceId $spaceRef.Id

if ($WorkerPools.Count -ne $workerPoolMembership.Count) {
Write-Verbose "Worker pools [$WorkerPools] vs [$workerPoolMembership] = $false"
return $false
} else {
foreach ($workerPool in $workerPoolMembership) {
if ($WorkerPools -notcontains $workerPool.Name) {
Write-Verbose "Worker pools: [$WorkerPools] vs [$workerPoolMembership] = $false"
return $false
}
}
}
} else {
if ([string]::IsNullOrEmpty($Space)) {
Write-Verbose "Worker '$Name' is not registered in Space '$Space'"
} else {
Write-Verbose "Worker '$Name' is not registered"
}
return $false
}
} else {
$machine = Get-MachineFromOctopusServer -ServerUrl $OctopusServerUrl -APIKey $ApiKey -Instance $Name -SpaceId $spaceRef.Id

$tentacleThumbprint = Get-TentacleThumbprint -Instance $Name
$workerPoolMembership = Get-WorkerPoolMembership -ServerUrl $OctopusServerUrl -ApiKey $ApiKey -Thumbprint $tentacleThumbprint -SpaceId $spaceRef.Id

if ($WorkerPools.Count -ne $workerPoolMembership.Count) {
Write-Verbose "Worker pools [$WorkerPools] vs [$workerPoolMembership] = $false"
return $false
} else {
foreach ($workerPool in $workerPoolMembership) {
if ($WorkerPools -notcontains $workerPool.Name) {
Write-Verbose "Worker pools: [$WorkerPools] vs [$workerPoolMembership] = $false"
return $false
if ($null -ne $machine) {
if ($Environments.Count -ne $machine.EnvironmentIds.Count) {
Write-Verbose "Environments: [$Environments] vs [$machine.EnvironmentIds]: $false"
return $false
} else {
foreach ($environmentId in $machine.EnvironmentIds) {
$environmentUrl = "/environments/$environmentId"
if ($null -ne $spaceRef) {
$environmentUrl = "/$($spaceRef.Id)" + $environmentUrl
}
$environment = Get-APIResult -ServerUrl $OctopusServerUrl -ApiKey $ApiKey -API $environmentUrl
if ($Environments -notcontains $environment.Name) {
Write-Verbose "Environments: Machine currently has environment $($environment.Name), which is not listed in the passed in list [$Environments]. Machine is not in desired state."
return $false
}
}
}
}

if ($Roles.Count -ne $machine.Roles.Count) {
Write-Verbose "Roles: [$Roles] vs [$($machine.Roles)]: $false"
return $false
} else {
$differences = Compare-Object -ReferenceObject $Roles -DifferenceObject $machine.Roles
if ($null -ne $differences) {
if ($Roles.Count -ne $machine.Roles.Count) {
Write-Verbose "Roles: [$Roles] vs [$($machine.Roles)]: $false"
return $false
} else {
$differences = Compare-Object -ReferenceObject $Roles -DifferenceObject $machine.Roles
if ($null -ne $differences) {
Write-Verbose "Roles: [$Roles] vs [$($machine.Roles)]: $false"
return $false
}
}
}
} else {
if ([string]::IsNullOrEmpty($Space)) {
Write-Verbose "Machine '$Name' is not registered"
} else {
Write-Verbose "Machine '$Name' is not registered in Space '$Space'"
if ([string]::IsNullOrEmpty($Space)) {
Write-Verbose "Machine '$Name' is not registered"
} else {
Write-Verbose "Machine '$Name' is not registered in Space '$Space'"
}
return $false
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions OctopusDSC/Tests/cTentacleAgent.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ try
}
}

Context 'Get-WorkerPoolMembership' {
It 'always returns an array, even if only one item returned' {
Mock Get-APIResult { return @( [pscustomobject] @{ Name = "Pool1"; Id = "WorkerPools-1" } )} -ParameterFilter {$api -eq "/workerpools/all"}
Mock Get-APIResult { return @( [pscustomobject] @{ Name = "Worker1"; WorkerPoolIds = @("WorkerPools-1"); Thumbprint = "12345678"} )} -ParameterFilter {$api -eq "/workers/all"}
$result = Get-WorkerPoolMembership -ServerUrl "https://example.com" -Thumbprint "12345678" -ApiKey "API-1234" -SpaceId $null

$result.Count | Should -Not -Be $null
}
}

Context 'Get-WorkerPoolMembership' {
It 'always returns an array, even if only one item returned' {
Mock Get-APIResult { return @( [pscustomobject] @{ Name = "Pool1"; Id = "WorkerPools-1" } )} -ParameterFilter {$api -eq "/workerpools/all"}
Mock Get-APIResult { return @( [pscustomobject] @{ Name = "Worker1"; WorkerPoolIds = @("WorkerPools-1"); Thumbprint = "12345678"} )} -ParameterFilter {$api -eq "/workers/all"}
$result = Get-WorkerPoolMembership -ServerUrl "http://localhost:8065" -Thumbprint "D80D5A3DF457E1EFB355451109588DBE26F59368" -ApiKey "API-JM91EXRDGJTCTT7SEEVMJ7E73R4" -SpaceId $null

$result.GetType() | Should -Be "System.Object[]"
$result.Count | Should -Not -Be $null
}
}

Context 'Get-TargetResource' {
Mock Get-ItemProperty { return @{ InstallLocation = "c:\Octopus\Tentacle\Stub" }}
Mock Get-Service { return @{ Status = "Running" }}
Expand Down

0 comments on commit 807dbac

Please sign in to comment.