-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagercuty.py
More file actions
320 lines (267 loc) · 9.02 KB
/
pagercuty.py
File metadata and controls
320 lines (267 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python3
"""PagerCuty - A Command-Line Utility for PagerDuty.
This module serves as the main entry point for the PagerCuty CLI tool,
providing command-line argument parsing and orchestration of PagerDuty operations.
"""
import argparse
import subprocess
import sys
from typing import Dict, List, Optional
import pdpyras
from src.pd_essentials import (
authorize,
get_user_id,
get_user_name,
read_config
)
from src.pd_functions import (
check_pagerduty_company_url,
check_pagerduty_status,
check_vim_or_vi_installed,
create_incident_for_service,
get_schedule_ids,
get_service_ids,
open_vim_for_incident_note,
pd_acknowledge_all,
pd_acknowledge_all_loop,
pd_acknowledge_id,
pd_acknowledge_user,
pd_list_all,
pd_list_user,
pd_resolve_all,
pd_resolve_id,
pd_resolve_like,
pd_resolve_user,
read_note_from_file,
tabulate_info
)
def setup_argument_parser(
user_name: str,
ack_interval_secs: int
) -> argparse.ArgumentParser:
"""Create and configure the argument parser.
Args:
user_name: User's display name for help text
ack_interval_secs: Acknowledgment interval in seconds
Returns:
Configured argument parser
"""
parser = argparse.ArgumentParser(
description="PagerCuty - A Command-Line Utility for PagerDuty."
)
parser.add_argument(
"--check_pd_status", "-pds",
action="store_true",
help=(
"Checks the Status of PagerDuty from both, Manual hit to Company's "
"URL and from the Official PagerDuty Status Page"
)
)
parser.add_argument(
"--create_an_incident", "-crin",
action="store_true",
help="Lets you Create a PagerDuty Incident"
)
parser.add_argument(
"-list",
choices=["all", "user"],
help=(
f"all - Lists all Non-Resolved Incidents on PagerDuty, "
f"user - Lists all Incidents on PagerDuty assigned to {user_name}"
)
)
parser.add_argument(
"-ack",
choices=["all", "user", "id"],
help=(
f"all - Acknowledges all [Triggered] Incidents on PagerDuty, "
f"user - Acknowledges all [Triggered] Incidents on PagerDuty "
f"assigned to {user_name}, "
f"id - Acknowledges a Specific Incident from Pagerduty"
)
)
parser.add_argument(
"-ackloop",
choices=["all", "user"],
help=(
f"all - Acknowledges all [Triggered] Incidents on PagerDuty every "
f"{ack_interval_secs} seconds, "
f"user - Acknowledges all [Triggered] Incidents on PagerDuty "
f"assigned to {user_name} every {ack_interval_secs} seconds"
)
)
parser.add_argument(
"--resolve", "-res",
choices=["all", "user", "id", "like"],
help=(
f"all - Resolves all Incidents from PagerDuty, "
f"user - Resolves all Incidents from PagerDuty assigned to "
f"{user_name}, "
f"id - Resolves a Specific Incident from Pagerduty, "
f"like - Resolves all Incidents containing a keyword"
)
)
parser.add_argument(
"--add_note", "-an",
action="store_true",
help="Add note to an Incident (List will be Provided)"
)
parser.add_argument(
"-get_service_ids",
action="store_true",
help="List all Service IDs and their Names"
)
parser.add_argument(
"-get_schedule_ids",
action="store_true",
help="List all Schedule IDs and their Names"
)
return parser
def validate_config(
config: Dict,
session: pdpyras.APISession
) -> Optional[List[str]]:
"""Validate configuration and return service IDs.
Args:
config: Configuration dictionary
session: PagerDuty API session
Returns:
List of service IDs if valid, None if invalid
"""
api_token = config.get("api_token")
service_ids_str = config.get("service_id")
if not api_token:
print("Missing API Token in config.yaml!")
return None
if not service_ids_str:
print(
"Missing Service ID in config.yaml.\n"
"Below is the list of Service IDs you can mention in config.yaml "
"(comma-separated)"
)
service_info = get_service_ids(session)
tabulate_info(service_info, ["Service ID", "Service Name"])
return None
return service_ids_str.split(",")
def handle_check_pd_status(
company_url_pd: str
) -> None:
"""Handle PagerDuty status check command.
Args:
company_url_pd: Company's PagerDuty URL
"""
api_status = "Up" if check_pagerduty_status() else "Down"
company_url_status = (
"Up" if check_pagerduty_company_url(company_url_pd) else "Down"
)
print(f"API Status: {api_status}\nURL Status: {company_url_status}")
def handle_create_incident(
api_token: str,
user_email: str,
session: pdpyras.APISession,
company_domain: str
) -> None:
"""Handle incident creation command.
Args:
api_token: PagerDuty API token
user_email: User's email address
session: PagerDuty API session
company_domain: Company's PagerDuty domain
"""
if not check_vim_or_vi_installed():
print(
"vim/vi editor is not installed. This functionality requires "
"vim/vi editor."
)
return
service_info = get_service_ids(session)
tabulate_info(service_info, ["Service ID", "Service Name"])
inc_service_id = input(
"Please enter the Service ID from the list above for the incident: "
)
inc_title = input("Please enter the Title for your Incident: ")
inc_note_file_path = open_vim_for_incident_note(inc_service_id)
inc_note = read_note_from_file(inc_note_file_path)
create_incident_for_service(
api_token,
user_email,
inc_service_id,
inc_title,
inc_note,
company_domain
)
subprocess.run(["rm", "-rf", inc_note_file_path], check=True)
def main() -> None:
"""Main entry point for PagerCuty CLI tool."""
config = read_config()
api_token = config.get("api_token")
session = pdpyras.APISession(api_token)
service_ids = validate_config(config, session)
if not service_ids:
sys.exit(1)
user_email = config.get("email")
user_id = get_user_id(user_email, authorize(api_token))
user_name = get_user_name(user_email, authorize(api_token))
ack_interval_secs = config.get("time_interval")
company_url_pd = config.get("company_pd_url")
company_domain = config.get("company_domain")
parser = setup_argument_parser(user_name, ack_interval_secs)
args = parser.parse_args()
if not any(vars(args).values()):
parser.print_help()
sys.exit(1)
try:
if args.check_pd_status:
handle_check_pd_status(company_url_pd)
if args.create_an_incident:
handle_create_incident(
api_token,
user_email,
session,
company_domain
)
if args.get_service_ids:
service_info = get_service_ids(session)
tabulate_info(service_info, ["Service ID", "Service Name"])
if args.get_schedule_ids:
schedule_info = get_schedule_ids(session)
tabulate_info(schedule_info, ["Schedule ID", "Schedule Name"])
if args.list == "all":
pd_list_all(service_ids, session)
elif args.list == "user":
pd_list_user(service_ids, session, user_id, user_name)
if args.ack == "all":
pd_acknowledge_all(session)
elif args.ack == "user":
pd_acknowledge_user(user_id, session, user_name)
elif args.ack == "id":
pd_acknowledge_id(session, service_ids)
if args.ackloop == "all":
print(
f"Hi {user_name},\n"
f"All [Triggered] Incidents will be Acknowledged every "
f"{ack_interval_secs} seconds..."
)
pd_acknowledge_all_loop(session, ack_interval_secs)
elif args.ackloop == "user":
print(
f"Hi {user_name},\n"
f"Your [Triggered] Incidents will be Acknowledged every "
f"{ack_interval_secs} seconds..."
)
pd_acknowledge_all_loop(session, ack_interval_secs)
if args.resolve == "all":
pd_resolve_all(session)
elif args.resolve == "user":
pd_resolve_user(user_id, session, user_name)
elif args.resolve == "id":
pd_resolve_id(session, service_ids)
elif args.resolve == "like":
pd_resolve_like(session, service_ids)
except Exception as exc:
print(f"An error occurred: {exc}")
sys.exit(1)
finally:
session.close()
if __name__ == "__main__":
main()