Skip to content

Instantly share code, notes, and snippets.

@GuitarRich
Created March 11, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuitarRich/0d3b30ec36a2368b7b1e to your computer and use it in GitHub Desktop.
Save GuitarRich/0d3b30ec36a2368b7b1e to your computer and use it in GitHub Desktop.
{
"Id": "ActionTemplates-21",
"Name": "Unicorn Sync",
"Description": "Calls the unicorn sync",
"ActionType": "Octopus.Script",
"Version": 12,
"Properties": {
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "$ErrorActionPreference = 'Stop'\r\n\r\nAdd-Type -Path \"${MicroChap}\\MicroCHAP.dll\"\r\n\r\nFunction Sync-Unicorn {\r\n\tParam(\r\n\t\t[Parameter(Mandatory=$True)]\r\n\t\t[string]$ControlPanelUrl,\r\n\r\n\t\t[Parameter(Mandatory=$True)]\r\n\t\t[string]$SharedSecret,\r\n\r\n\t\t[Parameter(Mandatory=$True)]\r\n\t\t[string[]]$Configurations,\r\n\r\n\t\t[string]$Verb = 'Sync'\r\n\t)\r\n\r\n\t# PARSE THE URL TO REQUEST\r\n\t$parsedConfigurations = ($Configurations) -join \"^\"\r\n\r\n\t$url = \"{0}?verb={1}&configuration={2}\" -f $ControlPanelUrl, $Verb, $parsedConfigurations\r\n\r\n\tWrite-Host \"Sync-Unicorn: Preparing authorization for $url\"\r\n\r\n\t# GET AN AUTH CHALLENGE\r\n\t$challenge = Get-Challenge -ControlPanelUrl $ControlPanelUrl\r\n\r\n\tWrite-Host \"Sync-Unicorn: Received challenge: $challenge\"\r\n\r\n\t# CREATE A SIGNATURE WITH THE SHARED SECRET AND CHALLENGE\r\n\t$signatureService = New-Object MicroCHAP.SignatureService -ArgumentList $SharedSecret\r\n\r\n\t$signature = $signatureService.CreateSignature($challenge, $url, $null)\r\n\r\n\tWrite-Host \"Sync-Unicorn: Created signature $signature, executing $Verb...\"\r\n\r\n\t# USING THE SIGNATURE, EXECUTE UNICORN\r\n\t$result = Invoke-WebRequest -Uri $url -Headers @{ \"X-MC-MAC\" = $signature; \"X-MC-Nonce\" = $challenge } -TimeoutSec 10800 -UseBasicParsing\r\n\r\n\t$result.Content\r\n}\r\n\r\nFunction Get-Challenge {\r\n\tParam(\r\n\t\t[Parameter(Mandatory=$True)]\r\n\t\t[string]$ControlPanelUrl\r\n\t)\r\n\r\n\t$url = \"$($ControlPanelUrl)?verb=Challenge\"\r\n\r\n\t$result = Invoke-WebRequest -Uri $url -TimeoutSec 360 -UseBasicParsing\r\n\r\n\t$result.Content\r\n}\r\n\r\n$configs = $Configurations.split(\"`n\")\r\nSync-Unicorn -ControlPanelUrl \"$($SiteUrl)/unicorn.aspx\" -SharedSecret $SharedSecret -Configurations $configs\r\n"
},
"SensitiveProperties": {},
"Parameters": [
{
"Name": "SharedSecret",
"Label": "Shared Secret",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Name": "SiteUrl",
"Label": "Site Url",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "MicroChap",
"Label": "MicroCHAP DLL Location",
"HelpText": null,
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "Configurations",
"Label": "Configurations",
"HelpText": "Add a configuration per line",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "MultiLineText"
}
}
],
"$Meta": {
"ExportedAt": "2016-03-11T16:08:01.215Z",
"OctopusVersion": "3.2.7",
"Type": "ActionTemplate"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment