From f581fdc1fae74333fd95c3b025dd4870f01c819a Mon Sep 17 00:00:00 2001
From: root <root@vm-146.idm.lab.bos.redhat.com>
Date: Fri, 7 Nov 2014 16:00:00 -0500
Subject: [PATCH] Ticket 47691 - using lib389 with RPMs

This is an intermediate fix. Need further tests and cleanup
---
 lib389/__init__.py   | 58 ++++++++++++++++++++++++++++++++++++++++++++--------
 lib389/_constants.py |  4 ++--
 lib389/tools.py      |  4 ++--
 3 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/lib389/__init__.py b/lib389/__init__.py
index 5eef206..f17b3fc 100644
--- a/lib389/__init__.py
+++ b/lib389/__init__.py
@@ -497,7 +497,7 @@ class DirSrv(SimpleLDAPObject):
                 
                 variable = value.split('=',1)[0]
                 value    = value.split('=',1)[1]  
-                value    = value.strip(' \t')     # remove heading/ending space/tab
+                value    = value.strip(' \t\n')     # remove heading/ending space/tab/newline
                 for property in (CONF_SERVER_DIR,
                                  CONF_SERVERBIN_DIR,
                                  CONF_CONFIG_DIR,
@@ -595,7 +595,12 @@ class DirSrv(SimpleLDAPObject):
             # first check the private repository
             if privconfig_head:
                 pattern = "%s*" % os.path.join(privconfig_head, DEFAULT_ENV_HEAD)
+		self.log.info("not all privconfig_head: pattern : %s" % pattern)
                 found = search_dir(instances, pattern, self.serverid)
+		if len(instances) > 0: 
+			self.log.info("List from %s" % privconfig_head) 
+			for instance in instances: 
+				self.log.info("list instance %r\n" % instance) 
                 if found:
                     assert len(instances) == 1
                 else:
@@ -606,16 +611,31 @@ class DirSrv(SimpleLDAPObject):
             # second, if not already found, search the system repository
             if not found:
                 pattern = "%s*" % os.path.join(sysconfig_head, DEFAULT_ENV_HEAD)
+		self.log.info("not found not all privconfig_head: pattern : %s" % pattern)
                 search_dir(instances, pattern, self.serverid)
+		if len(instances) > 0: 
+			self.log.info("List from %s" % privconfig_head) 
+			for instance in instances: 
+				self.log.info("list instance %r\n" % instance) 
             
         else:
             # all instances must be retrieved
             if privconfig_head:
                 pattern = "%s*" % os.path.join(privconfig_head, DEFAULT_ENV_HEAD)
+		self.log.info("all privconfig_head: pattern : %s" % pattern)
                 search_dir(instances, pattern)
+		if len(instances) > 0: 
+			self.log.info("List from %s" % privconfig_head) 
+			for instance in instances: 
+				self.log.info("list instance %r\n" % instance) 
             
             pattern = "%s*" % os.path.join(sysconfig_head, DEFAULT_ENV_HEAD)
+	    self.log.info("all pattern : %s" % pattern)
             search_dir(instances, pattern)
+	    if len(instances) > 0: 
+		self.log.info("List from %s" % privconfig_head) 
+		for instance in instances: 
+			self.log.info("list instance %r\n" % instance) 
                 
         return instances
             
@@ -662,7 +682,8 @@ class DirSrv(SimpleLDAPObject):
                 SER_DEPLOYED_DIR:    self.prefix,
                 SER_BACKUP_INST_DIR: self.backupdir}
         content = formatInfData(args)
-        DirSrvTools.runInfProg(prog, content, verbose)
+	verbose = True
+        DirSrvTools.runInfProg(prog, content, verbose, prefix=self.prefix)
 
         
     def create(self):
@@ -722,6 +743,8 @@ class DirSrv(SimpleLDAPObject):
 
         # Now time to remove the instance
         prog = get_sbin_dir(None, self.prefix) + CMD_PATH_REMOVE_DS
+	if not self.prefix and os.geteuid() != 0:
+	    raise ValueError("Error: without prefix deployment it is required to be root user")
         cmd = "%s -i %s%s" % (prog, DEFAULT_INST_HEAD, self.serverid)
         self.log.debug("running: %s " % cmd)
         try:
@@ -958,8 +981,12 @@ class DirSrv(SimpleLDAPObject):
         # goes under the directory where the DS is deployed
         listFilesToBackup = []
         here = os.getcwd()
-        os.chdir(self.prefix)
-        prefix_pattern = "%s/" % self.prefix
+	if self.prefix: 
+	    os.chdir("%s/" % self.prefix) 
+	    prefix_pattern = "%s/" % self.prefix 
+	else: 
+	    os.chdir("/") 
+	    prefix_pattern = None 
         
         # build the list of directories to scan
         instroot = "%s/slapd-%s" % (self.sroot, self.serverid)
@@ -978,9 +1005,21 @@ class DirSrv(SimpleLDAPObject):
         # now scan the directory list to find the files to backup
         for dirToBackup in ldir:
             for root, dirs, files in os.walk(dirToBackup):
+		
+                for b_dir in dirs:
+                    name = os.path.join(root, b_dir)
+                    log.debug("backupFS b_dir = %s (%s) [name=%s]" % (b_dir, self.prefix, name))
+                    if prefix_pattern:
+                        name = re.sub(prefix_pattern, '', name)
+
+                    if os.path.isdir(name):
+                        listFilesToBackup.append(name)
+                        log.debug("backupFS add = %s (%s)" % (name, self.prefix))
+		
                 for file in files:
                     name = os.path.join(root, file)
-                    name = re.sub(prefix_pattern, '', name)
+		    if prefix_pattern:
+                        name = re.sub(prefix_pattern, '', name)
 
                     if os.path.isfile(name):
                         listFilesToBackup.append(name)
@@ -994,8 +1033,7 @@ class DirSrv(SimpleLDAPObject):
 
         
         for name in listFilesToBackup:
-            if os.path.isfile(name):
-                tar.add(name)
+	    tar.add(name)
         tar.close()
         log.info("backupFS: archive done : %s" % backup_file)
         
@@ -1048,7 +1086,11 @@ class DirSrv(SimpleLDAPObject):
         
         # Then restore from the directory where DS was deployed
         here = os.getcwd()
-        os.chdir(self.prefix)
+	if self.prefix: 
+	    prefix_pattern = "%s/" % self.prefix 
+	else: 
+	    prefix_pattern = "/" 
+	os.chdir(prefix_pattern) 
         
         tar = tarfile.open(backup_file)
         for member in tar.getmembers():
diff --git a/lib389/_constants.py b/lib389/_constants.py
index 86c8b99..bd3c062 100644
--- a/lib389/_constants.py
+++ b/lib389/_constants.py
@@ -41,7 +41,7 @@ defaultProperties = {
 
 
 CFGSUFFIX = "o=NetscapeRoot"
-DEFAULT_USER = "nobody"
+DEFAULT_USER = "dirsrv"
 
 # Some DN constants
 DN_DM = "cn=Directory Manager"
@@ -148,7 +148,7 @@ PLUGIN_WHOAMI             = 'whoami'
 #
 # constants
 #
-DEFAULT_USER = "nobody"
+DEFAULT_USER = "dirsrv"
 DATA_DIR     = "data"
 TMP_DIR      = "tmp"
 
diff --git a/lib389/tools.py b/lib389/tools.py
index 7812d12..7612aac 100644
--- a/lib389/tools.py
+++ b/lib389/tools.py
@@ -169,7 +169,7 @@ class DirSrvTools(object):
         if hasattr(self, 'errlog'):
             errLog = self.errlog
         else:
-            errLog = os.path.join(self.prefix, "var/log/dirsrv/slapd-%s/errors" % self.serverid)
+            errLog = os.path.join(self.prefix or "/", "var/log/dirsrv/slapd-%s/errors" % self.serverid)
         done = False
         started = True
         lastLine = ""
@@ -527,7 +527,7 @@ class DirSrvTools(object):
         DirSrvTools.start(dirsrv, True)
 
     @staticmethod
-    def runInfProg(prog, content, verbose):
+    def runInfProg(prog, content, verbose, prefix=None):
         """run a program that takes an .inf style file on stdin"""
         cmd = [prog]
         if verbose:
-- 
1.9.3

