confighelper.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #include "confighelper.hpp"
  2. #include "jexcept.hpp"
  3. #include "jfile.hpp"
  4. #include "jmutex.hpp"
  5. #include "jprop.hpp"
  6. #include "jfile.hpp"
  7. #include "../deploy/XMLTags.h"
  8. #include "build-config.h"
  9. #define STANDARD_CONFIG_BUILDSETFILE "buildset.xml"
  10. #define STANDARD_CONFIG_CONFIGXML_DIR "/componentfiles/configxml/"
  11. #define STANDARD_CONFIG_PLUGIN_DIR_NAME "/plugins/"
  12. #define STANDARD_CONFIG_PLUGINS_DIR STANDARD_CONFIG_CONFIGXML_DIR STANDARD_CONFIG_PLUGIN_DIR_NAME
  13. #define PLUGIN_CGEN_COMP_LIST "cgencomplist.xml"
  14. #define ENV_GEN_RULES_DO_NOT_GENERATE_PROP "do_not_generate"
  15. CConfigHelper::CConfigHelper(IDeploymentCallback *pCallBack): m_pDefBldSet(NULL)
  16. {
  17. if (pCallBack != NULL)
  18. {
  19. m_cbDeployment.set(pCallBack);
  20. }
  21. m_strConfigXMLDir.clear();
  22. }
  23. CConfigHelper::~CConfigHelper()
  24. {
  25. }
  26. CConfigHelper* CConfigHelper::getInstance(const IPropertyTree *cfg, const char* esp_name, IDeploymentCallback *pCallBack)
  27. {
  28. static CConfigHelper *p_sConfigHelper = NULL;
  29. static CSingletonLock slock;
  30. StringBuffer xpath1, xpath2;
  31. if (slock.lock())
  32. {
  33. if (p_sConfigHelper != NULL)
  34. {
  35. slock.unlock();
  36. return p_sConfigHelper;
  37. }
  38. if (cfg == NULL || esp_name == NULL)
  39. {
  40. slock.unlock();
  41. return p_sConfigHelper = new CConfigHelper(pCallBack);
  42. }
  43. else
  44. {
  45. p_sConfigHelper = new CConfigHelper(pCallBack);
  46. xpath1.clear().appendf("%s/%s/%s[%s='%s']/%s",XML_TAG_SOFTWARE, XML_TAG_ESPPROCESS, XML_TAG_ESPSERVICE, XML_ATTR_NAME, esp_name, XML_TAG_LOCALCONFFILE);
  47. p_sConfigHelper->m_strConfFile = cfg->queryProp(xpath1.str());
  48. xpath2.clear().appendf("%s/%s/%s[%s='%s']/%s",XML_TAG_SOFTWARE, XML_TAG_ESPPROCESS, XML_TAG_ESPSERVICE, XML_ATTR_NAME, esp_name, XML_TAG_LOCALENVCONFFILE);
  49. p_sConfigHelper->m_strEnvConfFile = cfg->queryProp(xpath2.str());
  50. if (p_sConfigHelper->m_strConfFile.length() > 0 && p_sConfigHelper->m_strEnvConfFile.length() > 0 && checkFileExists(p_sConfigHelper->m_strConfFile.str()) && checkFileExists(p_sConfigHelper->m_strEnvConfFile.str()))
  51. {
  52. Owned<IProperties> pParams = createProperties(p_sConfigHelper->m_strConfFile.str());
  53. Owned<IProperties> pEnvParams = createProperties(p_sConfigHelper->m_strEnvConfFile.str());
  54. p_sConfigHelper->m_strConfigXMLDir = pEnvParams->queryProp(TAG_PATH);
  55. if (p_sConfigHelper->m_strConfigXMLDir.length() == 0)
  56. {
  57. p_sConfigHelper->m_strConfigXMLDir = INSTALL_DIR;
  58. }
  59. p_sConfigHelper->m_strBuildSetFileName = pParams->queryProp(TAG_BUILDSET);
  60. p_sConfigHelper->m_strBuildSetFilePath.append(p_sConfigHelper->m_strConfigXMLDir).append(STANDARD_CONFIG_CONFIGXML_DIR).append(
  61. p_sConfigHelper->m_strBuildSetFileName.length() > 0 ? p_sConfigHelper->m_strBuildSetFileName : STANDARD_CONFIG_BUILDSETFILE);
  62. if (p_sConfigHelper->m_cbDeployment.get() != NULL)
  63. {
  64. p_sConfigHelper->m_cbDeployment->printStatus(STATUS_NORMAL, NULL, NULL, NULL,
  65. "Adding plugin buildset %s", p_sConfigHelper->m_strBuildSetFilePath.str());
  66. }
  67. try
  68. {
  69. p_sConfigHelper->m_pDefBldSet.set(createPTreeFromXMLFile(p_sConfigHelper->m_strBuildSetFilePath.str()));
  70. p_sConfigHelper->appendBuildSetFromPlugins();
  71. }
  72. catch (IException *e)
  73. {
  74. if (p_sConfigHelper->m_cbDeployment.get() != NULL)
  75. {
  76. StringBuffer msg;
  77. e->errorMessage(msg);
  78. p_sConfigHelper->m_cbDeployment->printStatus(STATUS_ERROR, NULL, NULL, NULL,
  79. "Unable to add pluging buildset %s with error %s", p_sConfigHelper->m_strBuildSetFilePath.str(), msg.str());
  80. }
  81. delete e;
  82. }
  83. slock.unlock();
  84. return p_sConfigHelper;
  85. }
  86. else
  87. {
  88. delete p_sConfigHelper;
  89. p_sConfigHelper = NULL;
  90. slock.unlock();
  91. throw MakeStringException(-1, "Config file does not define values for %s and %s", xpath1.str(), xpath2.str());
  92. return NULL;
  93. }
  94. }
  95. }
  96. return p_sConfigHelper;
  97. }
  98. bool CConfigHelper::isInBuildSet(const char* comp_process_name, const char* comp_name) const
  99. {
  100. StringBuffer xpath;
  101. xpath.appendf("./%s/%s/%s[%s=\"%s\"][%s=\"%s\"]", XML_TAG_PROGRAMS, XML_TAG_BUILD, XML_TAG_BUILDSET, XML_ATTR_PROCESS_NAME, comp_process_name, XML_ATTR_NAME, comp_name);
  102. if (strcmp(XML_TAG_DIRECTORIES,comp_name) != 0 && m_pDefBldSet->hasProp(xpath.str()) == false)
  103. {
  104. return false;
  105. }
  106. else
  107. {
  108. return true;
  109. }
  110. }
  111. IPropertyTree* CConfigHelper::getBuildSetTree()
  112. {
  113. this->m_pDefBldSet->Link();
  114. return this->m_pDefBldSet;
  115. }
  116. void CConfigHelper::appendBuildSetFromPlugins()
  117. {
  118. const char *pMask = "*";
  119. StringBuffer strPath(this->m_strConfigXMLDir);
  120. strPath.append(STANDARD_CONFIG_CONFIGXML_DIR).append(STANDARD_CONFIG_PLUGIN_DIR_NAME);
  121. Owned<IFile> pluginRootDir = createIFile(strPath.str());
  122. if (checkFileExists(strPath.str()) == false)
  123. {
  124. if (m_cbDeployment.get() != NULL)
  125. {
  126. m_cbDeployment->printStatus(STATUS_WARN, NULL, NULL, NULL,
  127. "Could not find plugin directory at %s", strPath.str());
  128. }
  129. return;
  130. }
  131. Owned<IDirectoryIterator> pluginFiles = pluginRootDir->directoryFiles(pMask, false, true);
  132. ForEach(*pluginFiles)
  133. {
  134. if (pluginFiles->query().isDirectory() == true)
  135. {
  136. StringBuffer strPluginBuildSetPath;
  137. strPluginBuildSetPath.append(pluginFiles->query().queryFilename()).append("/").append(STANDARD_CONFIG_BUILDSETFILE);
  138. if (checkFileExists(strPluginBuildSetPath.str()) == true)
  139. {
  140. StringBuffer strXPath;
  141. strXPath.appendf("./%s/%s/%s", XML_TAG_PROGRAMS, XML_TAG_BUILD, XML_TAG_BUILDSET);
  142. Owned<IPropertyTree> pPluginBuildSet = createPTreeFromXMLFile(strPluginBuildSetPath.str());
  143. if (m_cbDeployment.get() != NULL)
  144. {
  145. m_cbDeployment->printStatus(STATUS_NORMAL, NULL, NULL, NULL,
  146. "Loading plugin BuildSet from %s", strPluginBuildSetPath.str());
  147. }
  148. Owned<IPropertyTreeIterator> pBuildSetIterator = pPluginBuildSet->getElements(strXPath);
  149. ForEach(*pBuildSetIterator)
  150. {
  151. m_pDefBldSet->addPropTree(strXPath.str(), LINK(&(pBuildSetIterator->query())));
  152. }
  153. }
  154. else
  155. {
  156. // Log message that buildset plugin file is missing
  157. if (m_cbDeployment != NULL)
  158. {
  159. m_cbDeployment->printStatus(STATUS_WARN, NULL, NULL, NULL,
  160. "buildset.xml file is missing. Looked in %s", strPluginBuildSetPath.str());
  161. }
  162. }
  163. }
  164. }
  165. }
  166. void CConfigHelper::getNewComponentListFromBuildSet(const IPropertyTree *pEnvTree, StringArray &sCompArray) const
  167. {
  168. if (pEnvTree == NULL || m_pDefBldSet == NULL)
  169. return;
  170. StringBuffer xpathBuildSetFile;
  171. xpathBuildSetFile.appendf("./%s/%s/%s", XML_TAG_PROGRAMS, XML_TAG_BUILD, XML_TAG_BUILDSET);
  172. Owned<IPropertyTreeIterator> iter = m_pDefBldSet->getElements(xpathBuildSetFile.str());
  173. ForEach(*iter)
  174. {
  175. StringBuffer xpath;
  176. IPropertyTree* pSetting = &iter->query();
  177. StringBuffer strBuildSetName(pSetting->queryProp(XML_ATTR_NAME));
  178. xpath.appendf("%s/%s/%s[%s=\"%s\"]", XML_TAG_PROGRAMS, XML_TAG_BUILD, XML_TAG_BUILDSET, XML_ATTR_NAME, strBuildSetName.str());
  179. if (pEnvTree->hasProp(xpath.str()) == false)
  180. {
  181. sCompArray.append(strBuildSetName.str());
  182. }
  183. }
  184. }
  185. void CConfigHelper::addNewComponentsFromBuildSetToEnv(IPropertyTree *pEnvTree) const
  186. {
  187. if (pEnvTree == NULL)
  188. return;
  189. StringArray sCompArray;
  190. getNewComponentListFromBuildSet(pEnvTree, sCompArray);
  191. if (sCompArray.length() == 0)
  192. return;
  193. for (int idx = 0; idx < sCompArray.length(); idx++)
  194. {
  195. StringBuffer xpath;
  196. xpath.appendf("%s/%s/%s[%s=\"%s\"]", XML_TAG_PROGRAMS, XML_TAG_BUILD, XML_TAG_BUILDSET, XML_ATTR_NAME, (sCompArray.item(idx)));
  197. if (pEnvTree->hasProp(xpath.str()) == true)
  198. continue;
  199. pEnvTree->queryPropTree(XML_TAG_PROGRAMS"/"XML_TAG_BUILD)->addPropTree(XML_TAG_BUILDSET, createPTreeFromIPT(m_pDefBldSet->queryPropTree(xpath.str())));
  200. }
  201. }
  202. void CConfigHelper::addPluginsToConfigGenCompList(IPropertyTree *pCGenComplist, const char *pPath) const
  203. {
  204. if (pCGenComplist == NULL)
  205. {
  206. return;
  207. }
  208. const char *pMask = "*";
  209. StringBuffer strPath;
  210. if (m_strConfigXMLDir.length() > 0)
  211. {
  212. strPath.set(m_strConfigXMLDir.str());
  213. strPath.append(STANDARD_CONFIG_CONFIGXML_DIR).append(STANDARD_CONFIG_PLUGIN_DIR_NAME);
  214. }
  215. else if(pPath != NULL)
  216. {
  217. strPath.set(pPath).append(STANDARD_CONFIG_PLUGIN_DIR_NAME);
  218. }
  219. else
  220. {
  221. return;
  222. }
  223. Owned<IFile> pluginRootDir = createIFile(strPath.str());
  224. if (checkFileExists(strPath.str()) == false)
  225. {
  226. return;
  227. }
  228. Owned<IDirectoryIterator> pluginFiles = pluginRootDir->directoryFiles(pMask, false, true);
  229. ForEach(*pluginFiles)
  230. {
  231. if (pluginFiles->query().isDirectory() == true)
  232. {
  233. StringBuffer strPluginCGenCompListPath;
  234. strPluginCGenCompListPath.append(pluginFiles->query().queryFilename()).append("/").append(PLUGIN_CGEN_COMP_LIST);
  235. if (checkFileExists(strPluginCGenCompListPath.str()) == true)
  236. {
  237. StringBuffer strXPath;
  238. strXPath.appendf("./%s", XML_TAG_COMPONENT);
  239. Owned<IPropertyTree> pPluginCGenCompList;
  240. try
  241. {
  242. pPluginCGenCompList.set(createPTreeFromXMLFile(strPluginCGenCompListPath.str()));
  243. }
  244. catch (IException *e)
  245. {
  246. if (m_cbDeployment.get() != NULL)
  247. {
  248. m_cbDeployment->printStatus(STATUS_WARN, NULL, NULL, NULL,
  249. "Unable to load cgencomplist.xml from %s", strPluginCGenCompListPath.str());
  250. }
  251. delete e;
  252. }
  253. Owned<IPropertyTreeIterator> pCGenCompListIterator = pPluginCGenCompList->getElements(strXPath);
  254. ForEach(*pCGenCompListIterator)
  255. {
  256. StringBuffer strXPath2(XML_TAG_COMPONENT);
  257. StringBuffer strXPath3(XML_TAG_COMPONENT"/"XML_TAG_FILE);
  258. strXPath2.appendf("[%s='%s']", XML_ATTR_NAME, pCGenCompListIterator->query().queryProp(XML_ATTR_NAME));
  259. if (pCGenComplist->hasProp(strXPath2.str()) == false)
  260. {
  261. pCGenComplist->addPropTree(XML_TAG_COMPONENT, LINK(&(pCGenCompListIterator->query())));
  262. if (m_cbDeployment.get() != NULL)
  263. {
  264. m_cbDeployment->printStatus(STATUS_NORMAL, NULL, NULL, NULL,
  265. "Loaded %s from %s", strXPath2.str(), strPluginCGenCompListPath.str());
  266. }
  267. }
  268. else
  269. {
  270. Owned<IPropertyTreeIterator> pFileListIter = pPluginCGenCompList->getElements(strXPath3);
  271. ForEach(*pFileListIter)
  272. {
  273. pCGenComplist->queryPropTree(strXPath2.str())->addPropTree(XML_TAG_FILE, LINK((&(pFileListIter->query()))));
  274. if (m_cbDeployment.get() != NULL)
  275. {
  276. m_cbDeployment->printStatus(STATUS_NORMAL, NULL, NULL, NULL,
  277. "Loading %s from %s", strXPath3.str(), strPluginCGenCompListPath.str());
  278. }
  279. }
  280. }
  281. }
  282. }
  283. else
  284. {
  285. if (m_cbDeployment != NULL)
  286. {
  287. m_cbDeployment->printStatus(STATUS_WARN, NULL, NULL, NULL,
  288. "cgencomplist.xml file is missing. Looked in %s", strPluginCGenCompListPath.str());
  289. }
  290. }
  291. }
  292. }
  293. }
  294. void CConfigHelper::addPluginsToGenEnvRules(IProperties *pGenEnvRulesProps) const
  295. {
  296. if (pGenEnvRulesProps == NULL)
  297. {
  298. return;
  299. }
  300. const char *pMask = "*";
  301. StringBuffer strPath(this->m_strConfigXMLDir);
  302. strPath.append(STANDARD_CONFIG_CONFIGXML_DIR).append(STANDARD_CONFIG_PLUGIN_DIR_NAME);
  303. Owned<IFile> pluginRootDir = createIFile(strPath.str());
  304. if (checkFileExists(strPath.str()) == false)
  305. {
  306. return;
  307. }
  308. Owned<IDirectoryIterator> pluginFiles = pluginRootDir->directoryFiles(pMask, false, true);
  309. ForEach(*pluginFiles)
  310. {
  311. if (pluginFiles->query().isDirectory() == true)
  312. {
  313. StringBuffer strPluginGenEnvRulesPath;
  314. strPluginGenEnvRulesPath.append(pluginFiles->query().queryFilename()).append("/").append(STANDARD_CONFIG_ALGORITHMFILE);
  315. if (checkFileExists(strPluginGenEnvRulesPath.str()) == true)
  316. {
  317. Owned<IProperties> pPluginGenEnvPropList = createProperties(strPluginGenEnvRulesPath.str());
  318. Owned<IPropertyIterator> pPluginGenEnvPropListIterator = pPluginGenEnvPropList->getIterator();
  319. ForEach(*pPluginGenEnvPropListIterator)
  320. {
  321. const char *pKeyName = pPluginGenEnvPropListIterator->getPropKey();
  322. if (pKeyName != NULL && *pKeyName != 0 && strcmp(pKeyName, ENV_GEN_RULES_DO_NOT_GENERATE_PROP) == 0)
  323. {
  324. StringBuffer strProp;
  325. if (pGenEnvRulesProps->hasProp(pKeyName) == false)
  326. {
  327. pPluginGenEnvPropList->getProp(pKeyName, strProp);
  328. pGenEnvRulesProps->appendProp(ENV_GEN_RULES_DO_NOT_GENERATE_PROP, strProp.str());
  329. if (m_cbDeployment != NULL)
  330. {
  331. m_cbDeployment->printStatus(STATUS_NORMAL, NULL, NULL, NULL,
  332. "Adding genenvrules %s", strProp.str());
  333. }
  334. }
  335. else
  336. {
  337. pPluginGenEnvPropList->getProp(pKeyName, strProp.clear());
  338. strProp.append(",");
  339. pGenEnvRulesProps->getProp(pKeyName, strProp);
  340. pGenEnvRulesProps->setProp(pKeyName, strProp.str());
  341. }
  342. }
  343. }
  344. }
  345. else
  346. {
  347. if (m_cbDeployment != NULL)
  348. {
  349. m_cbDeployment->printStatus(STATUS_WARN, NULL, NULL, NULL,
  350. "Failed to load plug-in genenvrules.conf file %s", strPluginGenEnvRulesPath.str());
  351. }
  352. }
  353. }
  354. }
  355. }