Skip to content

Commit

Permalink
commands: add ability to wait for ctdb when running smbd/winbindd
Browse files Browse the repository at this point in the history
Add a `--wait-for=ctdb` option to the `samba-container run` subcommand.
This option will make sambacc wait for the condition, ctdb available
and returning a valid pnn, to be true before starting the server.

Signed-off-by: John Mulligan <[email protected]>
  • Loading branch information
phlogistonjohn committed Jul 26, 2024
1 parent 6e09747 commit 2080471
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion sambacc/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
#

import logging
import time

from sambacc import samba_cmds
import sambacc.paths as paths

Expand All @@ -24,7 +27,34 @@
from .join import join


_logger = logging.getLogger(__name__)

INIT_ALL = "init-all"
SMBD = "smbd"
WINBINDD = "winbindd"
CTDBD = "ctdbd"
TARGETS = [SMBD, WINBINDD, CTDBD]


class WaitForCTDBCondition:
def met(self, ctx: Context) -> bool:
target = getattr(ctx.cli, "target", None)
if target == CTDBD:
raise Fail(f"Can not start and wait for {CTDBD}")
_logger.debug("Condition required: ctdb pnn available")
import sambacc.ctdb

pnn = sambacc.ctdb.current_pnn()
ok = pnn is not None
_logger.debug(
"Condition %s: ctdb pnn available: %s",
"met" if ok else "not met",
pnn,
)
return ok


_wait_for_conditions = {"ctdb": WaitForCTDBCondition}


def _run_container_args(parser):
Expand All @@ -47,6 +77,17 @@ def _run_container_args(parser):
" The special 'init-all' name will perform all known setup steps."
),
)
_wait_for_choices = _wait_for_conditions.keys()
parser.add_argument(
"--wait-for",
action="append",
choices=_wait_for_choices,
help=(
"Specify a condition to wait for prior to starting the server"
" process. Available conditions: `ctdb` - wait for ctdb"
" to run and provide a pnn."
),
)
parser.add_argument(
"--insecure-auto-join",
action="store_true",
Expand All @@ -57,7 +98,7 @@ def _run_container_args(parser):
)
parser.add_argument(
"target",
choices=["smbd", "winbindd", "ctdbd"],
choices=TARGETS,
help="Which process to run",
)

Expand All @@ -67,6 +108,12 @@ def run_container(ctx: Context) -> None:
"""Run a specified server process."""
if ctx.cli.no_init and ctx.cli.setup:
raise Fail("can not specify both --no-init and --setup")

if ctx.cli.wait_for:
conditions = [_wait_for_conditions[n]() for n in ctx.cli.wait_for]
while not all(c.met(ctx) for c in conditions):
time.sleep(1)

# running servers expect to make use of ctdb whenever it is configured
ctx.expects_ctdb = True
if not ctx.cli.no_init and not ctx.cli.setup:
Expand Down

0 comments on commit 2080471

Please sign in to comment.