Skip to content

Commit 66de9d7

Browse files
committed
Dev: Refactor
1 parent d0edb67 commit 66de9d7

File tree

2 files changed

+39
-72
lines changed

2 files changed

+39
-72
lines changed

crmsh/ui_sbd.py

+39-65
Original file line numberDiff line numberDiff line change
@@ -272,79 +272,53 @@ def _parse_args(self, args: tuple[str, ...]) -> dict[str, int|str]:
272272
logger.debug("Parsed arguments: %s", parameter_dict)
273273
return parameter_dict
274274

275-
@staticmethod
276-
def _adjust_timeout_dict(timeout_dict: dict) -> dict:
277-
watchdog_timeout = timeout_dict.get("watchdog")
278-
msgwait_timeout = timeout_dict.get("msgwait")
279-
if watchdog_timeout and msgwait_timeout and msgwait_timeout < 2*watchdog_timeout:
280-
logger.warning("It's recommended to set msgwait timeout >= 2*watchdog timeout")
281-
return timeout_dict
282-
if watchdog_timeout and not msgwait_timeout:
283-
timeout_dict["msgwait"] = 2*watchdog_timeout
284-
logger.info("No msgwait timeout specified, use 2*watchdog timeout: %s", 2*watchdog_timeout)
285-
return timeout_dict
286-
if msgwait_timeout and not watchdog_timeout:
287-
watchdog_timeout = msgwait_timeout//2
288-
timeout_dict["watchdog"] = watchdog_timeout
289-
logger.info("No watchdog timeout specified, use msgwait timeout/2: %s", watchdog_timeout)
290-
return timeout_dict
291-
return timeout_dict
292-
293-
def _configure_crashdump_watchdog(self, timeout: int) -> bool:
294-
def set_crashdump_option() -> bool:
295-
shell = sh.LocalShell()
296-
cib = xmlutil.text2elem(shell.get_stdout_or_raise_error(None, 'crm configure show xml'))
297-
ra = cibquery.ResourceAgent("stonith", "", "fence_sbd")
298-
res_id_list = cibquery.has_primitive(cib, ra)
299-
if not res_id_list:
300-
logger.error("No fence_sbd resource found")
301-
return False
302-
crashdump_value = cibquery.get_parameter_value(cib, res_id_list[0], "crashdump")
303-
if utils.is_boolean_false(crashdump_value):
304-
cmd = f"crm resource param {res_id_list[0]} set crashdump 1"
305-
shell.get_stdout_or_raise_error(None, cmd)
306-
logger.info("Set crashdump option for fence_sbd resource")
307-
return True
308-
309-
finished = True
310-
logger.info("Configure crashdump watchdog timeout")
311-
312-
for node in self.cluster_nodes:
313-
if not self.service_manager.service_is_active("kdump", node):
314-
logger.error("kdump service is not active on %s", node)
315-
return not finished
316-
317-
update_dict = {"SBD_TIMEOUT_ACTION": "flush,crashdump"}
318-
if sbd.SBDUtils.is_using_disk_based_sbd():
319-
if not set_crashdump_option():
320-
return not finished
321-
update_dict["SBD_OPTS"] = f"-C {timeout}"
322-
elif sbd.SBDUtils.is_using_diskless_sbd():
323-
update_dict["SBD_OPTS"] = f"-C {timeout} -Z"
324-
sbd.SBDManager.update_sbd_configuration(update_dict)
325-
326-
return finished
275+
def set_crashdump_option(self):
276+
'''
277+
Set crashdump option for fence_sbd resource
278+
'''
279+
shell = sh.LocalShell()
280+
cib = xmlutil.text2elem(shell.get_stdout_or_raise_error(None, 'crm configure show xml'))
281+
ra = cibquery.ResourceAgent("stonith", "", "fence_sbd")
282+
res_id_list = cibquery.has_primitive(cib, ra)
283+
if not res_id_list:
284+
logger.error("No fence_sbd resource found")
285+
return
286+
crashdump_value = cibquery.get_parameter_value(cib, res_id_list[0], "crashdump")
287+
if utils.is_boolean_false(crashdump_value):
288+
cmd = f"crm resource param {res_id_list[0]} set crashdump 1"
289+
shell.get_stdout_or_raise_error(None, cmd)
290+
logger.info("Set crashdump option for fence_sbd resource")
327291

328292
def _configure_diskbase(self, parameter_dict: dict):
329293
'''
330294
Configure disk-based SBD based on input parameters and runtime config
331295
'''
332296
update_dict = {}
297+
timeout_dict = {
298+
item: parameter_dict.get(item) or self.device_meta_dict_runtime.get(item)
299+
for item in self.TIMEOUT_TYPES if item != "crashdump-watchdog"
300+
}
301+
302+
crashdump_watchdog_timeout = parameter_dict.get("crashdump-watchdog")
303+
if crashdump_watchdog_timeout:
304+
self.set_crashdump_option()
305+
timeout_dict["msgwait"] = 2*timeout_dict["watchdog"] + crashdump_watchdog_timeout
306+
logger.info("Set msgwait timeout to 2*watchdog + crashdump-watchdog: %s", timeout_dict["msgwait"])
307+
update_dict["SBD_TIMEOUT_ACTION"] = "flush,crashdump"
308+
update_dict["SBD_OPTS"] = f"-C {crashdump_watchdog_timeout}"
309+
310+
if timeout_dict["msgwait"] < 2*timeout_dict["watchdog"]:
311+
logger.warning("It's recommended to set msgwait timeout >= 2*watchdog timeout")
312+
return
313+
333314
watchdog_device = parameter_dict.get("watchdog-device")
334315
if watchdog_device != self.watchdog_device_from_config:
335316
update_dict["SBD_WATCHDOG_DEV"] = watchdog_device
336-
timeout_dict = {k: v for k, v in parameter_dict.items() if k in self.TIMEOUT_TYPES}
337-
is_subdict_timeout = utils.is_subdict(timeout_dict, self.device_meta_dict_runtime)
338317

339-
if is_subdict_timeout and not update_dict:
340-
logger.info("No metadata of SBD device changed")
318+
if timeout_dict == self.device_meta_dict_runtime and not update_dict:
319+
logger.info("No change in SBD configuration")
341320
return
342321

343-
if not is_subdict_timeout:
344-
timeout_dict = self._adjust_timeout_dict(timeout_dict)
345-
# merge runtime timeout dict into parameter timeout dict without overwriting
346-
timeout_dict = {**self.device_meta_dict_runtime, **timeout_dict}
347-
348322
sbd_manager = sbd.SBDManager(
349323
device_list_to_init=self.device_list_from_config,
350324
timeout_dict=timeout_dict,
@@ -363,6 +337,10 @@ def _configure_diskless(self, parameter_dict: dict):
363337
watchdog_device = parameter_dict.get("watchdog-device")
364338
if watchdog_device != self.watchdog_device_from_config:
365339
update_dict["SBD_WATCHDOG_DEV"] = watchdog_device
340+
crashdump_watchdog_timeout = parameter_dict.get("crashdump-watchdog")
341+
if crashdump_watchdog_timeout:
342+
update_dict["SBD_TIMEOUT_ACTION"] = "flush,crashdump"
343+
update_dict["SBD_OPTS"] = f"-C {crashdump_watchdog_timeout} -Z"
366344
if not update_dict:
367345
logger.info("No change in SBD configuration")
368346
return
@@ -465,14 +443,10 @@ def do_configure(self, context, *args) -> bool:
465443
return True
466444

467445
parameter_dict = self._parse_args(args)
468-
crashdump_watchdog_timeout = parameter_dict.pop("crashdump-watchdog", None)
469446
if sbd.SBDUtils.is_using_disk_based_sbd():
470447
self._configure_diskbase(parameter_dict)
471448
elif sbd.SBDUtils.is_using_diskless_sbd():
472449
self._configure_diskless(parameter_dict)
473-
if crashdump_watchdog_timeout:
474-
if not self._configure_crashdump_watchdog(crashdump_watchdog_timeout):
475-
return False
476450
return True
477451

478452
except self.SyntaxError as e:

crmsh/utils.py

-7
Original file line numberDiff line numberDiff line change
@@ -3202,11 +3202,4 @@ def strip_ansi_escape_sequences(text):
32023202
"""
32033203
ansi_escape_pattern = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
32043204
return ansi_escape_pattern.sub('', text)
3205-
3206-
3207-
def is_subdict(sub_dict, main_dict):
3208-
"""
3209-
Check if sub_dict is a sub-dictionary of main_dict
3210-
"""
3211-
return all(main_dict.get(k) == v for k, v in sub_dict.items())
32123205
# vim:ts=4:sw=4:et:

0 commit comments

Comments
 (0)