9f2552
From 88d6ceb18e17c5a18bafb5092ae0c22241b212df Mon Sep 17 00:00:00 2001
9f2552
From: Mark Reynolds <mreynolds@redhat.com>
9f2552
Date: Mon, 1 Nov 2021 14:01:11 -0400
9f2552
Subject: [PATCH 08/12] Issue 4978 - make installer robust
9f2552
9f2552
Description:  When run in a container the server can fail to start
9f2552
              because the installer sets the db_home_dir to /dev/shm,
9f2552
              but in containers the default size of /dev/shm is too
9f2552
              small for libdb. We should detect if we are in a
9f2552
              container and not set db_home_dir to /dev/shm.
9f2552
9f2552
              During instance removal, if an instance was not properly
9f2552
              created then it can not be removed either. Make the
9f2552
              uninstall more robust to accept some errors and continue
9f2552
              removing the instance.
9f2552
9f2552
relates: https://github.com/389ds/389-ds-base/issues/4978
9f2552
9f2552
Reviewed by: firstyear & tbordaz(Thanks!)
9f2552
---
9f2552
 src/lib389/lib389/instance/setup.py | 9 +++++++++
9f2552
 src/lib389/lib389/utils.py          | 5 ++++-
9f2552
 2 files changed, 13 insertions(+), 1 deletion(-)
9f2552
9f2552
diff --git a/src/lib389/lib389/instance/setup.py b/src/lib389/lib389/instance/setup.py
9f2552
index be6854af8..7b0147cf9 100644
9f2552
--- a/src/lib389/lib389/instance/setup.py
9f2552
+++ b/src/lib389/lib389/instance/setup.py
9f2552
@@ -731,6 +731,15 @@ class SetupDs(object):
9f2552
             for line in template_dse.readlines():
9f2552
                 dse += line.replace('%', '{', 1).replace('%', '}', 1)
9f2552
 
9f2552
+        # Check if we are in a container, if so don't use /dev/shm for the db home dir
9f2552
+        # as containers typically don't allocate enough space for dev/shm and we don't
9f2552
+        # want to unexpectedly break the server after an upgrade
9f2552
+        container_result = subprocess.run(["systemd-detect-virt", "-c"], capture_output=True)
9f2552
+        if container_result.returncode == 0:
9f2552
+            # In a container, set the db_home_dir to the db path
9f2552
+            self.log.debug("Container detected setting db home directory to db directory.")
9f2552
+            slapd['db_home_dir'] = slapd['db_dir']
9f2552
+
9f2552
         with open(os.path.join(slapd['config_dir'], 'dse.ldif'), 'w') as file_dse:
9f2552
             dse_fmt = dse.format(
9f2552
                 schema_dir=slapd['schema_dir'],
9f2552
diff --git a/src/lib389/lib389/utils.py b/src/lib389/lib389/utils.py
9f2552
index 5ba0c6676..c63b4d0ee 100644
9f2552
--- a/src/lib389/lib389/utils.py
9f2552
+++ b/src/lib389/lib389/utils.py
9f2552
@@ -266,6 +266,8 @@ def selinux_label_port(port, remove_label=False):
9f2552
     :type remove_label: boolean
9f2552
     :raises: ValueError: Error message
9f2552
     """
9f2552
+    if port is None:
9f2552
+        return
9f2552
     try:
9f2552
         import selinux
9f2552
     except ImportError:
9f2552
@@ -662,7 +664,8 @@ def isLocalHost(host_name):
9f2552
         Uses gethostbyname()
9f2552
     """
9f2552
     # first see if this is a "well known" local hostname
9f2552
-    if host_name == 'localhost' or \
9f2552
+    if host_name is None or \
9f2552
+       host_name == 'localhost' or \
9f2552
        host_name == 'localhost.localdomain' or \
9f2552
        host_name == socket.gethostname():
9f2552
         return True
9f2552
-- 
9f2552
2.31.1
9f2552