Skip to content

Commit 469db54

Browse files
Enrico Usaienrico-usai
authored andcommitted
Add main function to the ec2_dev_2_volid.py script
Signed-off-by: Enrico Usai <[email protected]>
1 parent 522f955 commit 469db54

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

files/default/ec2_dev_2_volid.py

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,55 @@
77
import time
88
import boto3
99

10-
# Get dev
11-
try:
12-
dev = str(sys.argv[1])
13-
except IndexError:
14-
syslog.syslog(syslog.LOG_ERR, "Provide block device i.e. xvdf")
15-
sys.exit(1)
16-
17-
# Convert dev to mapping format
18-
if 'nvme' in dev:
19-
# For newer instances which expose EBS volumes as NVMe devices, translate the
20-
# device name so boto can discover it.
21-
output = os.popen('sudo /usr/local/sbin/cfncluster-ebsnvme-id -v /dev/' + dev).read().split(":")[1].strip()
22-
print(output)
23-
sys.exit(0)
24-
else:
25-
dev = dev.replace('xvd', 'sd')
26-
dev = '/dev/' + dev
27-
28-
# Get instance ID
29-
instanceId = urllib2.urlopen("http://169.254.169.254/latest/meta-data/instance-id").read()
30-
31-
# Get region
32-
region = urllib2.urlopen("http://169.254.169.254/latest/meta-data/placement/availability-zone").read()
33-
region = region[:-1]
34-
35-
# Connect to AWS using boto
36-
ec2 = boto3.client('ec2', region_name=region)
37-
38-
# Poll for blockdevicemapping
39-
devices = ec2.describe_instance_attribute(InstanceId=instanceId, Attribute='blockDeviceMapping').get('BlockDeviceMappings')
40-
devmap = dict((d.get('DeviceName'), d) for d in devices)
41-
x = 0
42-
while not devmap.has_key(dev):
43-
if x == 36:
44-
syslog.syslog("Dev %s did not appears in 180 seconds." % dev)
45-
sys.exit(1)
46-
syslog.syslog("Looking for dev %s in devmap %s" % (dev, devmap))
47-
time.sleep(5)
10+
11+
def main():
12+
syslog.syslog("Starting ec2_dev_2_volid.py script")
13+
# Get dev
14+
try:
15+
dev = str(sys.argv[1])
16+
syslog.syslog("Input block device is %s" % dev)
17+
except IndexError:
18+
syslog.syslog(syslog.LOG_ERR, "Provide block device i.e. xvdf")
19+
20+
# Convert dev to mapping format
21+
if 'nvme' in dev:
22+
# For newer instances which expose EBS volumes as NVMe devices, translate the
23+
# device name so boto can discover it.
24+
output = os.popen('sudo /usr/local/sbin/cfncluster-ebsnvme-id -v /dev/' + dev).read().split(":")[1].strip()
25+
print(output)
26+
sys.exit(0)
27+
else:
28+
dev = dev.replace('xvd', 'sd')
29+
dev = '/dev/' + dev
30+
31+
# Get instance ID
32+
instanceId = urllib2.urlopen("http://169.254.169.254/latest/meta-data/instance-id").read()
33+
34+
# Get region
35+
region = urllib2.urlopen("http://169.254.169.254/latest/meta-data/placement/availability-zone").read()
36+
region = region[:-1]
37+
38+
# Connect to AWS using boto
39+
ec2 = boto3.client('ec2', region_name=region)
40+
41+
# Poll for blockdevicemapping
4842
devices = ec2.describe_instance_attribute(InstanceId=instanceId, Attribute='blockDeviceMapping').get('BlockDeviceMappings')
4943
devmap = dict((d.get('DeviceName'), d) for d in devices)
50-
x += 1
51-
52-
volumeId = devmap.get(dev).get('Ebs').get('VolumeId')
53-
print(volumeId)
44+
x = 0
45+
while not devmap.has_key(dev):
46+
if x == 36:
47+
syslog.syslog("Dev %s did not appears in 180 seconds." % dev)
48+
sys.exit(1)
49+
syslog.syslog("Looking for dev %s in devmap %s" % (dev, devmap))
50+
time.sleep(5)
51+
devices = ec2.describe_instance_attribute(InstanceId=instanceId, Attribute='blockDeviceMapping').get('BlockDeviceMappings')
52+
devmap = dict((d.get('DeviceName'), d) for d in devices)
53+
x += 1
54+
55+
# Return volumeId
56+
volumeId = devmap.get(dev).get('Ebs').get('VolumeId')
57+
print(volumeId)
58+
59+
60+
if __name__ == '__main__':
61+
main()

0 commit comments

Comments
 (0)