TpCommon.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2021 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #pragma warning (disable : 4786)
  14. // TpWrapper.cpp: implementation of the CTpWrapper class.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "TpWrapper.hpp"
  18. #include <stdio.h>
  19. #include "workunit.hpp"
  20. #include "exception_util.hpp"
  21. #include "portlist.h"
  22. #include "daqueue.hpp"
  23. #include "dautils.hpp"
  24. #include "dameta.hpp"
  25. void CTpWrapper::appendTpMachine(double clientVersion, IConstEnvironment* constEnv, IConstInstanceInfo& instanceInfo, IArrayOf<IConstTpMachine>& machines)
  26. {
  27. SCMStringBuffer name, networkAddress, description, directory;
  28. Owned<IEspTpMachine> machine = createTpMachine();
  29. Owned<IConstMachineInfo> machineInfo = instanceInfo.getMachine();
  30. machine->setName(machineInfo->getName(name).str());
  31. machine->setOS(machineInfo->getOS());
  32. machine->setNetaddress(machineInfo->getNetAddress(networkAddress).str());
  33. machine->setDirectory(instanceInfo.getDirectory(directory).str());
  34. machine->setPort(instanceInfo.getPort());
  35. machine->setType(eqSparkThorProcess); //for now, the appendTpMachine is only used for SparkThor.
  36. machines.append(*machine.getLink());
  37. }
  38. extern TPWRAPPER_API ISashaCommand* archiveOrRestoreWorkunits(StringArray& wuids, IProperties* params, bool archive, bool dfu)
  39. {
  40. StringBuffer sashaAddress;
  41. if (params && params->hasProp("sashaServerIP"))
  42. {
  43. sashaAddress.set(params->queryProp("sashaServerIP"));
  44. sashaAddress.append(':').append(params->getPropInt("sashaServerPort", DEFAULT_SASHA_PORT));
  45. }
  46. else
  47. getSashaService(sashaAddress, "sasha-wu-archiver", true);
  48. SocketEndpoint ep(sashaAddress);
  49. Owned<INode> node = createINode(ep);
  50. Owned<ISashaCommand> cmd = createSashaCommand();
  51. cmd->setAction(archive ? SCA_ARCHIVE : SCA_RESTORE);
  52. if (dfu)
  53. cmd->setDFU(true);
  54. ForEachItemIn(i, wuids)
  55. cmd->addId(wuids.item(i));
  56. if (!cmd->send(node, 1*60*1000))
  57. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,
  58. "Sasha (%s) took too long to respond for Archive/restore workunit.",
  59. sashaAddress.str());
  60. return cmd.getClear();
  61. }
  62. extern TPWRAPPER_API IStringIterator* getContainerTargetClusters(const char* processType, const char* processName)
  63. {
  64. Owned<CStringArrayIterator> ret = new CStringArrayIterator;
  65. Owned<IPropertyTreeIterator> queues = getComponentConfigSP()->getElements("queues");
  66. ForEach(*queues)
  67. {
  68. IPropertyTree& queue = queues->query();
  69. if (!isEmptyString(processType))
  70. {
  71. const char* type = queue.queryProp("@type");
  72. if (isEmptyString(type) || !strieq(type, processType))
  73. continue;
  74. }
  75. const char* qName = queue.queryProp("@name");
  76. if (isEmptyString(qName))
  77. continue;
  78. if (!isEmptyString(processName) && !strieq(qName, processName))
  79. continue;
  80. ret->append_unique(qName);
  81. }
  82. if (!isEmptyString(processType) && !strieq("roxie", processType))
  83. return ret.getClear();
  84. Owned<IPropertyTreeIterator> services = getGlobalConfigSP()->getElements("services[@type='roxie']");
  85. ForEach(*services)
  86. {
  87. IPropertyTree& service = services->query();
  88. const char* targetName = service.queryProp("@target");
  89. if (isEmptyString(targetName))
  90. continue;
  91. if (!isEmptyString(processName) && !strieq(targetName, processName))
  92. continue;
  93. ret->append_unique(targetName);
  94. }
  95. return ret.getClear();
  96. }
  97. extern TPWRAPPER_API bool matchNetAddressRequest(const char* netAddressReg, bool ipReq, IConstTpMachine& tpMachine)
  98. {
  99. if (ipReq)
  100. return streq(netAddressReg, tpMachine.getNetaddress());
  101. return streq(netAddressReg, tpMachine.getConfigNetaddress());
  102. }
  103. extern TPWRAPPER_API bool validateDropZonePath(const char* dropZoneName, const char* netAddr, const char* pathToCheck)
  104. {
  105. if (isEmptyString(netAddr))
  106. throw makeStringException(ECLWATCH_INVALID_INPUT, "NetworkAddress not defined.");
  107. if (isEmptyString(pathToCheck))
  108. throw makeStringException(ECLWATCH_INVALID_INPUT, "Path not defined.");
  109. if (containsRelPaths(pathToCheck)) //Detect a path like: /home/lexis/runtime/var/lib/HPCCSystems/mydropzone/../../../
  110. return false;
  111. bool isIPAddressReq = isIPAddress(netAddr);
  112. IArrayOf<IConstTpDropZone> allTpDropZones;
  113. CTpWrapper tpWrapper;
  114. tpWrapper.getTpDropZones(9999, nullptr, false, allTpDropZones); //version 9999: get the latest information about dropzone
  115. ForEachItemIn(i, allTpDropZones)
  116. {
  117. IConstTpDropZone& dropZone = allTpDropZones.item(i);
  118. if (!isEmptyString(dropZoneName) && !streq(dropZoneName, dropZone.getName()))
  119. continue;
  120. StringBuffer directory(dropZone.getPath());
  121. addPathSepChar(directory);
  122. if (!hasPrefix(pathToCheck, directory, true))
  123. continue;
  124. IArrayOf<IConstTpMachine>& tpMachines = dropZone.getTpMachines();
  125. ForEachItemIn(ii, tpMachines)
  126. {
  127. if (matchNetAddressRequest(netAddr, isIPAddressReq, tpMachines.item(ii)))
  128. return true;
  129. }
  130. }
  131. return false;
  132. }