forked from scottythered/exlibris-status-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamo_updater.py
316 lines (291 loc) · 13.7 KB
/
dynamo_updater.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
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
#!/usr/bin/env python
# coding: utf-8
import re
from lxml import etree
import requests
from datetime import datetime
from dateutil import tz
import boto3
import os
def utc_time_now():
from_zone = tz.gettz("UTC")
return (datetime.utcnow()).replace(tzinfo=from_zone)
def utc_to_phoenix_time(time):
from_zone = tz.gettz("UTC")
to_zone = tz.gettz("America/Phoenix")
time_zoned = time.replace(tzinfo=from_zone)
return time_zoned.astimezone(to_zone)
def phoenix_time_now():
return utc_to_phoenix_time(utc_time_now())
def message_time_parse(text, mode):
times = re.search(
"Estimated Start: (.*) UTC Estimated End: (.*) UTC Description:", text
)
if mode == "start":
return datetime.strptime(times.group(1), "%A, %Y-%B-%d %H:%M")
elif mode == "stop":
return datetime.strptime(times.group(2), "%A, %Y-%B-%d %H:%M")
else:
raise NameError("Time mode not understood")
def matcherator(pattern, text):
if pattern == "front":
regex_pattern = r"\d\d\-[a-zA-z]{3}\-\d{4} UTC \d{1,2}\:\d{2}\:\d{2}"
elif pattern == "back":
regex_pattern = r"Ex Libris Cloud Services"
matches = re.findall(regex_pattern, text)
if len(matches) >= 1:
counter = 1
if pattern == "front":
for match in matches:
text = text.replace(
match, ("<match_{0}>{1}".format(str(counter), match))
)
counter += 1
elif pattern == "back":
for match in matches:
text = text.replace(
match, ("</match_{1}>".format(match, str(counter))), 1
)
counter += 1
return text
else:
return text
def changeomatic(raw_input):
first_pass = matcherator("front", raw_input)
second_pass = matcherator("back", first_pass)
return second_pass
def handler(event, context):
# get current time, check ExL's api
now = (utc_to_phoenix_time(utc_time_now())).strftime("%Y-%m-%d %H:%M")
body = {"act": "get_status", "client": "xml", "envs": "Primo MT NA04"}
raw_exlib_api_status = (
requests.post("https://status.exlibrisgroup.com/?page_id=5511", data=body)
).text
# retrieve previous ExL api status in DynamoDB
dynamodb = boto3.resource(
"dynamodb",
region_name=os.environ["region_name"],
aws_access_key_id=os.environ["access_key"],
aws_secret_access_key=os.environ["secret_access"],
)
table = dynamodb.Table(os.environ["table"])
response = table.get_item(Key={"product": "Primo"})
# if status is the same, only change the update date; if different, update entire DynamoDB entry
if response["Item"]["raw_api_response"] == raw_exlib_api_status:
table.update_item(
Key={"product": "Primo"},
UpdateExpression="SET last_update = :val1",
ExpressionAttributeValues={":val1": now},
)
else:
parsed_exlib_api_status = (
raw_exlib_api_status.replace("<br />", "")
.replace("<b>", "")
.replace("</b>", "")
.replace("\\n", " ")
.replace("\n", " ")
.replace("\\r", "")
.replace("\r", "")
).strip()
parsed_exlib_api_status = re.sub(r"\s{2,}", " ", parsed_exlib_api_status)
parsed_exlib_api_status = re.sub(r"<a[^<]+?>|</a>", "", parsed_exlib_api_status)
root = etree.fromstring(parsed_exlib_api_status)
exlib_api_data = (root.xpath("/exlibriscloudstatus/instance")[0]).attrib
asu_api = {}
asu_api["system_id"] = exlib_api_data["id"]
asu_api["system_service"] = exlib_api_data["service"]
if (
exlib_api_data["status"] == "OK"
and len(root.xpath("/exlibriscloudstatus/instance/schedule")) == 0
and len(root.xpath("/exlibriscloudstatus/instance/message")) == 0
):
asu_api["service_status"] = "OK"
asu_api["maintenance"] = False
asu_api["affected_env"] = "NA"
asu_api["maintenance_start"] = "NA"
asu_api["maintenance_stop"] = "NA"
asu_api["maintenance_message"] = "NA"
asu_api["maintenance_date"] = "NA"
elif (
exlib_api_data["status"] == "OK"
and len(root.xpath("/exlibriscloudstatus/instance/schedule")) == 1
and len(root.xpath("/exlibriscloudstatus/instance/message")) == 0
):
changed_exlib_api_status = changeomatic(parsed_exlib_api_status)
new_root = etree.fromstring(changed_exlib_api_status)
if len(new_root.xpath("/exlibriscloudstatus/instance/schedule/*")) == 1:
env = re.search(
"we will be performing the following maintenance on your (Sandbox|Production) environment",
changed_exlib_api_status,
)
asu_api["affected_env"] = env.group(1)
asu_api["service_status"] = "OK, Maintenance Scheduled"
asu_api["maintenance"] = True
asu_api["maintenance_start"] = utc_to_phoenix_time(
message_time_parse(changed_exlib_api_status, "start")
)
asu_api["maintenance_stop"] = utc_to_phoenix_time(
message_time_parse(changed_exlib_api_status, "stop")
)
asu_api["maintenance_message"] = (
"Due to routine maintenance, Library One Search may be unavailable between {0} and {1}, Phoenix time. "
"We apologize for the inconvenience.".format(
(asu_api["maintenance_start"]).strftime("%b %d at %I:%M %p"),
(asu_api["maintenance_stop"]).strftime("%b %d at %I:%M %p"),
)
)
asu_api["maintenance_date"] = (
message_time_parse(parsed_exlib_api_status, "start")
).strftime("%Y-%m-%d %H:%M")
elif len(new_root.xpath("/exlibriscloudstatus/instance/schedule/*")) > 1:
temp = []
prog = re.compile(r"\d\d-[a-zA-z]{3}-\d{4} UTC \d{1,2}:\d{2}:\d{2}")
for found in new_root.xpath("/exlibriscloudstatus/instance/schedule/*"):
result = prog.match(found.text)
dash_stripped = (result[0]).split(" ")[0]
time_obj = datetime.strptime(dash_stripped, "%d-%b-%Y")
temp.append(time_obj)
index = temp.index(min(temp))
earliest_exlib_api_status = (
new_root.xpath("/exlibriscloudstatus/instance/schedule/*")[index]
).text
env = re.search(
"we will be performing the following maintenance on your (Sandbox|Production) environment",
earliest_exlib_api_status,
)
asu_api["affected_env"] = env.group(1)
asu_api["service_status"] = "OK, Maintenance Scheduled"
asu_api["maintenance"] = True
asu_api["maintenance_start"] = utc_to_phoenix_time(
message_time_parse(earliest_exlib_api_status, "start")
)
asu_api["maintenance_stop"] = utc_to_phoenix_time(
message_time_parse(earliest_exlib_api_status, "stop")
)
asu_api["maintenance_message"] = (
"Due to routine maintenance, Library One Search may be unavailable between {0} and {1}, Phoenix time. "
"We apologize for the inconvenience.".format(
(asu_api["maintenance_start"]).strftime("%b %d at %I:%M %p"),
(asu_api["maintenance_stop"]).strftime("%b %d at %I:%M %p"),
)
)
asu_api["maintenance_date"] = (
message_time_parse(parsed_exlib_api_status, "start")
).strftime("%Y-%m-%d %H:%M")
else:
asu_api["service_status"] = "OK"
asu_api["maintenance"] = False
asu_api["affected_env"] = "NA"
asu_api["maintenance_start"] = "NA"
asu_api["maintenance_stop"] = "NA"
asu_api["maintenance_message"] = "NA"
asu_api["maintenance_date"] = "NA"
elif (
exlib_api_data["status"] == "MAINT"
and len(root.xpath("/exlibriscloudstatus/instance/message")) == 1
and len(root.xpath("/exlibriscloudstatus/instance/schedule")) == 1
):
asu_api["service_status"] = "Maintenance In-Progress"
asu_api["maintenance"] = True
asu_api["maintenance_start"] = utc_to_phoenix_time(
message_time_parse(parsed_exlib_api_status, "start")
)
asu_api["maintenance_stop"] = utc_to_phoenix_time(
message_time_parse(parsed_exlib_api_status, "stop")
)
asu_api["maintenance_message"] = (
"Due to routine maintenance, Library One Search may be unavailable between {0} and {1}, Phoenix time. "
"We apologize for the inconvenience.".format(
(asu_api["maintenance_start"]).strftime("%b %d at %I:%M %p"),
(asu_api["maintenance_stop"]).strftime("%b %d at %I:%M %p"),
)
)
asu_api["maintenance_date"] = (
message_time_parse(parsed_exlib_api_status, "start")
).strftime("%Y-%m-%d %H:%M")
try:
env = re.search(
"on your Primo (Sandbox|Production) environment",
parsed_exlib_api_status,
)
asu_api["affected_env"] = env.group(1)
except:
asu_api["affected_env"] = "NA"
elif (
exlib_api_data["status"] == "OK"
and len(root.xpath("/exlibriscloudstatus/instance/message")) == 1
and len(root.xpath("/exlibriscloudstatus/instance/schedule")) == 1
and "The scheduled maintenance on your environment has now finished."
in (root.xpath("/exlibriscloudstatus/instance/message")[0]).text
):
try:
regex_pattern = r"\d\d\-[a-zA-z]{3}\-\d{4} UTC \d{1,2}\:\d{2}\:\d{2}"
matches = re.findall(
regex_pattern,
(root.xpath("/exlibriscloudstatus/instance/schedule")[0].text),
)
if len(matches) >= 1:
env = re.search(
"we will be performing the following maintenance on your (Sandbox|Production) environment",
parsed_exlib_api_status,
)
asu_api["affected_env"] = env.group(1)
asu_api["service_status"] = "OK, Maintenance Scheduled"
asu_api["maintenance"] = True
asu_api["maintenance_start"] = utc_to_phoenix_time(
message_time_parse(parsed_exlib_api_status, "start")
)
asu_api["maintenance_stop"] = utc_to_phoenix_time(
message_time_parse(parsed_exlib_api_status, "stop")
)
asu_api["maintenance_message"] = (
"Due to routine maintenance, Library One Search may be unavailable between {0} and {1}, Phoenix time. "
"We apologize for the inconvenience.".format(
(asu_api["maintenance_start"]).strftime(
"%b %d at %I:%M %p"
),
(asu_api["maintenance_stop"]).strftime("%b %d at %I:%M %p"),
)
)
asu_api["maintenance_date"] = (
message_time_parse(parsed_exlib_api_status, "start")
).strftime("%Y-%m-%d %H:%M")
else:
asu_api["service_status"] = "OK, Maintenance Completed"
asu_api["affected_env"] = "NA"
asu_api["maintenance"] = False
asu_api["maintenance_start"] = "NA"
asu_api["maintenance_stop"] = "NA"
asu_api["maintenance_message"] = "NA"
except:
asu_api["service_status"] = "OK, Maintenance Completed"
asu_api["affected_env"] = "NA"
asu_api["maintenance"] = False
asu_api["maintenance_start"] = "NA"
asu_api["maintenance_stop"] = "NA"
asu_api["maintenance_message"] = "NA"
else:
asu_api["service_status"] = "unknown"
asu_api["maintenance"] = "unknown"
asu_api["affected_env"] = "unknown"
asu_api["maintenance_start"] = "unknown"
asu_api["maintenance_stop"] = "unknown"
asu_api["maintenance_message"] = "unknown"
asu_api["maintenance_date"] = "unknown"
table.update_item(
Key={"product": "Primo"},
UpdateExpression=(
"SET last_update = :val1, affected_env = :val2, maintenance = :val3, "
"maintenance_message = :val4, raw_api_response = :val5, "
"service_status = :val6, maintenance_date = :val7"
),
ExpressionAttributeValues={
":val1": now,
":val2": asu_api["affected_env"],
":val3": asu_api["maintenance"],
":val4": asu_api["maintenance_message"],
":val5": raw_exlib_api_status,
":val6": asu_api["service_status"],
":val7": asu_api["maintenance_date"],
},
)