forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy.py
executable file
·316 lines (295 loc) · 12.2 KB
/
legacy.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
import pprint
from datetime import datetime, timedelta
from email.mime.text import MIMEText
from himlarcli.keystone import Keystone
from himlarcli.nova import Nova
from himlarcli.glance import Glance
from himlarcli import utils as himutils
from himlarcli.mail import Mail
#from himlarcli.state import State
from himlarcli.parser import Parser
from himlarcli.parser import Printer
import novaclient.exceptions as novaexc
import time
import sys
from datetime import date, datetime
# OPS! It might need some updates. We use class Mail instead of Notify now.
himutils.is_virtual_env()
# Default value for date: today + 5 days at 14:00
today = datetime.today()
date = datetime(today.year, today.month, today.day, 15, 0) + timedelta(days=5)
# Load parser config from config/parser/*
parser = Parser()
parser.update_default('-m', date.strftime('%Y-%m-%d around %H:00'))
options = parser.parse_args()
printer = Printer(options.format)
ksclient = Keystone(options.config, debug=options.debug)
logger = ksclient.get_logger()
novaclient = Nova(options.config, debug=options.debug, log=logger)
novaclient.set_dry_run(options.dry_run)
glclient = Glance(options.config, debug=options.debug, log=logger)
domain = 'Dataporten'
zone = '%s-default-1' % ksclient.region
# aggregate in <loc>-legacy-1 AZ
legacy_aggregate = ['group1', 'group2', 'group3']
if 'host' in options and options.host:
if '.' in options.host:
host = options.host
else:
domain = ksclient.get_config('openstack', 'domain')
host = options.host + '.' + domain
else:
host = None
def action_show():
aggregate = novaclient.get_aggregate(options.aggregate)
print('\nMETADATA:')
for key, value in aggregate.metadata.items():
print("%s = %s" % (key, value))
print('\nHOSTS:')
for h in aggregate.hosts:
services = novaclient.get_service(h)
print('%s (%s)' % (h, services[0].status))
def action_list():
filters = dict()
if options.az:
filters['availability_zone'] = options.az
aggregates = novaclient.get_filtered_aggregates(**filters)
for aggregate in aggregates:
header = '%s (%s)' % (aggregate.name, aggregate.availability_zone)
enabled = 'active' if 'enabled' in aggregate.metadata else 'deactive'
printer.output_dict({'header': header})
if options.detailed:
printer.output_dict(aggregate.to_dict())
else:
printer.output_dict({'name': aggregate.name, 'status': enabled})
def action_instances():
instances = novaclient.get_instances(options.aggregate, host)
pp = pprint.PrettyPrinter(indent=1)
stats = dict()
for i in instances:
if i.status in stats:
stats[i.status] += 1
else:
stats[i.status] = 1
network = next(iter(i.addresses))
print('%s %s (status=%s, network=%s)' % (i.id, str(i.name), i.status, network))
print("\nSTATUS:")
print("=======")
pp.pprint(stats)
def action_users():
users = novaclient.get_users(options.aggregate, simple=True)
for user in users:
print(user)
def action_activate():
aggregates = novaclient.get_aggregates()
dry_run_txt = 'DRY-RUN: ' if options.dry_run else ''
for aggregate in aggregates:
# do not use on central1 or placeholder1
if aggregate not in legacy_aggregate:
continue
print('=============== %s ================' % aggregate)
metadata = novaclient.get_aggregate(aggregate)
# Enable this aggregate
if aggregate == options.aggregate:
for h in metadata.hosts:
print('%sEnable %s' % (dry_run_txt, h))
novaclient.enable_host(h)
tags = {'enabled': datetime.today(), 'disabled': None, 'mail': None}
if not options.dry_run:
novaclient.update_aggregate(aggregate, tags)
else: # Disable everything else
for h in metadata.hosts:
services = novaclient.get_service(h)
if services[0].status == 'enabled':
print('%sDisable %s' % (dry_run_txt, h))
novaclient.disable_host(h)
tags = {'disabled': datetime.today(), 'enabled': None}
if not options.dry_run:
novaclient.update_aggregate(aggregate, tags)
def action_migrate():
# Find enabled aggregate
aggregates = novaclient.get_aggregates()
active_aggregate = 'unknown'
for aggregate in aggregates:
if aggregate not in legacy_aggregate:
continue
info = novaclient.get_aggregate(aggregate)
if 'enabled' in info.metadata:
active_aggregate = aggregate
break
if active_aggregate == 'unknown':
himutils.sys_error('Could not find enabled aggregate to migrate to. Make sure to activate aggregate first!')
q = 'Migrate all instances from %s to %s' % (options.aggregate, active_aggregate)
if not himutils.confirm_action(q):
return
instances = novaclient.get_instances(options.aggregate, host)
count = 0
target_host = next(iter(novaclient.get_aggregate_hosts(active_aggregate) or []), None)
if not target_host:
himutils.sys_error('Could not find valid host in aggregate %s' % active_aggregate)
for instance in instances:
count += 1
if options.dry_run:
logger.debug('=> DRY-RUN: migrate instance %s' % str(instance.name))
else:
logger.debug('=> migrate instance %s' % str(instance.name))
try:
instance.migrate(host=target_host.hypervisor_hostname)
time.sleep(2)
if count%options.limit == 0 and (options.hard_limit and count < options.limit):
logger.debug('=> sleep for %s seconds', options.sleep)
time.sleep(options.sleep)
except novaexc.BadRequest as e:
sys.stderr.write("%s\n" % e)
sys.stderr.write("Error found. Cancel migration!\n")
break
if options.hard_limit and count >= options.limit:
logger.debug('=> use of hard limit and exit after %s instances', options.limit)
break
def action_terminate():
users = dict()
instances = novaclient.get_instances(options.aggregate)
snapshot_name = "-legacy_terminate_" + datetime.now().strftime("%Y-%m-%d")
# Generate instance list per user
count = 0
for i in instances:
count += 1
if count > options.limit:
break
email = None
user = ksclient.get_by_id('user', i.user_id)
if not user:
project = ksclient.get_by_id('project', i.tenant_id)
if hasattr(project, 'admin'):
email = project.admin
else:
continue
if not email:
if not user.name:
continue
if "@" not in user.name:
continue
email = user.name.lower()
if email not in users:
users[email] = dict()
users[email][i.name] = {'snapshot': i.name + snapshot_name}
# Snapshot and terminate instance
if not options.dry_run:
project = ksclient.get_by_id('project', i.tenant_id)
metadata = {
'created_by': 'automated by uh-iaas team',
'owner': project.id,
'visibility': 'shared'
}
if i.status == 'ACTIVE':
i.stop()
status = i.status
while status != 'SHUTOFF':
time.sleep(5)
tmp_i = novaclient.get_by_id('server', i.id)
status = tmp_i.status
image_name = i.name + snapshot_name
image_id = i.create_image(image_name=image_name, metadata=metadata)
time.sleep(2)
image = glclient.get_image_by_id(image_id)
ksclient.debug_log('create snapshot %s' % image_name)
while image.status != 'active':
time.sleep(5)
image = glclient.get_image_by_id(image_id)
ksclient.debug_log('waiting for snapshot %s to be ready' % image_name)
#i.delete()
if users:
mail = Mail(options.config, debug=options.debug)
# Email each users
for user, instances in users.items():
user_instances = ""
for server, info in instances.items():
user_instances += "%s (snapshot: %s)\n" % (server, info['snapshot'])
#action_date = himutils.get_date(options.date, date.today(), '%Y-%m-%d')
mapping = dict(region=ksclient.region.upper(),
#date=action_date.strftime("%d %B %Y"),
region_lower=ksclient.region.lower(),
instances=user_instances)
body_content = himutils.load_template(inputfile=options.template,
mapping=mapping,
log=ksclient.get_logger())
if not body_content:
print('ERROR! Could not find and parse mail body in \
%s' % options.msg)
sys.exit(1)
msg = MIMEText(body_content, 'plain', 'utf-8')
msg['Subject'] = ('[UH-IaaS]: Your legacy instances have been terminated (%s)'
% (ksclient.region))
if not options.dry_run:
mail.send_mail(user, msg)
print("Sending email to user %s" % user)
else:
print("Dry-run: Mail would be sendt to user %s" % user)
pp = pprint.PrettyPrinter(indent=1)
print("\nComplete list of users and instances:")
print("=====================================")
pp.pprint(users)
def action_notify():
users = dict()
instances = novaclient.get_instances(options.aggregate)
# update metadata
if not options.dry_run:
metadata = {'mail': options.date}
novaclient.update_aggregate(options.aggregate, metadata=metadata)
# Generate instance list per user
for i in instances:
email = None
user = ksclient.get_by_id('user', i.user_id)
if not user:
project = ksclient.get_by_id('project', i.tenant_id)
if hasattr(project, 'admin'):
email = project.admin
else:
continue
if not email:
if not user.name:
continue
if "@" not in user.name:
continue
email = user.name.lower()
if email not in users:
users[email] = dict()
users[email][i.name] = {'status': i.status}
if users:
mail = Mail(options.config, debug=options.debug)
# Email each users
for user, instances in users.items():
user_instances = ""
for server, info in instances.items():
user_instances += "%s (current status %s)\n" % (server, info['status'])
action_date = himutils.get_date(options.date, date.today(), '%Y-%m-%d')
mapping = dict(region=ksclient.region.upper(),
date=action_date.strftime("%d %B %Y"),
region_lower=ksclient.region.lower(),
instances=user_instances)
body_content = himutils.load_template(inputfile=options.template,
mapping=mapping,
log=ksclient.get_logger())
if not body_content:
print('ERROR! Could not find and parse mail body in \
%s' % options.msg)
sys.exit(1)
msg = MIMEText(body_content, 'plain', 'utf-8')
msg['Subject'] = ('[UH-IaaS]: Your legacy instances will be terminated on %s (%s)'
% (options.date, ksclient.region))
if not options.dry_run:
mail.send_mail(user, msg)
print("Sending email to user %s" % user)
else:
print("Dry-run: Mail would be sendt to user %s" % user)
pp = pprint.PrettyPrinter(indent=1)
print("\nComplete list of users and instances:")
print("=====================================")
pp.pprint(users)
# Run local function with the same name as the action
action = locals().get('action_' + options.action)
if not action:
logger.error("Function action_%s not implemented" % options.action)
sys.exit(1)
action()