Last active
September 30, 2021 20:57
-
-
Save ignatev/f97c7b4d014303d4864f7c1aae005790 to your computer and use it in GitHub Desktop.
create_delete_maintenance_windows_pagerduty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger: | |
- main | |
pool: | |
vmImage: 'ubuntu-latest' | |
steps: | |
- task: UsePythonVersion@0 | |
inputs: | |
versionSpec: '3.9' | |
- script: | | |
pip install pipenv | |
python -m pipenv lock -r > requirements.txt | |
pip install -r requirements.txt | |
displayName: 'Install pipenv, generate requirements.txt, install deps' | |
workingDirectory: $(Build.SourcesDirectory)/pagerduty | |
- script: | | |
python create_maintenance_window.py -d 120 -t SECRET_TOKEN | |
displayName: 'Create maintenance window for $(ServiceName)' | |
workingDirectory: $(Build.SourcesDirectory)/pagerduty | |
- bash: echo $(maintenance_window_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import datetime | |
from pdpyras import APISession | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-d", "--duration", help="Window duration in minutes", type=int) | |
parser.add_argument("-t", "--api-token", help="Pageduty API token") | |
parser.add_argument("-s", "--summary", help="Description for maintenance window") | |
args = parser.parse_args() | |
os.environ["TZ"] = "UTC" | |
session = APISession(args.api_token) | |
maintenance_window_start = datetime.datetime.now() | |
minutes = args.duration | |
duration = datetime.timedelta(minutes = minutes) | |
maintenance_window_end = maintenance_window_start + duration | |
payload = { | |
"type": "maintenance_window", | |
"description": "Immanentizing the eschaton", | |
"services": [ | |
{ | |
"id": "PJF6RWV", | |
"type": "service_reference" | |
} | |
] | |
} | |
payload['start_time'] = maintenance_window_start.strftime("%Y-%m-%dT%H:%M:%SZ") | |
payload['end_time'] = maintenance_window_end.strftime("%Y-%m-%dT%H:%M:%SZ") | |
pd_maintenance_window = session.rpost("maintenance_windows", json=payload) | |
print('##vso[task.setvariable variable=maintenance_window_id;]%s' % (pd_maintenance_window['id'])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import datetime | |
from pdpyras import APISession | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-id", "--window-id", help="Maintenance window id") | |
parser.add_argument("-t", "--api-token", help="Pageduty API token") | |
args = parser.parse_args() | |
os.environ["TZ"] = "UTC" | |
session = APISession(args.api_token) | |
session.rdelete("/maintenance_windows/%s" % args.window_id) | |
print('deleted') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
url = "https://pypi.org/simple" | |
verify_ssl = true | |
name = "pypi" | |
[packages] | |
pdpyras = "4.3.0" | |
[dev-packages] | |
[requires] | |
python_version = "3.9" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment