-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccu.py
executable file
·125 lines (84 loc) · 3.23 KB
/
ccu.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
#!/bin/env python
#
# distributed from ansible
#
#
# Talk to the Akamai API.
# this module being imported in akapi
### Imports
import email
import json
import smtplib
import sys
import time
## do_purge
#
# Do a purge.
def do_purge(args, httpCaller):
# Assemble the data to post.
data = { "type": args.type,
"objects": args.objects }
if args.type == 'cpcode':
data['action'] = "remove"
# If we're going to notify anyone, prepare to stash notification text.
if args.notify:
notification_text="Here are the results of purging {} {}:\n\n".format(args.type, args.objects)
# Submit the purge request, stash the result for later notification if
# we're notifying anyone, and print the result right now too unless quiet
# is on.
result = httpCaller.postResult('/ccu/v2/queues/default', json.dumps(data))
result_text = "Purge request sent. Result:\n\n"
result_text += json.dumps(result, sys.stdout, sort_keys=True, indent=1)
if args.notify:
notification_text = notification_text + result_text + "\n"
if not args.quiet:
print result_text
# Wait a bit before we start checking status.
waiting_text = "\nWaiting {0} seconds before starting to ping for status...\n".format(result['pingAfterSeconds'])
if args.notify:
notification_text = notification_text + waiting_text + "\n"
if not args.quiet:
print waiting_text
time.sleep(result['pingAfterSeconds'])
# Stash the original result, and clear the result variable for reuse.
purge_result = result
progress_uri = result['progressUri']
result = {}
result['purgeStatus'] = ""
# Check on the progress, until it finishes. Each time, stash the result
# for later notification if we're notifying anyone, and print the result
# right now too unless quiet is on.
while result['purgeStatus'] != "Done":
result = httpCaller.getResult(progress_uri)
result_text = "Status request sent. Result:\n\n"
result_text += json.dumps(result, sys.stdout, sort_keys=True, indent=1)
if args.notify:
notification_text = notification_text + result_text + "\n"
if not args.quiet:
print result_text
if 'pingAfterSeconds' in result:
waiting_text = "\nWaiting {0} seconds before pinging for status again...\n".format(result['pingAfterSeconds'])
if args.notify:
notification_text = notification_text + waiting_text + "\n"
if not args.quiet:
print waiting_text
time.sleep(result['pingAfterSeconds'])
# If we get this far, we're done! Say so, and send a notification if we're
# notifying anyone.
if not args.quiet:
print "\nDone!\n"
if args.notify:
notification_text = notification_text + "\nDone!\n\n"
msg = email.MIMEText.MIMEText(notification_text)
msg.add_header('From', 'Akamai Cache Purge Script <niranjan.bommu@gmail.com>')
msg.add_header('To', args.notify)
msg.add_header('Subject', "Akamai cache purge notification")
smtpconnection = smtplib.SMTP("localhost")
smtpconnection.ehlo()
smtpconnection.sendmail("niranjan.bommu@gmail.com", args.notify, msg.as_string())
smtpconnection.quit()
if not args.quiet:
if args.notify:
print "(sent notification to {0})\n".format(args.notify)
else:
print "(no e-mail notification sent)\n"