This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph.py
338 lines (295 loc) · 13.7 KB
/
graph.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from collections import defaultdict
import json
from osbs.utils import strip_registry_from_image
import sys
import datetime
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import re
def sizeof_fmt(num, suffix='B'):
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
class BuildTree(object):
def __init__(self, builds, pulp_base_url):
self.deps = defaultdict(set)
self.seen = set()
self.when = {}
self.known_pulp_layers = {}
self.found_image_name_sizes = {}
self.pulp_upload_size = {}
self.duration = {}
self.upload_size = {}
self.layer_size = {}
self.excess_repos = []
self.pulp_base_url = pulp_base_url
# A dict to store the reference to the actual upload_size for each uploaded tag
self.tags_aliases = {}
builds = [build for build in builds
if ('status' in build and
build['status'].get('phase') == 'Complete' and
'startTimestamp' in build['status'])]
builds.sort(key=lambda x: x['status']['startTimestamp'],
reverse=True)
for build in builds:
self.add(build)
def _get_layer_info(self, layer_id, pulp_repo_url):
layer_json = {}
try:
size = None
image_name = None
parent_id = None
if layer_id in self.known_pulp_layers.keys():
image_name = self.known_pulp_layers[layer_id]
size = self.found_image_name_sizes[image_name]
return (None, size, image_name)
else:
pulp_url = pulp_repo_url + '/%s/json' % layer_id
r = requests.get(pulp_url, verify=False)
r.raise_for_status()
layer_json = r.json()
except Exception as e:
sys.stderr.write(" _get_layer_info(%s): %s" % (layer_id, repr(e)))
return (None, None, None)
try:
size = layer_json.get('Size', 0)
parent_id = layer_json.get('parent', None)
labels = layer_json['config'].get('Labels', {'Name': '', 'Version': '', 'Release': ''})
image_name = '%s:%s-%s' % (labels['Name'], labels['Version'], labels['Release'])
except Exception as e:
sys.stderr.write(" _get_layer_info(%s): %s" % (layer_id, repr(e)))
return (parent_id, size, image_name)
def _get_size_with_parent_layers(self, image_name, parent_layer):
parent_layer_size = 0
while parent_layer:
parent_image_name = self.known_pulp_layers.get(parent_layer, None)
if not parent_image_name:
# Lookup parent layer image in current repo
parent_image_name = '-'.join(image_name.split(':')[0].split('/'))
pulp_repo_url = '%s/pulp/docker/v1/redhat-%s' % (
self.pulp_base_url, parent_image_name)
(parent_layer, current_parent_layer_size, _) = self._get_layer_info(
parent_layer, pulp_repo_url)
parent_layer_size += current_parent_layer_size
return parent_layer_size
def _get_upload_size(self, build):
annotations = build['metadata']['annotations']
image_id = annotations['image-id']
if image_id in self.pulp_upload_size.keys():
return (0, self.pulp_upload_size[image_id])
repos_json = json.loads(annotations['repositories'])
unique_repos = repos_json['unique']
if not unique_repos:
self.pulp_upload_size[image_id] = 0
return (0, 0)
repo = repos_json['primary'][0]
expected_image_name = "%s/%s:%s-%s" % (
repo.split('/')[-2],
repo.split('/')[-1].split(':')[0],
repo.split('/')[-1].split(':')[1].split('-')[0],
'-'.join(repo.split('/')[-1].split(':')[1].split('-')[1:])
)
full_image_name = unique_repos[0]
image_name = '-'.join(strip_registry_from_image(full_image_name).split('/'))
if not self.pulp_base_url:
# Base url for pulp is not specified - fall back to an old method
try:
size = json.loads(annotations['tar_metadata'])['size']
return (size, size)
except:
return (0, 0)
pulp_repo_url = '%s/pulp/docker/v1/redhat-%s' % (self.pulp_base_url, image_name.split(':')[0])
if image_id in self.known_pulp_layers.keys():
size = self.found_image_name_sizes[expected_image_name]
self.pulp_upload_size[image_id] = size
sys.stderr.write(" found cached size for %s: %s\n" % (image_id, size))
(parent_layer, layer_size, image_name) = self._get_layer_info(image_id, pulp_repo_url)
total_size = self._get_size_with_parent_layers(image_name, parent_layer)
return (size, total_size)
pulp_url = '%s/%s/json' % (pulp_repo_url, image_id)
sys.stderr.write("Looking for image size at %s\n" % pulp_url)
try:
r = requests.get(pulp_url, verify=False)
r.raise_for_status()
size = r.json()['Size']
self.pulp_upload_size[image_id] = size
(parent_layer, layer_size, image_name) = self._get_layer_info(image_id, pulp_repo_url)
total_size = self._get_size_with_parent_layers(image_name, parent_layer)
return (total_size, size)
except requests.HTTPError:
try:
sys.stderr.write(" looking for layers at %s with image_name %s\n" % (
pulp_repo_url, expected_image_name))
# Wrong image_id, go to repo's page and check every layer id
r = requests.get(pulp_repo_url, verify=False)
matches = re.findall(r'href="(.+)/"', r.text)
sys.stderr.write(" found %s layers\n" % len(matches))
# Skip the header link
for layer_id in matches[1:]:
(parent_layer, layer_size, image_name) = self._get_layer_info(
layer_id, pulp_repo_url)
self.found_image_name_sizes[image_name] = layer_size
self.known_pulp_layers[layer_id] = image_name
if image_name != expected_image_name:
sys.stderr.write(" %s != %s\n" % (image_name, expected_image_name))
continue
sys.stderr.write(" found layer with size %s\n" % layer_size)
parent_layer_size = self._get_size_with_parent_layers(image_name, parent_layer)
sys.stderr.write(" parent's layer size: %s\n" % parent_layer_size)
total_size = layer_size + parent_layer_size
self.pulp_upload_size[image_id] = total_size
return (total_size, layer_size)
else:
raise RuntimeError("No matching layer found")
except Exception as e:
sys.stderr.write(" cannot find layer info at this url: %s\n" % (repr(e)))
self.pulp_upload_size[image_id] = 0
return (0, 0)
def add(self, build):
try:
annotations = build['metadata']['annotations']
base_image_name = annotations['base-image-name']
repositories = json.loads(annotations['repositories'])
when = build['status']['startTimestamp']
duration = int(build['status']['duration']) / (10**9)
(upload_size, layer_size) = self._get_upload_size(build)
except (KeyError, IndexError):
return
repos = set([strip_registry_from_image(repo)
for repo in repositories['primary']])
duplicates = self.seen.intersection(repos)
repos -= duplicates
self.seen.update(repos)
self.deps[strip_registry_from_image(base_image_name)].update(repos)
if list(repos):
first_tag = list(repos)[0]
self.upload_size[first_tag] = upload_size
self.layer_size[first_tag] = layer_size
self.duration[first_tag] = duration
for repo in repos:
self.when[repo] = when
self.tags_aliases[repo] = first_tag
def _trim_layers(self, base):
layers = self.deps[base]
excess = set()
for layer in layers:
name, version = layer.split(':', 1)
if version == 'latest':
pass
elif not self.deps.get(layer):
# Leaf node
excess.add(layer)
self.deps[base] -= excess
self.excess_repos += excess
return excess
def trim_excess_tags(self):
while True:
images = [image for image in self.deps.keys()]
any_trimmed = False
for base in images:
if self.deps.get(base):
if self._trim_layers(base):
any_trimmed = True
if not any_trimmed:
break
def get_trimmed_layer_size(self):
trimmed_layers = []
images = [image for image in self.deps.keys()]
for base in images:
layers = self.deps.get(base, [])
for layer in layers:
if layer in trimmed_layers:
continue
if self.layer_size.get(base) and self.layer_size.get(layer):
self.layer_size[layer] -= self.layer_size[base]
trimmed_layers.append(layer)
return sum([v for k, v in self.layer_size.items()])
def calculate_totals(self):
trimmed_duration = sum([v for k, v in self.duration.items()])
trimmed_upload_size = sum([v for k, v in self.upload_size.items()])
trimmed_layer_size = sum([v for k, v in self.layer_size.items()])
for layer in self.excess_repos:
if layer in self.duration.keys():
trimmed_duration -= self.duration[layer]
if layer in self.upload_size.keys():
trimmed_upload_size -= self.upload_size[layer]
if layer in self.layer_size.keys():
trimmed_layer_size -= self.layer_size[layer]
return (trimmed_duration, trimmed_upload_size, trimmed_layer_size)
def __repr__(self):
return repr(self.deps)
def get_build_duration(self, tag_name):
if tag_name in self.duration.keys():
return str(datetime.timedelta(seconds=self.duration[tag_name]))
else:
if tag_name in self.tags_aliases.keys():
return str(datetime.timedelta(seconds=self.duration[self.tags_aliases[tag_name]]))
else:
return ""
def get_upload_size(self, tag_name):
if tag_name in self.upload_size.keys():
return sizeof_fmt(self.upload_size[tag_name])
else:
if tag_name in self.tags_aliases.keys():
return sizeof_fmt(self.upload_size[self.tags_aliases[tag_name]])
else:
return ""
def as_graph_easy_txt(self,
include_datestamp=False,
include_duration=False,
include_upload=False):
txt = ''
def formatwhen(name):
if include_datestamp:
try:
return "\\n{when}".format(when=self.when[name][:10])
except KeyError:
return ""
else:
return ""
def formatduration(name):
if include_duration:
return "\\n{duration}".format(duration=self.get_build_duration(name))
else:
return ""
def formatupload(name):
if include_upload:
return "\\n{upload}".format(upload=self.get_upload_size(name))
else:
return ""
for base, layers in self.deps.items():
for layer in layers:
txt += "[ {base}{when}{duration}{upload} ]".format(base=base,
when=formatwhen(base),
duration=formatduration(base),
upload=formatupload(base))
txt += " --> "
txt += "[ {layer}{when}{duration}{upload} ]".format(layer=layer,
when=formatwhen(layer),
duration=formatduration(layer),
upload=formatupload(layer))
txt += "\n"
return txt
def run(inputfile=None, pulp_base_url=None):
if inputfile is not None:
with open(inputfile) as fp:
builds = json.load(fp)
else:
builds = json.load(sys.stdin)
tree = BuildTree(builds, pulp_base_url)
tree.trim_excess_tags()
print(tree.as_graph_easy_txt(
include_datestamp=True, include_duration=True, include_upload=True))
(total_duration, total_upload_size, total_layers_size) = tree.calculate_totals()
sys.stderr.write("Total duration: %s\n" % str(datetime.timedelta(seconds=total_duration)))
sys.stderr.write("Total upload size: %s (%s to Pulp and %s to Brew/Koji)\n" % (
sizeof_fmt(total_upload_size + total_layers_size),
sizeof_fmt(total_layers_size),
sizeof_fmt(total_upload_size)))
if __name__ == '__main__':
args = [sys.argv[1] if len(sys.argv) > 1 else None,
sys.argv[2] if len(sys.argv) > 2 else None]
run(*args)