2022-06-23 02:20:56 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2022-10-25 14:43:20 +00:00
|
|
|
import argparse
|
2022-06-23 02:20:56 +00:00
|
|
|
import json
|
2022-12-30 10:47:28 +00:00
|
|
|
import sys
|
2023-05-03 11:33:16 +00:00
|
|
|
import tempfile
|
2022-06-23 02:20:56 +00:00
|
|
|
from concurrent.futures import ProcessPoolExecutor
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
from helpers.cmd import random_payload
|
|
|
|
from helpers.aws_cli import create_bucket, upload_object
|
2022-06-23 02:20:56 +00:00
|
|
|
|
2022-10-25 14:43:20 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
parser.add_argument('--size', help='Upload objects size in kb.')
|
|
|
|
parser.add_argument('--buckets', help='Number of buckets to create.')
|
|
|
|
parser.add_argument('--out', help='JSON file with output.')
|
|
|
|
parser.add_argument('--preload_obj', help='Number of pre-loaded objects.')
|
|
|
|
parser.add_argument('--endpoint', help='S3 Gateway address.')
|
2022-11-17 17:08:20 +00:00
|
|
|
parser.add_argument('--update', help='True/False, False by default. Save existed buckets from target file (--out). '
|
|
|
|
'New buckets will not be created.')
|
|
|
|
parser.add_argument('--location', help='AWS location. Will be empty, if has not be declared.', default="")
|
2022-06-23 02:20:56 +00:00
|
|
|
parser.add_argument('--versioning', help='True/False, False by default.')
|
2022-12-30 10:47:28 +00:00
|
|
|
parser.add_argument('--ignore-errors', help='Ignore preset errors')
|
|
|
|
parser.add_argument('--workers', help='Count of workers in preset. Max = 50, Default = 50', default=50)
|
2022-06-23 02:20:56 +00:00
|
|
|
|
2022-10-25 14:43:20 +00:00
|
|
|
args = parser.parse_args()
|
2022-06-23 02:20:56 +00:00
|
|
|
print(args)
|
|
|
|
|
2022-12-30 10:47:28 +00:00
|
|
|
ERROR_NO_BUCKETS = 1
|
|
|
|
ERROR_NO_OBJECTS = 2
|
|
|
|
MAX_WORKERS = 50
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
bucket_list = []
|
2022-12-30 10:47:28 +00:00
|
|
|
objects_list = []
|
|
|
|
ignore_errors = True if args.ignore_errors else False
|
2022-06-23 02:20:56 +00:00
|
|
|
|
2022-12-30 10:47:28 +00:00
|
|
|
workers = int(args.workers)
|
2022-06-23 02:20:56 +00:00
|
|
|
if args.update:
|
|
|
|
# Open file
|
|
|
|
with open(args.out) as f:
|
|
|
|
data_json = json.load(f)
|
|
|
|
bucket_list = data_json['buckets']
|
|
|
|
# Get CID list
|
|
|
|
else:
|
|
|
|
print(f"Create buckets: {args.buckets}")
|
2022-10-25 14:43:20 +00:00
|
|
|
|
2022-12-30 10:47:28 +00:00
|
|
|
with ProcessPoolExecutor(max_workers=min(MAX_WORKERS, workers)) as executor:
|
2022-11-17 17:08:20 +00:00
|
|
|
buckets_runs = {executor.submit(create_bucket, args.endpoint, args.versioning,
|
|
|
|
args.location): _ for _ in range(int(args.buckets))}
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
for run in buckets_runs:
|
|
|
|
if run.result() is not None:
|
2022-10-25 14:43:20 +00:00
|
|
|
bucket_list.append(run.result())
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
print("Create buckets: Completed")
|
|
|
|
|
|
|
|
print(f" > Buckets: {bucket_list}")
|
2022-12-30 10:47:28 +00:00
|
|
|
if not bucket_list:
|
|
|
|
print("No buckets to work with")
|
|
|
|
if not ignore_errors:
|
|
|
|
sys.exit(ERROR_NO_BUCKETS)
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
print(f"Upload objects to each bucket: {args.preload_obj} ")
|
2023-05-03 11:33:16 +00:00
|
|
|
payload_file = tempfile.NamedTemporaryFile()
|
|
|
|
random_payload(payload_file, args.size)
|
2022-06-23 02:20:56 +00:00
|
|
|
print(" > Create random payload: Completed")
|
|
|
|
|
|
|
|
for bucket in bucket_list:
|
|
|
|
print(f" > Upload objects for bucket {bucket}")
|
2022-12-30 10:47:28 +00:00
|
|
|
with ProcessPoolExecutor(max_workers=min(MAX_WORKERS, workers)) as executor:
|
2023-05-03 11:33:16 +00:00
|
|
|
objects_runs = {executor.submit(upload_object, bucket, payload_file.name,
|
2022-11-17 17:08:20 +00:00
|
|
|
args.endpoint): _ for _ in range(int(args.preload_obj))}
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
for run in objects_runs:
|
|
|
|
if run.result() is not None:
|
2022-12-30 10:47:28 +00:00
|
|
|
objects_list.append({'bucket': bucket, 'object': run.result()})
|
2022-06-23 02:20:56 +00:00
|
|
|
print(f" > Upload objects for bucket {bucket}: Completed")
|
|
|
|
|
|
|
|
print("Upload objects to each bucket: Completed")
|
|
|
|
|
2022-12-30 10:47:28 +00:00
|
|
|
if int(args.preload_obj) > 0 and not objects_list:
|
|
|
|
print("No objects were uploaded")
|
|
|
|
if not ignore_errors:
|
|
|
|
sys.exit(ERROR_NO_OBJECTS)
|
|
|
|
|
|
|
|
data = {'buckets': bucket_list, 'objects': objects_list, 'obj_size': args.size + " Kb"}
|
2022-06-23 02:20:56 +00:00
|
|
|
|
2022-10-25 15:01:08 +00:00
|
|
|
with open(args.out, 'w+') as f:
|
2022-10-25 15:06:43 +00:00
|
|
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
2022-06-23 02:20:56 +00:00
|
|
|
|
2022-12-30 10:47:28 +00:00
|
|
|
print("Result:")
|
2022-06-23 02:20:56 +00:00
|
|
|
print(f" > Total Buckets has been created: {len(bucket_list)}.")
|
2022-12-30 10:47:28 +00:00
|
|
|
print(f" > Total Objects has been created: {len(objects_list)}.")
|
2022-06-23 02:20:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|