Skip to content

Commit

Permalink
Updating scripts to include a space filter
Browse files Browse the repository at this point in the history
  • Loading branch information
BobJWalker committed Jan 7, 2022
1 parent 3b315d9 commit 613a7ac
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ param
$AdminInstanceUrl,
$AdminInstanceApiKey,
$AdminEnvironmentName,
$AdminSpaceName
$AdminSpaceName,
$OptionalSpaceId
)

function Invoke-OctopusApi
Expand Down Expand Up @@ -77,8 +78,15 @@ function Invoke-OctopusApi
}
}

$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
foreach ($space in $spacesList.Items)
$rawSpacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
$spacesList = $rawSpacesList.Items

if ([string]::IsNullOrWhiteSpace($OptionalSpaceId) -eq $false -and $OptionalSpaceId.ToLower().Trim() -ne "all")
{
$spacesList = @($spacesList | Where-Object {$_.Id -eq $OptionalSpaceId})
}

foreach ($space in $spacesList)
{
Write-Host "Queueing a runbook run for $($space.Name) using the space id $($space.Id). The runbook will run on $AdminInstanceUrl for the environment $AdminEnvironmentName in the space $AdminSpaceName"
Write-Host "Running command: octo run-runbook --project ""Standards"" --runbook ""Enforce Space Standards"" --environment ""$AdminEnvironmentName"" --variable=""Project.SpaceStandard.SpaceId:$($space.Id)"" --server=""$AdminInstanceUrl"" --apiKey=""$AdminInstanceApiKey"" --space=""$AdminSpaceName"" to queue the runbook"
Expand Down
44 changes: 21 additions & 23 deletions octopus-samples-instances/api-scripts/enforce-base-environments.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ param
(
$OctopusUrl,
$OctopusApiKey,
$EnvironmentsCsv
$EnvironmentsCsv,
$SpaceIdFilter
)

function Invoke-OctopusApi
Expand Down Expand Up @@ -74,35 +75,32 @@ function Invoke-OctopusApi
}
}

$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
$space = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces/$SpaceIdFilter" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
$environmentsToCheckList = @($EnvironmentsCsv -split ",")
foreach ($space in $spacesList.Items)
{
$environmentsList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments?skip=0&take=1000" -spaceId $space.Id -apiKey $OctopusApiKey -item $null -method "GET"
$environmentsList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments?skip=0&take=1000" -spaceId $space.Id -apiKey $OctopusApiKey -item $null -method "GET"

foreach ($environmentToCheck in $environmentsToCheckList)
foreach ($environmentToCheck in $environmentsToCheckList)
{
$found = $false
foreach ($environment in $environmentsList.Items)
{
$found = $false
foreach ($environment in $environmentsList.Items)
Write-Verbose "Checking $($environment.Name) with $environmentToCheck"
if ($environment.Name.ToLower().Trim() -eq $environmentToCheck.ToLower().Trim())
{
Write-Verbose "Checking $($environment.Name) with $environmentToCheck"
if ($environment.Name.ToLower().Trim() -eq $environmentToCheck.ToLower().Trim())
{
Write-Host "Environment $environmentToCheck already exists on space $($space.Name)"
$found = $true
break
}
Write-Host "Environment $environmentToCheck already exists on space $($space.Name)"
$found = $true
break
}
}

if ($found -eq $false)
{
$environmentToAdd = @{
Name = $environmentToCheck.Trim()
}

$newEnvironment = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments" -spaceId $space.Id -apiKey $OctopusApiKey -item $environmentToAdd -method "POST"
Write-Host "Added the environment $environmentToCheck with the new id $($newEnvironment.Id)"
if ($found -eq $false)
{
$environmentToAdd = @{
Name = $environmentToCheck.Trim()
}

$newEnvironment = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "environments" -spaceId $space.Id -apiKey $OctopusApiKey -item $environmentToAdd -method "POST"
Write-Host "Added the environment $environmentToCheck with the new id $($newEnvironment.Id)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,65 @@ param
$OctopusUrl,
$OctopusApiKey,
$MaxReleaseRetentionDaysAllowed,
$UnitToUse
$UnitToUse,
$SpaceIdFilter
)

$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)"
$space = Invoke-RestMethod -Uri "$octopusURL/api/spaces/$SpaceIdFilter" -Headers $header

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
}

$lifecycleList = Invoke-RestMethod -Uri "$octopusUrl/api/$($space.Id)/lifecycles/all" -headers $header
foreach ($lifecycle in $lifecycleList)
if ($lifecycle.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $lifecycle.TentacleRetentionPolicy.Unit -ne $unitToUse)
{
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 " Tentacle retention policy is set to $($lifecycle.TentacleRetentionPolicy.QuantityToKeep) $($lifecycle.TentacleRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse"
Write-Host " Release retention policy for $($phase.Name) is set to $($phase.ReleaseRetentionPolicy.QuantityToKeep) $($phase.ReleaseRetentionPolicy.Unit), updating to $maxReleaseRetentionDaysAllowed $unitToUse"
$changeLifecycle = $true
$lifeCycle.TentacleRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed
$lifeCycle.TentacleRetentionPolicy.Unit = $unitToUse
$lifeCycle.TentacleRetentionPolicy.ShouldKeepForever = $false
$phase.ReleaseRetentionPolicy.QuantityToKeep = $maxReleaseRetentionDaysAllowed
$phase.ReleaseRetentionPolicy.Unit = $unitToUse
$phase.ReleaseRetentionPolicy.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)
if ($null -ne $phease.TentacleRetentionPolicy -and ($phase.TentacleRetentionPolicy.QuantityToKeep -gt $maxReleaseRetentionDaysAllowed -or $phase.TentacleRetentionPolicy.Unit -ne $unitToUse))
{
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
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
Expand Up @@ -2,47 +2,45 @@ param
(
$OctopusUrl,
$OctopusApiKey,
$RunbookMaxRetentionRunPerEnvironment
$RunbookMaxRetentionRunPerEnvironment,
$spaceIdFilter
)

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

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

foreach ($space in $spaces) {
Write-Verbose "Working on space $($space.Name)"
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)"
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)"
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

$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"
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)"
}
$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 @@ -3,7 +3,8 @@ param
$OctopusUrl,
$OctopusApiKey,
$RolesAllowedCsv,
$TeamName
$TeamName,
$OptionalSpaceId
)

function Invoke-OctopusApi
Expand Down Expand Up @@ -110,29 +111,37 @@ foreach ($team in $teamList.Items)
$teamScopedUserRoles = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "teams/$($teamToUpdate.Id)/scopeduserroles?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"

$spaceIdList = @()
foreach ($userRole in $teamScopedUserRoles.Items)

if ([string]::IsNullOrWhiteSpace($OptionalSpaceId) -eq $false -and $OptionalSpaceId.ToLower().Trim() -ne "all")
{
$spaceIdList += $OptionalSpaceId
}
else
{
if ($null -eq $userRole.SpaceId)
foreach ($userRole in $teamScopedUserRoles.Items)
{
continue
}
if ($null -eq $userRole.SpaceId)
{
continue
}

if ($spaceIdList -notcontains $userRole.SpaceId)
{
Write-Verbose "Adding the space $($userRole.SpaceId) to the space list"
$spaceIdList += $userRole.SpaceId
if ($spaceIdList -notcontains $userRole.SpaceId)
{
Write-Verbose "Adding the space $($userRole.SpaceId) to the space list"
$spaceIdList += $userRole.SpaceId
}
}
}

$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
foreach ($space in $spacesList.Items)
{
Write-Verbose "Checking to see if $($space.Id) is already in the list"
if ($spaceIdList -notcontains $space.Id)
{
Write-Verbose "Adding the space $($space.SpaceId) to the space list"
$spaceIdList += $space.Id
}
$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
foreach ($space in $spacesList.Items)
{
Write-Verbose "Checking to see if $($space.Id) is already in the list"
if ($spaceIdList -notcontains $space.Id)
{
Write-Verbose "Adding the space $($space.SpaceId) to the space list"
$spaceIdList += $space.Id
}
}
}

foreach ($spaceId in $spaceIdList)
Expand Down
Loading

0 comments on commit 613a7ac

Please sign in to comment.