|
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"Parsing AMIs for {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 | + |
| 21 | + if 'amis' in data.keys(): |
| 22 | + amis = data['amis'] |
| 23 | + else: |
| 24 | + print(f"{build_id} does not have any AMIs for {arch} in meta.json") |
| 25 | + break |
23 | 26 | # 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}") |
| 27 | + # print("AFTER PATSDING") |
| 28 | + # data ={"amis":[{ |
| 29 | + # "name": "us-east-1", |
| 30 | + # "hvm": "ami-0016d5df3041499f9", |
| 31 | + # "snapshot": "snap-0c1ca4850fcd5e573" |
| 32 | + # }]} |
| 33 | + # amis = data['amis'] |
42 | 34 |
|
43 |
| -def checkTag(resourceId): |
44 |
| - checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} Name=value,Values=coreos' |
| 35 | + for ami in amis: |
| 36 | + region = ami["name"] |
| 37 | + checkAndAddTag(ami["hvm"], region, args.dry_run) |
| 38 | + checkAndAddTag(ami["snapshot"], region, args.dry_run) |
| 39 | + return |
| 40 | + |
| 41 | +def checkAndAddTag(resourceId, region, dry_run): |
| 42 | + checkTagCmd = f'aws ec2 describe-tags --filters Name=resource-id,Values={resourceId} --region {region} --output=json' |
45 | 43 | try:
|
46 | 44 | 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 |
| 45 | + if tagCheck.stdout == None or tagCheck.stdout == '': |
| 46 | + print(f"Cannot access {resourceId}") |
| 47 | + return |
| 48 | + tagCheck=json.loads(tagCheck.stdout) |
| 49 | + |
| 50 | + if any((tag['Key'] == 'FedoraGroup' and tag['Value'] == 'coreos') for tag in tagCheck['Tags']): |
| 51 | + print(f"{resourceId} already tagged with FedoraGroup=coreos tag") |
| 52 | + return |
| 53 | + else: |
| 54 | + if dry_run: |
| 55 | + print(f"Would add tag 'FedoraGroup=coreos' to {resourceId} in region {region}") |
| 56 | + return |
| 57 | + else: |
| 58 | + addTag(resourceId, region) |
| 59 | + print(f"FedoraGroup=coreos tag successfully added to {resourceId}") |
| 60 | + return |
50 | 61 | except subprocess.CalledProcessError as e:
|
51 | 62 | return(e.output)
|
52 |
| - |
| 63 | + |
53 | 64 | def addTag(resourceId, region):
|
54 |
| - UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraUser",Value="coreos" --region {region}' |
| 65 | + UpdateTagCmd = f'aws ec2 create-tags --resource {resourceId} --tags Key="FedoraGroup",Value="coreos" --region {region}' |
55 | 66 | try:
|
56 | 67 | subprocess.run([UpdateTagCmd], shell=True)
|
57 | 68 | except subprocess.CalledProcessError as e:
|
58 | 69 | return(e.output)
|
59 |
| - |
| 70 | + |
60 | 71 | def getBuildsForStream(stream):
|
61 | 72 | buildFetch = 'cosa buildfetch --stream='+ stream + ' --arch=all'
|
62 | 73 | try:
|
63 |
| - subprocess.call(['/bin/bash', '-i', '-c', buildFetch]) |
| 74 | + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetch]) |
64 | 75 | except subprocess.CalledProcessError as e:
|
65 | 76 | return(e.output)
|
66 |
| - |
67 |
| - f = open('builds/builds.json') |
| 77 | + |
| 78 | + f = open(f'builds/builds.json') |
68 | 79 | data = json.load(f)
|
69 | 80 | return data['builds']
|
70 |
| - |
| 81 | + |
71 | 82 | def buildFetch(stream, build, arch):
|
72 | 83 | buildFetchCmd = 'cosa buildfetch --stream='+ stream + ' --build=' + build + ' --arch=' + arch
|
73 | 84 | try:
|
74 |
| - subprocess.call(['/bin/bash', '-i', '-c', buildFetchCmd]) |
| 85 | + subprocess.check_output(['/bin/bash', '-i', '-c', buildFetchCmd]) |
75 | 86 | except subprocess.CalledProcessError as e:
|
76 | 87 | return(e.output)
|
77 |
| - |
| 88 | + |
78 | 89 | if __name__ == '__main__':
|
79 | 90 | main()
|
0 commit comments