Skip to content

Commit 85fed70

Browse files
committedApr 9, 2025
Dev: Refactor
1 parent d0edb67 commit 85fed70

File tree

3 files changed

+49
-73
lines changed

3 files changed

+49
-73
lines changed
 

‎crmsh/sbd.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,10 @@ def configure_sbd(self):
559559
Configure fence_sbd resource and related properties
560560
'''
561561
if self.diskless_sbd:
562-
utils.set_property("stonith-watchdog-timeout", SBDTimeout.STONITH_WATCHDOG_TIMEOUT_DEFAULT)
562+
if "stonith-watchdog" in self.timeout_dict:
563+
utils.set_property("stonith-watchdog-timeout", self.timeout_dict.get("stonith-watchdog"), conditional=True)
564+
else:
565+
utils.set_property("stonith-watchdog-timeout", SBDTimeout.STONITH_WATCHDOG_TIMEOUT_DEFAULT)
563566
else:
564567
if utils.get_property("stonith-watchdog-timeout", get_default=False):
565568
utils.delete_property("stonith-watchdog-timeout")

‎crmsh/ui_sbd.py

+45-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,
@@ -357,17 +331,27 @@ def _configure_diskless(self, parameter_dict: dict):
357331
Configure diskless SBD based on input parameters and runtime config
358332
'''
359333
update_dict = {}
334+
timeout_dict = {}
360335
watchdog_timeout = parameter_dict.get("watchdog")
361336
if watchdog_timeout and watchdog_timeout != self.watchdog_timeout_from_config:
362337
update_dict["SBD_WATCHDOG_TIMEOUT"] = str(watchdog_timeout)
363338
watchdog_device = parameter_dict.get("watchdog-device")
364339
if watchdog_device != self.watchdog_device_from_config:
365340
update_dict["SBD_WATCHDOG_DEV"] = watchdog_device
341+
crashdump_watchdog_timeout = parameter_dict.get("crashdump-watchdog")
342+
if crashdump_watchdog_timeout:
343+
update_dict["SBD_TIMEOUT_ACTION"] = "flush,crashdump"
344+
update_dict["SBD_OPTS"] = f"-C {crashdump_watchdog_timeout} -Z"
345+
sbd_watchdog_timeout = watchdog_timeout or self.watchdog_timeout_from_config
346+
stonith_watchdog_timeout = sbd_watchdog_timeout + crashdump_watchdog_timeout
347+
logger.info("Set stonith-watchdog-timeout to SBD_WATCHDOG_TIMEOUT + crashdump-watchdog: %s", stonith_watchdog_timeout)
348+
timeout_dict["stonith-watchdog"] = stonith_watchdog_timeout
366349
if not update_dict:
367350
logger.info("No change in SBD configuration")
368351
return
369352

370353
sbd_manager = sbd.SBDManager(
354+
timeout_dict=timeout_dict,
371355
update_dict=update_dict,
372356
diskless_sbd=True
373357
)
@@ -465,14 +449,10 @@ def do_configure(self, context, *args) -> bool:
465449
return True
466450

467451
parameter_dict = self._parse_args(args)
468-
crashdump_watchdog_timeout = parameter_dict.pop("crashdump-watchdog", None)
469452
if sbd.SBDUtils.is_using_disk_based_sbd():
470453
self._configure_diskbase(parameter_dict)
471454
elif sbd.SBDUtils.is_using_diskless_sbd():
472455
self._configure_diskless(parameter_dict)
473-
if crashdump_watchdog_timeout:
474-
if not self._configure_crashdump_watchdog(crashdump_watchdog_timeout):
475-
return False
476456
return True
477457

478458
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)