Skip to content

Commit 2144e8f

Browse files
committed
Updated as per last discussion
1 parent 5eca0e4 commit 2144e8f

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

src/osbuild-manifests/aws_tag.py

+51-40
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,90 @@
1-
import os
21
import subprocess
32
import json
43
import argparse
5-
6-
7-
4+
85
def main():
96
parser = argparse.ArgumentParser()
107
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')
119
args = parser.parse_args()
12-
10+
1311
builds = getBuildsForStream(args.stream)
1412
for build in builds:
1513
build_id=build['id']
1614
arches=build['arches']
1715
for arch in arches:
18-
print(f"The build is {build_id}")
16+
print(f"Parsing AMIs for {build_id} for {arch}")
1917
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')
2119
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+
break
2326
# 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']
30-
for ami in amis:
31-
checkAndAddTag(ami["hvm"], ami["name"])
32-
checkAndAddTag(ami["snapshot"], ami["name"])
33-
return
34-
35-
def checkAndAddTag(resourceId, region):
36-
tagExists = checkTag(resourceId)
37-
if tagExists:
38-
print(f"{resourceId} already tagged with FedoraUser=coreos tag")
39-
else:
40-
addTag(resourceId, region)
41-
print(f"FedoraUser=coreos tag successfully added to {resourceId}")
27+
# print("AFTER PATSDING")
28+
# data ={"amis":[{
29+
# "name": "us-east-1",
30+
# "hvm": "ami-0016d5df3041499f9",
31+
# "snapshot": "snap-0c1ca4850fcd5e573"
32+
# }]}
33+
# amis = data['amis']
4234

43-
def checkTag(resourceId):
44-
checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} Name=value,Values=coreos'
35+
for ami in amis:
36+
region = ami["name"]
37+
checkAndAddTag(ami["hvm"], region, args.dry_run)
38+
checkAndAddTag(ami["snapshot"], region, args.dry_run)
39+
return
40+
41+
def checkAndAddTag(resourceId, region, dry_run):
42+
checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} --region {region} --output=json'
4543
try:
4644
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
45+
if tagCheck.stdout == None or tagCheck.stdout == '':
46+
print(f"Cannot access {resourceId}")
47+
return
48+
tagCheck=json.loads(tagCheck.stdout)
49+
50+
if any((tag['Key'] == 'FedoraGroup' and tag['Value'] == 'coreos') for tag in tagCheck['Tags']):
51+
print(f"{resourceId} already tagged with FedoraGroup=coreos tag")
52+
return
53+
else:
54+
if dry_run:
55+
print(f"Would add tag 'FedoraGroup=coreos' to {resourceId} in region {region}")
56+
return
57+
else:
58+
addTag(resourceId, region)
59+
print(f"FedoraGroup=coreos tag successfully added to {resourceId}")
60+
return
5061
except subprocess.CalledProcessError as e:
5162
return(e.output)
52-
63+
5364
def addTag(resourceId, region):
54-
UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraUser",Value="coreos" --region {region}'
65+
UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraGroup",Value="coreos" --region {region}'
5566
try:
5667
subprocess.run([UpdateTagCmd], shell=True)
5768
except subprocess.CalledProcessError as e:
5869
return(e.output)
59-
70+
6071
def getBuildsForStream(stream):
6172
buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all'
6273
try:
63-
subprocess.call(['/bin/bash', '-i', '-c', buildFetch])
74+
subprocess.check_output(['/bin/bash', '-i', '-c', buildFetch])
6475
except subprocess.CalledProcessError as e:
6576
return(e.output)
66-
67-
f = open('builds/builds.json')
77+
78+
f = open(f'builds/builds.json')
6879
data = json.load(f)
6980
return data['builds']
70-
81+
7182
def buildFetch(stream, build, arch):
7283
buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --build=' + build + ' --arch=' + arch
7384
try:
74-
subprocess.call(['/bin/bash', '-i', '-c', buildFetchCmd])
85+
subprocess.check_output(['/bin/bash', '-i', '-c', buildFetchCmd])
7586
except subprocess.CalledProcessError as e:
7687
return(e.output)
77-
88+
7889
if __name__ == '__main__':
7990
main()

0 commit comments

Comments
 (0)