forked from joaquincasares/pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·256 lines (204 loc) · 7.99 KB
/
cli.py
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
#!/usr/bin/env python
import os
import smtplib
import sys
import ConfigParser
from optparse import OptionParser
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from optparse import OptionParser
import pagerduty
options = False
config = False
secondary = False
def email_msg(subject, msg_to, text, html):
msg_from = '"Pager Duty Scheduling"'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = ''
if reply_to:
msg.add_header('reply-to', reply_to)
# Setup bcc email addresses
bcc = ['']
msg_to = filter(None, msg_to + bcc)
part1 = MIMEText(text, 'plain')
msg.attach(part1)
part2 = MIMEText(html, 'html')
msg.attach(part2)
# Send the message via gmail SMTP server.
s = smtplib.SMTP(config.get('SMTP', 'server'))
s.starttls()
s.login(config.get('SMTP', 'email'),config.get('SMTP', 'password'))
s.sendmail(msg_from, msg_to, msg.as_string())
s.quit()
def format_results(primary, secondary=False, html=False):
if not secondary:
dates = primary.keys()
dates.sort()
placeholder = '{0:40}{1:30}\n'
if html:
placeholder = '<tr><td>{0}</td><td>{1}</td></tr>\n'
result = placeholder.format('Shift Start', 'Primary')
for date in dates:
result += placeholder.format(primary[date]['shift_start'], primary[date]['agent_name'])
else:
dates = primary.keys() + secondary.keys()
dates = set(dates)
dates = sorted(dates)
placeholder = '{0:40}{1:30}{2:30}\n'
if html:
placeholder = '<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>\n'
result = placeholder.format('Shift Start', 'Primary', 'Secondary')
for date in dates:
shift_start = primary[date]['shift_start'] if date in primary else secondary[date]['shift_start']
primary_agent = primary[date]['agent_name'] if date in primary else ''
secondary_agent = secondary[date]['agent_name'] if date in secondary else ''
result += placeholder.format(shift_start, primary_agent, secondary_agent)
if html:
return '<table border="1" cellpadding="5">\n%s</table>' % result
return result
def extract_emails(primary, secondary=False):
email_list = []
for key in primary.keys():
email_list.append(primary[key]['agent_email'])
if secondary:
for key in secondary.keys():
email_list.append(secondary[key]['agent_email'])
email_list = set(email_list)
email_list = sorted(email_list)
return email_list
def list_user_90_days(user):
global secondary
primary = pagerduty.get_user_schedule(needle_name=user)
if secondary:
secondary = pagerduty.get_user_schedule(secondary, needle_name=user)
print format_results(primary, secondary)
def list_90_days():
global secondary
primary = pagerduty.get_user_schedule()
if secondary:
secondary = pagerduty.get_user_schedule(secondary)
print format_results(primary, secondary)
def list_day():
global secondary
primary = pagerduty.get_daily_schedule()
if secondary:
secondary = pagerduty.get_daily_schedule(secondary)
print format_results(primary, secondary)
def list_tomorrow():
global secondary
primary = pagerduty.get_tomorrows_schedule()
if secondary:
secondary = pagerduty.get_tomorrows_schedule(secondary)
print format_results(primary, secondary)
def list_week():
global secondary
primary = pagerduty.get_weekly_schedule()
if secondary:
secondary = pagerduty.get_weekly_schedule(secondary)
print format_results(primary, secondary)
def email_today():
global secondary
primary = pagerduty.get_daily_schedule()
if secondary:
secondary = pagerduty.get_daily_schedule(secondary)
email = """Hello,
We wanted to notify you that you're on call today.
Once again, the Support Team appreciates your contributions. However, if you cannot fulfill your duty, please notify your backup immediately. It is vital for you to communicate this with your teammate.
%s
Thanks,
The Support Team
"""
txt = email % format_results(primary, secondary)
html = email.replace('\n', '<br>\n') % format_results(primary, secondary, html=True)
print txt
email_list = extract_emails(primary, secondary)
print 'Email list: %s' % email_list
if bypass_prompts or raw_input('Send email? [y/N] ').lower() == 'y':
email_msg("You're on call today", email_list, txt, html)
print "Daily Support Schedule Emailed!"
else:
print "Email not sent."
def email_week():
global secondary
primary = pagerduty.get_weekly_schedule()
if secondary:
secondary = pagerduty.get_weekly_schedule(secondary)
email = """Hello Team,
Below is the Pager Duty schedule for this week.
Once again, the Support Team appreciates your contributions. However, if you cannot fulfill your duty, please notify your backup immediately. It is vital for you to communicate this with your teammate.
%s
Thanks,
The Support Team
"""
txt = email % format_results(primary, secondary)
html = email.replace('\n', '<br>\n') % format_results(primary, secondary, html=True)
print txt
email_list = extract_emails(primary, secondary)
print 'Email list: %s' % email_list
if bypass_prompts or raw_input('Send email? [y/N] ').lower() == 'y':
email_msg("You're on call this week", email_list, txt, html)
print "Weekly Support Schedule Emailed!"
else:
print "Email not sent."
def read_configurations():
global config
global secondary
global bypass_prompts
global reply_to
configfile = os.path.join(os.path.expanduser('~'), '.pagerduty.cfg')
if not os.path.exists(configfile):
sys.stderr.write('Move pagerduty.cfg to ~/.pagerduty.cfg to begin.\n')
sys.exit(1)
config = ConfigParser.RawConfigParser()
config.read(configfile)
secondary = config.get('Cli', 'secondary_schedule') if config.has_option('Cli', 'secondary_schedule') else False
bypass_prompts = config.get('Cli', 'bypass_prompts') if config.has_option('Cli', 'bypass_prompts') else False
bypass_prompts = bypass_prompts.lower() == 'true'
reply_to = config.get('Cli', 'reply_to') if config.has_option('Cli', 'reply_to') else False
def parse_options():
global options
global parser
parser = OptionParser()
parser.add_option("-u", "--user", dest="user",
help="Show a user's rotations")
parser.add_option("-d", "--day", dest="day",
action="store_true", help="Show today's schedule")
parser.add_option("-t", "--tomorrow", dest="tomorrow",
action="store_true", help="Show tomorrow's schedule")
parser.add_option("-w", "--week", dest="week",
action="store_true", help="Show the week's schedule")
parser.add_option("-l", "--list", dest="list",
action="store_true", help="List the next 90 days")
parser.add_option("--email_today", dest="email_today",
action="store_true", help="Email the today's schedule")
parser.add_option("--email_week", dest="email_week",
action="store_true", help="Email the current week's schedule")
(options, args) = parser.parse_args()
def main():
parse_options()
read_configurations()
if len(sys.argv) > 1:
if options.user:
list_user_90_days(options.user)
elif options.list:
list_90_days()
elif options.day:
list_day()
elif options.tomorrow:
list_tomorrow()
elif options.week:
list_week()
elif options.email_today:
email_today()
elif options.email_week:
email_week()
else:
parser.print_help()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print