diff --git a/src/osbuild-manifests/aws_tag.py b/src/osbuild-manifests/aws_tag.py index 9fa139811e..489d954c66 100644 --- a/src/osbuild-manifests/aws_tag.py +++ b/src/osbuild-manifests/aws_tag.py @@ -1,79 +1,95 @@ -import os import subprocess import json import argparse - - - + def main(): parser = argparse.ArgumentParser() parser.add_argument('--stream', dest='stream', type=str, help='Fedora stream', required=True) + parser.add_argument('--dry-run', dest='dry_run', help='Check if the resources have tags but not add them', action='store_true') args = parser.parse_args() - + builds = getBuildsForStream(args.stream) for build in builds: build_id=build['id'] arches=build['arches'] for arch in arches: - print(f"The build is {build_id}") + print(f"The build is {build_id} for {arch}") buildFetch(args.stream, build_id, arch) - meta = open('builds/'+build_id+'/'+arch+'/meta.json') + meta = open(f'builds/{build_id}/{arch}/meta.json') data = json.load(meta) - + # Delete this when actually running. Just here while I make this script - data ={"amis":[{ - "name": "us-east-1", - "hvm": "ami-0016d5df3041499f9", - "snapshot": "snap-0c1ca4850fcd5e573" - }]} - amis = data['amis'] + # data ={"amis":[{ + # "name": "us-east-1", + # "hvm": "ami-0016d5df3041499f9", + # "snapshot": "snap-0c1ca4850fcd5e573" + # }]} + if 'amis' in data.keys(): + amis = data['amis'] + else: + print(f"{build_id} does not have any AMIs for {arch} in meta.json") + return for ami in amis: - checkAndAddTag(ami["hvm"], ami["name"]) - checkAndAddTag(ami["snapshot"], ami["name"]) - return - + if args.dry_run: + amiTagExists = checkTag(ami["hvm"], ami["name"]) + snapshotTagExists = checkTag(ami["snapshot"], ami["name"]) + if not amiTagExists: + print(f"Would add tags 'FedoraGroup=coreos' to {ami["hvm"]} in region {ami["name"]}") + else: + print(f"{ami["hvm"]} already tagged with FedoraGroup=coreos tag") + if not snapshotTagExists: + print(f"Would add tags 'FedoraGroup=coreos' to {ami["snapshot"]} in region {ami["name"]}") + else: + print(f"{ami["snapshot"]} already tagged with FedoraGroup=coreos tag") + else: + checkAndAddTag(ami["hvm"], ami["name"]) + checkAndAddTag(ami["snapshot"], ami["name"]) + return + def checkAndAddTag(resourceId, region): - tagExists = checkTag(resourceId) + tagExists = checkTag(resourceId, region) if tagExists: - print(f"{resourceId} already tagged with FedoraUser=coreos tag") + print(f"{resourceId} already tagged with FedoraGroup=coreos tag") else: addTag(resourceId, region) - print(f"FedoraUser=coreos tag successfully added to {resourceId}") - -def checkTag(resourceId): - checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} Name=value,Values=coreos' + print(f"FedoraGroup=coreos tag successfully added to {resourceId}") + +def checkTag(resourceId, region): + checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} Name=value,Values=coreos Name=key,Values=FedoraGroup --region {region} --output=json' try: tagCheck=subprocess.run([checkTagCmd], shell=True, capture_output=True, text=True) - if "FedoraUser" and "coreos" in tagCheck.stdout: + tagCheck=json.loads(tagCheck.stdout) + if any((tag['Value'] == 'coreos' and tag['Key'] == 'FedoraGroup') for tag in tagCheck['Tags']): return True return False except subprocess.CalledProcessError as e: return(e.output) - + def addTag(resourceId, region): - UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraUser",Value="coreos" --region {region}' + UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraGroup",Value="coreos" --region {region}' try: subprocess.run([UpdateTagCmd], shell=True) except subprocess.CalledProcessError as e: return(e.output) - + def getBuildsForStream(stream): buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all' try: - subprocess.call(['/bin/bash', '-i', '-c', buildFetch]) + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetch]) except subprocess.CalledProcessError as e: return(e.output) - - f = open('builds/builds.json') + + f = open(f'builds/builds.json') data = json.load(f) return data['builds'] - + def buildFetch(stream, build, arch): buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --build=' + build + ' --arch=' + arch try: - subprocess.call(['/bin/bash', '-i', '-c', buildFetchCmd]) + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetchCmd]) except subprocess.CalledProcessError as e: return(e.output) - + if __name__ == '__main__': main() + \ No newline at end of file