Skip to content

Commit

Permalink
Issue 6068 - Add dscontainer stop function
Browse files Browse the repository at this point in the history
Bug Description:
There currently is not a stop function in dscontainer. It would be nice
to have for use cases such as testing/debugging, plus custom container
setups run during the Docker build in which dscontainer is started to do
some custom configs, then later a stop function would be nice to
gracefully stop dscontainer. Discussed in
389ds#6058.

Fix Description:
A simple stop() function added to dscontainer that gracefully stops the
ns-slapd process.

Fixes: 389ds#6068
Co-authored-by: Viktor Ashirov <[email protected]>
  • Loading branch information
slominskir and vashirov committed Feb 5, 2024
1 parent 1f95b57 commit 8fe7586
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/lib389/cli/dscontainer
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import subprocess
import argparse, argcomplete
from argparse import RawTextHelpFormatter


from lib389 import DirSrv
from lib389 import DirSrv, pid_exists, pid_from_file
from lib389.cli_base import setup_script_logger
from lib389.instance.setup import SetupDs
from lib389.instance.options import General2Base, Slapd2Base
Expand All @@ -58,6 +57,9 @@ from lib389.idm.directorymanager import DirectoryManager
# is always available!
log = setup_script_logger("container-init", True)

# PID FILE
PID_FILE = "/data/run/slapd-localhost.pid"


# Handle any dead child process signals we receive. Wait for them to terminate, or
# if they are not found, move on.
Expand Down Expand Up @@ -337,9 +339,7 @@ binddn = cn=Directory Manager
ds_proc = subprocess.Popen([
"%s/ns-slapd" % paths.sbin_dir,
"-D", paths.config_dir,
# This container version doesn't actually use or need the pidfile to track
# the process.
# "-i", "/data/run/slapd-localhost.pid",
"-i", PID_FILE,
"-d", loglevel,
], stdout=None, stderr=None, env=os.environ.copy())

Expand Down Expand Up @@ -425,6 +425,18 @@ def begin_healthcheck(ds_proc, log_exception):
return (True, False)


def stop():
stop_timeout = os.getenv("DS_STOP_TIMEOUT", 60)
count = int(stop_timeout)
pid = pid_from_file(PID_FILE)
os.kill(pid, signal.SIGTERM)
while pid_exists(pid) and count > 0:
time.sleep(1)
count -= 1
if pid_exists(pid):
os.kill(pid, signal.SIGKILL)


if __name__ == '__main__':
# Before all else, we are INIT so setup sigchild
signal.signal(signal.SIGCHLD, _sigchild_handler)
Expand Down Expand Up @@ -462,6 +474,9 @@ container host.
parser.add_argument('-r', '--runit',
help="Actually run the instance! You understand what that means ...",
action='store_true', default=False, dest='runit')
parser.add_argument('-s', '--stop',
help="Stop the instance",
action='store_true', default=False, dest='stop')
parser.add_argument('-H', '--healthcheck',
help="Start a healthcheck inside of the container for an instance. You should understand what this means ...",
action='store_true', default=False, dest='healthcheck')
Expand All @@ -472,6 +487,8 @@ container host.

if args.runit:
begin_magic()
elif args.stop:
stop()
elif args.healthcheck:
if begin_healthcheck(None, False) == (False, True):
sys.exit(0)
Expand Down

0 comments on commit 8fe7586

Please sign in to comment.