39
39
40
40
CTDB_CONF : str = "/etc/ctdb/ctdb.conf"
41
41
CTDB_NODES : str = "/etc/ctdb/nodes"
42
+ ETC_SERVICES : str = "/etc/services"
42
43
43
44
44
45
class ClusterMetaObject (typing .Protocol ):
@@ -181,6 +182,28 @@ def read_ctdb_nodes(path: str = CTDB_NODES) -> list[str]:
181
182
return entries
182
183
183
184
185
+ def _svc_match (service_name : str , line : str ) -> bool :
186
+ if not line .strip () or line .startswith ("#" ):
187
+ return False
188
+ first = line .split ()[0 ]
189
+ return first == service_name
190
+
191
+
192
+ def ensure_ctdb_port_in_services (port : int , path : str ) -> None :
193
+ try :
194
+ with open (path , "r" ) as fh :
195
+ lines = [line .strip () for line in fh ]
196
+ except FileNotFoundError :
197
+ lines = []
198
+ cleaned = [line for line in lines if not _svc_match ("ctdb" , line )]
199
+ cleaned .append (f"ctdb { port } /tcp # custom ctdb port" )
200
+ cleaned .append (f"ctdb { port } /udp # custom ctdb port" )
201
+ with open (path , "w" ) as fh :
202
+ for line in cleaned :
203
+ fh .write (line )
204
+ fh .write ("\n " )
205
+
206
+
184
207
class PublicAddrAssignment (typing .TypedDict ):
185
208
address : str
186
209
interfaces : list [str ]
@@ -651,6 +674,7 @@ def ensure_ctdbd_etc_files(
651
674
src_path : str = SHARE_DIR ,
652
675
* ,
653
676
iconfig : typing .Optional [config .InstanceConfig ] = None ,
677
+ services_path : str = ETC_SERVICES ,
654
678
) -> None :
655
679
"""Ensure certain files that ctdbd expects to exist in its etc dir
656
680
do exist.
@@ -664,13 +688,15 @@ def ensure_ctdbd_etc_files(
664
688
link_legacy_scripts = ["00.ctdb.script" ]
665
689
666
690
public_addresses : list [PublicAddrAssignment ] = []
691
+ custom_ctdb_port = 0
667
692
if iconfig :
668
693
ctdb_conf = iconfig .ctdb_config ()
669
694
# todo: when we have a real config object for ctdb conf we can drop
670
695
# the typing.cast
671
696
public_addresses = typing .cast (
672
697
list [PublicAddrAssignment ], ctdb_conf .get ("public_addresses" , [])
673
698
)
699
+ custom_ctdb_port = typing .cast (int , ctdb_conf .get ("ctdb_port" , 0 ))
674
700
if public_addresses :
675
701
link_legacy_scripts .append ("10.interface.script" )
676
702
@@ -700,6 +726,8 @@ def ensure_ctdbd_etc_files(
700
726
if public_addresses :
701
727
pa_path = os .path .join (etc_path , "public_addresses" )
702
728
_ensure_public_addresses_file (pa_path , public_addresses )
729
+ if custom_ctdb_port :
730
+ ensure_ctdb_port_in_services (custom_ctdb_port , services_path )
703
731
704
732
705
733
_SRC_TDB_FILES = [
0 commit comments