Skip to content

Commit 3601e3d

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
sambacc: add a function to alter /etc/services for ctdb port
The samba wiki documents the way to have CTDB use a different port is to edit /etc/services. While this seems a bit strange we are running in a container so we won't side-effect anything else. See: https://wiki.samba.org/index.php/Advanced_CTDB_configuration#CTDB_port Signed-off-by: John Mulligan <[email protected]>
1 parent ea4b930 commit 3601e3d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

sambacc/ctdb.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
CTDB_CONF: str = "/etc/ctdb/ctdb.conf"
4141
CTDB_NODES: str = "/etc/ctdb/nodes"
42+
ETC_SERVICES: str = "/etc/services"
4243

4344

4445
class ClusterMetaObject(typing.Protocol):
@@ -181,6 +182,28 @@ def read_ctdb_nodes(path: str = CTDB_NODES) -> list[str]:
181182
return entries
182183

183184

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+
184207
class PublicAddrAssignment(typing.TypedDict):
185208
address: str
186209
interfaces: list[str]
@@ -651,6 +674,7 @@ def ensure_ctdbd_etc_files(
651674
src_path: str = SHARE_DIR,
652675
*,
653676
iconfig: typing.Optional[config.InstanceConfig] = None,
677+
services_path: str = ETC_SERVICES,
654678
) -> None:
655679
"""Ensure certain files that ctdbd expects to exist in its etc dir
656680
do exist.
@@ -664,13 +688,15 @@ def ensure_ctdbd_etc_files(
664688
link_legacy_scripts = ["00.ctdb.script"]
665689

666690
public_addresses: list[PublicAddrAssignment] = []
691+
custom_ctdb_port = 0
667692
if iconfig:
668693
ctdb_conf = iconfig.ctdb_config()
669694
# todo: when we have a real config object for ctdb conf we can drop
670695
# the typing.cast
671696
public_addresses = typing.cast(
672697
list[PublicAddrAssignment], ctdb_conf.get("public_addresses", [])
673698
)
699+
custom_ctdb_port = typing.cast(int, ctdb_conf.get("ctdb_port", 0))
674700
if public_addresses:
675701
link_legacy_scripts.append("10.interface.script")
676702

@@ -700,6 +726,8 @@ def ensure_ctdbd_etc_files(
700726
if public_addresses:
701727
pa_path = os.path.join(etc_path, "public_addresses")
702728
_ensure_public_addresses_file(pa_path, public_addresses)
729+
if custom_ctdb_port:
730+
ensure_ctdb_port_in_services(custom_ctdb_port, services_path)
703731

704732

705733
_SRC_TDB_FILES = [

0 commit comments

Comments
 (0)