2022-11-18 10:27:57 +00:00
|
|
|
import re
|
2023-08-08 15:57:06 +00:00
|
|
|
from helpers.cmd import execute_cmd, log
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2024-02-05 15:56:36 +00:00
|
|
|
def create_container(endpoint, policy, wallet_path, config, acl, local=False, depth=0):
|
2023-12-13 14:52:18 +00:00
|
|
|
if depth > 20:
|
|
|
|
raise ValueError(f"unable to create container: too many unsuccessful attempts")
|
|
|
|
|
|
|
|
if wallet_path:
|
2024-02-05 15:56:36 +00:00
|
|
|
wallet_file = f"--wallet {wallet_path}"
|
2023-12-13 14:52:18 +00:00
|
|
|
if config:
|
2024-02-05 15:56:36 +00:00
|
|
|
wallet_config = f"--config {config}"
|
|
|
|
if acl:
|
|
|
|
acl = f"--basic-acl {acl}"
|
2023-08-23 12:00:03 +00:00
|
|
|
cmd_line = f"frostfs-cli --rpc-endpoint {endpoint} container create {wallet_file} {wallet_config} " \
|
2024-02-05 15:56:36 +00:00
|
|
|
f" --policy '{policy}' {acl} --await"
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
output, success = execute_cmd(cmd_line)
|
|
|
|
|
|
|
|
if not success:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Container has not been created\n"
|
|
|
|
f"{output}", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
return False
|
2023-06-28 17:21:43 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
fst_str = output.split('\n')[0]
|
|
|
|
except Exception:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Incorrect output\n"
|
|
|
|
f"Output: {output or '<empty>'}", endpoint)
|
2023-06-28 17:21:43 +00:00
|
|
|
return False
|
|
|
|
splitted = fst_str.split(": ")
|
|
|
|
if len(splitted) != 2:
|
2023-08-08 15:57:06 +00:00
|
|
|
raise ValueError(f"no CID was parsed from command output:\t{fst_str}")
|
2023-12-13 14:52:18 +00:00
|
|
|
cid = splitted[1]
|
|
|
|
|
|
|
|
log(f"Created container {cid}", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2023-12-13 14:52:18 +00:00
|
|
|
if not local:
|
|
|
|
return cid
|
|
|
|
|
|
|
|
cmd_line = f"frostfs-cli netmap nodeinfo --rpc-endpoint {endpoint} {wallet_file} {wallet_config}"
|
|
|
|
output, success = execute_cmd(cmd_line)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
2023-12-13 14:52:18 +00:00
|
|
|
if not success:
|
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Failed to get nodeinfo\n"
|
|
|
|
f"{output}", endpoint)
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
fst_str = output.split('\n')[0]
|
|
|
|
except Exception:
|
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Incorrect output\n"
|
|
|
|
f"Output: {output or '<empty>'}", endpoint)
|
|
|
|
return False
|
|
|
|
splitted = fst_str.split(": ")
|
|
|
|
if len(splitted) != 2 or len(splitted[1]) == 0:
|
|
|
|
raise ValueError(f"no node key was parsed from command output:\t{fst_str}")
|
|
|
|
|
|
|
|
node_key = splitted[1]
|
|
|
|
|
|
|
|
cmd_line = f"frostfs-cli container nodes --rpc-endpoint {endpoint} {wallet_file} {wallet_config} --cid {cid}"
|
|
|
|
output, success = execute_cmd(cmd_line)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Failed to get container nodes\n"
|
|
|
|
f"{output}", endpoint)
|
|
|
|
return False
|
|
|
|
|
|
|
|
for output_str in output.split('\n'):
|
|
|
|
output_str = output_str.lstrip().rstrip()
|
|
|
|
if not output_str.startswith("Node "):
|
|
|
|
continue
|
|
|
|
splitted = output_str.split(": ")
|
|
|
|
if len(splitted) != 2 or len(splitted[1]) == 0:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
k = splitted[1].split(" ")[0]
|
|
|
|
except Exception:
|
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Incorrect output\n"
|
|
|
|
f"Output: {output or '<empty>'}", endpoint)
|
|
|
|
continue
|
|
|
|
if k == node_key:
|
|
|
|
return cid
|
|
|
|
|
|
|
|
log(f"Created container {cid} is not stored on {endpoint}, creating another one...", endpoint)
|
2024-02-05 15:56:36 +00:00
|
|
|
return create_container(endpoint, policy, wallet_path, config, acl, local, depth + 1)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
|
2022-12-15 13:32:34 +00:00
|
|
|
def upload_object(container, payload_filepath, endpoint, wallet_file, wallet_config):
|
2022-11-17 17:08:20 +00:00
|
|
|
object_name = ""
|
2023-08-23 12:00:03 +00:00
|
|
|
if wallet_file:
|
|
|
|
wallet_file = "--wallet " + wallet_file
|
|
|
|
if wallet_config:
|
|
|
|
wallet_config = "--config " + wallet_config
|
|
|
|
cmd_line = f"frostfs-cli --rpc-endpoint {endpoint} object put --file {payload_filepath} {wallet_file} {wallet_config} " \
|
2022-11-17 17:08:20 +00:00
|
|
|
f"--cid {container} --no-progress"
|
2022-11-18 10:27:57 +00:00
|
|
|
output, success = execute_cmd(cmd_line)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
if not success:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Object {object_name} has not been uploaded\n"
|
|
|
|
f"Error: {output}", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
return False
|
2023-06-28 17:21:43 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
# taking second string from command output
|
|
|
|
snd_str = output.split('\n')[1]
|
|
|
|
except Exception:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Incorrect output\n"
|
|
|
|
f"Output: {output or '<empty>'}", endpoint)
|
2023-06-28 17:21:43 +00:00
|
|
|
return False
|
|
|
|
splitted = snd_str.split(": ")
|
|
|
|
if len(splitted) != 2:
|
|
|
|
raise Exception(f"no OID was parsed from command output: \t{snd_str}")
|
|
|
|
return container, endpoint, splitted[1]
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
|
2022-12-15 13:32:34 +00:00
|
|
|
def get_object(cid, oid, endpoint, out_filepath, wallet_file, wallet_config):
|
2023-08-23 12:00:03 +00:00
|
|
|
if wallet_file:
|
|
|
|
wallet_file = "--wallet " + wallet_file
|
|
|
|
if wallet_config:
|
|
|
|
wallet_config = "--config " + wallet_config
|
|
|
|
cmd_line = f"frostfs-cli object get -r {endpoint} --cid {cid} --oid {oid} {wallet_file} {wallet_config} " \
|
2022-11-17 17:08:20 +00:00
|
|
|
f"--file {out_filepath}"
|
|
|
|
|
2022-11-18 10:27:57 +00:00
|
|
|
output, success = execute_cmd(cmd_line)
|
2022-11-17 17:08:20 +00:00
|
|
|
|
|
|
|
if not success:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Failed to get object {oid} from container {cid}\n"
|
|
|
|
f"Error: {output}", endpoint)
|
2022-11-17 17:08:20 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2022-11-18 10:27:57 +00:00
|
|
|
|
|
|
|
|
2022-12-15 13:32:34 +00:00
|
|
|
def search_object_by_id(cid, oid, endpoint, wallet_file, wallet_config, ttl=2):
|
2023-08-23 12:00:03 +00:00
|
|
|
if wallet_file:
|
|
|
|
wallet_file = "--wallet " + wallet_file
|
|
|
|
if wallet_config:
|
|
|
|
wallet_config = "--config " + wallet_config
|
|
|
|
cmd_line = f"frostfs-cli object search --ttl {ttl} -r {endpoint} --cid {cid} --oid {oid} {wallet_file} {wallet_config} "
|
2022-11-18 10:27:57 +00:00
|
|
|
|
|
|
|
output, success = execute_cmd(cmd_line)
|
|
|
|
|
|
|
|
if not success:
|
2023-08-08 15:57:06 +00:00
|
|
|
log(f"{cmd_line}\n"
|
|
|
|
f"Failed to search object {oid} for container {cid}\n"
|
|
|
|
f"Error: {output}", endpoint)
|
2022-11-18 10:27:57 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
re_rst = re.search(r'Found (\d+) objects', output)
|
|
|
|
|
|
|
|
if not re_rst:
|
2023-08-08 15:57:06 +00:00
|
|
|
raise Exception("Failed to parse search results")
|
2022-11-18 10:27:57 +00:00
|
|
|
|
|
|
|
return re_rst.group(1)
|