confighelper.cpp 17 KB

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