@@ -704,6 +704,42 @@ def check_nodestatus(cmd: samba_cmds.SambaCommand = samba_cmds.ctdb) -> None:
704
704
samba_cmds .execute (cmd_ctdb_check )
705
705
706
706
707
+ def _read_command_pnn (cmd : samba_cmds .SambaCommand ) -> typing .Optional [int ]:
708
+ """Run a ctdb command assuming it returns a pnn value. Return the pnn as an
709
+ int on success, None on command failure.
710
+ """
711
+ try :
712
+ out = subprocess .check_output (list (cmd ))
713
+ pnntxt = out .decode ("utf8" ).strip ()
714
+ except subprocess .CalledProcessError as err :
715
+ _logger .error (f"command { cmd !r} failed: { err !r} " )
716
+ return None
717
+ except FileNotFoundError :
718
+ _logger .error (f"ctdb command ({ cmd !r} ) not found" )
719
+ return None
720
+ try :
721
+ return int (pnntxt )
722
+ except ValueError :
723
+ _logger .debug (f"ctdb command wrote invalid pnn: { pnntxt !r} " )
724
+ return None
725
+
726
+
727
+ def current_pnn () -> typing .Optional [int ]:
728
+ """Run the `ctdb pnn` command. Returns the pnn value or None if the command
729
+ fails.
730
+ """
731
+ return _read_command_pnn (samba_cmds .ctdb ["pnn" ])
732
+
733
+
734
+ def leader_pnn () -> typing .Optional [int ]:
735
+ """Run the `ctdb leader` (or equivalent) command. Returns the pnn value or
736
+ None if the command fails.
737
+ """
738
+ # recmaster command: <ctdb recmaster|leader>
739
+ admin_cmd = samba_cmds .ctdb_leader_admin_cmd ()
740
+ return _read_command_pnn (samba_cmds .ctdb [admin_cmd ])
741
+
742
+
707
743
class CLILeaderStatus :
708
744
_isleader = False
709
745
@@ -717,29 +753,10 @@ class CLILeaderLocator:
717
753
"""
718
754
719
755
def __enter__ (self ) -> CLILeaderStatus :
720
- mypnn = recmaster = ""
721
- # mypnn = <ctdb pnn>
722
- pnn_cmd = samba_cmds .ctdb ["pnn" ]
723
- try :
724
- out = subprocess .check_output (list (pnn_cmd ))
725
- mypnn = out .decode ("utf8" ).strip ()
726
- except subprocess .CalledProcessError as err :
727
- _logger .error (f"command { pnn_cmd !r} failed: { err !r} " )
728
- except FileNotFoundError :
729
- _logger .error (f"ctdb command ({ pnn_cmd !r} ) not found" )
730
- # recmaster = <ctdb recmaster|leader>
731
- admin_cmd = samba_cmds .ctdb_leader_admin_cmd ()
732
- recmaster_cmd = samba_cmds .ctdb [admin_cmd ]
733
- try :
734
- out = subprocess .check_output (list (recmaster_cmd ))
735
- recmaster = out .decode ("utf8" ).strip ()
736
- except subprocess .CalledProcessError as err :
737
- _logger .error (f"command { recmaster_cmd !r} failed: { err !r} " )
738
- except FileNotFoundError :
739
- _logger .error (f"ctdb command ({ recmaster_cmd !r} ) not found" )
740
-
756
+ mypnn = current_pnn ()
757
+ leader = leader_pnn ()
741
758
sts = CLILeaderStatus ()
742
- sts ._isleader = bool ( mypnn ) and mypnn == recmaster
759
+ sts ._isleader = mypnn is not None and mypnn == leader
743
760
return sts
744
761
745
762
def __exit__ (
0 commit comments