55import urllib2
66import boto3
77import time
8+ import ConfigParser
9+ from botocore .config import Config
810
9- # Get volumeId
10- try :
11- volumeId = str (sys .argv [1 ])
12- except IndexError :
13- print "Provide an EBS volume ID to attach i.e. vol-cc789ea5"
14- sys .exit (1 )
15-
16- # Get instance ID
17- instanceId = urllib2 .urlopen ("http://169.254.169.254/latest/meta-data/instance-id" ).read ()
18-
19- # Get region
20- region = urllib2 .urlopen ("http://169.254.169.254/latest/meta-data/placement/availability-zone" ).read ()
21- region = region [:- 1 ]
22-
23- # Generate a list of system paths minus the root path
24- paths = [ device .path for device in parted .getAllDevices () ]
25-
26- # List of possible block devices
27- blockDevices = [ '/dev/xvdb' , '/dev/xvdc' , '/dev/xvdd' , '/dev/xvde' , '/dev/xvdf' , '/dev/xvdg' , '/dev/xvdh' , '/dev/xvdi' , '/dev/xvdj' , '/dev/xvdk' , '/dev/xvdl' , '/dev/xvdm' , '/dev/xvdn' , '/dev/xvdo' , '/dev/xvdp' , '/dev/xvdq' , '/dev/xvdr' , '/dev/xvds' , '/dev/xvdt' , '/dev/xvdu' , '/dev/xvdv' , '/dev/xvdw' , '/dev/xvdx' , '/dev/xvdy' , '/dev/xvdz' ]
28-
29- # List of available block devices after removing currently used block devices
30- availableDevices = [a for a in blockDevices if a not in paths ]
31-
32- # Connect to AWS using boto
33- ec2 = boto3 .client ('ec2' , region_name = region )
34-
35- # Attach the volume
36- dev = availableDevices [0 ].replace ('xvd' , 'sd' )
37- response = ec2 .attach_volume (VolumeId = volumeId , InstanceId = instanceId , Device = dev )
38-
39- # Poll for volume to attach
40- state = response .get ("State" )
41-
42- x = 0
43- while state != "attached" :
44- if x == 36 :
45- print "Volume %s failed to mount in 180 seconds." % (volumeId )
46- exit (1 )
47- if state in ["busy" or "detached" ]:
48- print "Volume %s in bad state %s" % (volumeId , state )
49- exit (1 )
50- print "Volume %s in state %s ... waiting to be 'attached'" % (volumeId , state )
51- time .sleep (5 )
52- x += 1
11+
12+ def main ():
13+ # Get EBS volume Id
5314 try :
54- state = ec2 .describe_volumes (VolumeIds = [volumeId ]).get ('Volumes' )[0 ].get ('Attachments' )[0 ].get ('State' )
55- except IndexError as e :
56- continue
15+ volumeId = str (sys .argv [1 ])
16+ except IndexError :
17+ print "Provide an EBS volume ID to attach i.e. vol-cc789ea5"
18+ sys .exit (1 )
19+
20+ # Get instance ID
21+ instanceId = urllib2 .urlopen ("http://169.254.169.254/latest/meta-data/instance-id" ).read ()
22+
23+ # Get region
24+ region = urllib2 .urlopen ("http://169.254.169.254/latest/meta-data/placement/availability-zone" ).read ()
25+ region = region [:- 1 ]
26+
27+ # Generate a list of system paths minus the root path
28+ paths = [device .path for device in parted .getAllDevices ()]
29+
30+ # List of possible block devices
31+ blockDevices = ['/dev/xvdb' , '/dev/xvdc' , '/dev/xvdd' , '/dev/xvde' , '/dev/xvdf' , '/dev/xvdg' , '/dev/xvdh' ,
32+ '/dev/xvdi' ,'/dev/xvdj' , '/dev/xvdk' , '/dev/xvdl' , '/dev/xvdm' , '/dev/xvdn' , '/dev/xvdo' ,
33+ '/dev/xvdp' , '/dev/xvdq' , '/dev/xvdr' , '/dev/xvds' , '/dev/xvdt' , '/dev/xvdu' , '/dev/xvdv' ,
34+ '/dev/xvdw' , '/dev/xvdx' , '/dev/xvdy' , '/dev/xvdz' ]
35+
36+ # List of available block devices after removing currently used block devices
37+ availableDevices = [a for a in blockDevices if a not in paths ]
38+
39+ # Parse configuration file to read proxy settings
40+ config = ConfigParser .RawConfigParser ()
41+ config .read ('/etc/boto.cfg' )
42+ proxy_config = Config ()
43+ if config .has_option ('Boto' , 'proxy' ) and config .has_option ('Boto' , 'proxy_port' ):
44+ proxy = config .get ('Boto' , 'proxy' )
45+ proxy_port = config .get ('Boto' , 'proxy_port' )
46+ proxy_config = Config (proxies = {'https' : "{}:{}" .format (proxy , proxy_port )})
47+
48+ # Connect to AWS using boto
49+ ec2 = boto3 .client ('ec2' , region_name = region , config = proxy_config )
50+
51+ # Attach the volume
52+ dev = availableDevices [0 ].replace ('xvd' , 'sd' )
53+ response = ec2 .attach_volume (VolumeId = volumeId , InstanceId = instanceId , Device = dev )
54+
55+ # Poll for volume to attach
56+ state = response .get ("State" )
57+ x = 0
58+ while state != "attached" :
59+ if x == 36 :
60+ print "Volume %s failed to mount in 180 seconds." % volumeId
61+ exit (1 )
62+ if state in ["busy" or "detached" ]:
63+ print "Volume %s in bad state %s" % (volumeId , state )
64+ exit (1 )
65+ print "Volume %s in state %s ... waiting to be 'attached'" % (volumeId , state )
66+ time .sleep (5 )
67+ x += 1
68+ try :
69+ state = ec2 .describe_volumes (VolumeIds = [volumeId ]).get ('Volumes' )[0 ].get ('Attachments' )[0 ].get ('State' )
70+ except IndexError as e :
71+ continue
72+
73+
74+ if __name__ == '__main__' :
75+ main ()
0 commit comments