Merge in RR/mini_queue from add-storage-handle to master
Squashed commit of the following:
commit 03e542d2a65802c28735873fae184209f0c83553
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 11:55:34 2022 +0100
Quickfix typo
commit b4d538e9445187435d87c5cf8ce1f4e448021129
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 11:41:42 2022 +0100
added prefetch count and make channel function
commit d46d1375e387d36641c06b062a8ccc54f114ef4c
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 11:20:39 2022 +0100
black on M.s request
commit bc47b20312a978f19b08531804bf42b00f0a88f0
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 11:19:57 2022 +0100
changed response
commit 9a475ecd8df9ca007e5f7fe146483b6403eccc3b
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 10:15:08 2022 +0100
.
commit 108bc3ea90d867575db8c1b1503c9df859222485
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 09:56:56 2022 +0100
quickrestore
commit ae04d17d8d041f612d86117e8e96c96ddffcbde3
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 09:37:30 2022 +0100
refactor
commit 68051a72eb93868eba8adba234258b9e5373ecaa
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 08:50:59 2022 +0100
added answer file template for rancher
commit 09ef45ead51c07732a20133acad0b8b2ae7d0a61
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 08:26:05 2022 +0100
Quickfix inconsistency
commit d925b0f3f91f29403c88fb6149566ec966af2973
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Wed Feb 16 08:20:40 2022 +0100
Quick refactor
commit 48795455cde8d97ed98e58c3004a87a26f331352
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Tue Feb 15 17:46:45 2022 +0100
bluckckck
commit 80e58efab0269dc513990f83b14ceb36b3e4dd8e
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Tue Feb 15 17:45:49 2022 +0100
Quick restatus setting
commit 83f276ee13348a678b7da84e25ca844dd348b4c9
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Tue Feb 15 17:30:16 2022 +0100
Quickreset to working status
commit d44cdcf922250639a6832cc3e16d0d967d9853fb
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Tue Feb 15 14:44:26 2022 +0100
added storage handle for minio WIP
106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
"""Implements a wrapper around a MinIO client that provides operations on the MinIO store required by the service."""
|
|
import os
|
|
from typing import Iterable
|
|
|
|
from minio import Minio
|
|
|
|
from mini_queue.utils.config import CONFIG
|
|
from mini_queue.utils.storage import StorageHandle
|
|
|
|
|
|
def get_minio_client(access_key=None, secret_key=None) -> Minio:
|
|
"""Instantiates a minio.Minio client.
|
|
|
|
Args:
|
|
access_key: Access key for MinIO client (username).
|
|
secret_key: Secret key for MinIO client (password).
|
|
|
|
Returns:
|
|
A minio.Minio client.
|
|
"""
|
|
access_key = CONFIG.minio.user if access_key is None else access_key
|
|
secret_key = CONFIG.minio.password if secret_key is None else secret_key
|
|
|
|
# TODO: secure=True/False?
|
|
return Minio(f"{CONFIG.minio.host}:{CONFIG.minio.port}", access_key=access_key, secret_key=secret_key, secure=False)
|
|
|
|
|
|
class MinioHandle(StorageHandle):
|
|
"""Wrapper around a MinIO client that provides operations on the MinIO store required by the service."""
|
|
|
|
def __init__(self):
|
|
"""Initializes a MinioHandle"""
|
|
super().__init__()
|
|
self.client: Minio = get_minio_client()
|
|
self.default_container_name = CONFIG.minio.bucket
|
|
self.backend = "s3"
|
|
|
|
def _StorageHandle__provide_container(self, container_name):
|
|
if not self.client.bucket_exists(container_name):
|
|
self.client.make_bucket(container_name)
|
|
|
|
def _StorageHandle__add_file(self, path, storage_path, container_name=None):
|
|
|
|
if container_name is None:
|
|
container_name = self.default_container_name
|
|
|
|
self._StorageHandle__provide_container(container_name)
|
|
|
|
with open(path, "rb") as f:
|
|
stat = os.stat(path)
|
|
self.client.put_object(container_name, storage_path, f, stat.st_size)
|
|
|
|
def list_files(self, container_name=None) -> Iterable[str]:
|
|
"""List all files in a container.
|
|
|
|
Args:
|
|
container_name: container to list files from.
|
|
|
|
Returns:
|
|
Iterable of filenames.
|
|
"""
|
|
return self._list_files("object_name", container_name=container_name)
|
|
|
|
def get_objects(self, container_name=None):
|
|
"""Gets all objects in a container.
|
|
|
|
Args:
|
|
container_name: container to get objects from.
|
|
|
|
Returns:
|
|
Iterable over all objects in the container.
|
|
"""
|
|
if container_name is None:
|
|
container_name = self.default_container_name
|
|
yield from self.client.list_objects(container_name, recursive=True)
|
|
|
|
def _StorageHandle__list_containers(self):
|
|
return self.client.list_buckets()
|
|
|
|
def _StorageHandle__purge(self) -> None:
|
|
"""Deletes all files and containers in the store."""
|
|
for container, obj in self.get_all_objects():
|
|
self.client.remove_object(container.name, obj.object_name)
|
|
|
|
for container in self.client.list_buckets():
|
|
self.client.remove_bucket(container.name)
|
|
|
|
def _StorageHandle__fget_object(self, container_name, object_name, target_path):
|
|
self.client.fget_object(container_name, object_name, target_path)
|
|
|
|
def _StorageHandle__remove_file(self, folder: str, filename: str, container_name: str = None) -> None:
|
|
"""Removes a file from the store.
|
|
|
|
Args:
|
|
folder: Folder containing file.
|
|
filename: Name of file (without folder) to remove.
|
|
container_name: container containing file.
|
|
"""
|
|
if container_name is None:
|
|
container_name = self.default_container_name
|
|
|
|
path = os.path.join(folder, filename)
|
|
|
|
if self.client.bucket_exists(container_name):
|
|
self.client.remove_object(container_name, path)
|