comparison env/bin/list_instances @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 #!/Users/pldms/Development/Projects/2020/david-matthews-galaxy/guppy_basecaller/env/bin/python3
2
3 import sys
4 from operator import attrgetter
5 from optparse import OptionParser
6
7 import boto
8 from boto.ec2 import regions
9
10
11 HEADERS = {
12 'ID': {'get': attrgetter('id'), 'length':15},
13 'Zone': {'get': attrgetter('placement'), 'length':15},
14 'Groups': {'get': attrgetter('groups'), 'length':30},
15 'Hostname': {'get': attrgetter('public_dns_name'), 'length':50},
16 'PrivateHostname': {'get': attrgetter('private_dns_name'), 'length':50},
17 'State': {'get': attrgetter('state'), 'length':15},
18 'Image': {'get': attrgetter('image_id'), 'length':15},
19 'Type': {'get': attrgetter('instance_type'), 'length':15},
20 'IP': {'get': attrgetter('ip_address'), 'length':16},
21 'PrivateIP': {'get': attrgetter('private_ip_address'), 'length':16},
22 'Key': {'get': attrgetter('key_name'), 'length':25},
23 'T:': {'length': 30},
24 }
25
26 def get_column(name, instance=None):
27 if name.startswith('T:'):
28 _, tag = name.split(':', 1)
29 return instance.tags.get(tag, '')
30 return HEADERS[name]['get'](instance)
31
32
33 def main():
34 parser = OptionParser()
35 parser.add_option("-r", "--region", help="Region (default us-east-1)", dest="region", default="us-east-1")
36 parser.add_option("-H", "--headers", help="Set headers (use 'T:tagname' for including tags)", default=None, action="store", dest="headers", metavar="ID,Zone,Groups,Hostname,State,T:Name")
37 parser.add_option("-t", "--tab", help="Tab delimited, skip header - useful in shell scripts", action="store_true", default=False)
38 parser.add_option("-f", "--filter", help="Filter option sent to DescribeInstances API call, format is key1=value1,key2=value2,...", default=None)
39 (options, args) = parser.parse_args()
40
41
42 # Connect the region
43 for r in regions():
44 if r.name == options.region:
45 region = r
46 break
47 else:
48 print("Region %s not found." % options.region)
49 sys.exit(1)
50 ec2 = boto.connect_ec2(region=region)
51
52 # Read headers
53 if options.headers:
54 headers = tuple(options.headers.split(','))
55 else:
56 headers = ("ID", 'Zone', "Groups", "Hostname")
57
58 # Create format string
59 format_string = ""
60 for h in headers:
61 if h.startswith('T:'):
62 format_string += "%%-%ds" % HEADERS['T:']['length']
63 else:
64 format_string += "%%-%ds" % HEADERS[h]['length']
65
66
67 # Parse filters (if any)
68 if options.filter:
69 filters = dict([entry.split('=') for entry in options.filter.split(',')])
70 else:
71 filters = {}
72
73 # List and print
74
75 if not options.tab:
76 print(format_string % headers)
77 print("-" * len(format_string % headers))
78
79 for r in ec2.get_all_reservations(filters=filters):
80 groups = [g.name for g in r.groups]
81 for i in r.instances:
82 i.groups = ','.join(groups)
83 if options.tab:
84 print("\t".join(tuple(get_column(h, i) for h in headers)))
85 else:
86 print(format_string % tuple(get_column(h, i) for h in headers))
87
88
89 if __name__ == "__main__":
90 main()