blob: dfd0e8c39b261d5d287fce79a6cad550a117e108 [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001import os
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +03002import sys
koder aka kdanilov22d134e2016-11-08 11:33:19 +02003import uuid
koder aka kdanilove21d7472015-02-14 19:02:04 -08004import logging
koder aka kdanilovf2865172016-12-30 03:35:11 +02005import datetime
koder aka kdanilov7acd6bd2015-02-12 14:28:30 -08006import contextlib
koder aka kdanilovf90de852017-01-20 18:12:27 +02007
kdanylov aka koderb0833332017-05-13 20:39:17 +03008from typing import Any, Tuple, Iterator, Iterable
koder aka kdanilovbb5fe072015-05-21 02:50:23 +03009
koder aka kdanilov22d134e2016-11-08 11:33:19 +020010try:
11 from petname import Generate as pet_generate
12except ImportError:
kdanylov aka koderb0833332017-05-13 20:39:17 +030013 def pet_generate(_1: str, _2: str) -> str:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020014 return str(uuid.uuid4())
15
kdanylov aka koderb0833332017-05-13 20:39:17 +030016from cephlib.common import run_locally, sec_to_str
kdanylov aka koder3a9e5db2017-05-09 20:00:44 +030017
18
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030019logger = logging.getLogger("wally")
koder aka kdanilov209e85d2015-04-27 23:11:05 +030020
21
koder aka kdanilova732a602017-02-01 20:29:56 +020022STORAGE_ROLES = {'ceph-osd'}
23
24
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030025class StopTestError(RuntimeError):
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030026 pass
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030027
28
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030029class LogError:
koder aka kdanilov22d134e2016-11-08 11:33:19 +020030 def __init__(self, message: str, exc_logger: logging.Logger = None) -> None:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030031 self.message = message
32 self.exc_logger = exc_logger
33
koder aka kdanilov22d134e2016-11-08 11:33:19 +020034 def __enter__(self) -> 'LogError':
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030035 return self
36
koder aka kdanilov22d134e2016-11-08 11:33:19 +020037 def __exit__(self, tp: type, value: Exception, traceback: Any) -> bool:
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030038 if value is None or isinstance(value, StopTestError):
koder aka kdanilov22d134e2016-11-08 11:33:19 +020039 return False
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030040
41 if self.exc_logger is None:
42 exc_logger = sys._getframe(1).f_globals.get('logger', logger)
43 else:
44 exc_logger = self.exc_logger
45
46 exc_logger.exception(self.message, exc_info=(tp, value, traceback))
koder aka kdanilov22d134e2016-11-08 11:33:19 +020047 raise StopTestError(self.message) from value
koder aka kdanilov4af1c1d2015-05-18 15:48:58 +030048
49
koder aka kdanilov22d134e2016-11-08 11:33:19 +020050class TaskFinished(Exception):
koder aka kdanilov2c473092015-03-29 17:12:13 +030051 pass
koder aka kdanilov4643fd62015-02-10 16:20:13 -080052
koder aka kdanilov2c473092015-03-29 17:12:13 +030053
koder aka kdanilovffaf48d2016-12-27 02:25:29 +020054def log_block(message: str, exc_logger:logging.Logger = None) -> LogError:
55 logger.debug("Starts : " + message)
56 return LogError(message, exc_logger)
57
58
59def check_input_param(is_ok: bool, message: str) -> None:
60 if not is_ok:
61 logger.error(message)
62 raise StopTestError(message)
63
64
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030065def yamable(data: Any) -> Any:
koder aka kdanilov168f6092015-04-19 02:33:38 +030066 if isinstance(data, (tuple, list)):
67 return map(yamable, data)
68
koder aka kdanilov168f6092015-04-19 02:33:38 +030069 if isinstance(data, dict):
70 res = {}
71 for k, v in data.items():
72 res[yamable(k)] = yamable(v)
73 return res
74
75 return data
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030076
77
koder aka kdanilov22d134e2016-11-08 11:33:19 +020078def get_creds_openrc(path: str) -> Tuple[str, str, str, str, bool]:
koder aka kdanilov89fb6102015-06-13 02:58:08 +030079 fc = open(path).read()
80
koder aka kdanilovb7197432015-07-15 00:40:43 +030081 echo = 'echo "$OS_INSECURE:$OS_TENANT_NAME:$OS_USERNAME:$OS_PASSWORD@$OS_AUTH_URL"'
koder aka kdanilov89fb6102015-06-13 02:58:08 +030082
83 msg = "Failed to get creads from openrc file"
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030084 with LogError(msg):
kdanylov aka koderb0833332017-05-13 20:39:17 +030085 data = run_locally(['/bin/bash'], input_data=(fc + "\n" + echo).encode('utf8')).decode("utf8")
koder aka kdanilov89fb6102015-06-13 02:58:08 +030086
87 msg = "Failed to get creads from openrc file: " + data
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030088 with LogError(msg):
koder aka kdanilov89fb6102015-06-13 02:58:08 +030089 data = data.strip()
koder aka kdanilovb7197432015-07-15 00:40:43 +030090 insecure_str, user, tenant, passwd_auth_url = data.split(':', 3)
91 insecure = (insecure_str in ('1', 'True', 'true'))
koder aka kdanilov89fb6102015-06-13 02:58:08 +030092 passwd, auth_url = passwd_auth_url.rsplit("@", 1)
93 assert (auth_url.startswith("https://") or
94 auth_url.startswith("http://"))
95
koder aka kdanilovb7197432015-07-15 00:40:43 +030096 return user, passwd, tenant, auth_url, insecure
koder aka kdanilov89fb6102015-06-13 02:58:08 +030097
98
koder aka kdanilov108ac362017-01-19 20:17:16 +020099@contextlib.contextmanager
100def empty_ctx(val: Any = None) -> Iterator[Any]:
101 yield val
102
103
koder aka kdanilov22d134e2016-11-08 11:33:19 +0200104def get_uniq_path_uuid(path: str, max_iter: int = 10) -> Tuple[str, str]:
105 for i in range(max_iter):
106 run_uuid = pet_generate(2, "_")
107 results_dir = os.path.join(path, run_uuid)
108 if not os.path.exists(results_dir):
109 break
110 else:
111 run_uuid = str(uuid.uuid4())
112 results_dir = os.path.join(path, run_uuid)
113
114 return results_dir, run_uuid
115
116
koder aka kdanilovf2865172016-12-30 03:35:11 +0200117def get_time_interval_printable_info(seconds: int) -> Tuple[str, str]:
118 exec_time_s = sec_to_str(seconds)
119 now_dt = datetime.datetime.now()
120 end_dt = now_dt + datetime.timedelta(0, seconds)
121 return exec_time_s, "{:%H:%M:%S}".format(end_dt)
koder aka kdanilov108ac362017-01-19 20:17:16 +0200122
123
koder aka kdanilova732a602017-02-01 20:29:56 +0200124def shape2str(shape: Iterable[int]) -> str:
125 return "*".join(map(str, shape))
126
127
128def str2shape(shape: str) -> Tuple[int, ...]:
129 return tuple(map(int, shape.split('*')))