-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstance_swap_root_disk.py
330 lines (278 loc) · 10.8 KB
/
instance_swap_root_disk.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto
import boto.ec2
import sys
import re
import datetime
#from config import config
import dateutil
import dateutil.parser
import pytz
import subprocess
import argparse
import time
import signal
from pprint import pprint
import atexit
import os
class GracefulInterruptHandler(object):
def __init__(self, sig=signal.SIGINT):
self.sig = sig
def __enter__(self):
self.interrupted = False
self.released = False
self.original_handler = signal.getsignal(self.sig)
def handler(signum, frame):
self.release()
self.interrupted = True
signal.signal(self.sig, handler)
return self
def __exit__(self, type, value, tb):
self.release()
def release(self):
if self.released:
return False
signal.signal(self.sig, self.original_handler)
self.released = True
return True
def create_snapshot_and_wait(ebs_volume, description, tags, sleep_interval, max_sleep):
new_snap = ebs_volume.create_snapshot(description = description )
ec2_conn.create_tags([new_snap.id], tags )
if max_sleep > 0:
tot_sleep = 0
print("snap progress is")
while ( new_snap.status != 'completed' ) and (tot_sleep < max_sleep ):
print(" " + str(new_snap.progress).strip() )
time.sleep(sleep_interval)
tot_sleep += sleep_interval
new_snap.update()
return new_snap
def detach_volume(ebs_volume, sleep_delay, max_count):
print("Detach volume " + ebs_volume.id + "\n" )
result = ebs_volume.detach()
i=0
ebs_volume.update()
while (i < max_count):
if ebs_volume.attachment_state() is not None:
print(str(ebs_volume.attachment_state()) + "..." )
time.sleep(sleep_delay)
ebs_volume.update()
i += 1
else:
break
print('\n')
if ebs_volume.attachment_state() is not None:
print("Detach failed. Try forcing\n")
result = ebs_volume.detach(force=True)
i=0
ebs_volume.update()
while (i < max_count):
if ebs_volume.attachment_state() is not None:
print(str(ebs_volume.attachment_state()) + "..." )
time.sleep(sleep_delay)
ebs_volume.update()
i += 1
else:
break
print('\n')
if ebs_volume.attachment_state() is not None:
print("Failed to detach volume after "+ str(max_count * sleep_delay) +"seconds\n")
return 7
else:
print("Detach was ok!"+ str(result) +"\n")
return 0
def compare_snaps(snap1,snap2):
if dateutil.parser.parse(snap1.start_time) < dateutil.parser.parse(snap2.start_time):
return -1
else:
return 1
def timedelta_total_seconds(timedelta):
return (
timedelta.microseconds + 0.0 +
(timedelta.seconds + timedelta.days * 24 * 3600) * 10 ** 6) / 10 ** 6
def attach_volume_at_letter_or_more(ebs_volume, my_instance_id, device_letter, sleep_delay, max_count):
is_attached=False
while (not is_attached and device_letter < 'z'):
last_device_ascii = ord(device_letter)
last_device = "/dev/sd%s" % chr(last_device_ascii)
print("Attach " + ebs_volume.id + " to " + last_device )
try:
result = ec2_conn.attach_volume(ebs_volume.id, my_instance_id, last_device)
is_attached = True
except boto.exception.EC2ResponseError as e:
if "is already in use" in e.error_message:
device_letter = chr(last_device_ascii + 1)
ebs_device = last_device
if not is_attached:
print("ERROR: Volume attach failed with result: " + result + " Bailing out.\n")
sys.exit(5)
print 'Attach Volume Result: ', result
i=0
ebs_volume.update()
while (i < max_count):
if ebs_volume.attachment_state() != "attached":
print(str(ebs_volume.attachment_state()) + "..." )
time.sleep(sleep_delay)
ebs_volume.update()
i += 1
else:
break
print('\n')
if ebs_volume.attachment_state() != "attached":
print("ERROR: Volume did not attach after " + str( max_count * sleep_delay ) + " seconds. Bailing out.\n")
sys.exit(5)
return (last_device_ascii,last_device)
def attach_volume_at(ebs_volume, my_instance_id, device_string, sleep_delay, max_count):
is_attached=False
while(True):
print("Attach " + ebs_volume.id + " to " + device_string )
try:
result = ec2_conn.attach_volume(ebs_volume.id, my_instance_id, device_string)
is_attached = True
except boto.exception.EC2ResponseError as e:
result = e.error_message
break
if not is_attached:
print("ERROR: Volume attach failed with result: " + result + " Bailing out.\n")
sys.exit(5)
print 'Attach Volume Result: ', result
i=0
ebs_volume.update()
while (i < max_count):
if ebs_volume.attachment_state() != "attached":
print(str(ebs_volume.attachment_state()) + "..." )
time.sleep(sleep_delay)
ebs_volume.update()
i += 1
else:
break
print('\n')
if ebs_volume.attachment_state() != "attached":
print("ERROR: Volume did not attach after " + str( max_count * sleep_delay ) + " seconds. Bailing out.\n")
sys.exit(5)
return True
max_count = 10
sleep_delay = 6
snapshot_sleep_delay = 300
nb_daily_backups = 7
nb_weekly_backups = 4
nb_monthly_backups = 3
parser = argparse.ArgumentParser(usage="--instance-id <instance-id> --root-volume <ebs-volume to set as root> [ --delete-existing-root ] [ --stop-instance ] [ --start-instance ]")
parser.add_argument('--instance-id', required=True, help="id of instance for which we need to swap the root device")
parser.add_argument('--root-volume', required=True, help="id of volume which will be used as root device")
parser.add_argument('--delete-existing-root', required=False, action="store_true", help="if set, existing root device will be deleted")
parser.add_argument('--stop-instance', required=False, action="store_true", help="if set, instance will be stopped if is running")
parser.add_argument('--start-instance', required=False, action="store_true", help="if set, instance will be started once the swap is done")
prg_args = parser.parse_args()
instance_id = prg_args.instance_id.strip()
root_volume = prg_args.root_volume.strip()
if prg_args.delete_existing_root:
do_delete_existing_root = True
else:
do_delete_existing_root = False
if prg_args.stop_instance:
do_stop_instance = True
else:
do_stop_instance = False
if prg_args.start_instance:
do_start_instance = True
else:
do_start_instance = False
print("Replace its root volume if instance %s to %s\n" % ( instance_id , root_volume ) )
lock_file="/dev/shm/swaproot_lock_%s_%s.pid" % ( instance_id, root_volume )
if os.path.exists(lock_file):
print("ERROR: There is already a lock for this sync at %s\n" % lock_file)
sys.exit(2)
else:
with open(lock_file, 'a') as fd:
fd.write("%i" % os.getpid())
@atexit.register
def remove_lock():
os.unlink(lock_file)
my_instance_id=subprocess.check_output(['ec2metadata','--instance-id'] ).strip()
print("My instance id: %s" % my_instance_id )
ec2_region_name = "eu-west-1" #config['ec2_region_name']
ec2_region_endpoint = "ec2.eu-west-1.amazonaws.com"
#sns_arn = config.get('arn')
#proxyHost = config.get('proxyHost')
#proxyPort = config.get('proxyPort')
proxyHost = None
proxyPort = None
aws_access_key = None
region = boto.ec2.regioninfo.RegionInfo(name=ec2_region_name, endpoint=ec2_region_endpoint)
# Connect to AWS using the credentials provided above or in Environment vars or using IAM role.
print 'Connecting to AWS'
if proxyHost:
# proxy:
# using roles
if aws_access_key:
conn = boto.ec2.connection.EC2Connection(aws_access_key, aws_secret_key, region=region, proxy=proxyHost, proxy_port=proxyPort)
else:
conn = boto.ec2.connection.EC2Connection(region=region, proxy=proxyHost, proxy_port=proxyPort)
else:
# non proxy:
# using roles
if aws_access_key:
conn = boto.ec2.connection.EC2Connection(aws_access_key, aws_secret_key, region=region)
else:
conn = boto.ec2.connection.EC2Connection(region=region)
ec2_conn = conn
instances_list = ec2_conn.get_only_instances()
my_reservations = ec2_conn.get_all_instances(instance_ids=[ my_instance_id ])
my_instance = [i for r in my_reservations for i in r.instances][0]
print("I am running in %s\n" % my_instance.placement )
instances_reservations = ec2_conn.get_all_instances(instance_ids=[ instance_id ])
if len(instances_reservations) != 1:
print("ERROR: Found no instance with id %s" % instance_id)
sys.exit(1)
l_instance = [ i for r in instances_reservations for i in r.instances ][0]
volumes_list = ec2_conn.get_all_volumes(filters={'volume-id':root_volume })
if len(volumes_list) != 1:
print("ERROR: Found no volume with id %s" % root_volume)
sys.exit(3)
l_volume = volumes_list[0]
do_attach=True
if l_volume.update(validate=True) != "available":
if l_volume.attach_data:
if l_volume.attach_data.instance_id == l_instance.id:
print("Volume is already attached to instance!..")
do_attach=False
if do_attach:
print("ERROR: Volume %s is not avalailable" % root_volume)
sys.exit(4)
if do_attach and l_instance.state != 'stopped':
if not(do_stop_instance):
print("ERROR: Instance %s state is %s, which is not stopped." % ( instance_id , l_instance.state ))
sys.exit(2)
print("Unplugging instance %s" % instance_id)
ec2_conn.stop_instances(instance_ids=[instance_id], force=True)
i=0
l_instance.update()
while (i < max_count):
if l_instance.state != "stopped":
print(str(l_instance.state) + "..." )
time.sleep(sleep_delay)
l_instance.update()
i += 1
else:
break
print('\n')
if do_attach:
print("Instance %s is stopped and volume %s is available. Let's go" % ( instance_id, root_volume))
for device, ebs in iter(sorted(l_instance.block_device_mapping.items())):
print("Mapping: " + device + "=>" + ebs.volume_id + "\n" )
if device == "/dev/sda1":
print("Volume %s is already attached at: %s. Detach it" % (ebs.volume_id, device))
volumes_list = ec2_conn.get_all_volumes(filters={'volume-id':ebs.volume_id })
res_detach = detach_volume(volumes_list[0], sleep_delay, max_count)
print("Detach status: %s" % res_detach)
if do_delete_existing_root and ebs.volume_id != l_volume.id:
print("Delete old root")
ec2_conn.delete_volume(ebs.volume_id)
print("Attach volume as root device")
attach_volume_at(l_volume, l_instance.id, "/dev/sda1", sleep_delay, max_count)
if do_start_instance:
print("Starting instance with new root")
ec2_conn.start_instances(instance_ids=[instance_id])
sys.exit(0)