summaryrefslogtreecommitdiffstats
path: root/ipapython
diff options
context:
space:
mode:
authorAlexander Bokovoy <abokovoy@redhat.com>2019-06-11 18:05:29 +0300
committerAlexander Bokovoy <abokovoy@redhat.com>2019-06-29 11:00:28 +0300
commitcdb94e0ff2c7bc03b2f0064b77fedabfa0ae8121 (patch)
tree1a6dfa9085eb66bc99a0c027a9c8ade718c4df5a /ipapython
parent84201e1daff7e2cd80352654dc7c81530b51dc74 (diff)
downloadfreeipa-cdb94e0ff2c7bc03b2f0064b77fedabfa0ae8121.tar.gz
freeipa-cdb94e0ff2c7bc03b2f0064b77fedabfa0ae8121.tar.xz
freeipa-cdb94e0ff2c7bc03b2f0064b77fedabfa0ae8121.zip
ipaserver.install.installutils: move commonly used utils to ipapython.ipautil
When creating ipa-client-samba tool, few common routines from the server installer code became useful for the client code as well. Move them to ipapython.ipautil and update references as well. Fixes: https://pagure.io/freeipa/issue/3999 Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com> Reviewed-By: Rob Crittenden <rcritten@redhat.com> Reviewed-By: Christian Heimes <cheimes@redhat.com>
Diffstat (limited to 'ipapython')
-rw-r--r--ipapython/ipautil.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index 106745354..22434cba7 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -28,6 +28,7 @@ import random
import math
import os
import sys
+import errno
import copy
import shutil
import socket
@@ -54,6 +55,7 @@ except ImportError:
netifaces = None
from ipapython.dn import DN
+from ipaplatform.paths import paths
logger = logging.getLogger(__name__)
@@ -1571,3 +1573,61 @@ class APIVersion(tuple):
@property
def minor(self):
return self[1]
+
+
+def remove_keytab(keytab_path):
+ """
+ Remove Kerberos keytab and issue a warning if the procedure fails
+
+ :param keytab_path: path to the keytab file
+ """
+ try:
+ logger.debug("Removing service keytab: %s", keytab_path)
+ os.remove(keytab_path)
+ except OSError as e:
+ if e.errno != errno.ENOENT:
+ logger.warning("Failed to remove Kerberos keytab '%s': %s",
+ keytab_path, e)
+ logger.warning("You may have to remove it manually")
+
+
+def remove_ccache(ccache_path=None, run_as=None):
+ """
+ remove Kerberos credential cache, essentially a wrapper around kdestroy.
+
+ :param ccache_path: path to the ccache file
+ :param run_as: run kdestroy as this user
+ """
+ logger.debug("Removing service credentials cache")
+ kdestroy_cmd = [paths.KDESTROY]
+ if ccache_path is not None:
+ logger.debug("Ccache path: '%s'", ccache_path)
+ kdestroy_cmd.extend(['-c', ccache_path])
+
+ try:
+ run(kdestroy_cmd, runas=run_as, env={})
+ except CalledProcessError as e:
+ logger.warning(
+ "Failed to clear Kerberos credentials cache: %s", e)
+
+
+def remove_file(filename):
+ """Remove a file and log any exceptions raised.
+ """
+ try:
+ os.unlink(filename)
+ except Exception as e:
+ # ignore missing file
+ if getattr(e, 'errno', None) != errno.ENOENT:
+ logger.error('Error removing %s: %s', filename, str(e))
+
+
+def rmtree(path):
+ """
+ Remove a directory structure and log any exceptions raised.
+ """
+ try:
+ if os.path.exists(path):
+ shutil.rmtree(path)
+ except Exception as e:
+ logger.error('Error removing %s: %s', path, str(e))