1
- import os
2
1
import subprocess
3
2
import json
4
3
import argparse
5
-
6
-
7
-
4
+
8
5
def main ():
9
6
parser = argparse .ArgumentParser ()
10
7
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' )
11
9
args = parser .parse_args ()
12
-
10
+
13
11
builds = getBuildsForStream (args .stream )
14
12
for build in builds :
15
13
build_id = build ['id' ]
16
14
arches = build ['arches' ]
17
15
for arch in arches :
18
- print (f"The build is { build_id } " )
16
+ print (f"The build is { build_id } for { arch } " )
19
17
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' )
21
19
data = json .load (meta )
22
-
20
+
23
21
# 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
30
32
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
+
35
49
def checkAndAddTag (resourceId , region ):
36
- tagExists = checkTag (resourceId )
50
+ tagExists = checkTag (resourceId , region )
37
51
if tagExists :
38
- print (f"{ resourceId } already tagged with FedoraUser =coreos tag" )
52
+ print (f"{ resourceId } already tagged with FedoraGroup =coreos tag" )
39
53
else :
40
54
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 '
45
59
try :
46
60
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' ]):
48
63
return True
49
64
return False
50
65
except subprocess .CalledProcessError as e :
51
66
return (e .output )
52
-
67
+
53
68
def 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 } '
55
70
try :
56
71
subprocess .run ([UpdateTagCmd ], shell = True )
57
72
except subprocess .CalledProcessError as e :
58
73
return (e .output )
59
-
74
+
60
75
def getBuildsForStream (stream ):
61
76
buildFetch = 'cosa buildfetch --stream=' + stream + ' --arch=all'
62
77
try :
63
- subprocess .call (['/bin/bash' , '-i' , '-c' , buildFetch ])
78
+ subprocess .check_output (['/bin/bash' , '-i' , '-c' , buildFetch ])
64
79
except subprocess .CalledProcessError as e :
65
80
return (e .output )
66
-
67
- f = open ('builds/builds.json' )
81
+
82
+ f = open (f 'builds/builds.json' )
68
83
data = json .load (f )
69
84
return data ['builds' ]
70
-
85
+
71
86
def buildFetch (stream , build , arch ):
72
87
buildFetchCmd = 'cosa buildfetch --stream=' + stream + ' --build=' + build + ' --arch=' + arch
73
88
try :
74
- subprocess .call (['/bin/bash' , '-i' , '-c' , buildFetchCmd ])
89
+ subprocess .check_output (['/bin/bash' , '-i' , '-c' , buildFetchCmd ])
75
90
except subprocess .CalledProcessError as e :
76
91
return (e .output )
77
-
92
+
78
93
if __name__ == '__main__' :
79
94
main ()
95
+
0 commit comments