|
25 | 25 | import sys
|
26 | 26 | from collections import OrderedDict
|
27 | 27 |
|
28 |
| - |
29 |
| -owners = ["247102896272"] |
30 | 28 | distros = OrderedDict([("alinux", "amzn"), ("centos6", "centos6"), ("centos7", "centos7"), ("ubuntu1404", "ubuntu-1404"), ("ubuntu1604", "ubuntu-1604")])
|
31 | 29 |
|
32 | 30 |
|
33 |
| -def get_ami_list(regions, date, version): |
| 31 | +def get_ami_list(regions, date, version, owner): |
34 | 32 | amis_json = {}
|
35 | 33 |
|
36 | 34 | for region_name in regions:
|
37 | 35 | try:
|
38 | 36 | ec2 = boto3.client('ec2', region_name=region_name)
|
39 |
| - images = ec2.describe_images(Owners=owners, Filters=[{'Name': 'name', "Values": ["cfncluster-%s*%s" % (version, date)]}]) |
| 37 | + images = ec2.describe_images(Owners=[owner], Filters=[{'Name': 'name', "Values": ["cfncluster-%s*%s" % (version, date)]}]) |
40 | 38 |
|
41 | 39 | amis = OrderedDict()
|
42 | 40 | for image in images.get('Images'):
|
@@ -76,20 +74,32 @@ def convert_json_to_txt(regions, amis_json):
|
76 | 74 | parser.add_argument('--date', type=str, help='release date [timestamp] (e.g. 201801112350)', required=True)
|
77 | 75 | parser.add_argument('--json-file', type=str, help='json output file path', required=False, default="amis.json")
|
78 | 76 | parser.add_argument('--txt-file', type=str, help='txt output file path', required=False, default="amis.txt")
|
| 77 | + parser.add_argument('--account-id', type=str, help='account id that owns the amis', required=False, default="247102896272") |
| 78 | + parser.add_argument('--append', type=str, help='append new amis to current amis.txt', required=False, default=False) |
| 79 | + parser.add_argument('--cloudformation-template', type=str, help='path to cloudfomation template', required=False, default='cloudformation/cfncluster.cfn.json') |
79 | 80 | args = parser.parse_args()
|
80 | 81 |
|
81 | 82 | # get all regions
|
82 | 83 | ec2 = boto3.client('ec2')
|
83 | 84 | regions = sorted(r.get('RegionName') for r in ec2.describe_regions().get('Regions'))
|
84 | 85 |
|
85 | 86 | # get ami list
|
86 |
| - amis_json = get_ami_list(regions=regions, date=args.date, version=args.version) |
| 87 | + amis_json = get_ami_list(regions=regions, date=args.date, version=args.version, owner=args.account_id) |
87 | 88 |
|
88 | 89 | # write amis.json file
|
89 | 90 | amis_json_file = open(args.json_file, "w")
|
90 | 91 | json.dump(amis_json, amis_json_file, indent=2, sort_keys=True)
|
91 | 92 | amis_json_file.close()
|
92 | 93 |
|
| 94 | + # append to amis.txt file |
| 95 | + if args.append: |
| 96 | + with open(args.cloudformation_template) as f: |
| 97 | + data = json.load(f) |
| 98 | + amis = data.get('Mappings').get('AWSRegionOS2AMI') |
| 99 | + amis.update(amis_json) |
| 100 | + amis_json = amis |
| 101 | + regions = sorted(amis_json) |
| 102 | + |
93 | 103 | # convert json to txt
|
94 | 104 | amis_txt = convert_json_to_txt(regions=regions, amis_json=amis_json)
|
95 | 105 |
|
|
0 commit comments