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 | class TerraDrsClient(DrsClient):
"""
Calls the terra DRS server.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.endpoint = (
"https://us-central1-broad-dsde-prod.cloudfunctions.net/martha_v3"
)
self.token = None
@dataclass
class GcloudInfo(object):
account: str
project: str
async def _get_auth_token(self) -> str:
"""Get Google Cloud authentication token.
User must run 'gcloud auth login' from the shell before starting this script.
Returns:
str: auth token
see https://github.com/DataBiosphere/terra-notebook-utils/blob/b53bb8656d
502ecbdbfe9c5edde3fa25bd90bbf8/terra_notebook_utils/gs.py#L25-L42
"""
creds, projects = google.auth.default()
creds.refresh(google.auth.transport.requests.Request())
token = creds.token
assert token, "No token retrieved."
logger.info("gcloud token successfully fetched")
return creds
async def download_part(
self, drs_object: DrsObject, start: int, size: int, destination_path: Path, verbose: bool = False
) -> Optional[Path]:
tries = 0
while True:
try:
headers = {"Range": f"bytes={start}-{size}"}
file_name = destination_path / f"{drs_object.name}.{start}.{size}.part"
context = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(
drs_object.access_methods[0].access_url, ssl=context
) as request:
if request.status > 399:
text = await request.content.read()
request.raise_for_status()
file = await aiofiles.open(file_name, "wb")
self.statistics.set_max_files_open()
async for data in request.content.iter_any(): # uses less memory
await file.write(data)
await file.close()
return Path(file_name)
except aiohttp.ClientResponseError as f:
tries += 1
if "The provided token has expired" in str(text):
if verbose:
logger.info(f"Error Text Body {str(text)}")
drs_object.errors.append(f"RECOVERABLE in AIOHTTP {str(f)}")
return None
time.sleep((random.randint(0, 1000) / 1000) + 2**tries)
if tries > 2:
if verbose:
logger.info(f"Error Text Body {str(text)}")
return None
except Exception as e:
tries += 1
time.sleep((random.randint(0, 1000) / 1000) + 2**tries)
if tries > 2:
if verbose:
logger.info(f"Miscellaneous Error {str(text)}")
drs_object.errors.append(f"NONRECOVERABLE ERROR {str(e)}")
return None
async def sign_url(self, drs_object: DrsObject, verbose: bool) -> DrsObject:
"""No-op. terra returns a signed url in `get_object`"""
assert isinstance(drs_object, DrsObject), "A DrsObject should be passed"
if (self.token is None or (self.token.expired and self.token.expiry is not None)):
if verbose:
logger.info("fetching new token")
self.token = await self._get_auth_token()
if verbose:
logger.info(f"status of token expiration {self.token.expiry}")
data = {"url": drs_object.id, "fields": ["accessUrl"]}
headers = {
"authorization": "Bearer " + self.token.token,
"content-type": "application/json",
}
tries = 0
context = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession(headers=headers) as session:
while (
True
): # This is here so that URL signing errors are caught they are rare, but I did capture one
try:
async with session.post(url=self.endpoint, json=data, ssl=context) as response:
while (True):
try:
self.statistics.set_max_files_open()
# these lines produced an error saying that the content.read() had already closed
if response.status > 399:
text = await response.content.read()
response.raise_for_status()
resp = await response.json(content_type=None)
assert "accessUrl" in resp, resp
if resp["accessUrl"] is None:
account_command = "gcloud config get-value account"
cmd = account_command.split(" ")
account = subprocess.check_output(cmd).decode("ascii")
raise Exception(
f"A valid URL was not returned from the server. \
Please check the access for {account}\n{resp}"
)
url_ = resp["accessUrl"]["url"]
type = "none"
if "storage.googleapis.com" in url_:
type = "gs"
drs_object.access_methods = [
AccessMethod(access_url=url_, type=type)
]
return drs_object
except ClientResponseError as e:
tries += 1
if self.token.expired and self.token.expiry is not None:
self.token = await self._get_auth_token()
time.sleep((random.randint(0, 1000) / 1000) + 2**tries)
if tries > 2:
if verbose:
logger.error(f"value of text error {str(text)}")
logger.error(f"A file has failed the signing process, specifically {str(e)}")
if "401" in str(e):
drs_object.errors.append(f"RECOVERABLE in AIOHTTP {str(e)}")
return DrsObject(
self_uri="",
id="",
checksums=[],
size=0,
name=None,
errors=[f"error: {str(text)}"],
)
except ClientConnectorError as e:
tries += 1
if self.token.expired and self.token.expiry is not None:
self.token = await self._get_auth_token()
time.sleep((random.randint(0, 1000) / 1000) + 2**tries)
if tries > 2:
drs_object.errors.append(str(e))
if verbose:
logger.error(f"retry failed in sign_url function. Exiting with error status: {str(e)}")
return DrsObject(
self_uri="",
id="",
checksums=[],
size=0,
name=None,
errors=[f"error: {str(text)}"],
)
async def get_object(self, object_id: str, verbose: bool = False) -> DrsObject:
"""Sends a POST request for the signed URL, hash, and file size of a given DRS object.
Args:
object_id (str): DRS URI
Raises:
Exception: The request was rejected by the server
Returns:
DownloadURL: The downloadable bundle ready for async download
"""
if (self.token is None or (self.token.expired and self.token.expiry is not None)):
if verbose:
logger.info("fetching new token")
self.token = await self._get_auth_token()
if verbose:
logger.info(f"status of token expiration {self.token.expiry}")
data = {"url": object_id, "fields": ["fileName", "size", "hashes"]}
headers = {
"authorization": "Bearer " + self.token.token,
"content-type": "application/json",
}
tries = 0
context = ssl.create_default_context(cafile=certifi.where())
async with aiohttp.ClientSession(headers=headers) as session:
while True: # this is here for the somewhat more common Martha disconnects.
try:
async with session.post(url=self.endpoint, json=data, ssl=context) as response:
while True:
try:
self.statistics.set_max_files_open()
if response.status > 399:
text = await response.content.read()
response.raise_for_status()
resp = await response.json(content_type=None)
md5_ = resp["hashes"]["md5"]
size_ = resp["size"]
name_ = resp["fileName"]
return DrsObject(
self_uri=object_id,
size=size_,
checksums=[Checksum(checksum=md5_, type="md5")],
id=object_id,
name=name_,
)
except ClientResponseError as e:
tries += 1
if verbose:
logger.info(f"Client Response Error {str(text)}")
time.sleep((random.randint(0, 1000) / 1000) + 2**tries)
if tries > 2:
if verbose:
logger.error(f"retry failed in get_object function. \
Exiting with error status: {str(e)}")
return DrsObject(
self_uri=object_id,
id=object_id,
checksums=[],
size=0,
name=None,
errors=[str(e)],
)
except ClientConnectorError as e:
tries += 1
if verbose:
logger.info(f"ClientConnectorError: {str(e)} while fetching object information")
time.sleep((random.randint(0, 1000) / 1000) + 2**tries)
if tries > 2:
if verbose:
logger.error(f"value of text error {str(text)}")
logger.error(f"retry failed in get_object function. Exiting with error status: {str(e)}")
return DrsObject(
self_uri=object_id,
id=object_id,
checksums=[],
size=0,
name=None,
errors=[str(e)],
)
|