-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issue with updating pre-existing step templates, added lifecycl…
…e policies
- Loading branch information
1 parent
5034895
commit f676e6b
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
octopus-samples-instances/api-scripts/enforce-lifecycle-policies.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
octopus-samples-instances/api-scripts/enforce-runbook-retention-policies.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters