|
1 |
| -import os |
2 | 1 | import subprocess
|
3 | 2 | import json
|
4 | 3 | import argparse
|
5 |
| - |
6 |
| - |
7 |
| - |
| 4 | + |
8 | 5 | def main():
|
9 | 6 | parser = argparse.ArgumentParser()
|
10 | 7 | parser.add_argument('--stream', dest='stream', type=str, help='Fedora stream', required=True)
|
| 8 | + parser.add_argument('--dry-run', dest='dry_run', help='Check if the resources have tags but not add them', action='store_true') |
11 | 9 | args = parser.parse_args()
|
12 |
| - |
| 10 | + |
13 | 11 | builds = getBuildsForStream(args.stream)
|
14 | 12 | for build in builds:
|
15 | 13 | build_id=build['id']
|
16 | 14 | arches=build['arches']
|
17 | 15 | for arch in arches:
|
18 |
| - print(f"The build is {build_id}") |
| 16 | + print(f"Parsing AMIs for {build_id} for {arch}") |
19 | 17 | buildFetch(args.stream, build_id, arch)
|
20 |
| - meta = open('builds/'+build_id+'/'+arch+'/meta.json') |
| 18 | + meta = open(f'builds/{build_id}/{arch}/meta.json') |
21 | 19 | data = json.load(meta)
|
22 |
| - |
| 20 | + |
| 21 | + if 'amis' in data.keys(): |
| 22 | + amis = data['amis'] |
| 23 | + else: |
| 24 | + print(f"{build_id} does not have any AMIs for {arch} in meta.json") |
| 25 | + continue |
23 | 26 | # Delete this when actually running. Just here while I make this script
|
24 |
| - data ={"amis":[{ |
25 |
| - "name": "us-east-1", |
26 |
| - "hvm": "ami-0016d5df3041499f9", |
27 |
| - "snapshot": "snap-0c1ca4850fcd5e573" |
28 |
| - }]} |
29 |
| - amis = data['amis'] |
| 27 | + # data ={"amis":[{ |
| 28 | + # "name": "us-east-1", |
| 29 | + # "hvm": "ami-0016d5df3041499f9", |
| 30 | + # "snapshot": "snap-0c1ca4850fcd5e573" |
| 31 | + # }]} |
| 32 | + # amis = data['amis'] |
| 33 | + |
30 | 34 | for ami in amis:
|
31 |
| - checkAndAddTag(ami["hvm"], ami["name"]) |
32 |
| - checkAndAddTag(ami["snapshot"], ami["name"]) |
33 |
| - return |
| 35 | + region = ami["name"] |
| 36 | + checkAndAddTag(ami["hvm"], region, args.dry_run) |
| 37 | + checkAndAddTag(ami["snapshot"], region, args.dry_run) |
| 38 | + return |
| 39 | + |
| 40 | +def checkAndAddTag(resourceId, region, dry_run): |
| 41 | + describeTagsCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} --region {region} --output=json' |
| 42 | + tagCheck=subprocess.run([describeTagsCmd], shell=True, capture_output=True, text=True) |
| 43 | + if tagCheck.stdout == None or tagCheck.stdout == '': |
| 44 | + print(f"No tags detected for {resourceId}; assuming it doesn't exist") |
| 45 | + return |
| 46 | + tagCheck=json.loads(tagCheck.stdout) |
34 | 47 |
|
35 |
| -def checkAndAddTag(resourceId, region): |
36 |
| - tagExists = checkTag(resourceId) |
37 |
| - if tagExists: |
38 |
| - print(f"{resourceId} already tagged with FedoraUser=coreos tag") |
| 48 | + if any((tag['Key'] == 'FedoraGroup' and tag['Value'] == 'coreos') for tag in tagCheck['Tags']): |
| 49 | + print(f"{resourceId} already tagged with FedoraGroup=coreos tag") |
| 50 | + return |
39 | 51 | else:
|
40 |
| - addTag(resourceId, region) |
41 |
| - print(f"FedoraUser=coreos tag successfully added to {resourceId}") |
| 52 | + addTag(resourceId, region, dry_run) |
42 | 53 |
|
43 |
| -def checkTag(resourceId): |
44 |
| - checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} Name=value,Values=coreos' |
45 |
| - try: |
46 |
| - tagCheck=subprocess.run([checkTagCmd], shell=True, capture_output=True, text=True) |
47 |
| - if "FedoraUser" and "coreos" in tagCheck.stdout: |
48 |
| - return True |
49 |
| - return False |
50 |
| - except subprocess.CalledProcessError as e: |
51 |
| - return(e.output) |
| 54 | +def addTag(resourceId, region, dry_run): |
| 55 | + if dry_run: |
| 56 | + print(f"Would add tag 'FedoraGroup=coreos' to {resourceId} in region {region}") |
| 57 | + else: |
| 58 | + UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraGroup",Value="coreos" --region {region}' |
| 59 | + subprocess.run([UpdateTagCmd], shell=True) |
| 60 | + print(f"'FedoraGroup=coreos' tag successfully added to {resourceId}") |
52 | 61 |
|
53 |
| -def addTag(resourceId, region): |
54 |
| - UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraUser",Value="coreos" --region {region}' |
55 |
| - try: |
56 |
| - subprocess.run([UpdateTagCmd], shell=True) |
57 |
| - except subprocess.CalledProcessError as e: |
58 |
| - return(e.output) |
59 |
| - |
60 | 62 | def getBuildsForStream(stream):
|
61 |
| - buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all' |
62 |
| - try: |
63 |
| - subprocess.call(['/bin/bash', '-i', '-c', buildFetch]) |
64 |
| - except subprocess.CalledProcessError as e: |
65 |
| - return(e.output) |
66 |
| - |
67 |
| - f = open('builds/builds.json') |
| 63 | + buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --arch=all' |
| 64 | + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetchCmd]) |
| 65 | + |
| 66 | + f = open(f'builds/builds.json') |
68 | 67 | data = json.load(f)
|
69 | 68 | return data['builds']
|
70 |
| - |
| 69 | + |
71 | 70 | def buildFetch(stream, build, arch):
|
72 | 71 | buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --build=' + build + ' --arch=' + arch
|
73 |
| - try: |
74 |
| - subprocess.call(['/bin/bash', '-i', '-c', buildFetchCmd]) |
75 |
| - except subprocess.CalledProcessError as e: |
76 |
| - return(e.output) |
77 |
| - |
| 72 | + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetchCmd]) |
| 73 | + |
78 | 74 | if __name__ == '__main__':
|
79 | 75 | main()
|
0 commit comments