From 15e1a8120d7b6292f30a1745e2369903b0c9e77e Mon Sep 17 00:00:00 2001 From: Maxim Yurchuk Date: Mon, 15 Apr 2024 16:33:31 +0000 Subject: [PATCH] Refactor ydb_slice (#3746) --- ydb/tools/ydbd_slice/__init__.py | 21 ++++++++++--- ydb/tools/ydbd_slice/handlers.py | 54 ++++++++++++++++---------------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/ydb/tools/ydbd_slice/__init__.py b/ydb/tools/ydbd_slice/__init__.py index 6a812c900b71..69c007d47d9e 100644 --- a/ydb/tools/ydbd_slice/__init__.py +++ b/ydb/tools/ydbd_slice/__init__.py @@ -555,7 +555,17 @@ def dispatch_run(func, args, walle_provider): ) v = vars(args) - slice = handlers.Slice(components, nodes, cluster_details, configurator, v.get('clear_logs'), args, walle_provider) + clear_logs = v.get('clear_logs') + yav_version = v.get('yav_version') + slice = handlers.Slice( + components, + nodes, + cluster_details, + configurator, + clear_logs, + yav_version, + walle_provider + ) func(slice) if clear_tmp: @@ -594,12 +604,13 @@ def _run(args): def add_update_raw_configs(modes, walle_provider): def _run(args): - dispatch_run(handlers.Slice.slice_update_raw_configs, args, walle_provider) + + dispatch_run(lambda self: handlers.Slice.slice_update_raw_configs(self, args.raw_cfg), args, walle_provider) mode = modes.add_parser( "update-raw-cfg", conflict_handler='resolve', - parents=[direct_nodes_args(), cluster_description_args(), component_args()], + parents=[direct_nodes_args(), cluster_description_args(), binaries_args(), component_args()], description="" ) mode.add_argument( @@ -646,7 +657,7 @@ def _run(args): mode = modes.add_parser( "clear", - parents=[direct_nodes_args(), cluster_description_args(), component_args()], + parents=[direct_nodes_args(), cluster_description_args(), binaries_args(), component_args()], description="Stop all kikimr instances at the nodes, format all kikimr drivers, shutdown dynamic slots. " "And don't start nodes afrer it. " "Use --hosts to specify particular hosts." @@ -660,7 +671,7 @@ def _run(args): mode = modes.add_parser( "format", - parents=[direct_nodes_args(), cluster_description_args(), component_args()], + parents=[direct_nodes_args(), cluster_description_args(), binaries_args(), component_args()], description="Stop all kikimr instances at the nodes, format all kikimr drivers at the nodes, start the instances. " "If you call format for all cluster, you will spoil it. " "Additional dynamic configuration will required after it. " diff --git a/ydb/tools/ydbd_slice/handlers.py b/ydb/tools/ydbd_slice/handlers.py index dd8a2e0884a6..36ef80bdbbda 100644 --- a/ydb/tools/ydbd_slice/handlers.py +++ b/ydb/tools/ydbd_slice/handlers.py @@ -21,7 +21,7 @@ def __str__(self): class Slice: - def __init__(self, components, nodes, cluster_details, configurator, do_clear_logs, args, walle_provider): + def __init__(self, components, nodes, cluster_details, configurator, do_clear_logs, yav_version, walle_provider): self.slice_kikimr_path = '/Berkanavt/kikimr/bin/kikimr' self.slice_cfg_path = '/Berkanavt/kikimr/cfg' self.slice_secrets_path = '/Berkanavt/kikimr/token' @@ -30,7 +30,7 @@ def __init__(self, components, nodes, cluster_details, configurator, do_clear_lo self.cluster_details = cluster_details self.configurator = configurator self.do_clear_logs = do_clear_logs - self.args = args + self.yav_version = yav_version self.walle_provider = walle_provider def _ensure_berkanavt_exists(self): @@ -136,12 +136,12 @@ def slice_install(self): self._format_drives() if 'bin' in self.components.get('kikimr', []): - self._update_kikimr(self.nodes, self.configurator.kikimr_bin, self.configurator.kikimr_compressed_bin) + self._update_kikimr() if 'cfg' in self.components.get('kikimr', []): static_cfg_path = self.configurator.create_static_cfg() - self._update_cfg(self.nodes, static_cfg_path) - self._deploy_secrets(self.nodes, self.args.yav_version) + self._update_cfg(static_cfg_path) + self._deploy_secrets() self._start_static() self._dynamic_configure() @@ -344,8 +344,8 @@ def _stop_slot(self, slot): tasks = self._stop_slot_ret(slot) self.nodes._check_async_execution(tasks, False) - def _stop_static(self, nodes): - nodes.execute_async("sudo service kikimr stop", check_retcode=False) + def _stop_static(self): + self.nodes.execute_async("sudo service kikimr stop", check_retcode=False) def _stop_dynamic(self): if 'dynamic_slots' in self.components: @@ -360,44 +360,44 @@ def slice_stop(self): self._stop_dynamic() if 'kikimr' in self.components: - self._stop_static(self.nodes) + self._stop_static() - def _update_kikimr(self, nodes, bin_path, compressed_path): - bin_directory = os.path.dirname(bin_path) - nodes.copy(bin_path, self.slice_kikimr_path, compressed_path=compressed_path) + def _update_kikimr(self): + bin_directory = os.path.dirname(self.configurator.kikimr_bin) + self.nodes.copy(self.configurator.kikimr_bin, self.slice_kikimr_path, compressed_path=self.configurator.kikimr_compressed_bin) for lib in ['libiconv.so', 'liblibaio-dynamic.so', 'liblibidn-dynamic.so']: lib_path = os.path.join(bin_directory, lib) if os.path.exists(lib_path): remote_lib_path = os.path.join('/lib', lib) - nodes.copy(lib_path, remote_lib_path) + self.nodes.copy(lib_path, remote_lib_path) - def _update_cfg(self, nodes, cfg_path): - nodes.copy(cfg_path, self.slice_cfg_path, directory=True) + def _update_cfg(self, cfg_path): + self.nodes.copy(cfg_path, self.slice_cfg_path, directory=True) - def _deploy_secrets(self, nodes, yav_version): - if not yav_version: + def _deploy_secrets(self): + if not self.yav_version: return - nodes.execute_async( + self.nodes.execute_async( "sudo bash -c 'set -o pipefail && sudo mkdir -p {secrets} && " "yav get version {yav_version} -o auth_file | sudo tee {auth}'".format( - yav_version=yav_version, + yav_version=self.yav_version, secrets=self.slice_secrets_path, auth=os.path.join(self.slice_secrets_path, 'kikimr.token') ) ) # creating symlinks, to attach auth.txt to node - nodes.execute_async( + self.nodes.execute_async( "sudo ln -f {secrets_auth} {cfg_auth}".format( secrets_auth=os.path.join(self.slice_secrets_path, 'kikimr.token'), cfg_auth=os.path.join(self.slice_cfg_path, 'auth.txt') ) ) - nodes.execute_async( + self.nodes.execute_async( "sudo bash -c 'set -o pipefail && yav get version {yav_version} -o tvm_secret | sudo tee {tvm_secret}'".format( - yav_version=yav_version, + yav_version=self.yav_version, tvm_secret=os.path.join(self.slice_secrets_path, 'tvm_secret') ) ) @@ -408,23 +408,23 @@ def slice_update(self): if 'kikimr' in self.components: if 'bin' in self.components.get('kikimr', []): - self._update_kikimr(self.nodes, self.configurator.kikimr_bin, self.configurator.kikimr_compressed_bin) + self._update_kikimr() self.slice_stop() if 'kikimr' in self.components: if 'cfg' in self.components.get('kikimr', []): static = self.configurator.create_static_cfg() - self._update_cfg(self.nodes, static) - self._deploy_secrets(self.nodes, self.args.yav_version) + self._update_cfg(static) + self._deploy_secrets() self._deploy_slot_configs() self.slice_start() - def slice_update_raw_configs(self): + def slice_update_raw_configs(self, raw_config_path): self.slice_stop() if 'kikimr' in self.components: if 'cfg' in self.components.get('kikimr', []): - kikimr_cfg = os.path.join(self.args.raw_cfg.config_path, 'kikimr-static') - self._update_cfg(self.nodes, kikimr_cfg) + kikimr_cfg = os.path.join(raw_config_path, 'kikimr-static') + self._update_cfg(kikimr_cfg) self.slice_start()