-
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.
Adding a script to invoke the standards Octopus Runbook for all spaces
- Loading branch information
1 parent
ffd3c7e
commit 4b7c0b2
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
octopus-samples-instances/api-scripts/call-enforce-space-standards-runbook.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,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" | ||
} |