main.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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(" -roxienodes <number of roxie nodes>: Number of nodes to be generated ");
  40. puts(" for roxie. If not specified or specified as 0, no roxie nodes");
  41. puts(" are generated");
  42. puts(" -thornodes <number of thor nodes>: Number of nodes to be generated ");
  43. puts(" for thor slaves. A node for thor master is automatically added. ");
  44. puts(" If not specified or specified as 0, no thor nodes");
  45. puts(" are generated");
  46. puts(" -slavesPerNode <number of thor slaves per node>: Number of thor nodes ");
  47. puts(" per slave.");
  48. puts(" -roxieondemand <enable roxie on demand(1) or disable roxie on demand(any ");
  49. puts(" other value)>: Enable roxie on demand by specifying 1 for flag. ");
  50. puts(" Any other value will disable roxie on demand");
  51. puts(" -o <categoryname=newdirvalue>: overrides for any common directories");
  52. puts(" There can be multiple of the -o options. Each override should still");
  53. puts(" contain a [NAME] and either a [CATEGORY] or [INST]. ");
  54. puts(" If category already exists, the directory value is updated. Otherwise");
  55. puts(" a new category is created.");
  56. puts(" For example, \"-o log=/var/logs/[NAME]/mylogs/[INST] -o run=/var/run/[NAME]/myrun/[INST]\"");
  57. puts(" -help: print out this usage.");
  58. }
  59. int main(int argc, char** argv)
  60. {
  61. InitModuleObjects();
  62. const char* out_envname = NULL;
  63. const char* in_ipfilename;
  64. StringBuffer ipAddrs;
  65. int roxieNodes=0, thorNodes=0, slavesPerNode=1;
  66. bool roxieOnDemand = true;
  67. MapStringTo<StringBuffer> dirMap;
  68. int i = 1;
  69. bool writeToFiles = false;
  70. int port = 80;
  71. while(i<argc)
  72. {
  73. if(stricmp(argv[i], "-help") == 0 || stricmp(argv[i], "-?") == 0)
  74. {
  75. usage();
  76. releaseAtoms();
  77. return 0;
  78. }
  79. else if (stricmp(argv[i], "-env") == 0)
  80. {
  81. i++;
  82. out_envname = argv[i++];
  83. }
  84. else if (stricmp(argv[i], "-roxienodes") == 0)
  85. {
  86. i++;
  87. roxieNodes = atoi(argv[i++]);
  88. }
  89. else if (stricmp(argv[i], "-thornodes") == 0)
  90. {
  91. i++;
  92. thorNodes = atoi(argv[i++]);
  93. }
  94. else if (stricmp(argv[i], "-slavespernode") == 0)
  95. {
  96. i++;
  97. slavesPerNode = atoi(argv[i++]);
  98. }
  99. else if (stricmp(argv[i], "-roxieondemand") == 0)
  100. {
  101. i++;
  102. if (atoi(argv[i++]) != 1)
  103. roxieOnDemand = false;
  104. }
  105. else if (stricmp(argv[i], "-ip") == 0)
  106. {
  107. i++;
  108. ipAddrs.append(argv[i++]);
  109. }
  110. else if(stricmp(argv[i], "-ipfile") == 0)
  111. {
  112. i++;
  113. in_ipfilename = argv[i++];
  114. ipAddrs.loadFile(in_ipfilename);
  115. }
  116. else if(stricmp(argv[i], "-o") == 0)
  117. {
  118. i++;
  119. StringArray sbarr;
  120. DelimToStringArray(argv[i++], sbarr, "=");
  121. if (sbarr.length() != 2)
  122. continue;
  123. if (strstr(sbarr.item(1), "[NAME]") && (strstr(sbarr.item(1), "[INST]") || strstr(sbarr.item(1), "[COMPONENT]")))
  124. dirMap.setValue(sbarr.item(0), sbarr.item(1));
  125. else
  126. {
  127. fprintf(stderr, "Error: Directory Override must contain [NAME] and either [INST] or [COMPONENT]\n");
  128. releaseAtoms();
  129. return 1;
  130. }
  131. }
  132. else
  133. {
  134. fprintf(stderr, "Error: unknown command line parameter: %s\n", argv[i]);
  135. usage();
  136. releaseAtoms();
  137. return 1;
  138. }
  139. }
  140. if (!out_envname)
  141. {
  142. fprintf(stderr, "Error: Output environment xml file is required. Please specify.\n");
  143. usage();
  144. releaseAtoms();
  145. return 1;
  146. }
  147. if (ipAddrs.length() == 0)
  148. {
  149. fprintf(stderr, "Error: Ip addresses are required. Please specify.\n");
  150. usage();
  151. releaseAtoms();
  152. return 1;
  153. }
  154. try
  155. {
  156. validateIPS(ipAddrs.str());
  157. StringBuffer optionsXml, envXml;
  158. const char* pServiceName = "WsDeploy_wsdeploy_esp";
  159. Owned<IPropertyTree> pCfg = createPTreeFromXMLFile(ENVGEN_PATH_TO_ESP_CONFIG);
  160. optionsXml.appendf("<XmlArgs roxieNodes=\"%d\" thorNodes=\"%d\" slavesPerNode=\"%d\" roxieOnDemand=\"%s\" ipList=\"%s\"/>", roxieNodes,
  161. thorNodes, slavesPerNode, roxieOnDemand?"true":"false", ipAddrs.str());
  162. buildEnvFromWizard(optionsXml, pServiceName, pCfg, envXml, &dirMap);
  163. if(envXml.length())
  164. {
  165. StringBuffer env;
  166. StringBuffer thisip;
  167. queryHostIP().getIpText(thisip);
  168. env.appendf("<"XML_HEADER">\n<!-- Generated with envgen on ip %s -->\n", thisip.str());
  169. env.append(envXml);
  170. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  171. Owned<IConstEnvironment> constEnv = factory->loadLocalEnvironment(env);
  172. validateEnv(constEnv);
  173. Owned<IFile> pFile;
  174. pFile.setown(createIFile(out_envname));
  175. Owned<IFileIO> pFileIO;
  176. pFileIO.setown(pFile->open(IFOcreaterw));
  177. pFileIO->write(0, env.length(), env.str());
  178. }
  179. }
  180. catch(IException *excpt)
  181. {
  182. StringBuffer errMsg;
  183. fprintf(stderr, "Exception: %d:\n%s\n", excpt->errorCode(), excpt->errorMessage(errMsg).str());
  184. releaseAtoms();
  185. excpt->Release();
  186. return 1;
  187. }
  188. catch(...)
  189. {
  190. fprintf(stderr, "Unknown exception\n");
  191. releaseAtoms();
  192. return 1;
  193. }
  194. releaseAtoms();
  195. return 0;
  196. }