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
+
1
6
import subprocess
2
7
import json
3
8
import argparse
4
-
9
+
5
10
def main ():
6
11
parser = argparse .ArgumentParser ()
7
12
parser .add_argument ('--stream' , dest = 'stream' , type = str , help = 'Fedora stream' , required = True )
8
13
parser .add_argument ('--dry-run' , dest = 'dry_run' , help = 'Check if the resources have tags but not add them' , action = 'store_true' )
9
14
args = parser .parse_args ()
10
-
15
+
11
16
builds = getBuildsForStream (args .stream )
12
17
for build in builds :
13
18
build_id = build ['id' ]
@@ -17,63 +22,49 @@ def main():
17
22
buildFetch (args .stream , build_id , arch )
18
23
meta = open (f'builds/{ build_id } /{ arch } /meta.json' )
19
24
data = json .load (meta )
20
-
25
+
21
26
if 'amis' in data .keys ():
22
27
amis = data ['amis' ]
23
28
else :
24
29
print (f"{ build_id } does not have any AMIs for { arch } in meta.json" )
25
30
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
-
34
31
for ami in amis :
35
32
region = ami ["name" ]
36
33
checkAndAddTag (ami ["hvm" ], region , args .dry_run )
37
34
checkAndAddTag (ami ["snapshot" ], region , args .dry_run )
38
- return
39
-
35
+
40
36
def checkAndAddTag (resourceId , region , dry_run ):
41
37
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 )
43
39
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" )
45
41
return
46
42
tagCheck = json .loads (tagCheck .stdout )
47
43
48
44
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 } " )
51
52
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 :
62
53
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 } " )
65
56
66
57
def getBuildsForStream (stream ):
67
58
buildFetchCmd = 'cosa buildfetch --stream=' + stream + ' --arch=all'
68
- subprocess .check_output ([ '/bin/bash' , '-i' , '-c' , buildFetchCmd ] )
69
-
59
+ subprocess .check_output (buildFetchCmd . split ( ' ' ) )
60
+
70
61
f = open (f'builds/builds.json' )
71
62
data = json .load (f )
72
63
return data ['builds' ]
73
-
64
+
74
65
def buildFetch (stream , build , arch ):
75
66
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
+
78
69
if __name__ == '__main__' :
79
70
main ()
0 commit comments