Skip to content

Commit 5eca0e4

Browse files
committed
fix
1 parent 6644cdd commit 5eca0e4

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

src/osbuild-manifests/aws_tag.py

+50-30
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,68 @@
88
def main():
99
parser = argparse.ArgumentParser()
1010
parser.add_argument('--stream', dest='stream', type=str, help='Fedora stream', required=True)
11-
parser.add_argument('--arch', dest='arch', type=str, help='Architecture', default='x86_64')
1211
args = parser.parse_args()
1312

14-
builds = getBuildsForStream(args.stream, args.arch)
13+
builds = getBuildsForStream(args.stream)
1514
for build in builds:
16-
print("The build is "+build)
17-
buildFetch(args.stream, build, args.arch)
18-
meta = open('builds/'+build+'/'+args.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-
amis = data['amis']
28-
for ami in amis:
29-
UpdateTagCmd = 'aws ec2 create-tags --resource ' + ami['hvm'] + ' --tags '+ 'Key="FedoraUser",Value="coreos"' + ' --region=' + ami['name']
30-
try:
31-
subprocess.call(['/bin/bash', '-i', '-c', UpdateTagCmd])
32-
except subprocess.CalledProcessError as e:
33-
return(e.output)
34-
return
35-
36-
def getBuildsForStream(stream, arch):
37-
buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch='+ arch
15+
build_id=build['id']
16+
arches=build['arches']
17+
for arch in arches:
18+
print(f"The build is {build_id}")
19+
buildFetch(args.stream, build_id, arch)
20+
meta = open('builds/'+build_id+'/'+arch+'/meta.json')
21+
data = json.load(meta)
22+
23+
# 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}")
42+
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)
52+
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+
def getBuildsForStream(stream):
61+
buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all'
3862
try:
3963
subprocess.call(['/bin/bash', '-i', '-c', buildFetch])
4064
except subprocess.CalledProcessError as e:
4165
return(e.output)
4266

4367
f = open('builds/builds.json')
4468
data = json.load(f)
45-
builds = []
46-
47-
for i in data['builds']:
48-
builds.append(i['id'])
49-
return builds
69+
return data['builds']
5070

5171
def buildFetch(stream, build, arch):
52-
buildFetchCmd = 'cosa buildfetch --stream='+ stream + '--build=' + build + '--arch=' + arch
72+
buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --build=' + build + ' --arch=' + arch
5373
try:
5474
subprocess.call(['/bin/bash', '-i', '-c', buildFetchCmd])
5575
except subprocess.CalledProcessError as e:

0 commit comments

Comments
 (0)