Octopus.Script exported 2020-04-13 by octobob belongs to ‘Octopus’ category.
Step template to Register an Listening Worker with Octopus Deploy using the API. Useful for when you need to wait to install additional software and a restart when spinning up a new worker.
Parameters
When steps based on the template are included in a project’s deployment process, the parameters below can be set.
Octopus Base Url
RegisterListeningWorker.Octopus.Base.Url =
The base url of your Octopus Deploy instance. Example: https://samples.octopus.app
Octopus Api Key
RegisterListeningWorker.Octopus.Api.Key =
The API key of a user in Octopus Deploy who has permissions to register the cluster.
Machine Name
RegisterListeningWorker.Machine.Name =
The name of the machine to register with Octopus Deploy.
Machine Address
RegisterListeningWorker.Machine.Address =
The machine address (IP Address or Domain Name) to connect to
Port Number
RegisterListeningWorker.Machine.Port = 10933
The port the tentacle is listening on
Worker Pool CSV List
RegisterListeningWorker.WorkerPool.List =
Comma separated list of Worker Pools to assign the worker to in Octopus Deploy. This can be the worker pool name or the id.
Machine Policy Id Or Name
RegisterListeningWorker.MachinePolicy.IdOrName = Default Machine Policy
Enter in the name or the Id of the Machine Policy in Octopus Deploy for the AKS Cluster.
Overwrite Existing Registration
RegisterListeningWorker.Overwrite.Existing = False
Indicates if the existing worker should be overwritten
Script body
Steps based on this template will execute the following PowerShell script.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$OctopusAPIKey = $OctopusParameters["RegisterListeningWorker.Octopus.Api.Key"]
$RegistrationName = $OctopusParameters["RegisterListeningWorker.Machine.Name"]
$RegistrationAddress = $OctopusParameters["RegisterListeningWorker.Machine.Address"]
$OctopusUrl = $OctopusParameters["RegisterListeningWorker.Octopus.Base.Url"]
$WorkerPools = $OctopusParameters["RegisterListeningWorker.WorkerPool.List"]
$SpaceId = $OctopusParameters["Octopus.Space.Id"]
$MachinePolicyIdOrName = $OctopusParameters["RegisterListeningWorker.MachinePolicy.IdOrName"]
$PortNumber = $OctopusParameters["RegisterListeningWorker.Machine.Port"]
$OverwriteExisting = $OctopusParameters["RegisterListeningWorker.Overwrite.Existing"]
$OverwriteExisting = $OverwriteExisting -eq "True"
Write-Host "Machine Name: $RegistrationName"
Write-Host "Machine Address: $RegistrationAddress"
Write-Host "Machine Port: $PortNumber"
Write-Host "Octopus Url: $OctopusUrl"
Write-Host "Worker Pools: $WorkerPools"
Write-Host "Environments: $Environments"
Write-Host "Machine Policy Name or Id: $MachinePolicyIdOrName"
Write-Host "Overwrite Existing: $OverwriteExisting"
$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("X-Octopus-ApiKey", $OctopusAPIKey)
$baseApiUrl = "$OctopusUrl/api"
$baseApiInformation = Invoke-RestMethod $baseApiUrl -Headers $header
if ((Get-Member -InputObject $baseApiInformation.Links -Name "Spaces" -MemberType Properties) -ne $null)
{
$baseApiUrl = "$baseApiUrl/$SpaceId"
}
Write-Host "Base API Url: $baseApiUrl"
$existingMachineResultsUrl = "$baseApiUrl/workers?partialName=$RegistrationName&skip=0&take=1000"
Write-Host "Attempting to find existing machine with similar name at $existingMachineResultsUrl"
$existingMachineResponse = Invoke-RestMethod $existingMachineResultsUrl -Headers $header
Write-Host $existingMachineResponse
$machineFound = $false
foreach ($item in $existingMachineResponse.Items)
{
if ($item.Name -eq $RegistrationName)
{
$machineFound = $true
if ($OverwriteExisting)
{
$machineId = $item.Id
}
break
}
}
if ($machineFound -and $OverwriteExisting -eq $false)
{
Write-Highlight "Machine already exists, skipping registration"
Exit 0
}
$workerPoolList = $WorkerPools -split ","
$workerPoolIdList = @()
Write-Host "Getting the ids for all environments specified"
foreach($workerPool in $workerPoolList)
{
Write-Host "Getting the id for the worker pool $workerPool"
if ($workerPool.StartsWith("WorkerPools-") -eq $true)
{
Write-Host "The worker pool is already an id, using that instead of looking it up"
$workerPoolIdList += $workerPool
}
else
{
$workerPoolEscaped = $workerPool.Replace(" ", "%20")
$workerPoolUrl = "$baseApiUrl/workerpools?skip=0&take=1000&partialName=$workerPoolEscaped"
$workerPoolResponse = Invoke-RestMethod $workerPoolUrl -Headers $header
$workerPoolId = $workerPoolResponse.Items[0].Id
Write-Host "The id for worker pool $workerPool is $workerPoolId"
$workerPoolIdList += $workerPoolId
}
}
$machinePolicyId = $machinePolicyIdOrName
if ($machinePolicyIdOrName.StartsWith("MachinePolicies-") -eq $false)
{
Write-Host "The machine policy specified $machinePolicyIdOrName appears to be a name"
$machinePolicyNameEscaped = $machinePolicyIdOrName.Replace(" ", "%20")
$machinePolicyResponse = Invoke-RestMethod "$baseApiUrl/machinepolicies?partialName=$machinePolicyNameEscaped" -Headers $header
$machinePolicyId = $machinePolicyResponse.Items[0].Id
Write-Host "The machine policy id is $machinePolicyId"
}
$discoverUrl = "$baseApiUrl/machines/discover?host=$RegistrationAddress&port=$PortNumber&type=TentaclePassive"
Write-Host "Discovering the machine $discoverUrl"
$discoverResponse = Invoke-RestMethod $discoverUrl -Headers $header
Write-Host "ProjectResponse: $discoverResponse"
$machineThumbprint = $discoverResponse.EndPoint.Thumbprint
Write-Host "Thumbprint = $machineThumbprint"
$rawRequest = @{
Id = $machineId;
MachinePolicyId = $MachinePolicyId;
Name = $RegistrationName;
IsDisabled = $false;
HealthStatus = "Unknown";
HasLatestCalamari = $true;
StatusSummary = $null;
IsInProcess = $true;
Links = $null;
WorkerPoolIds = $workerPoolIdList;
Endpoint = @{
Id = $null;
CommunicationStyle = "TentaclePassive";
Links = $null;
Uri = "https://$RegistrationAddress`:$PortNumber";
Thumbprint = "$machineThumbprint";
ProxyId = $null
}
}
$jsonRequest = $rawRequest | ConvertTo-Json -Depth 10
Write-Host "Sending in the request $jsonRequest"
$machineUrl = "$baseApiUrl/workers"
$method = "POST"
if ($OverwriteExisting -and $machineId -ne $null)
{
$machineUrl = "$machineUrl/$machineId"
$method = "PUT"
}
Write-Host "Posting to url $machineUrl"
$machineResponse = Invoke-RestMethod $machineUrl -Headers $header -Method $method -Body $jsonRequest
Write-Host "Create workers's response: $machineResponse"
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "e83b3265-64f9-4870-8802-54884c43eaf0",
"Name": "Register Listening Worker with Octopus",
"Description": "Step template to Register an Listening Worker with Octopus Deploy using the API. Useful for when you need to wait to install additional software and a restart when spinning up a new worker.",
"Version": 1,
"ExportedAt": "2020-04-13T15:37:29.866Z",
"ActionType": "Octopus.Script",
"Author": "octobob",
"Packages": [],
"Parameters": [
{
"Id": "e98dc4e2-0766-4d2d-a753-eafe294fdeea",
"Name": "RegisterListeningWorker.Octopus.Base.Url",
"Label": "Octopus Base Url",
"HelpText": "The base url of your Octopus Deploy instance. Example: https://samples.octopus.app",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "cb3cdd41-3d1f-49c6-820e-acfa24bf5a88",
"Name": "RegisterListeningWorker.Octopus.Api.Key",
"Label": "Octopus Api Key",
"HelpText": "The API key of a user in Octopus Deploy who has permissions to register the cluster.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Id": "ca9d9733-2032-466b-9e80-2aa6abd3c977",
"Name": "RegisterListeningWorker.Machine.Name",
"Label": "Machine Name",
"HelpText": "The name of the machine to register with Octopus Deploy.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "1d3d8695-27a3-4d3e-9457-6b483253a609",
"Name": "RegisterListeningWorker.Machine.Address",
"Label": "Machine Address",
"HelpText": "The machine address (IP Address or Domain Name) to connect to",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "ea762a1e-07ee-4eec-af7a-b6e29bf274d8",
"Name": "RegisterListeningWorker.Machine.Port",
"Label": "Port Number",
"HelpText": "The port the tentacle is listening on",
"DefaultValue": "10933",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "fa4bd89d-bb86-4d3f-87ff-17125cd88f24",
"Name": "RegisterListeningWorker.WorkerPool.List",
"Label": "Worker Pool CSV List",
"HelpText": "Comma separated list of Worker Pools to assign the worker to in Octopus Deploy. This can be the worker pool name or the id.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "5ef91f37-b13b-413d-955c-872c7f274c7e",
"Name": "RegisterListeningWorker.MachinePolicy.IdOrName",
"Label": "Machine Policy Id Or Name",
"HelpText": "Enter in the name or the Id of the Machine Policy in Octopus Deploy for the AKS Cluster.",
"DefaultValue": "Default Machine Policy",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "d456924c-3720-4893-9a40-36bb1c00b331",
"Name": "RegisterListeningWorker.Overwrite.Existing",
"Label": "Overwrite Existing Registration",
"HelpText": "Indicates if the existing worker should be overwritten",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
}
],
"Properties": {
"Octopus.Action.RunOnServer": "true",
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n\n$OctopusAPIKey = $OctopusParameters[\"RegisterListeningWorker.Octopus.Api.Key\"]\n$RegistrationName = $OctopusParameters[\"RegisterListeningWorker.Machine.Name\"]\n$RegistrationAddress = $OctopusParameters[\"RegisterListeningWorker.Machine.Address\"]\n$OctopusUrl = $OctopusParameters[\"RegisterListeningWorker.Octopus.Base.Url\"]\n$WorkerPools = $OctopusParameters[\"RegisterListeningWorker.WorkerPool.List\"]\n$SpaceId = $OctopusParameters[\"Octopus.Space.Id\"]\n$MachinePolicyIdOrName = $OctopusParameters[\"RegisterListeningWorker.MachinePolicy.IdOrName\"]\n$PortNumber = $OctopusParameters[\"RegisterListeningWorker.Machine.Port\"]\n$OverwriteExisting = $OctopusParameters[\"RegisterListeningWorker.Overwrite.Existing\"]\n$OverwriteExisting = $OverwriteExisting -eq \"True\"\n\n\nWrite-Host \"Machine Name: $RegistrationName\"\nWrite-Host \"Machine Address: $RegistrationAddress\"\nWrite-Host \"Machine Port: $PortNumber\"\nWrite-Host \"Octopus Url: $OctopusUrl\"\nWrite-Host \"Worker Pools: $WorkerPools\"\nWrite-Host \"Environments: $Environments\"\nWrite-Host \"Machine Policy Name or Id: $MachinePolicyIdOrName\"\nWrite-Host \"Overwrite Existing: $OverwriteExisting\"\n\n$header = New-Object \"System.Collections.Generic.Dictionary[[String],[String]]\"\n$header.Add(\"X-Octopus-ApiKey\", $OctopusAPIKey)\n\n$baseApiUrl = \"$OctopusUrl/api\"\n$baseApiInformation = Invoke-RestMethod $baseApiUrl -Headers $header\nif ((Get-Member -InputObject $baseApiInformation.Links -Name \"Spaces\" -MemberType Properties) -ne $null)\n{ \t\n\t$baseApiUrl = \"$baseApiUrl/$SpaceId\" \n}\n\nWrite-Host \"Base API Url: $baseApiUrl\"\n\n$existingMachineResultsUrl = \"$baseApiUrl/workers?partialName=$RegistrationName&skip=0&take=1000\"\nWrite-Host \"Attempting to find existing machine with similar name at $existingMachineResultsUrl\"\n$existingMachineResponse = Invoke-RestMethod $existingMachineResultsUrl -Headers $header\nWrite-Host $existingMachineResponse\n\n$machineFound = $false\nforeach ($item in $existingMachineResponse.Items)\n{\n\tif ($item.Name -eq $RegistrationName)\n {\n \t$machineFound = $true\n if ($OverwriteExisting)\n {\n \t$machineId = $item.Id \n }\n break\n }\n}\n\nif ($machineFound -and $OverwriteExisting -eq $false)\n{\n\tWrite-Highlight \"Machine already exists, skipping registration\"\n Exit 0\n}\n\n$workerPoolList = $WorkerPools -split \",\"\n$workerPoolIdList = @()\nWrite-Host \"Getting the ids for all environments specified\"\nforeach($workerPool in $workerPoolList)\n{\n\tWrite-Host \"Getting the id for the worker pool $workerPool\"\n \n if ($workerPool.StartsWith(\"WorkerPools-\") -eq $true)\n {\n \tWrite-Host \"The worker pool is already an id, using that instead of looking it up\"\n \t$workerPoolIdList += $workerPool\n }\n else\n {\n \t$workerPoolEscaped = $workerPool.Replace(\" \", \"%20\")\n $workerPoolUrl = \"$baseApiUrl/workerpools?skip=0&take=1000&partialName=$workerPoolEscaped\"\n $workerPoolResponse = Invoke-RestMethod $workerPoolUrl -Headers $header \n\n $workerPoolId = $workerPoolResponse.Items[0].Id\n Write-Host \"The id for worker pool $workerPool is $workerPoolId\"\n $workerPoolIdList += $workerPoolId\n } \n}\n\n$machinePolicyId = $machinePolicyIdOrName\nif ($machinePolicyIdOrName.StartsWith(\"MachinePolicies-\") -eq $false)\n{\n\tWrite-Host \"The machine policy specified $machinePolicyIdOrName appears to be a name\"\n\t$machinePolicyNameEscaped = $machinePolicyIdOrName.Replace(\" \", \"%20\")\n\t$machinePolicyResponse = Invoke-RestMethod \"$baseApiUrl/machinepolicies?partialName=$machinePolicyNameEscaped\" -Headers $header\n \n $machinePolicyId = $machinePolicyResponse.Items[0].Id\n Write-Host \"The machine policy id is $machinePolicyId\"\n}\n\n$discoverUrl = \"$baseApiUrl/machines/discover?host=$RegistrationAddress&port=$PortNumber&type=TentaclePassive\"\nWrite-Host \"Discovering the machine $discoverUrl\"\n$discoverResponse = Invoke-RestMethod $discoverUrl -Headers $header \nWrite-Host \"ProjectResponse: $discoverResponse\"\n\n$machineThumbprint = $discoverResponse.EndPoint.Thumbprint\nWrite-Host \"Thumbprint = $machineThumbprint\"\n\n$rawRequest = @{\n Id = $machineId;\n MachinePolicyId = $MachinePolicyId;\n Name = $RegistrationName;\n IsDisabled = $false;\n HealthStatus = \"Unknown\";\n HasLatestCalamari = $true;\n StatusSummary = $null;\n IsInProcess = $true;\n Links = $null;\n WorkerPoolIds = $workerPoolIdList;\n Endpoint = @{\n Id = $null;\n CommunicationStyle = \"TentaclePassive\";\n Links = $null;\n Uri = \"https://$RegistrationAddress`:$PortNumber\";\n Thumbprint = \"$machineThumbprint\";\n ProxyId = $null\n }\n}\n\n$jsonRequest = $rawRequest | ConvertTo-Json -Depth 10\n\nWrite-Host \"Sending in the request $jsonRequest\"\n\n$machineUrl = \"$baseApiUrl/workers\"\n$method = \"POST\"\nif ($OverwriteExisting -and $machineId -ne $null)\n{\n\t$machineUrl = \"$machineUrl/$machineId\" \n \t$method = \"PUT\"\n}\n\nWrite-Host \"Posting to url $machineUrl\"\n$machineResponse = Invoke-RestMethod $machineUrl -Headers $header -Method $method -Body $jsonRequest\n\nWrite-Host \"Create workers's response: $machineResponse\""
},
"Category": "Octopus",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/octopus-register-listening-worker.json",
"Website": "/step-templates/e83b3265-64f9-4870-8802-54884c43eaf0",
"Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRFT6Tl////L5Pg8vj9Y67omsvwPJrisdfzfbzs5fL7y+T32Ov5isLucLXqvt31CJPHWwAABMJJREFUeNrs3deW4jAMAFDF3U75/89dlp0ZhiU4blJEjvQ8hYubLJsA00UCBCIQgQhEIAIRiEAEIhCBCEQgAhGIQAQiEIEIhD8kJm+t+QprfdKfB9HbYpx6CWfspj8HMi+gMgHL/AmQA8W3JTKH+ALFvzCeL0RbpyoCPE9IJeNOSQwh5Z3qd6yRGWQ2qi2cZQWxqj1WzQYSjeoJmJlAklOd4VlArOqPhQEkqBERToeMcfRJBkC0Uep8CfBpjz4JsHJ0zF3dkEWNje0kiB/sUC6eApndaIiCMyAa1PiwJ0AWhRGJHJJQHG2dC7h1rNbO1QOxSA7lNCkkKrQIpJCAB1GREILYIC1NAiwbpKFJgGWDNExcwGstfExcZBCHC6nOglshHtmhViLIig1RNBCN7qjtW8C0Z1UvJcC1Z9XmwMBzzvobmgAyEzgq91dtEEsBsQSQQAFZCSBAATEEEApHZbrVBIkkEIUPSVeB+KtALA0kXQUSrwKZBCIQBnk8Y4i5CsReBeKvkqLM+BCSDWJlrZFvGk9SRTHshkgjZCGAaArIxm3H3grhVzFlW2msfl1ca79UJ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