Skip to content

Commit

Permalink
feat: Add initial orb sources (#1)
Browse files Browse the repository at this point in the history
* feat: Add initial commands for maintenance windows

* fix: Fix duration type

* fix: Fix parameters for create maintenance window

* fix: Fix fail arg for curl

* fix: Evaulate description for maintenance

* feat: Evaluate end maintenance window id

* feat: Add id output for maintenance window

* fix: Fix maintenance window default id

* fix: Fix duplicate eval

* feat: Add run-with-maintenance command

* docs: Add examples, add jq requirement to orb description
  • Loading branch information
Timo Reymann committed Feb 9, 2022
1 parent 5e87890 commit b313f5a
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
4 changes: 3 additions & 1 deletion src/@orb.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
version: 2.1

description: |
PagerDuty functionality for your pipelines
PagerDuty functionality for your pipelines.
This orb requires jq to be installed (which is the case for cimg/* docker images).
display:
home_url: "https://github.com/trustedshops-public/circleci-orb-pagerduty"
source_url: "https://github.com/trustedshops-public/circleci-orb-pagerduty"
12 changes: 0 additions & 12 deletions src/commands/command.yml

This file was deleted.

35 changes: 35 additions & 0 deletions src/commands/create-maintenance-window.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
description: >
Create maintenance window.
The created maintenance window id is stored inside $PAGERDUTY_MAINTENANCE_WINDOW_ID
parameters:
pagerduty-token:
default: PAGERDUTY_TOKEN
description: Environment variable containing pagerduty token for api calls
type: env_var_name
duration:
default: 15
description: Duration of the maintenance interval in minutes
type: integer
description:
default: "CircleCI Deployment ${CIRCLE_WORKFLOW_ID}-${CIRCLE_JOB} | for more information see ${CIRCLE_BUILD_URL}"
description: Description for the maintenance window, can contain env vars
type: string
services:
description: List of services, seperated with comma WITHOUT spaces
type: string
from-mail:
default: PAGERDUTY_EMAIL
description: Environment variable containing a valid mail address as which the pagerduty maintenance window should be created
type: env_var_name

steps:
- run:
name: Create pagerduty maintenance window
environment:
PARAM_PAGERDUTY_TOKEN: <<parameters.pagerduty-token>>
PARAM_PAGERDUTY_MAIL: <<parameters.from-mail>>
PARAM_DURATION_MINUTES: <<parameters.duration>>
PARAM_DESCRIPTION: <<parameters.description>>
PARAM_SERVICES: <<parameters.services>>
command: <<include(scripts/maintenance-window-create.sh)>>
20 changes: 20 additions & 0 deletions src/commands/delete-maintenance-window.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
description: >
Delete maintenance window
parameters:
maintenance-window-id:
default: PAGERDUTY_MAINTENANCE_WINDOW_ID
description: Maintenance window id
type: string
pagerduty-token:
default: PAGERDUTY_TOKEN
description: Environment variable containing pagerduty token for api calls
type: env_var_name

steps:
- run:
name: Delete pagerduty maintenance window
environment:
PARAM_MAINTENANCE_WINDOW_ID: <<parameters.maintenance-window-id>>
PARAM_PAGERDUTY_TOKEN: <<parameters.pagerduty-token>>
command: <<include(scripts/maintenance-window-delete.sh)>>
37 changes: 37 additions & 0 deletions src/commands/run-with-maintenance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
description: >
Run given steps with a maintenance window
parameters:
pagerduty-token:
default: PAGERDUTY_TOKEN
description: Environment variable containing pagerduty token for api calls
type: env_var_name
duration:
default: 15
description: Duration of the maintenance interval in minutes
type: integer
description:
default: "CircleCI Deployment ${CIRCLE_WORKFLOW_ID}-${CIRCLE_JOB} | for more information see ${CIRCLE_BUILD_URL}"
description: Description for the maintenance window, can contain env vars
type: string
services:
description: List of services, seperated with comma WITHOUT spaces
type: string
from-mail:
default: PAGERDUTY_EMAIL
description: Environment variable containing a valid mail address as which the pagerduty maintenance window should be created
type: env_var_name
steps:
type: steps
description: Steps to execute with connection open

steps:
- create-maintenance-window:
pagerduty-token: <<parameters.pagerduty-token>>
duration: <<parameters.duration>>
description: <<parameters.description>>
services: <<parameters.services>>
from-mail: <<parameters.from-mail>>
- <<parameters.steps>>
- delete-maintenance-window:
pagerduty-token: <<parameters.pagerduty-token>>
21 changes: 21 additions & 0 deletions src/examples/example-manual-create-no-end.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
description: Run your deployment with manual control over maintenance
usage:
version: 2.1
orbs:
pagerduty: trustedshops-public/pagerduty@<version>
workflows:
version: 2
continious:
jobs:
- deploy:
steps:
- pagerduty/create-maintenance-window:
services: PXXXX,PXYZ
duration: 60
- run:
name: Run deployment
script: make deploy
# Don't stop, but rather wait one hour because e.g. your legacy system requires some time to recover
filters:
branches:
only: [ main ]
20 changes: 20 additions & 0 deletions src/examples/example-run-with.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
description: Run your deployment with pagerduty maintenance
usage:
version: 2.1
orbs:
pagerduty: trustedshops-public/pagerduty@<version>
workflows:
version: 2
continious:
jobs:
- deploy:
steps:
- pagerduty/run-with-maintenance:
services: PXXXX,PXYZ
steps:
- run:
name: Run deployment
script: make deploy
filters:
branches:
only: [ main ]
14 changes: 0 additions & 14 deletions src/examples/example.yml

This file was deleted.

13 changes: 0 additions & 13 deletions src/executors/default.yml

This file was deleted.

15 changes: 0 additions & 15 deletions src/jobs/job.yml

This file was deleted.

Empty file removed src/scripts/.gitkeep
Empty file.
62 changes: 62 additions & 0 deletions src/scripts/maintenance-window-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

# Check if jq is installed
if ! command -v jq &> /dev/null
then
echo "jq is required, but could not be found."
exit 2
fi

# Join all arguments by given separator
function join_by {
local IFS="$1";
shift;
echo "$*";
}

# Escape string for json
function json_escape() {
jq -R -s '.' <<< "$1"
}

PARAM_DESCRIPTION=$(eval echo "$PARAM_DESCRIPTION")

# date format to use for pagerduty
date_format='%Y-%m-%dT%H:%M:%S-00:00'
pagerduty_token=$(eval echo "\$$PARAM_PAGERDUTY_TOKEN")

# Build service list with json objects
IFS=","
services_json=()
for service in ${PARAM_SERVICES}
do
services_json+=('{"id": "'"${service}"'", "type": "service_reference"}')
done

# Create maintenance
response=$(curl --request POST \
--fail \
--url "https://api.pagerduty.com/maintenance_windows" \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header "Authorization: Token token=${pagerduty_token}" \
--header 'Content-Type: application/json' \
--header "From: ${PARAM_PAGERDUTY_MAIL}" \
--data @- <<TEXT
{
"maintenance_window": {
"type": "maintenance_window",
"start_time": "$(date -u +${date_format})",
"end_time": "$(date -u -d "+ ${PARAM_DURATION_MINUTES} minutes" +${date_format})",
"description": $(json_escape "${PARAM_DESCRIPTION}"),
"services": [$(join_by "," "${services_json[@]}")]
}
}
TEXT
)

# Extract and set env var
id=$(echo "$response" | jq -r ".maintenance_window.id")
export PAGERDUTY_MAINTENANCE_WINDOW_ID="${id}"
echo "export PAGERDUTY_MAINTENANCE_WINDOW_ID='${id}'" >> "$BASH_ENV"

echo "Maintenance window created: ${PAGERDUTY_MAINTENANCE_WINDOW_ID}"
16 changes: 16 additions & 0 deletions src/scripts/maintenance-window-delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

if ! command -v jq &> /dev/null
then
echo "jq is required, but could not be found."
exit 2
fi

pagerduty_token=$(eval echo "\$$PARAM_PAGERDUTY_TOKEN")
PARAM_MAINTENANCE_WINDOW_ID=$(eval echo "\$$PARAM_MAINTENANCE_WINDOW_ID")

curl --request DELETE \
--url "https://api.pagerduty.com/maintenance_windows/${PARAM_MAINTENANCE_WINDOW_ID}" \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header "Authorization: Token token=${pagerduty_token}" \
--header 'Content-Type: application/json' | jq

0 comments on commit b313f5a

Please sign in to comment.