Skip to content

Commit

Permalink
Fixed issue with updating pre-existing step templates, added lifecycl…
Browse files Browse the repository at this point in the history
…e policies
  • Loading branch information
BobJWalker committed Dec 17, 2021
1 parent 5034895 commit f676e6b
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
param
(
$OctopusUrl,
$OctopusApiKey,
$MaxReleaseRetentionDaysAllowed,
$UnitToUse
)

$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

# Get spaces
$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces/all" -Headers $header
Write-Output "Space Count: $($spaces.Length)"
foreach ($space in $spaces) {
Write-Output "Working on space $($space.Name)"

$lifecycleList = Invoke-RestMethod -Uri "$octopusUrl/api/$($space.Id)/lifecycles/all" -headers $header
foreach ($lifecycle in $lifecycleList)
{
Write-Output " Checking lifecycle $($lifecycle.Name)"
$changeLifecycle = $false
if ($lifecycle.ReleaseRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.ReleaseRetentionPolicy.Unit -ne $unitToUse)
{
Write-Host " Release retention policy is set to $($lifecycle.ReleaseRetentionPolicy.QuantityToKeep) $($lifecycle.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse"
$changeLifecycle = $true
$lifeCycle.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed
$lifeCycle.ReleaseRetentionPolicy.Unit = $unitToUse
$lifeCycle.ReleaseRetentionPolicy.ShouldKeepForever = $false
}

if ($lifecycle.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.TentacleRetentionPolicy.Unit -ne $unitToUse)
{
Write-Host " Tentacle retention policy is set to $($lifecycle.TentacleRetentionPolicy.QuantityToKeep) $($lifecycle.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse"
$changeLifecycle = $true
$lifeCycle.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed
$lifeCycle.TentacleRetentionPolicy.Unit = $unitToUse
$lifeCycle.TentacleRetentionPolicy.ShouldKeepForever = $false
}

foreach ($phase in $lifecycle.Phases)
{
if ($null -ne $phase.ReleaseRetentionPolicy -and ($phase.ReleaseRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.ReleaseRetentionPolicy.Unit -ne $unitToUse))
{
Write-Host " Release retention policy for $($phase.Name) is set to $($phase.ReleaseRetentionPolicy.QuantityToKeep) $($phase.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse"
$changeLifecycle = $true
$phase.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed
$phase.ReleaseRetentionPolicy.Unit = $unitToUse
$phase.ReleaseRetentionPolicy.ShouldKeepForever = $false
}

if ($null -ne $phease.TentacleRetentionPolicy -and ($phase.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.TentacleRetentionPolicy.Unit -ne $unitToUse))
{
Write-Host " Tentacle retention policy for $($phase.Name) is set to $($phase.TentacleRetentionPolicy.QuantityToKeep) $($phase.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse"
$changeLifecycle = $true
$phase.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed
$phase.TentacleRetentionPolicy.Unit = $unitToUse
$phase.TentacleRetentionPolicy.ShouldKeepForever = $false
}
}

if ($changeLifecycle -eq $true)
{
Write-Verbose " Invoking a PUT on $octopusURL/api/$($space.Id)/lifecycles/$($lifecycle.Id)"
Write-Verbose " $($lifecycle | ConvertTo-Json -Depth 10)"
$lifecycleResponse = Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/lifecycles/$($lifecycle.Id)" -Body ($lifecycle | ConvertTo-Json -Depth 10) -Headers $header
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
param
(
$OctopusUrl,
$OctopusApiKey,
$RunbookMaxRetentionRunPerEnvironment
)

$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }

$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces/all" -Headers $header
Write-Output "Space Count: $($spaces.Length)"

foreach ($space in $spaces) {
Write-Verbose "Working on space $($space.Name)"

try {
$projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header
}
catch {
Write-Warning "$($_.Exception.ToString())"
continue;
}
Write-Output "Project Count: $($projects.Length)"

foreach ($project in $projects) {
Write-Verbose "Working on project $($project.Name)"

$projectRunbooks = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)/runbooks" -Headers $header
$runbooks = $projectRunbooks.Items
Write-Output "Runbook Count: $($runbooks.Length)"

foreach ($runbook in $runbooks) {
Write-Verbose "Working on runbook $($runbook.Name)"
$currentRetentionQuantityToKeep = $runbook.RunRetentionPolicy.QuantityToKeep

if ($currentRetentionQuantityToKeep -gt $runbookMaxRetentionRunPerEnvironment) {
Write-Output "Runbook '$($runbook.Name)' ($($runbook.Id)) has a retention run policy to keep of: $($currentRetentionQuantityToKeep) which is greater than $($runbookMaxRetentionRunPerEnvironment)"
$runbook.RunRetentionPolicy.QuantityToKeep = $runbookMaxRetentionRunPerEnvironment
Write-Output "Updating runbook run quantity to keep for '$($runbook.Name)' ($($runbook.Id)) to $runbookMaxRetentionRunPerEnvironment"

$runbookResponse = Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/runbooks/$($runbook.Id)" -Body ($runbook | ConvertTo-Json -Depth 10) -Headers $header
if ($runbookResponse.RunRetentionPolicy.QuantityToKeep -ne $runbookMaxRetentionRunPerEnvironment) {
throw "Update for '$($runbook.Name)' ($($runbook.Id)) doesnt look like it worked. QtyToKeep is: $($runbookResponse.RunRetentionPolicy.QuantityToKeep)"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ foreach ($space in $spacesList.Items)
else
{
Write-Host "The step template $($stepTemplate.Name) is on version $($stepTemplate.Version) while the matching community template is on version $($communityActionTemplate.Version). Updating the step template."

$actionTemplate = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates/$($communityActionTemplateToInstall.Id)/installation/$($space.Id)" -spaceId $null -apiKey $OctopusApiKey -item $null -method "PUT"
Write-Host "Succesfully updated the step template. The version is now $($actionTemplate.Version)"
}

break
Expand All @@ -140,7 +143,7 @@ foreach ($space in $spacesList.Items)
exit 1
}

Write-Host "Installing/Updating the step template $expectedCommunityStepTemplate to $($space.Name)."
Write-Host "Installing the step template $expectedCommunityStepTemplate to $($space.Name)."
$actionTemplate = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "communityactiontemplates/$($communityActionTemplateToInstall.Id)/installation/$($space.Id)" -spaceId $null -apiKey $OctopusApiKey -item $null -method "POST"
Write-Host "Succesfully installed the step template. The Id of the new action template is $($actionTemplate.Id)"
}
Expand Down

0 comments on commit f676e6b

Please sign in to comment.