1+ #!/usr/bin/python3
2+
3+ # Script to go through all builds and add the FedoraGroup=coreos
4+ # tag to all AMIs and snapshots that we know about.
5+
16import subprocess
27import json
38import argparse
4-
9+
510def main ():
611 parser = argparse .ArgumentParser ()
712 parser .add_argument ('--stream' , dest = 'stream' , type = str , help = 'Fedora stream' , required = True )
813 parser .add_argument ('--dry-run' , dest = 'dry_run' , help = 'Check if the resources have tags but not add them' , action = 'store_true' )
914 args = parser .parse_args ()
10-
15+
1116 builds = getBuildsForStream (args .stream )
1217 for build in builds :
1318 build_id = build ['id' ]
@@ -17,63 +22,49 @@ def main():
1722 buildFetch (args .stream , build_id , arch )
1823 meta = open (f'builds/{ build_id } /{ arch } /meta.json' )
1924 data = json .load (meta )
20-
25+
2126 if 'amis' in data .keys ():
2227 amis = data ['amis' ]
2328 else :
2429 print (f"{ build_id } does not have any AMIs for { arch } in meta.json" )
2530 continue
26- # Delete this when actually running. Just here while I make this script
27- # data ={"amis":[{
28- # "name": "us-east-1",
29- # "hvm": "ami-0016d5df3041499f9",
30- # "snapshot": "snap-0c1ca4850fcd5e573"
31- # }]}
32- # amis = data['amis']
33-
3431 for ami in amis :
3532 region = ami ["name" ]
3633 checkAndAddTag (ami ["hvm" ], region , args .dry_run )
3734 checkAndAddTag (ami ["snapshot" ], region , args .dry_run )
38- return
39-
35+
4036def checkAndAddTag (resourceId , region , dry_run ):
4137 describeTagsCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={ resourceId } --region { region } --output=json'
42- tagCheck = subprocess .run ([ describeTagsCmd ], shell = True , capture_output = True , text = True )
38+ tagCheck = subprocess .run (describeTagsCmd . split ( ' ' ) , capture_output = True , text = True )
4339 if tagCheck .stdout == None or tagCheck .stdout == '' :
44- print (f"No tags detected for { resourceId } ; assuming it doesn't exist" )
40+ print (f"\t No tags detected for { resourceId } ; assuming it doesn't exist" )
4541 return
4642 tagCheck = json .loads (tagCheck .stdout )
4743
4844 if any ((tag ['Key' ] == 'FedoraGroup' and tag ['Value' ] == 'coreos' ) for tag in tagCheck ['Tags' ]):
49- print (f"{ resourceId } already tagged with FedoraGroup=coreos tag" )
50- return
45+ print (f"\t { resourceId } in { region } already tagged with FedoraGroup=coreos tag" )
46+ else :
47+ addTag (resourceId , region , dry_run )
48+
49+ def addTag (resourceId , region , dry_run ):
50+ if dry_run :
51+ print (f"\t Would add tag 'FedoraGroup=coreos' to { resourceId } in region { region } " )
5152 else :
52- if dry_run :
53- print (f"Would add tag 'FedoraGroup=coreos' to { resourceId } in region { region } " )
54- return
55- else :
56- addTag (resourceId , region , dry_run )
57-
58- def addTag (resourceId , region , dry_run ):
59- if dry_run :
60- print (f"Would add tag 'FedoraGroup=coreos' to { resourceId } in region { region } " )
61- else :
6253 UpdateTagCmd = f'aws ec2 create-tags --resource { resourceId } --tags Key="FedoraGroup",Value="coreos" --region { region } '
63- subprocess .run ([ UpdateTagCmd ], shell = True )
64- print (f"'FedoraGroup=coreos' tag successfully added to { resourceId } " )
54+ subprocess .run (UpdateTagCmd . split ( ' ' ))
55+ print (f"\t 'FedoraGroup=coreos' tag successfully added to { resourceId } in { region } " )
6556
6657def getBuildsForStream (stream ):
6758 buildFetchCmd = 'cosa buildfetch --stream=' + stream + ' --arch=all'
68- subprocess .check_output ([ '/bin/bash' , '-i' , '-c' , buildFetchCmd ] )
69-
59+ subprocess .check_output (buildFetchCmd . split ( ' ' ) )
60+
7061 f = open (f'builds/builds.json' )
7162 data = json .load (f )
7263 return data ['builds' ]
73-
64+
7465def buildFetch (stream , build , arch ):
7566 buildFetchCmd = 'cosa buildfetch --stream=' + stream + ' --build=' + build + ' --arch=' + arch
76- subprocess .check_output ([ '/bin/bash' , '-i' , '-c' , buildFetchCmd ] )
77-
67+ subprocess .check_output (buildFetchCmd . split ( ' ' ) )
68+
7869if __name__ == '__main__' :
7970 main ()
0 commit comments