PagerDuty - Open Maintenance Window

Octopus.Script exported 2020-07-23 by tfbryan belongs to ‘PagerDuty’ category.

Open a new maintenance window for the specified services, using the PagerDuty v2 API.

No new incidents will be created for a service that is currently in maintenance.

This script sets an output variable WindowId that can be used in the PagerDuty - Close Maintenance Window template.

Parameters

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

Minutes

Minutes = 60

Please set an estimated number of minutes for the maintenance window. The maintenance window will remain open for this duration unless it’s proactively closed sooner.

RequesterId

RequesterId =

Please configure an email address of the user creating the maintenance window.

Example: Octopus@mydomain.com

Token

Token =

Please supply the API token of your PagerDuty instance.

Found here: https://mydomain.pagerduty.com/api_keys

Description

Description =

Please supply a short description for this maintenance window.

ServiceIds

ServiceIds =

Please supply a comma separated list of the PagerDuty service ids that will be included in this maintenance window.

Found here: https://mydomain.pagerduty.com/services/**ABC123**

Example: ABC123, ABC456

Subdomain

Subdomain

The subdomain of the PagerDuty instance.

Found here: https://mydomain.pagerduty.com/

Script body

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

param(
    [array]$ServiceIds = @(""),
    [string]$RequesterId = "",
    [string]$Description = "",
    [int]$Minutes = 10,
    [string]$Token = ""
) 

$ErrorActionPreference = "Stop" 

function Get-Param($Name, [switch]$Required, $Default) {
    $Result = $null

    if ($OctopusParameters -ne $null) {
        $Result = $OctopusParameters[$Name]
    }

    if ($Result -eq $null) {
        $variable = Get-Variable $Name -EA SilentlyContinue   
        if ($variable -ne $null) {
            $Result = $variable.Value
        }
    }

    if ($Result -eq $null -or $Result -eq "") {
        if ($Required) {
            throw "Missing parameter value $Name"
        } else {
            $Result = $Default
        }
    }

    return $Result
}

& {
    param([array]$ServiceIds, [string]$RequesterId, [string]$Description, [int]$Minutes, [string]$Token)

    Write-Host "Opening PagerDuty window for $Description"

    try {
        $ArrayOfServices = $ServiceIds.split(",") | foreach { $_.trim() }
        $Start = ((Get-Date)).ToString("yyyy-MM-ddTHH:mm:sszzzzZ");
        $End = ((Get-Date).AddMinutes($Minutes)).ToString("yyyy-MM-ddTHH:mm:sszzzzZ");
        $ServiceIdArray = @()
        
        foreach($ServiceId in $ArrayOfServices){
        	$ServiceIdArray += @{"id"=$ServiceId; "type"="service_reference"}
        }
        
        $Uri = "https://api.pagerduty.com/maintenance_windows"
        $Headers = @{
          "Authorization" = "Token token=$Token"
          "Accept" = "application/vnd.pagerduty+json;version=2"
          "From" = $RequesterId
		}

        Write-Host "Window will be open from $Start -> $End"

        $Post = @{
            maintenance_window= @{
            	type = 'maintenance_window'
                start_time = $Start
                end_time = $End
                description = $Description
                services = $ServiceIdArray
            }
        } | ConvertTo-Json -Depth 4

        $ResponseObj = Invoke-RestMethod -Uri $Uri -Method Post -Body $Post -ContentType "application/json" -Headers $Headers
        $WindowId = $ResponseObj.maintenance_window.id

        Write-Host "Window Id $WindowId created"

        if(Get-Command -name "Set-OctopusVariable" -ErrorAction SilentlyContinue) {
            Set-OctopusVariable -name "WindowId" -value $WindowId
        } else {
            Write-Host "Octopus output variable not set"
        }
    } catch [System.Exception] {
        Write-Host "Error while opening PagerDuty window"
        Write-Host $_.Exception.Message
        
        $ResponseStream = $_.Exception.Response.GetResponseStream()
        $Reader = New-Object System.IO.StreamReader($ResponseStream)
        $Reader.ReadToEnd() | Write-Host
        
        Exit 1
    }
} (Get-Param 'ServiceIds' -Required) (Get-Param 'RequesterId' -Required) (Get-Param 'Description' -Required) (Get-Param 'Minutes' -Required) (Get-Param 'Token' -Required)

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": "93a10982-f675-42cd-ac3a-46ef28a46afa",
  "Name": "PagerDuty - Open Maintenance Window",
  "Description": "Open a new maintenance window for the specified services, using the PagerDuty v2 API.\n\nNo new incidents will be created for a service that is currently in maintenance.\n\nThis script sets an output variable **WindowId** that can be used in the _PagerDuty - Close Maintenance Window_ template.",
  "Version": 16,
  "ExportedAt": "2020-07-23T08:13:54.382Z",
  "ActionType": "Octopus.Script",
  "Author": "tfbryan",
  "Parameters": [
    {
      "Name": "Minutes",
      "Label": "Minutes",
      "HelpText": "Please set an estimated number of minutes for the maintenance window. The maintenance window will remain open for this duration unless it's proactively closed sooner.",
      "DefaultValue": "60",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "RequesterId",
      "Label": "RequesterId",
      "HelpText": "Please configure an email address of the user creating the maintenance window.\n\nExample: Octopus@mydomain.com",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Token",
      "Label": "Token",
      "HelpText": "Please supply the API token of your PagerDuty instance.\n\nFound here: https://mydomain.pagerduty.com/api_keys",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Description",
      "Label": "Description",
      "HelpText": "Please supply a short description for this maintenance window.",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "ServiceIds",
      "Label": "ServiceIds",
      "HelpText": "Please supply a comma separated list of the PagerDuty service ids that will be included in this maintenance window.\n\nFound here: https://mydomain.pagerduty.com/services/**ABC123**\n\nExample: ABC123, ABC456",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Name": "Subdomain",
      "Label": "Subdomain",
      "HelpText": "The subdomain of the PagerDuty instance.\n\nFound here: https://**mydomain**.pagerduty.com/",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "param(\n    [array]$ServiceIds = @(\"\"),\n    [string]$RequesterId = \"\",\n    [string]$Description = \"\",\n    [int]$Minutes = 10,\n    [string]$Token = \"\"\n) \n\n$ErrorActionPreference = \"Stop\" \n\nfunction Get-Param($Name, [switch]$Required, $Default) {\n    $Result = $null\n\n    if ($OctopusParameters -ne $null) {\n        $Result = $OctopusParameters[$Name]\n    }\n\n    if ($Result -eq $null) {\n        $variable = Get-Variable $Name -EA SilentlyContinue   \n        if ($variable -ne $null) {\n            $Result = $variable.Value\n        }\n    }\n\n    if ($Result -eq $null -or $Result -eq \"\") {\n        if ($Required) {\n            throw \"Missing parameter value $Name\"\n        } else {\n            $Result = $Default\n        }\n    }\n\n    return $Result\n}\n\n& {\n    param([array]$ServiceIds, [string]$RequesterId, [string]$Description, [int]$Minutes, [string]$Token)\n\n    Write-Host \"Opening PagerDuty window for $Description\"\n\n    try {\n        $ArrayOfServices = $ServiceIds.split(\",\") | foreach { $_.trim() }\n        $Start = ((Get-Date)).ToString(\"yyyy-MM-ddTHH:mm:sszzzzZ\");\n        $End = ((Get-Date).AddMinutes($Minutes)).ToString(\"yyyy-MM-ddTHH:mm:sszzzzZ\");\n        $ServiceIdArray = @()\n        \n        foreach($ServiceId in $ArrayOfServices){\n        \t$ServiceIdArray += @{\"id\"=$ServiceId; \"type\"=\"service_reference\"}\n        }\n        \n        $Uri = \"https://api.pagerduty.com/maintenance_windows\"\n        $Headers = @{\n          \"Authorization\" = \"Token token=$Token\"\n          \"Accept\" = \"application/vnd.pagerduty+json;version=2\"\n          \"From\" = $RequesterId\n\t\t}\n\n        Write-Host \"Window will be open from $Start -> $End\"\n\n        $Post = @{\n            maintenance_window= @{\n            \ttype = 'maintenance_window'\n                start_time = $Start\n                end_time = $End\n                description = $Description\n                services = $ServiceIdArray\n            }\n        } | ConvertTo-Json -Depth 4\n\n        $ResponseObj = Invoke-RestMethod -Uri $Uri -Method Post -Body $Post -ContentType \"application/json\" -Headers $Headers\n        $WindowId = $ResponseObj.maintenance_window.id\n\n        Write-Host \"Window Id $WindowId created\"\n\n        if(Get-Command -name \"Set-OctopusVariable\" -ErrorAction SilentlyContinue) {\n            Set-OctopusVariable -name \"WindowId\" -value $WindowId\n        } else {\n            Write-Host \"Octopus output variable not set\"\n        }\n    } catch [System.Exception] {\n        Write-Host \"Error while opening PagerDuty window\"\n        Write-Host $_.Exception.Message\n        \n        $ResponseStream = $_.Exception.Response.GetResponseStream()\n        $Reader = New-Object System.IO.StreamReader($ResponseStream)\n        $Reader.ReadToEnd() | Write-Host\n        \n        Exit 1\n    }\n} (Get-Param 'ServiceIds' -Required) (Get-Param 'RequesterId' -Required) (Get-Param 'Description' -Required) (Get-Param 'Minutes' -Required) (Get-Param 'Token' -Required)",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.RunOnServer": "false"
  },
  "Category": "PagerDuty",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/pagerduty-open-maintenance-window.json",
  "Website": "/step-templates/93a10982-f675-42cd-ac3a-46ef28a46afa",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABhQTFRF////QbwAveatVcMrmtp9N7oA1+/NcMxCz2uBkgAAAsJJREFUeNrsm9ua6iAMRktIwvu/8WzHK20NR1vds9bXa+jqD4ht2DYAAAAAAAAAAAAAAAAA+Au4PZK/VUQePVL53kTSA4gggggiiCCCCCKIIIIIIogggggiiCDyN0VcSoyK97aopUpOK0Vcy+115Q17caXfN5qlVca9ZLu3aeGVFopoSa38u7MsDRq7B93MuIgU6+2rlkpJ4wyLaOr1SBaH4jMeoyKebaQ3C7oTs/NFfP1Q1jRHOdfjZYeSLhCZG8x62GS6QmTK43gZzleIzD69vH6CjIkUm+vSfNEaOCmi6/tcEEi/yNxMv+PLZ8iAiMyPAtPlTQ6I6OGWMOCo1/oqaFVmRcp+B1UkQnOuTPe9SKXJ32ZnRfJunHj/RkDCNatlwz/9D/H5pkxHtjQl2p1Y03+wWRF5uqfsI+PxoVe1cAa9SWRsaD6vSzlosinj9SJjveYgkY8W2XpE5AoRe0MiQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiIkQiKLEylflIj3fLH6pkSib4iNTcoliWhUEahWrR5aN1nnEtl9SS/RLTV9nn6utWockJMPooS9ykBR9e7LvfmsiNcQrZRw7AsjbhUcYYtqlfKWfpGUaxzVB8eBVZs9aHJJcdAnlDnJJSIS/+6P1En6FSL7UkBb3uQZIvuVcrrqt3vNWpNIfX1eUhT9bpGjn67ZSPwKkcOnJ+cuWStEXqwvZd1qfo7Iy1M95dQ8pkWC00mja/BQHpMiOTpl5TpSkJ3H8pgSsVqn/cfPkvp2soil0lAgLKXn3ELW0ThGRcxy6/FQ0ZIaTo3Zrf7bt22diDUUsfcdcnXX+2wJjoX2H5td9PLhA/lfRRKJkAiJkAiJkAiJkAiJkAiJkAiJkAiJkAiJkMhXiDy9D/1aEXeRh2sDAAAAAAAAAAAAAAAAgIAfAQYAXo0zRlgxYJcAAAAASUVORK5CYII=",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Thursday, July 23, 2020