-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-quay.py
123 lines (104 loc) · 4.24 KB
/
list-quay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
# __author__ = Stephan Kristyn <[email protected]> & Marcel Oed <[email protected]>
import requests
from pprint import pprint
import json
from argparse import ArgumentParser
from sys import exit
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def main(args):
quay_token = '<YOUR TOKEN>'
namespace = '<YOUR CUSTOMER NAMESPACE>'
header = {
'Authorization': 'Bearer ' + quay_token
}
url = 'https://quay.io/api/v1/repository'
params = {
'public': False,
'namespace': namespace
}
# Fetch available Repositorys
myResponse = requests.get(url, headers=header, params=params)
quay_repositories = set()
if(myResponse.ok):
jData = json.loads(myResponse.content)
for entity in jData["repositories"]:
if entity["kind"] == "image":
quay_repositories.add(entity["name"])
else:
myResponse.raise_for_status()
# Do we limit our lookup on one repository?
if args.repo != None:
if args.repo in quay_repositories:
quay_repositories = set()
quay_repositories.add(args.repo)
else:
print(bcolors.BOLD + bcolors.FAIL + '\n\tERROR: Repository %s not found!\n' % args.repo + bcolors.ENDC )
exit(1)
# do we exclude repos ?
if args.exclude != None:
exclude_list = [x.strip() for x in args.exclude.split(",")]
quay_repositories = quay_repositories.difference(exclude_list)
# Get build state
state = dict()
quay_repositories = sorted(quay_repositories)
for repo in quay_repositories:
state[repo] = []
url = 'https://quay.io/api/v1/repository/' + namespace + '/' + repo + '/build/'
params = {
'limit': '25'
}
myResponse = requests.get(url, headers=header, params=params)
if(myResponse.ok):
jData = json.loads(myResponse.content)
for build in jData['builds']:
if args.tag in build["tags"]:
state[repo].append({
'id': build['id'],
'phase': build['phase'],
'started': build['started'],
'tags': build['tags'],
'commit': build['trigger_metadata']['commit'],
'message': build['trigger_metadata']['commit_info']['message'],
})
else:
myResponse.raise_for_status()
# Outputs
for repo in quay_repositories:
print('Service: ' + bcolors.ENDC + bcolors.BOLD + bcolors.OKBLUE + repo + bcolors.ENDC)
print('\t' + bcolors.HEADER + bcolors.UNDERLINE + \
'Build Start\t\t\t' + 'Tags\t\t' + 'Phase' + bcolors.ENDC)
for build in state[repo]:
align_buffer = " " * ( 16 - len(", ".join(build['tags'])) )
if build['phase'] == 'error':
state_color = bcolors.FAIL + bcolors.BOLD
elif build['phase'] == 'complete':
state_color = bcolors.OKGREEN
else:
state_color = bcolors.WARNING
print('\t' + build['started'] + '\t' + ", ".join(build['tags']) + \
align_buffer + state_color + build['phase'] + bcolors.ENDC)
if not state[repo]:
print(bcolors.FAIL + bcolors.BOLD + '\tNOTHING FOUND\t\t\t-\t\terror' + bcolors.ENDC)
print('')
if __name__ == "__main__":
parser = ArgumentParser(description='Script for verifing the presence of Docker builds for certain tags in Quay.')
requiredNamed = parser.add_argument_group('required arguments')
# required arguments
requiredNamed.add_argument('-t', '--tag', metavar='TAG', \
help='Lookup for certain build tag', required=True)
# optional arguments
parser.add_argument('-r', '--repo', metavar='SAVEUP-EXAMPLE', \
help="Limit lookup to one repository")
parser.add_argument('-e', '--exclude', metavar='repo1,repo2', \
help="Exclude specified repositories")
args = parser.parse_args()
main(args)