main.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "jliball.hpp"
  15. #include "XMLTags.h"
  16. #include "deployutils.hpp"
  17. #include "build-config.h"
  18. #define ENVGEN_PATH_TO_ESP_CONFIG INSTALL_DIR""CONFIG_DIR"/configmgr/esp.xml"
  19. #define STANDARD_CONFIGXMLDIR COMPONENTFILES_DIR"/configxml/"
  20. #define STANDARD_CONFIG_BUILDSETFILE "buildset.xml"
  21. void usage()
  22. {
  23. const char* version = "1.1";
  24. printf("HPCC Systems environment generator. version %s. Usage:\n", version);
  25. puts(" envgen -env <environment file> -ip <ip addr> [options]");
  26. puts("");
  27. puts("options: ");
  28. puts(" -env : Full path of the environment file that will be generated.");
  29. puts(" If a file with the same name exists, a new name with _xxx ");
  30. puts(" will be generated ");
  31. puts(" -ip : Ip addresses that should be used for environment generation");
  32. puts(" Allowed formats are ");
  33. puts(" X.X.X.X;");
  34. puts(" X.X.X.X-XXX;");
  35. puts(" -ipfile: name of the file that contains Ip addresses");
  36. puts(" Allowed formats are ");
  37. puts(" X.X.X.X;");
  38. puts(" X.X.X.X-XXX;");
  39. puts(" -supportnodes <number of support nodes>: Number of nodes to be used");
  40. puts(" for non-Thor and non-Roxie components. If not specified or ");
  41. puts(" specified as 0, thor and roxie nodes may overlap with support");
  42. puts(" nodes. If an invalid value is provided, support nodes are ");
  43. puts(" treated to be 0");
  44. puts(" -roxienodes <number of roxie nodes>: Number of nodes to be generated ");
  45. puts(" for roxie. If not specified or specified as 0, no roxie nodes");
  46. puts(" are generated");
  47. puts(" -thornodes <number of thor nodes>: Number of nodes to be generated ");
  48. puts(" for thor slaves. A node for thor master is automatically added. ");
  49. puts(" If not specified or specified as 0, no thor nodes");
  50. puts(" are generated");
  51. puts(" -slavesPerNode <number of thor slaves per node>: Number of thor nodes ");
  52. puts(" per slave.");
  53. puts(" -roxieondemand <enable roxie on demand(1) or disable roxie on demand(any ");
  54. puts(" other value)>: Enable roxie on demand by specifying 1 for flag. ");
  55. puts(" Any other value will disable roxie on demand");
  56. puts(" -o <categoryname=newdirvalue>: overrides for any common directories");
  57. puts(" There can be multiple of the -o options. Each override should still");
  58. puts(" contain a [NAME] and either a [CATEGORY] or [INST]. ");
  59. puts(" If category already exists, the directory value is updated. Otherwise");
  60. puts(" a new category is created.");
  61. puts(" For example, \"-o log=/var/logs/[NAME]/mylogs/[INST] -o run=/var/run/[NAME]/myrun/[INST]\"");
  62. puts(" -help: print out this usage.");
  63. }
  64. int main(int argc, char** argv)
  65. {
  66. InitModuleObjects();
  67. const char* out_envname = NULL;
  68. const char* in_ipfilename;
  69. StringBuffer ipAddrs;
  70. int roxieNodes=0, thorNodes=0, slavesPerNode=1, supportNodes=0;
  71. bool roxieOnDemand = true;
  72. MapStringTo<StringBuffer> dirMap;
  73. int i = 1;
  74. bool writeToFiles = false;
  75. int port = 80;
  76. while(i<argc)
  77. {
  78. if(stricmp(argv[i], "-help") == 0 || stricmp(argv[i], "-?") == 0)
  79. {
  80. usage();
  81. releaseAtoms();
  82. return 0;
  83. }
  84. else if (stricmp(argv[i], "-env") == 0)
  85. {
  86. i++;
  87. out_envname = argv[i++];
  88. }
  89. else if (stricmp(argv[i], "-supportnodes") == 0)
  90. {
  91. i++;
  92. supportNodes = atoi(argv[i++]);
  93. }
  94. else if (stricmp(argv[i], "-roxienodes") == 0)
  95. {
  96. i++;
  97. roxieNodes = atoi(argv[i++]);
  98. }
  99. else if (stricmp(argv[i], "-thornodes") == 0)
  100. {
  101. i++;
  102. thorNodes = atoi(argv[i++]);
  103. }
  104. else if (stricmp(argv[i], "-slavespernode") == 0)
  105. {
  106. i++;
  107. slavesPerNode = atoi(argv[i++]);
  108. }
  109. else if (stricmp(argv[i], "-roxieondemand") == 0)
  110. {
  111. i++;
  112. if (atoi(argv[i++]) != 1)
  113. roxieOnDemand = false;
  114. }
  115. else if (stricmp(argv[i], "-ip") == 0)
  116. {
  117. i++;
  118. ipAddrs.append(argv[i++]);
  119. }
  120. else if(stricmp(argv[i], "-ipfile") == 0)
  121. {
  122. i++;
  123. in_ipfilename = argv[i++];
  124. ipAddrs.loadFile(in_ipfilename);
  125. }
  126. else if(stricmp(argv[i], "-o") == 0)
  127. {
  128. i++;
  129. StringArray sbarr;
  130. DelimToStringArray(argv[i++], sbarr, "=");
  131. if (sbarr.length() != 2)
  132. continue;
  133. if (strstr(sbarr.item(1), "[NAME]") && (strstr(sbarr.item(1), "[INST]") || strstr(sbarr.item(1), "[COMPONENT]")))
  134. dirMap.setValue(sbarr.item(0), sbarr.item(1));
  135. else
  136. {
  137. fprintf(stderr, "Error: Directory Override must contain [NAME] and either [INST] or [COMPONENT]\n");
  138. releaseAtoms();
  139. return 1;
  140. }
  141. }
  142. else
  143. {
  144. fprintf(stderr, "Error: unknown command line parameter: %s\n", argv[i]);
  145. usage();
  146. releaseAtoms();
  147. return 1;
  148. }
  149. }
  150. if (!out_envname)
  151. {
  152. fprintf(stderr, "Error: Output environment xml file is required. Please specify.\n");
  153. usage();
  154. releaseAtoms();
  155. return 1;
  156. }
  157. if (ipAddrs.length() == 0)
  158. {
  159. fprintf(stderr, "Error: Ip addresses are required. Please specify.\n");
  160. usage();
  161. releaseAtoms();
  162. return 1;
  163. }
  164. try
  165. {
  166. validateIPS(ipAddrs.str());
  167. StringBuffer optionsXml, envXml;
  168. const char* pServiceName = "WsDeploy_wsdeploy_esp";
  169. Owned<IPropertyTree> pCfg = createPTreeFromXMLFile(ENVGEN_PATH_TO_ESP_CONFIG);
  170. optionsXml.appendf("<XmlArgs supportNodes=\"%d\" roxieNodes=\"%d\" thorNodes=\"%d\" slavesPerNode=\"%d\" roxieOnDemand=\"%s\" ipList=\"%s\"/>", supportNodes, roxieNodes,
  171. thorNodes, slavesPerNode, roxieOnDemand?"true":"false", ipAddrs.str());
  172. buildEnvFromWizard(optionsXml, pServiceName, pCfg, envXml, &dirMap);
  173. if(envXml.length())
  174. {
  175. StringBuffer env;
  176. StringBuffer thisip;
  177. queryHostIP().getIpText(thisip);
  178. env.appendf("<"XML_HEADER">\n<!-- Generated with envgen on ip %s -->\n", thisip.str());
  179. env.append(envXml);
  180. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  181. Owned<IConstEnvironment> constEnv = factory->loadLocalEnvironment(env);
  182. validateEnv(constEnv);
  183. Owned<IFile> pFile;
  184. pFile.setown(createIFile(out_envname));
  185. Owned<IFileIO> pFileIO;
  186. pFileIO.setown(pFile->open(IFOcreaterw));
  187. pFileIO->write(0, env.length(), env.str());
  188. }
  189. }
  190. catch(IException *excpt)
  191. {
  192. StringBuffer errMsg;
  193. fprintf(stderr, "Exception: %d:\n%s\n", excpt->errorCode(), excpt->errorMessage(errMsg).str());
  194. releaseAtoms();
  195. excpt->Release();
  196. return 1;
  197. }
  198. catch(...)
  199. {
  200. fprintf(stderr, "Unknown exception\n");
  201. releaseAtoms();
  202. return 1;
  203. }
  204. releaseAtoms();
  205. return 0;
  206. }