1- import os
21import subprocess
32import json
43import argparse
5-
6-
7-
4+
85def 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"The build is { 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+
2321 # 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' ]
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
3032 for ami in amis :
31- checkAndAddTag (ami ["hvm" ], ami ["name" ])
32- checkAndAddTag (ami ["snapshot" ], ami ["name" ])
33- return
34-
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+
3549def checkAndAddTag (resourceId , region ):
36- tagExists = checkTag (resourceId )
50+ tagExists = checkTag (resourceId , region )
3751 if tagExists :
38- print (f"{ resourceId } already tagged with FedoraUser =coreos tag" )
52+ print (f"{ resourceId } already tagged with FedoraGroup =coreos tag" )
3953 else :
4054 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'
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 '
4559 try :
4660 tagCheck = subprocess .run ([checkTagCmd ], shell = True , capture_output = True , text = True )
47- if "FedoraUser" and "coreos" in tagCheck .stdout :
61+ tagCheck = json .loads (tagCheck .stdout )
62+ if any ((tag ['Value' ] == 'coreos' and tag ['Key' ] == 'FedoraGroup' ) for tag in tagCheck ['Tags' ]):
4863 return True
4964 return False
5065 except subprocess .CalledProcessError as e :
5166 return (e .output )
52-
67+
5368def addTag (resourceId , region ):
54- UpdateTagCmd = f'aws ec2 create-tags --resource { resourceId } --tags Key="FedoraUser ",Value="coreos" --region { region } '
69+ UpdateTagCmd = f'aws ec2 create-tags --resource { resourceId } --tags Key="FedoraGroup ",Value="coreos" --region { region } '
5570 try :
5671 subprocess .run ([UpdateTagCmd ], shell = True )
5772 except subprocess .CalledProcessError as e :
5873 return (e .output )
59-
74+
6075def getBuildsForStream (stream ):
6176 buildFetch = 'cosa buildfetch --stream=' + stream + ' --arch=all'
6277 try :
63- subprocess .call (['/bin/bash' , '-i' , '-c' , buildFetch ])
78+ subprocess .check_output (['/bin/bash' , '-i' , '-c' , buildFetch ])
6479 except subprocess .CalledProcessError as e :
6580 return (e .output )
66-
67- f = open ('builds/builds.json' )
81+
82+ f = open (f 'builds/builds.json' )
6883 data = json .load (f )
6984 return data ['builds' ]
70-
85+
7186def buildFetch (stream , build , arch ):
7287 buildFetchCmd = 'cosa buildfetch --stream=' + stream + ' --build=' + build + ' --arch=' + arch
7388 try :
74- subprocess .call (['/bin/bash' , '-i' , '-c' , buildFetchCmd ])
89+ subprocess .check_output (['/bin/bash' , '-i' , '-c' , buildFetchCmd ])
7590 except subprocess .CalledProcessError as e :
7691 return (e .output )
77-
92+
7893if __name__ == '__main__' :
7994 main ()
95+
0 commit comments