rs_common/prefect_utils.md
Utility Python module for the tutorials, to be shared with the prefect or dask workers.
WARNING: AFTER EACH MODIFICATION, RESTART THE JUPYTER NOTEBOOK KERNEL !
format_env_user(block_name, any_owner_id)
Format the Prefect secret block for the current user
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
95 96 97 98 | |
get_ip_address()
Return IP address, see: https://stackoverflow.com/a/166520
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
90 91 92 | |
get_s3_bucket(s3_path)
Return a prefect S3 bucket object and S3 "object name" (= S3 path without s3://bucket-name) from the given S3 path. We use the prefect higher-level functions instead of those from boto3.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
get_share_bucket(sub_folder='')
async
Get the prefect share bucket folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sub_folder
|
str
|
subfolder to use in the bucket. If empty, use the default folder and secret block. |
''
|
Else use a specific secret block. WARNING
|
this only works in a single-threaded environment |
required |
Returns:
| Type | Description |
|---|---|
tuple[S3Bucket, str]
|
The prefect block for the share bucket and the block name. |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
hack_for_jupyter(func, *args, **kwargs)
From Jupyter we need this hack to deploy prefect flows
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
331 332 333 334 | |
init_global_env(new_owner_id=None)
Init the global variables above from the env vars. Needs to be called from the client, then called again from the prefect workers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_owner_id
|
str | None
|
new ower/user id, if known. |
None
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
init_prefect_blocks()
async
Init prefect blocks from the client environment (= from jupyter)
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
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 | |
read_apikey(optional=False, save_to_env=True)
async
Read the API key, either from the environment variable or from an interactive input form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
optional
|
bool
|
If False and if the env var is missing, ask it from an interactive input form. |
False
|
save_to_env
|
bool
|
If True, saves the API key to the ~/.env file. |
True
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
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 | |
read_prefect_blocks(any_owner_id=None)
async
Read prefect blocks from the prefect flow and tasks into env vars and global vars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
owner_id
|
Read prefect blocks for a specific user. |
required |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
remove_bucket_credentials(obs_ids)
async
Remove bucket credentials from the prefect secret block that contains the env variables for the current user/owner_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_ids
|
list[str] | str
|
" |
required |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
s3_delete(s3_prefix, log=False)
Remove all files from S3 bucket with the given prefix, using low-level client.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |
s3_download_dir(s3_path, local_path=None)
async
See: S3Bucket.get_directory
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
506 507 508 509 510 511 512 513 | |
s3_download_file(s3_path, to_path, **download_kwargs)
async
See: S3Bucket.download_object_to_path
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
495 496 497 498 499 500 501 502 503 | |
s3_upload_dir(from_folder, s3_path, **upload_kwargs)
async
See: S3Bucket.upload_from_folder
Uploads files within a folder (excluding the folder itself) to the object storage service folder.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
480 481 482 483 484 485 486 487 488 489 490 491 492 | |
s3_upload_empty_file(s3_path, **upload_kwargs)
async
Upload an empty temp file to the S3 bucket.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
s3_upload_file(from_path, s3_path, **upload_kwargs)
async
See: S3Bucket.upload_from_path
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
451 452 453 454 455 456 457 458 459 | |
save_bucket_credentials(osam_client=None, obs_credentials=None)
async
Save bucket credentials in the prefect secret block that contains the env variables for the current user/owner_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
osam_client
|
OsamClient | None
|
Used to read the user's credentials from the OSAM service. They are saved in the env vars: |
None
|
obs_credentials
|
dict[str, BucketCredentials] | None
|
Used to save custom bucket credentials. For each dict key " |
None
|
saved in the env vars
|
S3_ |
required |
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
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 | |
update_prefect_block(block_name, add_values=None, remove_keys=None)
async
Write a prefect secret block with values, or update it if it already exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_name
|
str
|
Prefect secret block name |
required |
add_values
|
dict | None
|
Key/values to add from the block |
None
|
remove_keys
|
list | None
|
Key/values to remove from the block |
None
|
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
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 | |
wait_for_deployment(name, wait=1, max_retry=30)
async
Wait for prefect deployment to be finished.
Source code in docs/rs-client-libraries/rs_common/prefect_utils.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |