Worker - Health check

Octopus.Script exported 2023-02-16 by harrisonmeister belongs to ‘Octopus’ category.

Run a health check against a worker.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Worker name

WorkerName =

Name of the worker to do a health check on.

Worker Pool

WorkerPoolName =

(Optional) Worker pool the worker belongs to.

Api Key

WorkerApiKey =

Api Key to use to issue health check.

Script body

Steps based on this template will execute the following PowerShell script.

# Define parameters
$baseUrl = $OctopusParameters['Octopus.Web.ServerUri'] 
$apiKey = $WorkerApiKey
$spaceId = $OctopusParameters['Octopus.Space.Id']
$spaceName = $OctopusParameters['Octopus.Space.Name']
$environmentName = $OctopusParameters['Octopus.Environment.Name']
$workerName = $OctopusParameters['WorkerName']
$workerPoolName = $OctopusParameters['WorkerPoolName']

# Check for null or empty
if ([string]::IsNullOrEmpty($baseUrl))
{
	$baseUrl = $OctopusParameters['#{if Octopus.Web.ServerUri}Octopus.Web.ServerUri#{else}Octopus.Web.BaseUrl#{/if}']
}

# Get worker
if (![string]::IsNullOrEmpty($workerPoolName))
{
    # Get worker pool
    $workerPool = (Invoke-RestMethod -Method Get -Uri "$baseUrl/api/$spaceId/workerpools/all" -Headers @{"X-Octopus-ApiKey"="$apiKey"}) | Where-Object {$_.Name -eq $workerPoolName}
    
    # Check to make sure it exists
    if ($null -ne $workerPool)
    {
        $worker = (Invoke-RestMethod -Method Get -Uri "$baseUrl/api/$spaceId/workerpools/$($workerPool.Id)/workers" -Headers @{"X-Octopus-ApiKey"="$apiKey"}).Items | Where-Object {$_.Name -eq "$workerName"}
    }
    else
    {
    	Write-Error "Worker pool $workerPoolName not found!"
    }
}
else
{
    $worker = (Invoke-RestMethod -Method Get -Uri "$baseUrl/api/$spaceId/workers/all" -Headers @{"X-Octopus-ApiKey"="$apiKey"}) | Where-Object {$_.Name -eq "$workerName"}
}

# Check to make sure something was returned
if ($null -eq $worker)
{
	if (![string]::IsNullOrEmpty($workerPoolName))
    {
    	Write-Error "Unable to find $workerName in $workerPoolName!"
    }
    else
    {
    	Write-Error "Unable to find $workerName!"
    }
}

# Build payload
$jsonPayload = @{
	Name = "Health"
    Description = "Check $workerName health"
    Arguments = @{
    	Timeout = "00:05:00"
        MachineIds = @(
        	$worker.Id
        )
    OnlyTestConnection = "false"
    }
    SpaceId = "$spaceId"
}

# Display message
Write-Output "Beginning health check of $workerName ..."

# Execute health check
$healthCheck = (Invoke-RestMethod -Method Post -Uri "$baseUrl/api/tasks" -Body ($jsonPayload | ConvertTo-Json -Depth 10) -Headers @{"X-Octopus-ApiKey"="$apiKey"})

# Check to see if the health check is queued
while ($healthCheck.IsCompleted -eq $false)
{
    $healthCheck = (Invoke-RestMethod -Method Get -Uri "$baseUrl/api/tasks/$($healthCheck.Id)" -Headers @{"X-Octopus-ApiKey"="$apiKey"})
}

if ($healthCheck.State -eq "Failed")
{
	Write-Error "Health check failed!"
}
else
{
	Write-Output "Health check completed with $($healthCheck.State)."
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "c6c23c7b-876d-4758-a908-511f066156d7",
  "Name": "Worker - Health check",
  "Description": "Run a health check against a worker.",
  "Version": 4,
  "ExportedAt": "2023-02-16T15:38:44.043Z",
  "ActionType": "Octopus.Script",
  "Author": "harrisonmeister",
  "Packages": [],
  "Parameters": [
    {
      "Id": "4ec84f25-8c23-4576-b7fe-3b60ca6f0f3c",
      "Name": "WorkerName",
      "Label": "Worker name",
      "HelpText": "Name of the worker to do a health check on.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "d876958b-1d93-45fa-996e-2005de2919ee",
      "Name": "WorkerPoolName",
      "Label": "Worker Pool",
      "HelpText": "(Optional) Worker pool the worker belongs to.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "1939947b-e777-4e4a-8caf-729dacb25aed",
      "Name": "WorkerApiKey",
      "Label": "Api Key",
      "HelpText": "Api Key to use to issue health check.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "Sensitive"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptBody": "# Define parameters\n$baseUrl = $OctopusParameters['Octopus.Web.ServerUri'] \n$apiKey = $WorkerApiKey\n$spaceId = $OctopusParameters['Octopus.Space.Id']\n$spaceName = $OctopusParameters['Octopus.Space.Name']\n$environmentName = $OctopusParameters['Octopus.Environment.Name']\n$workerName = $OctopusParameters['WorkerName']\n$workerPoolName = $OctopusParameters['WorkerPoolName']\n\n# Check for null or empty\nif ([string]::IsNullOrEmpty($baseUrl))\n{\n\t$baseUrl = $OctopusParameters['#{if Octopus.Web.ServerUri}Octopus.Web.ServerUri#{else}Octopus.Web.BaseUrl#{/if}']\n}\n\n# Get worker\nif (![string]::IsNullOrEmpty($workerPoolName))\n{\n    # Get worker pool\n    $workerPool = (Invoke-RestMethod -Method Get -Uri \"$baseUrl/api/$spaceId/workerpools/all\" -Headers @{\"X-Octopus-ApiKey\"=\"$apiKey\"}) | Where-Object {$_.Name -eq $workerPoolName}\n    \n    # Check to make sure it exists\n    if ($null -ne $workerPool)\n    {\n        $worker = (Invoke-RestMethod -Method Get -Uri \"$baseUrl/api/$spaceId/workerpools/$($workerPool.Id)/workers\" -Headers @{\"X-Octopus-ApiKey\"=\"$apiKey\"}).Items | Where-Object {$_.Name -eq \"$workerName\"}\n    }\n    else\n    {\n    \tWrite-Error \"Worker pool $workerPoolName not found!\"\n    }\n}\nelse\n{\n    $worker = (Invoke-RestMethod -Method Get -Uri \"$baseUrl/api/$spaceId/workers/all\" -Headers @{\"X-Octopus-ApiKey\"=\"$apiKey\"}) | Where-Object {$_.Name -eq \"$workerName\"}\n}\n\n# Check to make sure something was returned\nif ($null -eq $worker)\n{\n\tif (![string]::IsNullOrEmpty($workerPoolName))\n    {\n    \tWrite-Error \"Unable to find $workerName in $workerPoolName!\"\n    }\n    else\n    {\n    \tWrite-Error \"Unable to find $workerName!\"\n    }\n}\n\n# Build payload\n$jsonPayload = @{\n\tName = \"Health\"\n    Description = \"Check $workerName health\"\n    Arguments = @{\n    \tTimeout = \"00:05:00\"\n        MachineIds = @(\n        \t$worker.Id\n        )\n    OnlyTestConnection = \"false\"\n    }\n    SpaceId = \"$spaceId\"\n}\n\n# Display message\nWrite-Output \"Beginning health check of $workerName ...\"\n\n# Execute health check\n$healthCheck = (Invoke-RestMethod -Method Post -Uri \"$baseUrl/api/tasks\" -Body ($jsonPayload | ConvertTo-Json -Depth 10) -Headers @{\"X-Octopus-ApiKey\"=\"$apiKey\"})\n\n# Check to see if the health check is queued\nwhile ($healthCheck.IsCompleted -eq $false)\n{\n    $healthCheck = (Invoke-RestMethod -Method Get -Uri \"$baseUrl/api/tasks/$($healthCheck.Id)\" -Headers @{\"X-Octopus-ApiKey\"=\"$apiKey\"})\n}\n\nif ($healthCheck.State -eq \"Failed\")\n{\n\tWrite-Error \"Health check failed!\"\n}\nelse\n{\n\tWrite-Output \"Health check completed with $($healthCheck.State).\"\n}\n"
  },
  "Category": "Octopus",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/octopus-worker-healthcheck.json",
  "Website": "/step-templates/c6c23c7b-876d-4758-a908-511f066156d7",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRFT6Tl////L5Pg8vj9Y67omsvwPJrisdfzfbzs5fL7y+T32Ov5isLucLXqvt31CJPHWwAABMJJREFUeNrs3deW4jAMAFDF3U75/89dlp0ZhiU4blJEjvQ8hYubLJsA00UCBCIQgQhEIAIRiEAEIhCBCEQgAhGIQAQiEIEIhD8kJm+t+QprfdKfB9HbYpx6CWfspj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hdlNibwlKYOR6T8xjI7W8n+xV7T+GH4tMzWwR+lZhRkJYSsC0thpmCYqyngOz3rN2FLBZ2wZflBCggUHF0Vnp88JKienzIXLSEZCZqU7IKr/gQW9yx3pzV7Y9kvWZWTRRIqDmTtRUnU7b2lLcTYmoqHqnmiO1poER0SPkAeZMAZxaJx0Y3TCdAclsIqDz03ALcyxfTCZBsthoGXWmigGyVhWPLFJJfuuKQWycoEFdXbH4dJJoJxNR1eD/kshz6yn48cF8yW8sFoitflB1w6Q8n+/15Za7oA17/pYNmYgP5fmWm8L1NOHPWgK8kuFew1/JXtOA0yJCv7ah7X8ObUuT5kObU30+fDZm8+zqP+HTIpK0xQ796b5Kv2hSIQAQiEIEIRCACEYhABCIQgQhEIAIRiEAEIpBf8UeAAQAEjtYmlDTcCgAAAABJRU5ErkJggg==",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, February 16, 2023