forked from TrueCloudLab/frostfs-testlib
[#183] Read all configuration files for service config
Signed-off-by: Andrey Berezin <a.berezin@yadro.com>
This commit is contained in:
parent
751381cd60
commit
55cebc042c
5 changed files with 68 additions and 52 deletions
|
@ -5,51 +5,74 @@ from typing import Any
|
|||
import yaml
|
||||
|
||||
from frostfs_testlib import reporter
|
||||
from frostfs_testlib.shell.interfaces import CommandOptions
|
||||
from frostfs_testlib.shell.interfaces import CommandOptions, Shell
|
||||
from frostfs_testlib.storage.configuration.interfaces import ServiceConfigurationYml
|
||||
from frostfs_testlib.storage.dataclasses.node_base import ServiceClass
|
||||
|
||||
|
||||
def extend_dict(extend_me: dict, extend_by: dict):
|
||||
if isinstance(extend_by, dict):
|
||||
for k, v in extend_by.items():
|
||||
if k in extend_me:
|
||||
extend_dict(extend_me.get(k), v)
|
||||
else:
|
||||
extend_me[k] = v
|
||||
else:
|
||||
extend_me += extend_by
|
||||
|
||||
|
||||
class ServiceConfiguration(ServiceConfigurationYml):
|
||||
def __init__(self, service: "ServiceClass") -> None:
|
||||
self.service = service
|
||||
self.shell = self.service.host.get_shell()
|
||||
self.confd_path = os.path.join(self.service.config_dir, "conf.d")
|
||||
def __init__(self, service_name: str, shell: Shell, config_dir: str, main_config_path: str) -> None:
|
||||
self.service_name = service_name
|
||||
self.shell = shell
|
||||
self.main_config_path = main_config_path
|
||||
self.confd_path = os.path.join(config_dir, "conf.d")
|
||||
self.custom_file = os.path.join(self.confd_path, "99_changes.yml")
|
||||
|
||||
def _path_exists(self, path: str) -> bool:
|
||||
return not self.shell.exec(f"test -e {path}", options=CommandOptions(check=False)).return_code
|
||||
|
||||
def _get_data_from_file(self, path: str) -> dict:
|
||||
content = self.shell.exec(f"cat {path}").stdout
|
||||
data = yaml.safe_load(content)
|
||||
return data
|
||||
def _get_config_files(self):
|
||||
config_files = [self.main_config_path]
|
||||
|
||||
def get(self, key: str) -> str:
|
||||
with reporter.step(f"Get {key} configuration value for {self.service}"):
|
||||
config_files = [self.service.main_config_path]
|
||||
if self._path_exists(self.confd_path):
|
||||
files = self.shell.exec(f"find {self.confd_path} -type f").stdout.strip().split()
|
||||
# Sorting files in backwards order from latest to first one
|
||||
config_files.extend(sorted(files, key=lambda x: -int(re.findall("^\d+", os.path.basename(x))[0])))
|
||||
|
||||
if self._path_exists(self.confd_path):
|
||||
files = self.shell.exec(f"find {self.confd_path} -type f").stdout.strip().split()
|
||||
# Sorting files in backwards order from latest to first one
|
||||
config_files.extend(sorted(files, key=lambda x: -int(re.findall("^\d+", os.path.basename(x))[0])))
|
||||
return config_files
|
||||
|
||||
result = None
|
||||
for file in files:
|
||||
data = self._get_data_from_file(file)
|
||||
result = self._find_option(key, data)
|
||||
if result is not None:
|
||||
break
|
||||
def _get_configuration(self, config_files: list[str]) -> dict:
|
||||
if not config_files:
|
||||
return [{}]
|
||||
|
||||
splitter = "+++++"
|
||||
files_str = " ".join(config_files)
|
||||
all_content = self.shell.exec(
|
||||
f"echo Getting config files; for file in {files_str}; do (echo {splitter}; sudo cat ${{file}}); done"
|
||||
).stdout
|
||||
files_content = all_content.split("+++++")[1:]
|
||||
files_data = [yaml.safe_load(file_content) for file_content in files_content]
|
||||
|
||||
mergedData = {}
|
||||
for data in files_data:
|
||||
extend_dict(mergedData, data)
|
||||
|
||||
return mergedData
|
||||
|
||||
def get(self, key: str) -> str | Any:
|
||||
with reporter.step(f"Get {key} configuration value for {self.service_name}"):
|
||||
config_files = self._get_config_files()
|
||||
configuration = self._get_configuration(config_files)
|
||||
result = self._find_option(key, configuration)
|
||||
return result
|
||||
|
||||
def set(self, values: dict[str, Any]):
|
||||
with reporter.step(f"Change configuration for {self.service}"):
|
||||
with reporter.step(f"Change configuration for {self.service_name}"):
|
||||
if not self._path_exists(self.confd_path):
|
||||
self.shell.exec(f"mkdir {self.confd_path}")
|
||||
|
||||
if self._path_exists(self.custom_file):
|
||||
data = self._get_data_from_file(self.custom_file)
|
||||
data = self._get_configuration([self.custom_file])
|
||||
else:
|
||||
data = {}
|
||||
|
||||
|
@ -61,5 +84,5 @@ class ServiceConfiguration(ServiceConfigurationYml):
|
|||
self.shell.exec(f"chmod 777 {self.custom_file}")
|
||||
|
||||
def revert(self):
|
||||
with reporter.step(f"Revert changed options for {self.service}"):
|
||||
with reporter.step(f"Revert changed options for {self.service_name}"):
|
||||
self.shell.exec(f"rm -rf {self.custom_file}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue