Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/osbuild-manifests/aws_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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)
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}")
buildFetch(args.stream, build_id, arch)
meta = open('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']
for ami in amis:
checkAndAddTag(ami["hvm"], ami["name"])
checkAndAddTag(ami["snapshot"], ami["name"])
return

def checkAndAddTag(resourceId, region):
tagExists = checkTag(resourceId)
if tagExists:
print(f"{resourceId} already tagged with FedoraUser=coreos tag")
else:
addTag(resourceId, region)
print(f"FedoraUser=coreos tag successfully added to {resourceId}")
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it would be nice to add a --dry-run option to this script and do something like:

Suggested change
if tagExists:
print(f"{resourceId} already tagged with FedoraUser=coreos tag")
else:
addTag(resourceId, region)
print(f"FedoraUser=coreos tag successfully added to {resourceId}")
if tagExists:
print(f"{resourceId} already tagged with FedoraUser=coreos tag")
return
if dryrun:
print(f"--dry-run specified. Would add FedoraUser=coreos tag to {resourceId}")
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'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the Name=value,Values=coreos do here?

Also do we need to add --region to this command too?

Copy link
Collaborator

@gursewak1997 gursewak1997 Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need --region when we are giving --resource-id as an arg.

And for Name=value,Values=coreos, that's the way to filter out data where Name is basically "key" and Values is well "value" from tags json data that's spit out from aws ec2 describe tags
An example of what I get from this ami is this:

{
    "Tags": [
        {
            "Key": "FedoraUser",
            "ResourceId": "ami-xyx12345",
            "ResourceType": "image",
            "Value": "coreos"
        }
   ]
}

try:
tagCheck=subprocess.run([checkTagCmd], shell=True, capture_output=True, text=True)
if "FedoraUser" and "coreos" in tagCheck.stdout:
return True
return False
except subprocess.CalledProcessError as e:
return(e.output)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should drop catching these exceptions


def addTag(resourceId, region):
UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraUser",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'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe for consistency with other vars

Suggested change
buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all'
buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --arch=all'

try:
subprocess.call(['/bin/bash', '-i', '-c', buildFetch])
except subprocess.CalledProcessError as e:
return(e.output)

f = open('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])
except subprocess.CalledProcessError as e:
return(e.output)

if __name__ == '__main__':
main()