From f676e6b966d777aeb240d66fdd8778b0d719c67c Mon Sep 17 00:00:00 2001 From: BobJWalker Date: Fri, 17 Dec 2021 15:23:30 -0600 Subject: [PATCH] Fixed issue with updating pre-existing step templates, added lifecycle policies --- .../enforce-lifecycle-policies.ps1 | 68 +++++++++++++++++++ .../enforce-runbook-retention-policies.ps1 | 48 +++++++++++++ .../manage-community-step-templates.ps1 | 5 +- 3 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 create mode 100644 octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 diff --git a/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 b/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 new file mode 100644 index 0000000..27e7e3e --- /dev/null +++ b/octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1 @@ -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 + } + } +} \ No newline at end of file diff --git a/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 b/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 new file mode 100644 index 0000000..e0cfa42 --- /dev/null +++ b/octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1 @@ -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)" + } + } + } + } +} diff --git a/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 b/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 index e2d95b6..5de8f4c 100644 --- a/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 +++ b/octopus-samples-instances/api-scripts/manage-community-step-templates.ps1 @@ -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 @@ -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)" }