From a5fb29566f47735c9b8084f154af0c8b2e1b7d08 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Thu, 14 Sep 2017 13:59:25 +0200
Subject: [PATCH] library/ipasssd.py: Compatibilty to ipa 4.4 and later, new
 version check

For ipa versions prior to 4.5 it is needed to use ipa-client-install script
as a source for functions. But the script contains a global finally clause
in which the generated ccache file gets removed. Threfore the script is
temporarily copied to ipa_client_install.py and the global finally clause
gets removed from the copy. All this is done in a temporary directory, which
gets removed right after the import has been done.
---
 library/ipasssd.py | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/library/ipasssd.py b/library/ipasssd.py
index 3c9c9dce..616a3393 100644
--- a/library/ipasssd.py
+++ b/library/ipasssd.py
@@ -91,14 +91,45 @@ RETURN = '''
 '''
 
 import os
+import sys
+import tempfile
 import SSSDConfig
+
 from ansible.module_utils.basic import AnsibleModule
-from ipalib.install import sysrestore
+try:
+    from ipalib.install import sysrestore
+except ImportError:
+    from ipapython import sysrestore
 from ipaplatform.paths import paths
 from ipapython.ipautil import file_exists
-from ipaclient.install.client import get_server_connection_interface, \
-    configure_nsswitch_database
-
+try:
+    from ipaclient.install.client import get_server_connection_interface, \
+        configure_nsswitch_database
+except ImportError:
+    # Create temporary copy of ipa-client-install script (as
+    # ipa_client_install.py) to be able to import the script easily and also
+    # to remove the global finally clause in which the generated ccache file
+    # gets removed. The ccache file will be needed in the next step.
+    # This is done in a temporary directory that gets removed right after
+    # ipa_client_install has been imported.
+    import shutil
+    temp_dir = tempfile.mkdtemp(dir="/tmp")
+    sys.path.append(temp_dir)
+    temp_file = "%s/ipa_client_install.py" % temp_dir
+
+    with open("/usr/sbin/ipa-client-install", "r") as f_in:
+        with open(temp_file, "w") as f_out:
+            for line in f_in:
+                if line.startswith("finally:"):
+                    break
+                f_out.write(line)
+    import ipa_client_install
+
+    shutil.rmtree(temp_dir, ignore_errors=True)
+    sys.path.remove(temp_dir)
+
+    get_server_connection_interface = ipa_client_install.get_server_connection_interface
+    configure_nsswitch_database = ipa_client_install.configure_nsswitch_database
 
 def sssd_enable_service(module, sssdconfig, service):
     try:
-- 
GitLab