瀏覽代碼

Merge pull request #2313 from smeda/issue2263

Generate .ldaprc file using configgen

Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 13 年之前
父節點
當前提交
8a43cbe715

+ 177 - 5
deployment/configgen/main.cpp

@@ -51,6 +51,13 @@ void usage()
   puts("          If not specified, the following defaults are used. ");
   puts("          For win32, '.'");
   puts("          For Linux, '"CONFIG_DIR"'");
+  puts("   -ldapconfig : Generates a .ldaprc file and puts it in the specified");
+  puts("          output directory. If output directory is not specified,");
+  puts("          default output directory is used as mentioned in -od option");
+  puts("          if a LDAPServer is not defined in the environment, the .ldaprc ");
+  puts("          file is not generated. If an -ip is not provided, the first");
+  puts("          instance of the first LDAPserver is used to generate the ");
+  puts("          .ldaprc file");
   puts("   -list: Lists out the components for a specific ip in the format");
   puts("          componentType=componentName;config file directory. Does not ");
   puts("          generate any output files. If masters and slaves exist for ");
@@ -60,7 +67,7 @@ void usage()
   puts("          that have an instance defined. Does not require an ip. Does ");
   puts("          not generate any output files. Output is written to stdout ");
   puts("          in the csv format as follows");
-  puts("          ProcessType,componentNameinstanceip,instanceport,runtimedir,logdir");
+  puts("          ProcessType,componentName,instanceip,instanceport,runtimedir,logdir");
   puts("          Missing fields will be empty.");
   puts("   -listdirs: Lists out any directories that need to be created during ");
   puts("          init time. Currently, directories for any drop zones ");
@@ -75,6 +82,11 @@ void usage()
   puts("          Software/Directories section in the following format. ");
   puts("          <CategoryName>=<DirectoryValue>");
   puts("          Each directory will be listed on a new line.");
+  puts("   -listldaps: Lists out all LDAPServer instances defined in the ");
+  puts("          environment in the following format. If the same component");
+  puts("          has more than one instance, it will be listed as two separate.");
+  puts("          entries in the output");
+  puts("          componentName,instanceip");
   puts("   -machines: Lists out all names or ips of machines specified in the environment");
   puts("          Output is written to stdout, one machine per line.");
   puts("   -validateonly: Validates the environment, without generating permanent ");
@@ -109,6 +121,45 @@ void deleteRecursive(const char* path)
     }
 }
 
+void copyDirectoryRecursive(const char *source, const char *target)
+{
+  bool first = true;
+  Owned<IDirectoryIterator> dir = createDirectoryIterator(source, "*");
+
+  ForEach (*dir)
+  {
+    IFile &sourceFile = dir->query();
+
+    if (sourceFile.isFile())
+    {
+      StringBuffer targetname(target);
+      targetname.append(PATHSEPCHAR);
+      dir->getName(targetname);
+      OwnedIFile destFile = createIFile(targetname.str());
+
+      if (first)
+      {
+        if (!recursiveCreateDirectory(target))
+          throw MakeStringException(-1,"Cannot create directory %s",target);
+
+        first = false;
+      }
+
+      copyFile(destFile, &sourceFile);
+    }
+    else if (sourceFile.isDirectory())
+    {
+      StringBuffer newSource(source);
+      StringBuffer newTarget(target);
+      newSource.append(PATHSEPCHAR);
+      newTarget.append(PATHSEPCHAR);
+      dir->getName(newSource);
+      dir->getName(newTarget);
+      copyDirectoryRecursive(newSource.str(), newTarget.str());
+    }
+  }
+}
+
 //returns temp path that ends with path sep
 //
 #ifdef _WIN32
@@ -194,7 +245,8 @@ int processRequest(const char* in_cfgname, const char* out_dirname, const char*
                    const char* compName, const char* compType, const char* in_filename, 
                    const char* out_filename, bool generateOutput, const char* ipAddr, 
                    bool listComps, bool verbose, bool listallComps, bool listdirs, 
-                   bool listdropzones, bool listcommondirs, bool listMachines, bool validateOnly)
+                   bool listdropzones, bool listcommondirs, bool listMachines, bool validateOnly,
+                   bool listldaps, bool ldapconfig)
 {
   Owned<IPropertyTree> pEnv = createPTreeFromXMLFile(in_cfgname);
   short nodeIndex = 1;
@@ -247,12 +299,119 @@ int processRequest(const char* in_cfgname, const char* out_dirname, const char*
       throw e;
     }
   }
-  else if (!listComps && !listallComps && !listdirs && !listdropzones && !listcommondirs && !listMachines)
+  else if (ldapconfig)
+  {
+    char tempdir[_MAX_PATH];
+    StringBuffer sb;
+
+    while(true)
+    {
+      sb.clear().appendf("%d", msTick());
+      getTempPath(tempdir, sizeof(tempdir), sb.str());
+
+      if (!checkDirExists(tempdir))
+      {
+        if (recursiveCreateDirectory(tempdir))
+          break;
+      }
+    }
+
+    StringBuffer out;
+    xPath.clear().append(XML_TAG_SOFTWARE"/"XML_TAG_LDAPSERVERPROCESS);
+    Owned<IPropertyTreeIterator> ldaps = pEnv->getElements(xPath.str());
+    Owned<IPropertyTree> pSelComps(createPTree("SelectedComponents"));
+    bool flag = false;
+
+    ForEach(*ldaps)
+    {
+      IPropertyTree* ldap = &ldaps->query();
+      IPropertyTree* inst;
+      int count = 1;
+      xPath.clear().appendf(XML_TAG_INSTANCE"[%d]", count);
+
+      while ((inst = ldap->queryPropTree(xPath.str())) != NULL)
+      {
+        if (ipAddr && *ipAddr && strcmp(ipAddr, inst->queryProp(XML_ATTR_NETADDRESS)))
+        {
+          ldap->removeTree(inst);
+          continue;
+        }
+
+        if (!flag)
+        {
+          inst->addProp(XML_ATTR_DIRECTORY, ".");
+          sb.clear().append(tempdir).append(PATHSEPCHAR).append(ldap->queryProp(XML_ATTR_NAME));
+          xPath.clear().appendf(XML_TAG_INSTANCE"[%d]", ++count);
+          flag = true;
+        }
+        else
+        {
+          ldap->removeTree(inst);
+        }
+      }
+
+      if (flag)
+      {
+        pSelComps->addPropTree(XML_TAG_LDAPSERVERPROCESS, createPTreeFromIPT(ldap));
+        break;
+      }
+    }
+
+    if (flag)
+    {
+      try
+      {
+        toXML(pEnv, envXML.clear());
+        m_pEnvironment.setown(factory->loadLocalEnvironment(envXML));
+        m_pConstEnvironment.set(m_pEnvironment);
+        Owned<IEnvDeploymentEngine> m_configGenMgr;
+        m_configGenMgr.setown(createConfigGenMgr(*m_pConstEnvironment, callback, pSelComps, in_dirname?in_dirname:"", tempdir, compName, compType, ipAddr));
+        m_configGenMgr->deploy(DEFLAGS_CONFIGFILES, DEBACKUP_NONE, false, false);
+        copyDirectoryRecursive(sb.str(), out_dirname);
+        deleteRecursive(tempdir);
+      }
+      catch (IException* e)
+      {
+        deleteRecursive(tempdir);
+        throw e;
+      }
+    }
+  }
+  else if (!listComps && !listallComps && !listdirs && !listdropzones && !listcommondirs && !listMachines
+           && !listldaps)
   {
     Owned<IEnvDeploymentEngine> m_configGenMgr;
     m_configGenMgr.setown(createConfigGenMgr(*m_pConstEnvironment, callback, NULL, in_dirname?in_dirname:"", out_dirname?out_dirname:"", compName, compType, ipAddr));
     m_configGenMgr->deploy(DEFLAGS_CONFIGFILES, DEBACKUP_NONE, false, false);
   }
+  else if (listldaps)
+  {
+    StringBuffer out;
+    xPath.appendf("Software/%s/", XML_TAG_LDAPSERVERPROCESS);
+    Owned<IPropertyTreeIterator> ldaps = pEnv->getElements(xPath.str());
+
+    ForEach(*ldaps)
+    {
+      IPropertyTree* ldap = &ldaps->query();
+      Owned<IPropertyTreeIterator> insts = ldap->getElements(XML_TAG_INSTANCE);
+
+      ForEach(*insts)
+      {
+        IPropertyTree* inst = &insts->query();
+        StringBuffer computerName(inst->queryProp(XML_ATTR_COMPUTER));
+        xPath.clear().appendf("Hardware/Computer[@name=\"%s\"]", computerName.str());
+        IPropertyTree* pComputer = pEnv->queryPropTree(xPath.str());
+
+        if (pComputer)
+        {
+          const char* netAddr = pComputer->queryProp("@netAddress");
+          out.appendf("%s,%s\n", ldap->queryProp(XML_ATTR_NAME), netAddr);
+        }
+      }
+    }
+
+    fprintf(stdout, "%s", out.str());
+  }
   else if (listdirs || listdropzones)
   {
     StringBuffer out;
@@ -458,6 +617,8 @@ int main(int argc, char** argv)
   bool listcommondirs = false;
   bool listMachines = false;
   bool validateOnly = false;
+  bool ldapconfig = false;
+  bool listldaps = false;
 
   int i = 1;
   bool writeToFiles = false;
@@ -510,6 +671,11 @@ int main(int argc, char** argv)
       i++;
       in_dirname = argv[i++];
     }
+    else if (stricmp(argv[i], "-ldapconfig") == 0)
+    {
+      i++;
+      ldapconfig = true;
+    }
     else if (stricmp(argv[i], "-list") == 0)
     {
       i++;
@@ -535,6 +701,11 @@ int main(int argc, char** argv)
       i++;
       listcommondirs = true;
     }
+    else if (stricmp(argv[i], "-listldaps") == 0)
+    {
+      i++;
+      listldaps = true;
+    }
     else if (stricmp(argv[i], "-machines") == 0)
     {
       i++;
@@ -567,14 +738,15 @@ int main(int argc, char** argv)
     return 1;
   }
 
-  if (ipAddr.length() == 0  && !listallComps && !validateOnly && !listcommondirs && !listMachines)
+  if (ipAddr.length() == 0  && !listallComps && !validateOnly && !listcommondirs && !listMachines && !listldaps && !ldapconfig)
     ipAddr.clear().append("."); // Meaning match any local address
 
   try
   {
     processRequest(in_cfgname, out_dirname, in_dirname, compName, 
       compType,in_filename, out_filename, generateOutput, ipAddr.length() ? ipAddr.str(): NULL,
-      listComps, verbose, listallComps, listdirs, listdropzones, listcommondirs, listMachines, validateOnly);
+      listComps, verbose, listallComps, listdirs, listdropzones, listcommondirs, listMachines,
+      validateOnly, listldaps, ldapconfig);
   }
   catch(IException *excpt)
   {

+ 1 - 0
deployment/deploy/XMLTags.h

@@ -55,6 +55,7 @@
 #define XML_TAG_INSTALLSET             "InstallSet"
 #define XML_TAG_INSTANCE               "Instance"
 #define XML_TAG_INSTANCES              "Instances"
+#define XML_TAG_LDAPSERVERPROCESS      "LDAPServerProcess"
 #define XML_TAG_LOCATION               "Location"
 #define XML_TAG_MAP                    "Map"
 #define XML_TAG_MAPTABLES              "MapTables"

+ 1 - 1
deployment/deploy/deploy.cpp

@@ -933,7 +933,7 @@ public:
     //---------------------------------------------------------------------------
   CConfigGenMgr(IConstEnvironment& environment, IDeploymentCallback& callback, 
     IPropertyTree* pSelectedComponents, const char* inputDir, const char* outputDir, const char* compName, const char* compType, const char* ipAddr)
-    : CEnvironmentDeploymentEngine(environment, callback, pSelectedComponents),
+    : CEnvironmentDeploymentEngine(environment, callback, NULL),
     m_inDir(inputDir), 
     m_outDir(outputDir),
     m_compName(compName),

+ 1 - 0
initfiles/componentfiles/configxml/CMakeLists.txt

@@ -61,6 +61,7 @@ FOREACH( iFILES
     ${CMAKE_CURRENT_SOURCE_DIR}/generic.xsd
     ${CMAKE_CURRENT_SOURCE_DIR}/installset.xsd
     ${CMAKE_CURRENT_SOURCE_DIR}/ldapserver.xsd
+    ${CMAKE_CURRENT_SOURCE_DIR}/ldapserver.xsl
     ${CMAKE_CURRENT_SOURCE_DIR}/mysqlserver.xsd
     ${CMAKE_CURRENT_SOURCE_DIR}/plugin.xsd
     ${CMAKE_CURRENT_SOURCE_DIR}/regress.xsd

+ 3 - 0
initfiles/componentfiles/configxml/cgencomplist_linux.xml

@@ -49,6 +49,9 @@
   <Component name="esp" processName='EspProcess' schema='esp.xsd'>
     <File name="esp.xsl" method="xslt" destName="esp.xml"/>
   </Component>
+  <Component name="ldapServer" processName="LDAPServerProcess">
+    <File name="ldapserver.xsl" method="xslt" destName=".ldaprc"/>
+  </Component>
   <Component name="regressionSuite" processName='RegressionSuite' schema='regress.xsd'>
     <File name="regressionSuite.xsl" method="xslt" destName="test_suites.bat"/>
     <File name="runregress.xsl" method="xslt" destName="regress.ini"/>

+ 3 - 0
initfiles/componentfiles/configxml/cgencomplist_win.xml

@@ -52,6 +52,9 @@
   <Component name="GAB_Configuration">
     <File name="GABConfig.xsl" method="xslt" destName="@name+.cfg"/>
   </Component>
+  <Component name="ldapServer" processName="LDAPServerProcess">
+    <File name="ldapserver.xsl" method="xslt" destName=".ldaprc"/>
+  </Component>
   <Component name="regressionSuite" processName='RegressionSuite' schema='regress.xsd'>
     <File name="regressionSuite.xsl" method="xslt" destName="test_suites.bat"/>
     <File name="runregress.xsl" method="xslt" destName="regress.ini"/>

+ 42 - 0
initfiles/componentfiles/configxml/ldapserver.xsl

@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+################################################################################
+#    Copyright (C) 2011 HPCC Systems.
+#
+#    All rights reserved. This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+################################################################################
+-->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml:space="default">
+<xsl:param name="process" select="'unknown'"/>
+
+<xsl:strip-space elements="*"/>
+<xsl:output method="text" indent="no" omit-xml-declaration="yes"/>
+
+<xsl:template match="text()"/>
+    <xsl:template match="/">
+        <xsl:apply-templates select="Environment/Software/LDAPServerProcess[@name=$process]"/>
+    </xsl:template>
+
+   <xsl:template match="LDAPServerProcess">
+ host <xsl:value-of select="Instance[1]/@netAddress"/>
+ #base dc=internal,dc=sds
+ filesBasedn="<xsl:value-of select="@filesBasedn"/>"
+ resourcesBasedn="<xsl:value-of select="@resourcesBasedn"/>"
+ sudoersBasedn="<xsl:value-of select="@sudoersBasedn"/>"
+ systemBasedn="<xsl:value-of select="@systemBasedn"/>"
+ usersBasedn="<xsl:value-of select="@usersBasedn"/>"
+ workunitsBasedn="<xsl:value-of select="@workunitsBasedn"/>"
+   </xsl:template>
+</xsl:stylesheet>