Mercurial > repos > shellac > sam_consensus_v3
comparison env/bin/list_instances @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 #!/Users/cmdms/OneDrive-UOB/Development/Projects/2021/sam-consensus-v3/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() |
