manage minio script refactor

This commit is contained in:
Julius Unverfehrt 2022-02-24 17:12:44 +01:00
parent 0114977075
commit 880e22832c

View File

@ -1,9 +1,13 @@
import argparse
import gzip
import os
from pathlib import Path
from tqdm import tqdm
from pyinfra.config import CONFIG
from pyinfra.storage.minio import MinioHandle
from pyinfra.storage.storages import get_s3_storage
from pyinfra.test.unit_tests.conftest import bucket_name
def parse_args():
@ -17,35 +21,37 @@ def parse_args():
add_group.add_argument("--file", "-f")
add_group.add_argument("--directory", "-d")
parser_remove = subparsers.add_parser("remove", help="Remove a file from the MinIO store")
parser_remove.add_argument("dossier_id")
parser_remove.add_argument("file_path")
subparsers.add_parser("purge", help="Delete all files and buckets in the MinIO store")
args = parser.parse_args()
return args
def add_file_compressed(storage, bucket_name, dossier_id, path) -> None:
path_gz = str(Path(dossier_id) / (Path(path).name + ".gz"))
with open(path, "rb") as f:
data = gzip.compress(f.read())
storage.put(bucket_name, path_gz, data)
if __name__ == "__main__":
client = MinioHandle()
storage = get_s3_storage()
bucket_name = CONFIG.test.bucket
args = parse_args()
if args.command == "add":
if args.file:
client.add_file_compressed(args.file, folder=args.dossier_id)
add_file_compressed(storage, bucket_name, args.dossier_id, args.file)
elif args.directory:
for fname in tqdm([*os.listdir(args.directory)], desc="Adding files"):
path = os.path.join(args.directory, fname)
client.add_file_compressed(path, folder=args.dossier_id)
elif args.command == "remove":
fname = os.path.basename(args.file_path)
client.remove_file(folder=args.dossier_id, filename=fname)
path = Path(args.directory) / fname
add_file_compressed(storage, bucket_name, args.dossier_id, path)
elif args.command == "purge":
client.purge()
storage.clear_bucket(bucket_name)