Skip to content

Commit

Permalink
Adding a script to invoke the standards Octopus Runbook for all spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
BobJWalker committed Jan 6, 2022
1 parent ffd3c7e commit 4b7c0b2
Showing 1 changed file with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
param
(
$OctopusUrl,
$OctopusApiKey,
$AdminInstanceUrl,
$AdminInstanceApiKey,
$AdminEnvrionmentName,
$AdminSpaceName
)

function Invoke-OctopusApi
{
param
(
$octopusUrl,
$endPoint,
$spaceId,
$apiKey,
$method,
$item
)

if ([string]::IsNullOrWhiteSpace($SpaceId))
{
$url = "$OctopusUrl/api/$EndPoint"
}
else
{
$url = "$OctopusUrl/api/$spaceId/$EndPoint"
}

if ([string]::IsNullOrWhiteSpace($method))
{
$method = "GET"
}

try
{
if ($null -eq $item)
{
Write-Verbose "No data to post or put, calling bog standard invoke-restmethod for $url"
return Invoke-RestMethod -Method $method -Uri $url -Headers @{"X-Octopus-ApiKey" = "$ApiKey" } -ContentType 'application/json; charset=utf-8' -TimeoutSec 60
}

$body = $item | ConvertTo-Json -Depth 10
Write-Verbose $body

Write-Verbose "Invoking $method $url"
return Invoke-RestMethod -Method $method -Uri $url -Headers @{"X-Octopus-ApiKey" = "$ApiKey" } -Body $body -ContentType 'application/json; charset=utf-8' -TimeoutSec 60
}
catch
{
Write-Host "There was an error making a $method call to $url. All request information (JSON body specifically) are logged as verbose. Please check that for more information." -ForegroundColor Red

if ($null -ne $_.Exception.Response)
{
if ($_.Exception.Response.StatusCode -eq 401)
{
Write-Host "Unauthorized error returned from $url, please verify API key and try again" -ForegroundColor Red
}
elseif ($_.ErrorDetails.Message)
{
Write-Host -Message "Error calling $url StatusCode: $($_.Exception.Response) $($_.ErrorDetails.Message)" -ForegroundColor Red
Write-Host $_.Exception -ForegroundColor Red
}
else
{
Write-Host $_.Exception -ForegroundColor Red
}
}
else
{
Write-Host $_.Exception -ForegroundColor Red
}

Exit 1
}
}

$spacesList = Invoke-OctopusApi -OctopusUrl $octopusUrl -endPoint "spaces?skip=0&take=1000" -spaceId $null -apiKey $OctopusApiKey -item $null -method "GET"
foreach ($space in $spacesList.Item)
{
Write-Host "Queing a runbook run for $($space.Name) using the space id $($space.Id)"
octo run-runbook --project "Standards" --runbook "Enforce Space Standards" --environment $AdminEnvrionmentName --variable="Project.SpaceStandard.SpaceId:$($space.Id)" --server="$AdminInstanceUrl" --apiKey="$AdminInstanceApiKey" --space="$AdminSpaceName"
}

0 comments on commit 4b7c0b2

Please sign in to comment.