confighelper.cpp 17 KB

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