From bc181f6c8419413b9969f216611bf341e52a6669 Mon Sep 17 00:00:00 2001 From: Simon Pichugin Date: Apr 20 2018 15:28:45 +0000 Subject: Issue 49612 - lib389 remove_ds_instance() does not remove systemd units Bug description: When running remove_ds_instance(), it does not remove the systemd files that identify this instance. Also, in previous DS versions, the /etc/dirsrv/slapd-INSTANCE would have been moved to /etc/dirsrv/slapd-INSTANCE.removed. This does not happen now. Fix description: Change remove_ds_instance function so it disables systemd unit and, in the result, it will remove symlinks. Also make the function move the config_dir to config_dir.removed as remove-ds.pl does. Add a basic test to src/lib389/lib389/tests/instance/remove_test.py Fix local_simple_allocate, so it sets serverid properly. https://pagure.io/389-ds-base/issue/49612 Reviewed by: mreynolds (Thanks!) --- diff --git a/src/lib389/lib389/__init__.py b/src/lib389/lib389/__init__.py index 315516f..92a3805 100644 --- a/src/lib389/lib389/__init__.py +++ b/src/lib389/lib389/__init__.py @@ -415,7 +415,8 @@ class DirSrv(SimpleLDAPObject, object): # The lack of this value basically rules it out in most cases self.isLocal = True - self.ds_paths = Paths(instance=self) + self.ds_paths = Paths(serverid, instance=self) + self.serverid = serverid # Do we have ldapi settings? # Do we really need .strip() on this? diff --git a/src/lib389/lib389/instance/remove.py b/src/lib389/lib389/instance/remove.py index c30fa17..440ff7b 100644 --- a/src/lib389/lib389/instance/remove.py +++ b/src/lib389/lib389/instance/remove.py @@ -8,6 +8,7 @@ import os import shutil +import subprocess def remove_ds_instance(dirsrv): """ @@ -41,6 +42,17 @@ def remove_ds_instance(dirsrv): _log.debug("Checking for instance marker at %s" % marker_path) assert os.path.exists(marker_path) + # Move the config_dir to config_dir.removed + config_dir = dirsrv.ds_paths.config_dir + config_dir_rm = "{}.removed".format(config_dir) + + if os.path.exists(config_dir_rm): + _log.debug("Removing previously existed %s" % config_dir_rm) + shutil.rmtree(config_dir_rm) + + _log.debug("Copying %s to %s" % (config_dir, config_dir_rm)) + shutil.copytree(config_dir, config_dir_rm) + # Remove these paths: # for path in ('backup_dir', 'cert_dir', 'config_dir', 'db_dir', # 'ldif_dir', 'lock_dir', 'log_dir', 'run_dir'): @@ -52,6 +64,10 @@ def remove_ds_instance(dirsrv): os.remove(marker_path) _log.debug("Removing %s" % marker_path) + # Remove the systemd symlink + _log.debug("Removing the systemd symlink") + subprocess.check_call(["systemctl", "disable", "dirsrv@{}".format(dirsrv.serverid)]) + # Done! _log.debug("Complete") diff --git a/src/lib389/lib389/tests/instance/remove_test.py b/src/lib389/lib389/tests/instance/remove_test.py new file mode 100644 index 0000000..4442726 --- /dev/null +++ b/src/lib389/lib389/tests/instance/remove_test.py @@ -0,0 +1,53 @@ +# --- BEGIN COPYRIGHT BLOCK --- +# Copyright (C) 2018 Red Hat, Inc. +# All rights reserved. +# +# License: GPL (version 3 or any later version). +# See LICENSE for details. +# --- END COPYRIGHT BLOCK --- + +import os +import subprocess +import pytest +from lib389.instance.remove import remove_ds_instance +from lib389._constants import ReplicaRole +from lib389.topologies import create_topology + + +@pytest.fixture(scope="module") +def topology_st(request): + """Create DS standalone instance""" + + topology = create_topology({ReplicaRole.STANDALONE: 1}) + + def fin(): + if topology.standalone.exists(): + topology.standalone.delete() + request.addfinalizer(fin) + + return topology + + +def test_basic(topology_st): + """Check that all DS directories and systemd items were removed""" + + inst = topology_st.standalone + + remove_ds_instance(inst) + + paths = [inst.ds_paths.backup_dir, + inst.ds_paths.cert_dir, + inst.ds_paths.config_dir, + inst.ds_paths.db_dir, + inst.get_changelog_dir(), + inst.ds_paths.ldif_dir, + inst.ds_paths.lock_dir, + inst.ds_paths.log_dir, + "{}/sysconfig/dirsrv-{}".format(inst.ds_paths.sysconf_dir, inst.serverid)] + for path in paths: + assert not os.path.exists(path) + + try: + subprocess.check_output(['systemctl', 'is-enabled', 'dirsrv@{}'.format(inst.serverid)], encoding='utf-8') + except subprocess.CalledProcessError as ex: + assert "disabled" in ex.output