-
Notifications
You must be signed in to change notification settings - Fork 4
/
artefacts.py
162 lines (137 loc) · 5.38 KB
/
artefacts.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
import json
import zlib
import aiohttp.web
import oci.model
import ocm
import consts
import util
class ArtefactBlob(aiohttp.web.View):
async def get(self):
'''
---
description:
Returns a requested artefact (from a OCM Component) as an octet-stream. This route is
limited to artefacts with `localBlob` as access-type. If artefact is not specified
unambiguously, the first match will be used.
tags:
- Artefacts
produces:
- application/octet-stream
parameters:
- in: query
name: component
type: string
required: true
description: component-name:component-version
- in: query
name: artefact
type: string
required: true
description: |
has two forms:
1. str - interpreted as `name` attribute
2. json (object) - str-to-str mapping for attributes
- in: query
name: ocm_repository
type: string
required: false
description: ocm-repository-url
- in: query
name: unzip
type: boolean
required: false
default: true
description:
if true and artefact's access is gzipped, returned content will be unzipped (for
convenience)
'''
params = self.request.rel_url.query
component_id = util.param(params, 'component', required=True)
if component_id.count(':') != 1:
raise aiohttp.web.HTTPBadRequest(text='Malformed component-id')
artefact = util.param(params, 'artefact', required=True).strip()
if artefact.startswith('{'):
artefact = json.loads(artefact)
# special-handling for name/version (should refactor in ocm)
artefact_name = artefact.pop('name', None)
artefact_version = artefact.pop('version', None)
elif artefact.startswith('['):
raise aiohttp.web.HTTPBadRequest(
text='Bad artefact: Either name or json-object is allowed',
)
else:
artefact_name = artefact
artefact = {}
artefact_version = None
ocm_repository = util.param(params, 'ocm_repository')
unzip = util.param_as_bool(params, 'unzip', default=True)
component_descriptor_lookup = self.request.app[consts.APP_COMPONENT_DESCRIPTOR_LOOKUP]
try:
component_descriptor = await component_descriptor_lookup(
component_id,
ocm_repository,
)
component = component_descriptor.component
except oci.model.OciImageNotFoundException:
raise aiohttp.web.HTTPBadRequest(text=f'Did not find {component_id=}')
def matches(a: ocm.Artifact):
if artefact_name and artefact_name != a.name:
return False
if artefact_version and artefact_version != a.version:
return False
for attr, value in artefact.items():
if a.extraIdentity.get(attr) != value:
return False
return True
for a in component.iter_artefacts():
if matches(a):
break
else:
raise aiohttp.web.HTTPBadRequest(text='Did not find requested artefact')
artefact = a
access = artefact.access
if not isinstance(access, ocm.LocalBlobAccess):
raise aiohttp.web.HTTPBadRequest(
text=f'{artefact.name=} has {access.type=}; only localBlobAccess is supported',
)
access: ocm.LocalBlobAccess
digest = access.globalAccess.digest if access.globalAccess else access.localReference
oci_client = self.request.app[consts.APP_OCI_CLIENT]
blob = await oci_client.blob(
image_reference=component.current_ocm_repo.component_oci_ref(component),
digest=digest,
absent_ok=True,
)
if access.mediaType == 'application/pdf':
file_ending = '.pdf'
elif access.mediaType == 'application/tar+gzip':
file_ending = '.tar.gz'
elif access.mediaType == 'application/tar':
file_ending = '.tar'
else:
file_ending = ''
fname = f'{component.name}_{component.version}_{artefact.name}{file_ending}'
if unzip and access.mediaType == 'application/gzip':
response = aiohttp.web.StreamResponse(
headers={
'Content-Type': artefact.type,
'Content-Disposition': f'attachment; filename="{fname}"',
},
)
await response.prepare(self.request)
decompressor = zlib.decompressobj(wbits=31)
async for chunk in blob.content.iter_chunked(4096):
await response.write(decompressor.decompress(chunk))
await response.write(decompressor.flush())
else:
response = aiohttp.web.StreamResponse(
headers={
'Content-Type': access.mediaType,
'Content-Disposition': f'attachment; filename="{fname}"',
},
)
await response.prepare(self.request)
async for chunk in blob.content.iter_chunked(4096):
await response.write(chunk)
await response.write_eof()
return response