|
| 1 | +import subprocess |
| 2 | +import json |
| 3 | +import argparse |
| 4 | + |
| 5 | +def main(): |
| 6 | + parser = argparse.ArgumentParser() |
| 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') |
| 9 | + args = parser.parse_args() |
| 10 | + |
| 11 | + builds = getBuildsForStream(args.stream) |
| 12 | + for build in builds: |
| 13 | + build_id=build['id'] |
| 14 | + arches=build['arches'] |
| 15 | + for arch in arches: |
| 16 | + print(f"The build is {build_id} for {arch}") |
| 17 | + buildFetch(args.stream, build_id, arch) |
| 18 | + meta = open(f'builds/{build_id}/{arch}/meta.json') |
| 19 | + data = json.load(meta) |
| 20 | + |
| 21 | + # Delete this when actually running. Just here while I make this script |
| 22 | + # data ={"amis":[{ |
| 23 | + # "name": "us-east-1", |
| 24 | + # "hvm": "ami-0016d5df3041499f9", |
| 25 | + # "snapshot": "snap-0c1ca4850fcd5e573" |
| 26 | + # }]} |
| 27 | + if 'amis' in data.keys(): |
| 28 | + amis = data['amis'] |
| 29 | + else: |
| 30 | + print(f"{build_id} does not have any AMIs for {arch} in meta.json") |
| 31 | + return |
| 32 | + for ami in amis: |
| 33 | + if args.dry_run: |
| 34 | + amiTagExists = checkTag(ami["hvm"], ami["name"]) |
| 35 | + snapshotTagExists = checkTag(ami["snapshot"], ami["name"]) |
| 36 | + if not amiTagExists: |
| 37 | + print(f"Would add tags 'FedoraGroup=coreos' to {ami["hvm"]} in region {ami["name"]}") |
| 38 | + else: |
| 39 | + print(f"{ami["hvm"]} already tagged with FedoraGroup=coreos tag") |
| 40 | + if not snapshotTagExists: |
| 41 | + print(f"Would add tags 'FedoraGroup=coreos' to {ami["snapshot"]} in region {ami["name"]}") |
| 42 | + else: |
| 43 | + print(f"{ami["snapshot"]} already tagged with FedoraGroup=coreos tag") |
| 44 | + else: |
| 45 | + checkAndAddTag(ami["hvm"], ami["name"]) |
| 46 | + checkAndAddTag(ami["snapshot"], ami["name"]) |
| 47 | + return |
| 48 | + |
| 49 | +def checkAndAddTag(resourceId, region): |
| 50 | + tagExists = checkTag(resourceId, region) |
| 51 | + if tagExists: |
| 52 | + print(f"{resourceId} already tagged with FedoraGroup=coreos tag") |
| 53 | + else: |
| 54 | + addTag(resourceId, region) |
| 55 | + print(f"FedoraGroup=coreos tag successfully added to {resourceId}") |
| 56 | + |
| 57 | +def checkTag(resourceId, region): |
| 58 | + checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} Name=value,Values=coreos Name=key,Values=FedoraGroup --region {region} --output=json' |
| 59 | + try: |
| 60 | + tagCheck=subprocess.run([checkTagCmd], shell=True, capture_output=True, text=True) |
| 61 | + tagCheck=json.loads(tagCheck.stdout) |
| 62 | + if any((tag['Value'] == 'coreos' and tag['Key'] == 'FedoraGroup') for tag in tagCheck['Tags']): |
| 63 | + return True |
| 64 | + return False |
| 65 | + except subprocess.CalledProcessError as e: |
| 66 | + return(e.output) |
| 67 | + |
| 68 | +def addTag(resourceId, region): |
| 69 | + UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraGroup",Value="coreos" --region {region}' |
| 70 | + try: |
| 71 | + subprocess.run([UpdateTagCmd], shell=True) |
| 72 | + except subprocess.CalledProcessError as e: |
| 73 | + return(e.output) |
| 74 | + |
| 75 | +def getBuildsForStream(stream): |
| 76 | + buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all' |
| 77 | + try: |
| 78 | + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetch]) |
| 79 | + except subprocess.CalledProcessError as e: |
| 80 | + return(e.output) |
| 81 | + |
| 82 | + f = open(f'builds/builds.json') |
| 83 | + data = json.load(f) |
| 84 | + return data['builds'] |
| 85 | + |
| 86 | +def buildFetch(stream, build, arch): |
| 87 | + buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --build=' + build + ' --arch=' + arch |
| 88 | + try: |
| 89 | + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetchCmd]) |
| 90 | + except subprocess.CalledProcessError as e: |
| 91 | + return(e.output) |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments