-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
292 lines (237 loc) · 9.5 KB
/
helpers.py
File metadata and controls
292 lines (237 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import getpass
import inspect
import logging
import os
import sys
from datetime import datetime
from pathlib import Path
from typing import Tuple, Any
import coloredlogs
import re
import argparse
import yaml
LOGGER = logging.getLogger()
class Config:
_config: dict[str, str | dict[str, str] | None] = {
"admin_domain_password": "yolobanana",
"admin_vm_password": "yolobanana",
"admin_vm_ssh_key": "",
"admin_vm_ssh_keypair_name": "my_ssh_public_key",
"project_ipv4_subnet": "192.168.200.0/24",
"public_network": "public",
"network_mtu": "0",
"number_of_floating_ips_per_project": "1",
"vm_flavor": "SCS-1L-1",
"vm_image": "Ubuntu 24.04",
"vm_volume_size_gb": "10",
"verify_ssl_certificate": "false",
"cloud_init_extra_script": """#!/bin/bash\necho "HELLO WORLD"; date > READY; whoami >> READY""",
"wait_for_server_timeout": "300",
}
_file: str | None = None
@staticmethod
def get(key: str, regex: str = ".+", multi_line: bool = False) -> str:
if key not in Config._config:
LOGGER.error(f"{key} not in config")
sys.exit(1)
values: list[str] = []
if multi_line:
values = str(Config._config[key]).splitlines()
else:
values.append(str(Config._config[key]))
for value in values:
matcher = re.compile(regex, re.MULTILINE | re.DOTALL)
if not matcher.fullmatch(value):
LOGGER.error(
f"{key} : >>>{value}<<< : does not match to regex >>>{regex}<<<"
)
sys.exit(1)
if len(values) > 1:
return "\n".join(values)
else:
return values[0]
@staticmethod
def load_config(config_file: str):
profile_path = str(
os.path.realpath(os.path.dirname(os.path.realpath(__file__)))
+ "/../../../profiles/"
)
potential_profile_file = str(
Path(os.getenv("OPENSTACK_WORKLOAD_MANAGER_PROFILES", profile_path))
/ Path(config_file)
)
if os.getenv("OPENSTACK_WORKLOAD_MANAGER_PROFILES", None):
potential_profile_file = str(
Path(
os.getenv("OPENSTACK_WORKLOAD_MANAGER_PROFILES", "NONE")
) # satisfy type-check
/ Path(config_file)
)
LOGGER.info(
"Environment variable OPENSTACK_WORKLOAD_MANAGER_PROFILES set,"
f" searching for potential {potential_profile_file}"
)
if os.path.exists(config_file):
Config._file = config_file
elif not str(config_file).startswith("/") and os.path.exists(
potential_profile_file
):
Config._file = potential_profile_file
else:
LOGGER.error(
f"Cannot find a profile at {config_file} or {potential_profile_file}"
)
sys.exit(1)
Config._file = os.path.realpath(Config._file)
try:
LOGGER.info(f"Reading {Config._file}")
with open(str(Config._file), "r") as file_fd:
Config._config.update(yaml.safe_load(file_fd))
except Exception as e:
LOGGER.error(f"Unable to read configuration: {e}")
sys.exit(1)
@staticmethod
def check_config(cls):
methods = inspect.getmembers(cls, predicate=inspect.isfunction)
for name, method in methods:
if name.startswith("get_"): # Check for "get_" prefix
method()
for quota_type in ["compute_quotas", "block_storage_quotas", "network_quotas"]:
if quota_type not in Config._config:
continue
for key_name in Config._config[quota_type].keys():
Config.quota(key_name, quota_type, 0)
@staticmethod
def show_effective_config():
Config.check_config(Config)
LOGGER.info(
"The effective configuration from %s : \n>>>\n---\n%s\n<<<"
% (
Config._file,
yaml.dump(Config._config, default_flow_style=False, width=10000),
)
)
@staticmethod
def get_public_network() -> str:
return Config.get("public_network", "[a-zA-Z][a-zA-Z0-9]*")
@staticmethod
def get_number_of_floating_ips_per_project() -> int:
return int(Config.get("number_of_floating_ips_per_project", r"[1-9]\d*"))
@staticmethod
def get_admin_vm_password() -> str:
if Config.get("admin_vm_password").upper() == "ASK_PASSWORD":
Config._config["admin_vm_password"] = getpass.getpass("Enter the wanted admin_vm_password: ")
return Config.get("admin_vm_password", regex=r".{5,}")
@staticmethod
def get_vm_flavor() -> str:
return Config.get("vm_flavor")
@staticmethod
def get_cloud_init_extra_script() -> str:
return Config.get("cloud_init_extra_script", multi_line=True)
@staticmethod
def get_vm_image() -> str:
return Config.get("vm_image")
@staticmethod
def get_vm_volume_size_gb() -> int:
return int(Config.get("vm_volume_size_gb", regex=r"\d+"))
@staticmethod
def get_admin_vm_ssh_keypair_name() -> str:
return Config.get("admin_vm_ssh_keypair_name")
@staticmethod
def get_wait_for_server_timeout() -> int:
return int(Config.get("wait_for_server_timeout", regex=r"\d+"))
@staticmethod
def get_project_ipv4_subnet() -> str:
return Config.get("project_ipv4_subnet", regex=r"\d+\.\d+\.\d+\.\d+/\d\d")
@staticmethod
def get_admin_vm_ssh_key() -> str:
return Config.get("admin_vm_ssh_key", r"ssh-\S+\s\S+(\s\S+)+", multi_line=True)
@staticmethod
def get_admin_domain_password() -> str:
if Config.get("admin_domain_password").upper() == "ASK_PASSWORD":
Config._config["admin_domain_password"] = getpass.getpass("Enter the wanted admin_domain_password: ")
return Config.get("admin_domain_password", regex=r".{5,}")
@staticmethod
def get_verify_ssl_certificate() -> bool:
value = Config.get("verify_ssl_certificate", regex=r"true|false|True|False")
if value.lower() == "false":
return False
else:
return True
@staticmethod
def configured_quota_names(quota_category: str) -> list[str]:
if quota_category in Config._config:
value = Config._config[quota_category]
if isinstance(value, dict):
return list(value.keys())
return []
@staticmethod
def quota(quota_name: str, quota_category: str, default_value: int) -> int:
if quota_category in Config._config:
value = Config._config[quota_category].get( # type: ignore
quota_name, default_value
)
if isinstance(value, int):
return value
else:
LOGGER.error(
f"Quota {quota_category} -> {quota_name} is not an integer"
)
sys.exit(1)
else:
return default_value
@staticmethod
def get_network_mtu():
return int(Config.get("network_mtu", regex=r"\d+"))
class DomainCache:
_domains: dict[str, str] = dict()
@staticmethod
def ident_by_id(domain_id: str) -> str:
if domain_id not in DomainCache._domains:
raise RuntimeError(f"There is no domain with id {domain_id}")
return f"domain '{DomainCache._domains[domain_id]}/{domain_id}'"
@staticmethod
def add(domain_id: str, name: str):
DomainCache._domains[domain_id] = name
class ProjectCache:
PROJECT_CACHE: dict[str, dict[str, str]] = dict()
@staticmethod
def ident_by_id(project_id: str) -> str:
if project_id not in ProjectCache.PROJECT_CACHE:
raise RuntimeError(f"There is no project with id {project_id}")
project = f'{ProjectCache.PROJECT_CACHE[project_id]["name"]}/{project_id}'
domain = DomainCache.ident_by_id(
ProjectCache.PROJECT_CACHE[project_id]["domain_id"]
)
return f"project '{project}' in {domain}"
@staticmethod
def add(project_id: str, data: dict[str, str]):
ProjectCache.PROJECT_CACHE[project_id] = data
def setup_logging(log_level: str) -> Tuple[logging.Logger, str]:
log_format_string = (
"%(asctime)-10s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s"
)
logger = logging.getLogger()
log_file = "STDOUT"
logging.basicConfig(format=log_format_string, level=log_level.upper())
coloredlogs.DEFAULT_FIELD_STYLES["levelname"] = {"bold": True, "color": ""}
coloredlogs.install(fmt=log_format_string, level=log_level.upper())
return logger, log_file
def cloud_checker(value: str) -> str:
if not re.fullmatch("[a-zA-Z0-9-]+", value):
raise argparse.ArgumentTypeError("specify a value for os_cloud")
return value
def item_checker(value: str) -> str:
if not re.fullmatch(r"[a-zA-Z0-9]+[a-zA-Z0-9\-]*[a-zA-Z0-9]+", value):
raise argparse.ArgumentTypeError("specify a valid name for an item")
return value
def iso_timestamp() -> str:
return datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
def deep_merge_dict(d1: dict, d2: dict) -> dict[str, Any]:
result = d1.copy()
for key, value in d2.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = deep_merge_dict(result[key], value)
else:
result[key] = value
return result