-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwdcloud.py
More file actions
266 lines (222 loc) · 8.09 KB
/
wdcloud.py
File metadata and controls
266 lines (222 loc) · 8.09 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
# -*- coding: utf-8 -*-
"""This module implements an abstract class loading a more specific cloud class.
Author: Peter Pakos <peter.pakos@wandisco.com>
Copyright (C) 2019 WANdisco
"""
from __future__ import print_function
import os
import abc
import prettytable
from string import Template
from ppmail import Mailer
from CONFIG import CONFIG
import logging
log = logging.getLogger('cloud_tools')
class WDCloud(object):
VERSION = '1.2.2'
__metaclass__ = abc.ABCMeta
def __init__(self, cloud_provider, profile_name):
cloud_names = {
'aws': 'AWS',
'gcp': 'GCP',
'azure': 'Azure'
}
self._cloud_name = cloud_names[cloud_provider]
self._profile_name = profile_name
self._regions = []
try:
self._mailer = Mailer(slack=True)
except Exception as e:
log.critical(e)
exit(1)
self._bp_url = {
'AWS': 'https://workspace.wandisco.com/display/IT/AWS+Best+Practices+at+WANdisco',
'GCP': 'https://workspace.wandisco.com/display/IT/GCP+Best+Practices+at+WANdisco',
'Azure': 'https://workspace.wandisco.com/display/IT/Azure+Best+Practices+at+WANdisco'
}
@staticmethod
def loader(cloud_provider, profile_name):
module = __import__('wd' + cloud_provider)
return getattr(module, cloud_provider.upper())(cloud_provider, profile_name)
@abc.abstractmethod
def list(self, *args, **kwargs):
pass
@abc.abstractmethod
def tag(self, *args, **kwargs):
pass
def exclude(self, instance_id, *args, **kwargs):
self.tag(instance_id=instance_id, key='EXCLUDE', value='True', *args, **kwargs)
def include(self, instance_id, *args, **kwargs):
self.tag(instance_id=instance_id, key='EXCLUDE', value='False', *args, **kwargs)
def list_regions(self, disable_border, disable_header, *args, **kwargs):
table = prettytable.PrettyTable(['Region'], border=not disable_border, header=not disable_header,
sortby='Region')
table.align = 'l'
for region in self._regions:
table.add_row([region])
print(table)
@staticmethod
def _get_uptime(seconds):
y = divmod(seconds, 86400*364)
w = divmod(y[1], 86400*7)
d = divmod(w[1], 86400)
h = divmod(d[1], 3600)
m = divmod(h[1], 60)
s = m[1]
uptime = []
if y[0] > 0:
uptime.append('%dy' % y[0])
if w[0] > 0:
uptime.append('%dw' % w[0])
if d[0] > 0:
uptime.append('%dd' % d[0])
if h[0] > 0:
uptime.append('%dh' % h[0])
if m[0] > 0:
uptime.append('%dm' % m[0])
uptime.append('%ds' % s)
uptime = ' '.join(uptime)
return uptime
@staticmethod
def _date_diff(date1, date2):
diff = (date1 - date2)
diff = (diff.microseconds + (diff.seconds + diff.days * 24 * 3600) * 10 ** 6) / 10 ** 6
return diff
def _send_alert(self, mail_type, user, region_ids, name_dict, uptime_dict, warning_threshold, critical_threshold,
stop=False, dept=None, rg_dict=None, resource='instance'):
user_name = user.split('.')[0].capitalize()
profiles = dept if dept else [self._profile_name.upper()]
number = 0
if self._cloud_name == 'Azure':
table = prettytable.PrettyTable(['Region', 'RG', 'Name', 'Uptime'])
for region, ids in region_ids.items():
number += len(ids)
for iid in ids:
table.add_row([
region,
rg_dict[iid],
name_dict[iid],
uptime_dict[iid]
])
else:
table = prettytable.PrettyTable(['Region', 'Instance ID', 'Name', 'Uptime'])
for region, ids in region_ids.items():
number += len(ids)
for iid in ids:
table.add_row([
region,
iid,
name_dict[iid],
uptime_dict[iid]
])
table.align = 'l'
if number > 1:
s = 's'
have = 'have'
else:
s = ''
have = 'has'
if len(profiles) > 1:
ss = 's'
else:
ss = ''
if number > 1:
some_of_them = 'and either all or some of them'
else:
some_of_them = 'that'
sender = CONFIG.EMAIL_FROM
if user.endswith('.api'):
user = user.replace('.api', '')
recipient = user + '@' + CONFIG.EMAIL_DOMAIN
cc_recipient = []
if mail_type in ['warning', 'critical']:
for profile in profiles:
profile = profile.partition('-')[0].lower()
if profile in CONFIG.HEADS:
for head in CONFIG.HEADS[profile]:
if head not in cc_recipient:
cc_recipient.append(head)
if recipient in cc_recipient:
cc_recipient.remove(recipient)
if cc_recipient:
cc = ' (cc: %s)' % ', '.join(cc_recipient)
else:
cc = ''
subject = '%s %s %s: running %ss' % (mail_type.upper(), self._cloud_name, '/'.join(profiles), resource)
stop_term = 'STOPPED' if resource == 'instance' else 'DELETED'
if stop:
stop_msg = '\nANY RESOURCES RUNNING FOR LONGER THAN %s HOURS WILL BE %s IMMEDIATELY!\
\n\nPlease check your %s account and make sure there are no more offending resources.\n' % \
(critical_threshold, stop_term, self._cloud_name)
else:
stop_msg = '\nPLEASE IMMEDIATELY STOP OR TERMINATE ANY RESOURCES THAT ARE NO LONGER IN USE!\n'
template = open('%s/templates/%s.txt' % (os.path.dirname(os.path.realpath(__file__)), mail_type))
template = Template(template.read())
message = template.substitute({
'user_name': user_name,
'number': number,
's': s,
'cloud': self._cloud_name,
'profile': '/'.join(profiles),
'ss': ss,
'table': table,
'warning_threshold': warning_threshold,
'critical_threshold': critical_threshold,
'bp_url': self._bp_url[self._cloud_name],
'some_of_them': some_of_them,
'stop_msg': stop_msg,
'have': have,
'resource': resource,
'stop_term': stop_term
})
print('Sending %s notification to %s%s... ' % (mail_type, recipient, cc), end='')
response = self._mailer.send(
sender=sender,
recipients=recipient,
subject=subject,
message=message,
code=True,
cc=cc_recipient
)
if response:
print('SUCCESS')
else:
print('FAIL')
def _check_region(self, region):
if region not in self._regions:
print('Region must be one of the following:\n- %s' %
'\n- '.join(self._regions))
exit(1)
else:
self._regions = [region]
@abc.abstractmethod
def sg(self, *args, **kwargs):
pass
@abc.abstractmethod
def public_buckets(self, *args, **kwargs):
pass
@abc.abstractmethod
def create_image(self, *args, **kwargs):
pass
@abc.abstractmethod
def run(self, *args, **kwargs):
pass
@abc.abstractmethod
def stop(self, *args, **kwargs):
pass
@abc.abstractmethod
def start(self, *args, **kwargs):
pass
@abc.abstractmethod
def terminate(self, *args, **kwargs):
pass
@abc.abstractmethod
def list_hdi(self, *args, **kwargs):
pass
@staticmethod
def _ip_sum(ip, n):
import socket
import struct
unpacked = struct.unpack('!L', socket.inet_aton(ip))[0]
packed = socket.inet_ntoa(struct.pack('!L', unpacked + n))
return packed