environment.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 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. #include "jlib.hpp"
  14. #include "environment.hpp"
  15. #include "jptree.hpp"
  16. #include "jexcept.hpp"
  17. #include "jiter.ipp"
  18. #include "jmisc.hpp"
  19. #include "jencrypt.hpp"
  20. #include "jutil.hpp"
  21. #include "mpbase.hpp"
  22. #include "daclient.hpp"
  23. #include "dadfs.hpp"
  24. #include "dafdesc.hpp"
  25. #include "dasds.hpp"
  26. #include "dalienv.hpp"
  27. #include <string>
  28. #include <unordered_map>
  29. #include <tuple>
  30. #define SDS_LOCK_TIMEOUT 30000
  31. #define DEFAULT_DROPZONE_INDEX 1
  32. #define DROPZONE_BY_MACHINE_SUFFIX "-dropzoneByMachine-"
  33. #define DROPZONE_SUFFIX "dropzone-"
  34. #define MACHINE_PREFIX "machine-"
  35. #define SPARKTHOR_SUFFIX "sparkthor-"
  36. static int environmentTraceLevel = 1;
  37. static Owned <IConstEnvironment> cache;
  38. class CLocalEnvironment;
  39. class CConstMachineInfoIterator : public CSimpleInterfaceOf<IConstMachineInfoIterator>
  40. {
  41. public:
  42. CConstMachineInfoIterator();
  43. virtual bool first() override;
  44. virtual bool next() override;
  45. virtual bool isValid() override;
  46. virtual IConstMachineInfo & query() override;
  47. virtual unsigned count() const override;
  48. protected:
  49. Owned<IConstMachineInfo> curr;
  50. Owned<CLocalEnvironment> constEnv;
  51. unsigned index = 1;
  52. unsigned maxIndex = 0;
  53. };
  54. class CConstDropZoneServerInfoIterator : public CSimpleInterfaceOf<IConstDropZoneServerInfoIterator>
  55. {
  56. public:
  57. CConstDropZoneServerInfoIterator(const IConstDropZoneInfo * dropZone);
  58. virtual bool first() override;
  59. virtual bool next() override;
  60. virtual bool isValid() override;
  61. virtual IConstDropZoneServerInfo & query() override;
  62. virtual unsigned count() const override;
  63. protected:
  64. Owned<IConstDropZoneServerInfo> curr;
  65. Owned<CLocalEnvironment> constEnv;
  66. Owned<IPropertyTreeIterator> serverListIt;
  67. unsigned maxIndex = 0;
  68. };
  69. class CConstDropZoneInfoIterator : public CSimpleInterfaceOf<IConstDropZoneInfoIterator>
  70. {
  71. public:
  72. CConstDropZoneInfoIterator();
  73. virtual bool first() override;
  74. virtual bool next() override;
  75. virtual bool isValid() override;
  76. virtual IConstDropZoneInfo & query() override;
  77. virtual unsigned count() const override;
  78. protected:
  79. Owned<IConstDropZoneInfo> curr;
  80. Owned<CLocalEnvironment> constEnv;
  81. unsigned index = 1;
  82. unsigned maxIndex = 0;
  83. };
  84. class CConstDfuQueueInfoIterator : public CSimpleInterfaceOf<IConstDfuQueueInfoIterator>
  85. {
  86. public:
  87. CConstDfuQueueInfoIterator();
  88. virtual bool first() override;
  89. virtual bool next() override;
  90. virtual bool isValid() override;
  91. virtual IConstDfuQueueInfo & query() override;
  92. virtual unsigned count() const override;
  93. protected:
  94. Owned<IConstDfuQueueInfo> curr;
  95. Owned<CLocalEnvironment> constEnv;
  96. unsigned index = 1;
  97. unsigned maxIndex = 0;
  98. };
  99. class CConstSparkThorInfoIterator : public CSimpleInterfaceOf<IConstSparkThorInfoIterator>
  100. {
  101. public:
  102. CConstSparkThorInfoIterator();
  103. virtual bool first() override;
  104. virtual bool next() override;
  105. virtual bool isValid() override;
  106. virtual IConstSparkThorInfo & query() override;
  107. virtual unsigned count() const override;
  108. protected:
  109. Owned<IConstSparkThorInfo> curr;
  110. Owned<CLocalEnvironment> constEnv;
  111. unsigned index = 1;
  112. unsigned maxIndex = 0;
  113. };
  114. class CConstInstanceInfoIterator : public CSimpleInterfaceOf<IConstInstanceInfoIterator>
  115. {
  116. public:
  117. CConstInstanceInfoIterator(const CLocalEnvironment * env, IPropertyTreeIterator * itr);
  118. virtual bool first() override;
  119. virtual bool next() override;
  120. virtual bool isValid() override;
  121. virtual IConstInstanceInfo & query() override;
  122. virtual unsigned count() const override;
  123. protected:
  124. Owned<IPropertyTreeIterator> instanceItr;
  125. Owned<IConstInstanceInfo> curr;
  126. const CLocalEnvironment* constEnv;
  127. unsigned index = 1;
  128. unsigned maxIndex = 0;
  129. };
  130. //==========================================================================================
  131. class CConstInstanceInfo;
  132. class CLocalEnvironment : implements IConstEnvironment, public CInterface
  133. {
  134. private:
  135. // NOTE - order is important - we need to construct before p and (especially) destruct after p
  136. Owned<IRemoteConnection> conn;
  137. Owned<IPropertyTree> p;
  138. mutable MapStringToMyClass<IConstEnvBase> cache;
  139. mutable Mutex safeCache;
  140. mutable bool dropZoneCacheBuilt;
  141. mutable bool machineCacheBuilt;
  142. mutable bool sparkThorCacheBuilt;
  143. mutable bool clusterGroupKeyNameCache;
  144. StringBuffer fileAccessUrl;
  145. struct KeyPairMapEntity
  146. {
  147. std::string publicKey, privateKey;
  148. };
  149. mutable std::unordered_map<std::string, KeyPairMapEntity> keyPairMap;
  150. mutable std::unordered_map<std::string, std::string> keyGroupMap;
  151. StringBuffer xPath;
  152. mutable unsigned numOfMachines;
  153. mutable unsigned numOfDropZones;
  154. mutable unsigned numOfSparkThors;
  155. mutable bool isDropZoneRestrictionLoaded = false;
  156. mutable bool dropZoneRestrictionEnabled = true;
  157. void buildDfuQueueCache() const;
  158. mutable unsigned numOfDfuQueues = 0;
  159. mutable bool dfuQueueCacheBuilt = false;
  160. IConstEnvBase * getCache(const char *path) const;
  161. void setCache(const char *path, IConstEnvBase *value) const;
  162. void buildMachineCache() const;
  163. void buildDropZoneCache() const;
  164. void buildSparkThorCache() const;
  165. void init();
  166. void ensureClusterGroupKeyMap() const // keyPairMap and keyGroupMap it alters is mutable
  167. {
  168. if (!clusterGroupKeyNameCache)
  169. {
  170. StringBuffer keysDir;
  171. envGetConfigurationDirectory("keys",nullptr, nullptr, keysDir);
  172. Owned<IPropertyTreeIterator> keyPairIt = p->getElements("EnvSettings/Keys/KeyPair");
  173. ForEach(*keyPairIt)
  174. {
  175. IPropertyTree &keyPair = keyPairIt->query();
  176. const char *name = keyPair.queryProp("@name");
  177. const char *publicKeyPath = keyPair.queryProp("@publicKey");
  178. const char *privateKeyPath = keyPair.queryProp("@privateKey");
  179. if (isEmptyString(name))
  180. {
  181. WARNLOG("skipping invalid EnvSettings/Key/KeyPair entry, name not defined");
  182. continue;
  183. }
  184. if (isEmptyString(publicKeyPath) || isEmptyString(privateKeyPath))
  185. {
  186. WARNLOG("skipping invalid EnvSettings/Key/KeyPair entry, name=%s", name);
  187. continue;
  188. }
  189. StringBuffer absPublicKeyPath, absPrivateKeyPath;
  190. if (!isAbsolutePath(publicKeyPath))
  191. {
  192. absPublicKeyPath.append(keysDir);
  193. addPathSepChar(absPublicKeyPath);
  194. absPublicKeyPath.append(publicKeyPath);
  195. }
  196. else
  197. absPublicKeyPath.append(publicKeyPath);
  198. if (!isAbsolutePath(privateKeyPath))
  199. {
  200. absPrivateKeyPath.append(keysDir);
  201. addPathSepChar(absPrivateKeyPath);
  202. absPrivateKeyPath.append(privateKeyPath);
  203. }
  204. else
  205. absPrivateKeyPath.append(privateKeyPath);
  206. keyPairMap[name] = { absPublicKeyPath.str(), absPrivateKeyPath.str() };
  207. }
  208. /* From 7.0.0 until 7.0.6, the <Keys> section of the environment required
  209. * the mappings to be defined as "Cluster" instead of "ClusterGroup" - See: HPCC-21192
  210. */
  211. #define BKWRDCOMPAT_CLUSTER_VS_CLUSTERGROUP
  212. const char *groupKeysPath = "EnvSettings/Keys/ClusterGroup";
  213. #ifdef BKWRDCOMPAT_CLUSTER_VS_CLUSTERGROUP
  214. for (unsigned i=0; i<2; i++) // once for std. "ClusterGroup", 2nd time for legacy "Cluster"
  215. {
  216. #endif
  217. Owned<IPropertyTreeIterator> clusterGroupIter = p->getElements(groupKeysPath);
  218. #ifdef BKWRDCOMPAT_CLUSTER_VS_CLUSTERGROUP
  219. if (clusterGroupIter->first() && keyGroupMap.size()) // NB: always 0 1st time around.
  220. {
  221. WARNLOG("Invalid configuration: mixed 'Keys/ClusterGroup' definitions and legacy 'Keys/Cluster' definitions found, legacy 'Keys/Cluster' definition will be ignored.");
  222. break;
  223. }
  224. #endif
  225. ForEach(*clusterGroupIter)
  226. {
  227. IPropertyTree &clusterGroup = clusterGroupIter->query();
  228. const char *groupName = clusterGroup.queryProp("@name");
  229. if (isEmptyString(groupName))
  230. {
  231. WARNLOG("skipping %s entry with no name", groupKeysPath);
  232. continue;
  233. }
  234. if (clusterGroup.hasProp("@keyPairName"))
  235. {
  236. const char *keyPairName = clusterGroup.queryProp("@keyPairName");
  237. if (isEmptyString(keyPairName))
  238. {
  239. WARNLOG("skipping invalid %s entry, name=%s", groupKeysPath, groupName);
  240. continue;
  241. }
  242. keyGroupMap[groupName] = keyPairName;
  243. }
  244. }
  245. #ifdef BKWRDCOMPAT_CLUSTER_VS_CLUSTERGROUP
  246. groupKeysPath = "EnvSettings/Keys/Cluster";
  247. }
  248. #endif
  249. clusterGroupKeyNameCache = true;
  250. }
  251. }
  252. public:
  253. IMPLEMENT_IINTERFACE;
  254. CLocalEnvironment(IRemoteConnection *_conn, IPropertyTree *x=nullptr, const char* path="Environment");
  255. CLocalEnvironment(const char* path="config.xml");
  256. virtual ~CLocalEnvironment();
  257. virtual IStringVal & getName(IStringVal & str) const;
  258. virtual IStringVal & getXML(IStringVal & str) const;
  259. virtual IPropertyTree & getPTree() const;
  260. virtual IEnvironment& lock() const;
  261. virtual IConstDomainInfo * getDomain(const char * name) const;
  262. virtual IConstMachineInfo * getMachine(const char * name) const;
  263. virtual IConstMachineInfo * getMachineByAddress(const char * machineIp) const;
  264. virtual IConstMachineInfo * getMachineForLocalHost() const;
  265. virtual IConstDropZoneInfo * getDropZone(const char * name) const;
  266. virtual IConstInstanceInfo * getInstance(const char * type, const char * version, const char *domain) const;
  267. virtual CConstInstanceInfo * getInstanceByIP(const char *type, const char *version, IpAddress &ip) const;
  268. virtual IConstComputerTypeInfo * getComputerType(const char * name) const;
  269. virtual bool getRunInfo(IStringVal & path, IStringVal & dir, const char *type, const char *version, const char *machineaddr, const char *defprogname) const;
  270. virtual void preload();
  271. virtual IRemoteConnection* getConnection() const { return conn.getLink(); }
  272. void setXML(const char * logicalName);
  273. const char* getPath() const { return xPath.str(); }
  274. void unlockRemote();
  275. virtual bool isConstEnvironment() const { return true; }
  276. virtual void clearCache();
  277. virtual IConstMachineInfoIterator * getMachineIterator() const;
  278. virtual IConstDropZoneInfoIterator * getDropZoneIteratorByAddress(const char * address) const;
  279. virtual IConstDropZoneInfo * getDropZoneByAddressPath(const char * netaddress, const char *targetPath) const;
  280. virtual IConstDropZoneInfoIterator * getDropZoneIterator() const;
  281. unsigned getNumberOfMachines() const { buildMachineCache(); return numOfMachines; }
  282. IConstMachineInfo * getMachineByIndex(unsigned index) const;
  283. unsigned getNumberOfDropZones() const { buildDropZoneCache(); return numOfDropZones; }
  284. IConstDropZoneInfo * getDropZoneByIndex(unsigned index) const;
  285. bool isDropZoneRestrictionEnabled() const;
  286. unsigned getNumberOfDfuQueues() const { buildDfuQueueCache(); return numOfDfuQueues; }
  287. IConstDfuQueueInfo * getDfuQueueByIndex(unsigned index) const;
  288. virtual IConstDfuQueueInfoIterator * getDfuQueueIterator() const;
  289. bool isValidDfuQueueName(const char * queueName) const;
  290. virtual const char *getClusterGroupKeyPairName(const char *group) const override
  291. {
  292. synchronized procedure(safeCache);
  293. ensureClusterGroupKeyMap();
  294. return keyGroupMap[group].c_str();
  295. }
  296. virtual const char *getPublicKeyPath(const char *keyPairName) const override
  297. {
  298. synchronized procedure(safeCache);
  299. ensureClusterGroupKeyMap();
  300. return keyPairMap[keyPairName].publicKey.c_str();
  301. }
  302. virtual const char *getPrivateKeyPath(const char *keyPairName) const override
  303. {
  304. synchronized procedure(safeCache);
  305. ensureClusterGroupKeyMap();
  306. return keyPairMap[keyPairName].privateKey.c_str();
  307. }
  308. virtual const char *getFileAccessUrl() const
  309. {
  310. synchronized procedure(safeCache);
  311. return fileAccessUrl.length() ? fileAccessUrl.str() : nullptr;
  312. }
  313. virtual IConstDaFileSrvInfo *getDaFileSrvGroupInfo(const char *name) const override;
  314. virtual IConstSparkThorInfo *getSparkThor(const char *name) const;
  315. virtual IConstSparkThorInfoIterator *getSparkThorIterator() const;
  316. unsigned getNumberOfSparkThors() const { buildSparkThorCache(); return numOfSparkThors; }
  317. IConstSparkThorInfo *getSparkThorByIndex(unsigned index) const;
  318. };
  319. class CLockedEnvironment : implements IEnvironment, public CInterface
  320. {
  321. public:
  322. //note that order of construction/destruction is important
  323. Owned<CLocalEnvironment> c;
  324. Owned<CLocalEnvironment> env;
  325. Owned<CLocalEnvironment> constEnv;
  326. IMPLEMENT_IINTERFACE;
  327. CLockedEnvironment(CLocalEnvironment *_c)
  328. {
  329. Owned<IRemoteConnection> connection = _c->getConnection();
  330. if (connection)
  331. {
  332. constEnv.set(_c); //save original constant environment
  333. //we only wish to allow one party to allow updating the environment.
  334. //
  335. //create a new /NewEnvironment subtree, locked for read/write access for self and entire subtree; delete on disconnect
  336. //
  337. StringBuffer newName("/New");
  338. newName.append(constEnv->getPath());
  339. const unsigned int mode = RTM_CREATE | RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE |
  340. RTM_LOCK_SUB | RTM_DELETE_ON_DISCONNECT;
  341. Owned<IRemoteConnection> conn = querySDS().connect(newName.str(), myProcessSession(), mode, SDS_LOCK_TIMEOUT);
  342. if (conn == nullptr)
  343. {
  344. if (environmentTraceLevel > 0)
  345. PrintLog("Failed to create locked environment %s", newName.str());
  346. throw MakeStringException(-1, "Failed to get a lock on environment /%s", newName.str());
  347. }
  348. //save the locked environment
  349. env.setown(new CLocalEnvironment(conn, nullptr, newName.str()));
  350. //get a lock on the const environment
  351. const unsigned int mode2 = RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE | RTM_LOCK_SUB;
  352. Owned<IRemoteConnection> conn2 = querySDS().connect(constEnv->getPath(), myProcessSession(), mode2, SDS_LOCK_TIMEOUT);
  353. if (conn2 == nullptr)
  354. {
  355. if (environmentTraceLevel > 0)
  356. PrintLog("Failed to lock environment %s", constEnv->getPath());
  357. throw MakeStringException(-1, "Failed to get a lock on environment /%s", constEnv->getPath());
  358. }
  359. //copy const environment to our member environment
  360. Owned<IPropertyTree> pSrc = conn2->getRoot();
  361. c.setown( new CLocalEnvironment(nullptr, createPTreeFromIPT(pSrc)));
  362. conn2->rollback();
  363. }
  364. else
  365. {
  366. c.set(_c);
  367. }
  368. }
  369. virtual ~CLockedEnvironment()
  370. {
  371. }
  372. virtual IStringVal & getName(IStringVal & str) const
  373. { return c->getName(str); }
  374. virtual IStringVal & getXML(IStringVal & str) const
  375. { return c->getXML(str); }
  376. virtual IPropertyTree & getPTree() const
  377. { return c->getPTree(); }
  378. virtual IConstDomainInfo * getDomain(const char * name) const
  379. { return c->getDomain(name); }
  380. virtual IConstMachineInfo * getMachine(const char * name) const
  381. { return c->getMachine(name); }
  382. virtual IConstMachineInfo * getMachineByAddress(const char * machineIp) const
  383. { return c->getMachineByAddress(machineIp); }
  384. virtual IConstMachineInfo * getMachineForLocalHost() const
  385. { return c->getMachineForLocalHost(); }
  386. virtual IConstDropZoneInfo * getDropZone(const char * name) const
  387. { return c->getDropZone(name); }
  388. virtual IConstInstanceInfo * getInstance(const char *type, const char *version, const char *domain) const
  389. { return c->getInstance(type, version, domain); }
  390. virtual bool getRunInfo(IStringVal & path, IStringVal & dir, const char *type, const char *version, const char *machineaddr,const char *defprogname) const
  391. { return c->getRunInfo(path, dir, type, version, machineaddr, defprogname); }
  392. virtual IConstComputerTypeInfo * getComputerType(const char * name) const
  393. { return c->getComputerType(name); }
  394. virtual IEnvironment & lock() const
  395. { ((CInterface*)this)->Link(); return *(IEnvironment*)this; }
  396. virtual void commit();
  397. virtual void rollback();
  398. virtual void setXML(const char * pstr)
  399. { c->setXML(pstr); }
  400. virtual void preload()
  401. { c->preload(); }
  402. virtual bool isConstEnvironment() const { return false; }
  403. virtual void clearCache() { c->clearCache(); }
  404. virtual IConstMachineInfoIterator * getMachineIterator() const
  405. { return c->getMachineIterator(); }
  406. virtual IConstDropZoneInfoIterator * getDropZoneIteratorByAddress(const char * address) const
  407. { return c->getDropZoneIteratorByAddress(address); }
  408. virtual IConstDropZoneInfo * getDropZoneByAddressPath(const char * netaddress, const char *targetPath) const
  409. { return c->getDropZoneByAddressPath(netaddress, targetPath); }
  410. virtual IConstDropZoneInfoIterator * getDropZoneIterator() const
  411. { return c->getDropZoneIterator(); }
  412. virtual bool isDropZoneRestrictionEnabled() const
  413. { return c->isDropZoneRestrictionEnabled(); }
  414. virtual const char *getClusterGroupKeyPairName(const char *cluster) const override
  415. { return c->getClusterGroupKeyPairName(cluster); }
  416. virtual const char *getPublicKeyPath(const char *keyPairName) const override
  417. { return c->getPublicKeyPath(keyPairName); }
  418. virtual const char *getPrivateKeyPath(const char *keyPairName) const override
  419. { return c->getPrivateKeyPath(keyPairName); }
  420. virtual const char *getFileAccessUrl() const
  421. { return c->getFileAccessUrl(); }
  422. virtual IConstDaFileSrvInfo *getDaFileSrvGroupInfo(const char *name) const override
  423. { return c->getDaFileSrvGroupInfo(name); }
  424. virtual IConstSparkThorInfo *getSparkThor(const char *name) const
  425. { return c->getSparkThor(name); }
  426. virtual IConstSparkThorInfoIterator *getSparkThorIterator() const
  427. { return c->getSparkThorIterator(); }
  428. virtual IConstDfuQueueInfoIterator * getDfuQueueIterator() const
  429. { return c->getDfuQueueIterator(); }
  430. virtual bool isValidDfuQueueName(const char * queueName) const
  431. { return c->isValidDfuQueueName(queueName); }
  432. };
  433. void CLockedEnvironment::commit()
  434. {
  435. if (constEnv)
  436. {
  437. //get a lock on const environment momentarily
  438. const unsigned int mode2 = RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE | RTM_LOCK_SUB;
  439. Owned<IRemoteConnection> conn2 = querySDS().connect(constEnv->getPath(), myProcessSession(), mode2, SDS_LOCK_TIMEOUT);
  440. if (conn2 == nullptr)
  441. {
  442. if (environmentTraceLevel > 0)
  443. PrintLog("Failed to lock environment %s", constEnv->getPath());
  444. throw MakeStringException(-1, "Failed to get a lock on environment /%s", constEnv->getPath());
  445. }
  446. //copy locked environment to const environment
  447. Owned<IPropertyTree> pSrc = &getPTree();
  448. Owned<IPropertyTree> pDst = conn2->queryRoot()->getBranch(nullptr);
  449. // JCS - I think it could (and would be more efficient if it had kept the original read lock connection to Env
  450. // - instead of using NewEnv as lock point, still work on copy, then changeMode of original connect
  451. // - as opposed to current scheme, where it recoonects in write mode and has to lazy fetch original env to update.
  452. // ensures pDst is equal to pSrc, whilst minimizing changes to pDst
  453. try { synchronizePTree(pDst, pSrc); }
  454. catch (IException *) { conn2->rollback(); throw; }
  455. conn2->commit();
  456. }
  457. else
  458. {
  459. Owned<IRemoteConnection> conn = c->getConnection();
  460. conn->commit();
  461. }
  462. }
  463. void CLockedEnvironment::rollback()
  464. {
  465. if (constEnv)
  466. {
  467. //get a lock on const environment momentarily
  468. const unsigned int mode2 = RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE | RTM_LOCK_SUB;
  469. Owned<IRemoteConnection> conn2 = querySDS().connect(constEnv->getPath(), myProcessSession(), mode2, SDS_LOCK_TIMEOUT);
  470. if (conn2 == nullptr)
  471. {
  472. if (environmentTraceLevel > 0)
  473. PrintLog("Failed to lock environment %s", constEnv->getPath());
  474. throw MakeStringException(-1, "Failed to get a lock on environment /%s", constEnv->getPath());
  475. }
  476. //copy const environment to locked environment (as it stands now) again losing any changes we made
  477. Owned<IPropertyTree> pSrc = conn2->getRoot();
  478. Owned<IPropertyTree> pDst = &getPTree();
  479. pDst->removeTree( pDst->queryPropTree("Hardware") );
  480. pDst->removeTree( pDst->queryPropTree("Software") );
  481. pDst->removeTree( pDst->queryPropTree("Programs") );
  482. pDst->removeTree( pDst->queryPropTree("Data") );
  483. mergePTree(pDst, pSrc);
  484. conn2->rollback();
  485. }
  486. else
  487. {
  488. Owned<IRemoteConnection> conn = c->getConnection();
  489. conn->rollback();
  490. }
  491. }
  492. //==========================================================================================
  493. // the following class implements notification handler for subscription to dali for environment
  494. // updates by other clients and is used by environment factory below. This also serves as
  495. // a sample self-contained implementation that can be easily tailored for other purposes.
  496. //==========================================================================================
  497. class CSdsSubscription : implements ISDSSubscription, public CInterface
  498. {
  499. public:
  500. CSdsSubscription(IEnvironmentFactory &_factory) : factory(_factory)
  501. {
  502. m_constEnvUpdated = false;
  503. sub_id = factory.subscribe(this);
  504. }
  505. virtual ~CSdsSubscription()
  506. {
  507. /* note that ideally, we would make this class automatically
  508. unsubscribe in this destructor. However, underlying dali client
  509. layer (CDaliSubscriptionManagerStub) links to this object and so
  510. object would not get destroyed just by an application releasing it.
  511. The application either needs to explicitly unsubscribe or close
  512. the environment which unsubscribes during close down. */
  513. }
  514. void unsubscribe()
  515. {
  516. synchronized block(m_mutexEnv);
  517. if (sub_id)
  518. {
  519. factory.unsubscribe(sub_id);
  520. sub_id = 0;
  521. }
  522. }
  523. IMPLEMENT_IINTERFACE;
  524. //another client (like configenv) may have updated the environment and we got notified
  525. //(thanks to our subscription) but don't just reload it yet since this notification is sent on
  526. //another thread asynchronously and we may be actively working with the old environment. Just
  527. //invoke handleEnvironmentChange() when we are ready to invalidate cache in environment factory.
  528. //
  529. void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen=0, const void *valueData=nullptr)
  530. {
  531. DBGLOG("Environment was updated by another client of Dali server. Invalidating cache.\n");
  532. synchronized block(m_mutexEnv);
  533. m_constEnvUpdated = true;
  534. }
  535. void handleEnvironmentChange()
  536. {
  537. synchronized block(m_mutexEnv);
  538. if (m_constEnvUpdated)
  539. {
  540. Owned<IConstEnvironment> constEnv = factory.openEnvironment();
  541. constEnv->clearCache();
  542. m_constEnvUpdated = false;
  543. }
  544. }
  545. private:
  546. SubscriptionId sub_id;
  547. Mutex m_mutexEnv;
  548. bool m_constEnvUpdated;
  549. IEnvironmentFactory &factory;
  550. };
  551. //==========================================================================================
  552. class CEnvironmentFactory : public CInterface,
  553. implements IEnvironmentFactory, implements IDaliClientShutdown
  554. {
  555. public:
  556. IMPLEMENT_IINTERFACE;
  557. typedef ArrayOf<SubscriptionId> SubscriptionIDs;
  558. SubscriptionIDs subIDs;
  559. Mutex mutex;
  560. Owned<CSdsSubscription> subscription;
  561. CEnvironmentFactory()
  562. {
  563. }
  564. virtual void clientShutdown();
  565. virtual ~CEnvironmentFactory()
  566. {
  567. close(); //just in case it was not explicitly closed
  568. }
  569. virtual IConstEnvironment* openEnvironment()
  570. {
  571. synchronized procedure(mutex);
  572. if (!cache)
  573. {
  574. Owned<IRemoteConnection> conn = querySDS().connect("/Environment", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  575. if (conn)
  576. cache.setown(new CLocalEnvironment(conn));
  577. }
  578. if (!cache)
  579. throw MakeStringException(0, "Failed to get environment information");
  580. return cache.getLink();
  581. }
  582. virtual IEnvironment* updateEnvironment()
  583. {
  584. Owned<IConstEnvironment> pConstEnv = openEnvironment();
  585. synchronized procedure(mutex);
  586. return &pConstEnv->lock();
  587. }
  588. virtual IEnvironment * loadLocalEnvironmentFile(const char * filename)
  589. {
  590. Owned<IPropertyTree> ptree = createPTreeFromXMLFile(filename, ipt_lowmem);
  591. Owned<CLocalEnvironment> pLocalEnv = new CLocalEnvironment(nullptr, ptree);
  592. return new CLockedEnvironment(pLocalEnv);
  593. }
  594. virtual IEnvironment * loadLocalEnvironment(const char * xml)
  595. {
  596. Owned<IPropertyTree> ptree = createPTreeFromXMLString(xml, ipt_lowmem);
  597. Owned<CLocalEnvironment> pLocalEnv = new CLocalEnvironment(nullptr, ptree);
  598. return new CLockedEnvironment(pLocalEnv);
  599. }
  600. void close()
  601. {
  602. SubscriptionIDs copySubIDs;
  603. {
  604. synchronized procedure(mutex);
  605. cache.clear();
  606. //save the active subscriptions in another array
  607. //so they can be unsubscribed without causing deadlock
  608. // since ~CSdsSubscription() would ask us to unsubscribe the
  609. //same requiring a mutex lock (copy is a little price for this
  610. //normally small/empty array).
  611. //
  612. ForEachItemIn(i, subIDs)
  613. copySubIDs.append(subIDs.item(i));
  614. subIDs.kill();
  615. }
  616. //now unsubscribe all outstanding subscriptions
  617. //
  618. subscription.clear();
  619. ForEachItemIn(i, copySubIDs)
  620. querySDS().unsubscribe( copySubIDs.item(i) );
  621. }
  622. virtual SubscriptionId subscribe(ISDSSubscription* pSubHandler)
  623. {
  624. SubscriptionId sub_id = querySDS().subscribe("/Environment", *pSubHandler);
  625. synchronized procedure(mutex);
  626. subIDs.append(sub_id);
  627. return sub_id;
  628. }
  629. virtual void unsubscribe(SubscriptionId sub_id)
  630. {
  631. synchronized procedure(mutex);
  632. aindex_t i = subIDs.find(sub_id);
  633. if (i != NotFound)
  634. {
  635. querySDS().unsubscribe(sub_id);
  636. subIDs.remove(i);
  637. }
  638. }
  639. virtual void validateCache()
  640. {
  641. if (!subscription)
  642. subscription.setown( new CSdsSubscription(*this) );
  643. subscription->handleEnvironmentChange();
  644. }
  645. private:
  646. IRemoteConnection* connect(const char *xpath, unsigned flags)
  647. {
  648. return querySDS().connect(xpath, myProcessSession(), flags, SDS_LOCK_TIMEOUT);
  649. }
  650. };
  651. static CEnvironmentFactory *factory=nullptr;
  652. void CEnvironmentFactory::clientShutdown()
  653. {
  654. closeEnvironment();
  655. }
  656. MODULE_INIT(INIT_PRIORITY_ENV_ENVIRONMENT)
  657. {
  658. return true;
  659. }
  660. MODULE_EXIT()
  661. {
  662. ::Release(factory);
  663. }
  664. //==========================================================================================
  665. class CConstEnvBase : public CInterface
  666. {
  667. protected:
  668. const CLocalEnvironment* env; // Not linked - would be circular....
  669. // That could cause problems
  670. Linked<IPropertyTree> root;
  671. public:
  672. CConstEnvBase(const CLocalEnvironment* _env, IPropertyTree *_root)
  673. : env(_env), root(_root)
  674. {
  675. }
  676. IStringVal& getXML(IStringVal &str) const
  677. {
  678. StringBuffer x;
  679. toXML(root->queryBranch("."), x);
  680. str.set(x.str());
  681. return str;
  682. };
  683. IStringVal& getName(IStringVal &str) const
  684. {
  685. str.set(root->queryProp("@name"));
  686. return str;
  687. }
  688. IPropertyTree& getPTree() const
  689. {
  690. return *LINK(root);
  691. }
  692. };
  693. #define IMPLEMENT_ICONSTENVBASE \
  694. virtual IStringVal& getXML(IStringVal &str) const { return CConstEnvBase::getXML(str); } \
  695. virtual IStringVal& getName(IStringVal &str) const { return CConstEnvBase::getName(str); } \
  696. virtual IPropertyTree& getPTree() const { return CConstEnvBase::getPTree(); }
  697. //==========================================================================================
  698. class CConstDomainInfo : public CConstEnvBase, implements IConstDomainInfo
  699. {
  700. public:
  701. IMPLEMENT_IINTERFACE;
  702. IMPLEMENT_ICONSTENVBASE;
  703. CConstDomainInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  704. virtual void getAccountInfo(IStringVal &name, IStringVal &pw) const
  705. {
  706. if (root->hasProp("@username"))
  707. name.set(root->queryProp("@username"));
  708. else
  709. name.clear();
  710. if (root->hasProp("@password"))
  711. {
  712. StringBuffer pwd;
  713. decrypt(pwd, root->queryProp("@password"));
  714. pw.set(pwd.str());
  715. }
  716. else
  717. pw.clear();
  718. }
  719. virtual void getSnmpSecurityString(IStringVal & securityString) const
  720. {
  721. if (root->hasProp("@snmpSecurityString"))
  722. {
  723. StringBuffer sec_string;
  724. decrypt(sec_string, root->queryProp("@snmpSecurityString"));
  725. securityString.set(sec_string.str());
  726. }
  727. else
  728. securityString.set("");
  729. }
  730. virtual void getSSHAccountInfo(IStringVal &name, IStringVal &sshKeyFile, IStringVal& sshKeyPassphrase) const
  731. {
  732. if (root->hasProp("@username"))
  733. name.set(root->queryProp("@username"));
  734. else
  735. name.clear();
  736. if (root->hasProp("@sshKeyFile"))
  737. sshKeyFile.set(root->queryProp("@sshKeyFile"));
  738. else
  739. sshKeyFile.clear();
  740. if (root->hasProp("@sshKeyPassphrase"))
  741. sshKeyPassphrase.set(root->queryProp("@sshKeyPassphrase"));
  742. else
  743. sshKeyPassphrase.clear();
  744. }
  745. };
  746. //==========================================================================================
  747. struct mapOsEnums { EnvMachineOS val; const char *str; };
  748. static EnvMachineOS getEnum(IPropertyTree *p, const char *propname, mapOsEnums *map)
  749. {
  750. const char *v = p->queryProp(propname);
  751. if (v && *v)
  752. {
  753. while (map->str)
  754. {
  755. if (stricmp(v, map->str)==0)
  756. return map->val;
  757. map++;
  758. }
  759. throw MakeStringException(0, "Unknown operating system: \"%s\"", v);
  760. }
  761. return MachineOsUnknown;
  762. }
  763. struct mapStateEnums { EnvMachineState val; const char *str; };
  764. static EnvMachineState getEnum(IPropertyTree *p, const char *propname, mapStateEnums *map)
  765. {
  766. const char *v = p->queryProp(propname);
  767. if (v && *v)
  768. {
  769. while (map->str)
  770. {
  771. if (stricmp(v, map->str)==0)
  772. return map->val;
  773. map++;
  774. }
  775. assertex(!"Unexpected value in getEnum");
  776. }
  777. return MachineStateUnknown;
  778. }
  779. mapOsEnums OperatingSystems[] = {
  780. { MachineOsW2K, "W2K" },
  781. { MachineOsSolaris, "solaris" },
  782. { MachineOsLinux, "linux" },
  783. { MachineOsSize, nullptr }
  784. };
  785. mapStateEnums MachineStates[] = {
  786. { MachineStateAvailable, "Available" },
  787. { MachineStateUnavailable, "Unavailable" },
  788. { MachineStateUnknown, "Unknown" }
  789. };
  790. //==========================================================================================
  791. class CConstMachineInfo : public CConstEnvBase, implements IConstMachineInfo
  792. {
  793. public:
  794. IMPLEMENT_IINTERFACE;
  795. IMPLEMENT_ICONSTENVBASE;
  796. CConstMachineInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  797. virtual IConstDomainInfo* getDomain() const
  798. {
  799. return env->getDomain(root->queryProp("@domain"));
  800. }
  801. virtual IStringVal& getNetAddress(IStringVal &str) const
  802. {
  803. str.set(root->queryProp("@netAddress"));
  804. return str;
  805. }
  806. virtual IStringVal& getDescription(IStringVal &str) const
  807. {
  808. UNIMPLEMENTED;
  809. }
  810. virtual unsigned getNicSpeedMbitSec() const
  811. {
  812. const char * v = root->queryProp("@nicSpeed");
  813. if (v && *v)
  814. return atoi(v);
  815. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  816. if (type)
  817. return type->getNicSpeedMbitSec();
  818. return 0;
  819. }
  820. virtual EnvMachineOS getOS() const
  821. {
  822. EnvMachineOS os = getEnum(root, "@opSys", OperatingSystems);
  823. if (os != MachineOsUnknown)
  824. return os;
  825. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  826. if (type)
  827. return type->getOS();
  828. return MachineOsUnknown;
  829. }
  830. virtual EnvMachineState getState() const
  831. {
  832. return getEnum(root, "@state", MachineStates);
  833. }
  834. };
  835. //==========================================================================================
  836. class CConstComputerTypeInfo : public CConstEnvBase, implements IConstComputerTypeInfo
  837. {
  838. public:
  839. IMPLEMENT_IINTERFACE;
  840. IMPLEMENT_ICONSTENVBASE;
  841. CConstComputerTypeInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  842. virtual EnvMachineOS getOS() const
  843. {
  844. EnvMachineOS os = getEnum(root, "@opSys", OperatingSystems);
  845. if (os != MachineOsUnknown)
  846. return os;
  847. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  848. if (type && (type.get() != this))
  849. return type->getOS();
  850. return MachineOsUnknown;
  851. }
  852. virtual unsigned getNicSpeedMbitSec() const
  853. {
  854. const char * v = root->queryProp("@nicSpeed");
  855. if (v && *v)
  856. return atoi(v);
  857. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  858. if (type && (type.get() != this))
  859. return type->getNicSpeedMbitSec();
  860. return 0;
  861. }
  862. };
  863. //==========================================================================================
  864. class CConstInstanceInfo : public CConstEnvBase, implements IConstInstanceInfo
  865. {
  866. public:
  867. IMPLEMENT_IINTERFACE;
  868. IMPLEMENT_ICONSTENVBASE;
  869. CConstInstanceInfo(const CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root)
  870. {
  871. }
  872. virtual IConstMachineInfo * getMachine() const
  873. {
  874. return env->getMachine(root->queryProp("@computer"));
  875. }
  876. virtual IStringVal & getEndPoint(IStringVal & str) const
  877. {
  878. SCMStringBuffer ep;
  879. Owned<IConstMachineInfo> machine = getMachine();
  880. if (machine)
  881. {
  882. machine->getNetAddress(ep);
  883. const char *port = root->queryProp("@port");
  884. if (port)
  885. ep.s.append(':').append(port);
  886. }
  887. str.set(ep.str());
  888. return str;
  889. }
  890. virtual IStringVal & getExecutableDirectory(IStringVal & str) const
  891. {
  892. // this is the deploy directory so uses local path separators (I suspect this call is LEGACY now)
  893. SCMStringBuffer ep;
  894. Owned<IConstMachineInfo> machine = getMachine();
  895. if (machine)
  896. {
  897. machine->getNetAddress(ep);
  898. ep.s.insert(0, PATHSEPSTR PATHSEPSTR);
  899. }
  900. ep.s.append(PATHSEPCHAR).append(root->queryProp("@directory"));
  901. str.set(ep.str());
  902. return str;
  903. }
  904. virtual IStringVal & getDirectory(IStringVal & str) const
  905. {
  906. str.set(root->queryProp("@directory"));
  907. return str;
  908. }
  909. virtual bool doGetRunInfo(IStringVal & progpath, IStringVal & workdir, const char *defprogname, bool useprog) const
  910. {
  911. // this is remote path i.e. path should match *target* nodes format
  912. Owned<IConstMachineInfo> machine = getMachine();
  913. if (!machine)
  914. return false;
  915. char psep;
  916. bool appendexe;
  917. switch (machine->getOS())
  918. {
  919. case MachineOsSolaris:
  920. case MachineOsLinux:
  921. psep = '/';
  922. appendexe = false;
  923. break;
  924. default:
  925. psep = '\\';
  926. appendexe = true;
  927. }
  928. StringBuffer tmp;
  929. const char *program = useprog?root->queryProp("@program"):nullptr; // if program specified assume absolute
  930. if (!program||!*program)
  931. {
  932. SCMStringBuffer ep;
  933. machine->getNetAddress(ep);
  934. const char *dir = root->queryProp("@directory");
  935. if (dir)
  936. {
  937. if (isPathSepChar(*dir))
  938. dir++;
  939. if (!*dir)
  940. return false;
  941. tmp.append(psep).append(psep).append(ep.s).append(psep);
  942. do {
  943. if (isPathSepChar(*dir))
  944. tmp.append(psep);
  945. else
  946. tmp.append(*dir);
  947. dir++;
  948. } while (*dir);
  949. if (!isPathSepChar(tmp.charAt(tmp.length()-1)))
  950. tmp.append(psep);
  951. tmp.append(defprogname);
  952. size32_t l = strlen(defprogname);
  953. if (appendexe&&((l<5)||(stricmp(defprogname+l-4,".exe")!=0)))
  954. tmp.append(".exe");
  955. }
  956. program = tmp.str();
  957. }
  958. progpath.set(program);
  959. const char *workd = root->queryProp("@workdir"); // if program specified assume absolute
  960. workdir.set(workd?workd:"");
  961. return true;
  962. }
  963. virtual bool getRunInfo(IStringVal & progpath, IStringVal & workdir, const char *defprogname) const
  964. {
  965. return doGetRunInfo(progpath,workdir,defprogname,true);
  966. }
  967. virtual unsigned getPort() const
  968. {
  969. return root->getPropInt("@port", 0);
  970. }
  971. };
  972. class CConstDropZoneServerInfo : public CConstEnvBase, implements IConstDropZoneServerInfo
  973. {
  974. public:
  975. IMPLEMENT_IINTERFACE;
  976. IMPLEMENT_ICONSTENVBASE;
  977. CConstDropZoneServerInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root), prop(root) {}
  978. virtual StringBuffer & getName(StringBuffer & name) const
  979. {
  980. name.append(prop->queryProp("@name"));
  981. return name;
  982. }
  983. virtual StringBuffer & getServer(StringBuffer & server) const
  984. {
  985. server.append(prop->queryProp("@server"));
  986. return server;
  987. }
  988. private:
  989. IPropertyTree * prop;
  990. };
  991. class CConstDropZoneInfo : public CConstEnvBase, implements IConstDropZoneInfo
  992. {
  993. public:
  994. IMPLEMENT_IINTERFACE;
  995. IMPLEMENT_ICONSTENVBASE;
  996. CConstDropZoneInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root)
  997. {
  998. getStandardPosixPath(posixPath, root->queryProp("@directory"));
  999. }
  1000. virtual IStringVal& getComputerName(IStringVal &str) const
  1001. {
  1002. str.set(root->queryProp("@computer"));
  1003. return str;
  1004. }
  1005. virtual IStringVal& getDescription(IStringVal &str) const
  1006. {
  1007. str.set(root->queryProp("@description"));
  1008. return str;
  1009. }
  1010. virtual IStringVal& getDirectory(IStringVal &str) const
  1011. {
  1012. str.set(posixPath.str());
  1013. return str;
  1014. }
  1015. virtual IStringVal& getUMask(IStringVal &str) const
  1016. {
  1017. if (root->hasProp("@umask"))
  1018. str.set(root->queryProp("@umask"));
  1019. return str;
  1020. }
  1021. virtual bool isECLWatchVisible() const
  1022. {
  1023. return root->getPropBool("@ECLWatchVisible", true);
  1024. }
  1025. virtual IConstDropZoneServerInfoIterator * getServers() const
  1026. {
  1027. return new CConstDropZoneServerInfoIterator(this);
  1028. }
  1029. private:
  1030. StringBuffer posixPath;
  1031. };
  1032. class CConstDfuQueueInfo : public CConstEnvBase, implements IConstDfuQueueInfo
  1033. {
  1034. public:
  1035. IMPLEMENT_IINTERFACE;
  1036. IMPLEMENT_ICONSTENVBASE;
  1037. CConstDfuQueueInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root)
  1038. {
  1039. }
  1040. virtual IStringVal& getDfuQueueName(IStringVal &str) const
  1041. {
  1042. str.set(root->queryProp("@queue"));
  1043. return str;
  1044. }
  1045. };
  1046. class CConstSparkThorInfo : public CConstEnvBase, implements IConstSparkThorInfo
  1047. {
  1048. public:
  1049. IMPLEMENT_IINTERFACE;
  1050. IMPLEMENT_ICONSTENVBASE;
  1051. CConstSparkThorInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  1052. virtual IStringVal &getBuild(IStringVal &str) const
  1053. {
  1054. str.set(root->queryProp("@build"));
  1055. return str;
  1056. }
  1057. virtual IStringVal &getThorClusterName(IStringVal &str) const
  1058. {
  1059. str.set(root->queryProp("@ThorClusterName"));
  1060. return str;
  1061. }
  1062. virtual unsigned getSparkExecutorCores() const
  1063. {
  1064. return root->getPropInt("@SPARK_EXECUTOR_CORES", 0);
  1065. }
  1066. virtual unsigned long getSparkExecutorMemory() const
  1067. {
  1068. return readSizeSetting(root->queryProp("@SPARK_EXECUTOR_MEMORY"), 0);
  1069. }
  1070. virtual unsigned getSparkMasterPort() const
  1071. {
  1072. return root->getPropInt("@SPARK_MASTER_PORT", 0);
  1073. }
  1074. virtual unsigned getSparkMasterWebUIPort() const
  1075. {
  1076. return root->getPropInt("@SPARK_MASTER_WEBUI_PORT", 0);
  1077. }
  1078. virtual unsigned getSparkWorkerCores() const
  1079. {
  1080. return root->getPropInt("@SPARK_WORKER_CORES", 0);
  1081. }
  1082. virtual unsigned long getSparkWorkerMemory() const
  1083. {
  1084. return readSizeSetting(root->queryProp("@SPARK_WORKER_MEMORY"), 0);
  1085. }
  1086. virtual unsigned getSparkWorkerPort() const
  1087. {
  1088. return root->getPropInt("@SPARK_WORKER_PORT", 0);
  1089. }
  1090. virtual IConstInstanceInfoIterator *getInstanceIterator() const
  1091. {
  1092. return new CConstInstanceInfoIterator(env, root->getElements("Instance"));
  1093. }
  1094. };
  1095. #if 0
  1096. //==========================================================================================
  1097. class CConstProcessInfo : public CConstEnvBase, implements IConstProcessInfo
  1098. {
  1099. IArrayOf<IConstInstanceInfo> w;
  1100. CArrayIteratorOf<IInterface, IIterator> it;
  1101. public:
  1102. IMPLEMENT_IINTERFACE;
  1103. IMPLEMENT_ICONSTENVBASE;
  1104. CConstProcessInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root), it(w)
  1105. {
  1106. Owned<IPropertyTreeIterator> _it = root->getElements("*"); // MORE - should be instance
  1107. for (_it->first(); _it->isValid(); _it->next())
  1108. {
  1109. IPropertyTree *rp = &_it->query();
  1110. w.append(*new CConstInstanceInfo(env, rp)); // CConstInstanceInfo will link rp
  1111. }
  1112. }
  1113. bool first() { return it.first(); }
  1114. bool isValid() { return it.isValid(); }
  1115. bool next() { return it.next(); }
  1116. IConstInstanceInfo & query() { return (IConstInstanceInfo &) it.query();}
  1117. virtual IConstInstanceInfo * getInstance(const char *domain)
  1118. {
  1119. for (int pass=0; pass<2; pass++)
  1120. {
  1121. ForEachItemIn(idx, w)
  1122. {
  1123. Owned<IConstMachineInfo> m = w.item(idx).getMachine();
  1124. if (m)
  1125. {
  1126. Owned<IConstDomainInfo> dm = m->getDomain();
  1127. if (dm)
  1128. {
  1129. StringBuffer thisdomain;
  1130. //dm->getName(StringBufferAdaptor(thisdomain)); // confuses g++
  1131. StringBufferAdaptor strval(thisdomain);
  1132. dm->getName(strval);
  1133. if (thisdomain.length() && strcmp(domain, thisdomain.str())==0)
  1134. return LINK(&w.item(idx));
  1135. }
  1136. }
  1137. }
  1138. }
  1139. return nullptr;
  1140. }
  1141. };
  1142. #endif
  1143. class CConstDaFileSrvInfo : public CConstEnvBase, implements IConstDaFileSrvInfo
  1144. {
  1145. public:
  1146. IMPLEMENT_IINTERFACE;
  1147. IMPLEMENT_ICONSTENVBASE;
  1148. CConstDaFileSrvInfo(const CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root)
  1149. {
  1150. }
  1151. virtual const char *getName() const override
  1152. {
  1153. return root->queryProp("@name");
  1154. }
  1155. virtual unsigned getPort() const override
  1156. {
  1157. return root->getPropInt("@rowServicePort");
  1158. }
  1159. virtual bool getSecure() const override
  1160. {
  1161. return root->getPropBool("@rowServiceSSL");
  1162. }
  1163. };
  1164. //==========================================================================================
  1165. CLocalEnvironment::CLocalEnvironment(const char* environmentFile)
  1166. {
  1167. if (environmentFile && *environmentFile)
  1168. {
  1169. IPropertyTree* root = createPTreeFromXMLFile(environmentFile);
  1170. if (root)
  1171. p.set(root);
  1172. }
  1173. init();
  1174. }
  1175. CLocalEnvironment::CLocalEnvironment(IRemoteConnection *_conn, IPropertyTree* root/*=nullptr*/,
  1176. const char* path/*="/Environment"*/)
  1177. : xPath(path)
  1178. {
  1179. conn.set(_conn);
  1180. if (root)
  1181. p.set(root);
  1182. else
  1183. p.setown(conn->getRoot());
  1184. init();
  1185. }
  1186. void CLocalEnvironment::init()
  1187. {
  1188. machineCacheBuilt = false;
  1189. dropZoneCacheBuilt = false;
  1190. sparkThorCacheBuilt = false;
  1191. dfuQueueCacheBuilt = false;
  1192. numOfMachines = 0;
  1193. numOfDropZones = 0;
  1194. numOfSparkThors = 0;
  1195. numOfDfuQueues = 0;
  1196. isDropZoneRestrictionLoaded = false;
  1197. clusterGroupKeyNameCache = false;
  1198. ::getFileAccessUrl(fileAccessUrl);
  1199. }
  1200. CLocalEnvironment::~CLocalEnvironment()
  1201. {
  1202. if (conn)
  1203. conn->rollback();
  1204. }
  1205. IEnvironment& CLocalEnvironment::lock() const
  1206. {
  1207. return *new CLockedEnvironment((CLocalEnvironment*)this);
  1208. }
  1209. IStringVal & CLocalEnvironment::getName(IStringVal & str) const
  1210. {
  1211. synchronized procedure(safeCache);
  1212. str.set(p->queryProp("@name"));
  1213. return str;
  1214. }
  1215. IStringVal & CLocalEnvironment::getXML(IStringVal & str) const
  1216. {
  1217. StringBuffer xml;
  1218. {
  1219. synchronized procedure(safeCache);
  1220. toXML(p->queryBranch("."), xml);
  1221. }
  1222. str.set(xml.str());
  1223. return str;
  1224. }
  1225. IPropertyTree & CLocalEnvironment::getPTree() const
  1226. {
  1227. synchronized procedure(safeCache);
  1228. return *LINK(p);
  1229. }
  1230. IConstEnvBase * CLocalEnvironment::getCache(const char *path) const
  1231. {
  1232. IConstEnvBase * ret = cache.getValue(path);
  1233. ::Link(ret);
  1234. return ret;
  1235. }
  1236. void CLocalEnvironment::setCache(const char *path, IConstEnvBase *value) const
  1237. {
  1238. cache.setValue(path, value);
  1239. }
  1240. IConstDomainInfo * CLocalEnvironment::getDomain(const char * name) const
  1241. {
  1242. if (!name)
  1243. return nullptr;
  1244. StringBuffer xpath;
  1245. xpath.appendf("Hardware/Domain[@name=\"%s\"]", name);
  1246. synchronized procedure(safeCache);
  1247. IConstEnvBase *cached = getCache(xpath.str());
  1248. if (!cached)
  1249. {
  1250. IPropertyTree *d = p->queryPropTree(xpath.str());
  1251. if (!d)
  1252. return nullptr;
  1253. cached = new CConstDomainInfo((CLocalEnvironment *) this, d);
  1254. setCache(xpath.str(), cached);
  1255. }
  1256. return (IConstDomainInfo *) cached;
  1257. }
  1258. void CLocalEnvironment::buildMachineCache() const
  1259. {
  1260. synchronized procedure(safeCache);
  1261. if (!machineCacheBuilt)
  1262. {
  1263. Owned<IPropertyTreeIterator> it = p->getElements("Hardware/Computer");
  1264. ForEach(*it)
  1265. {
  1266. const char *name = it->query().queryProp("@name");
  1267. if (name)
  1268. {
  1269. StringBuffer x("Hardware/Computer[@name=\"");
  1270. x.append(name).append("\"]");
  1271. Owned<IConstEnvBase> cached = new CConstMachineInfo((CLocalEnvironment *) this, &it->query());
  1272. cache.setValue(x.str(), cached);
  1273. }
  1274. const char * netAddress = it->query().queryProp("@netAddress");
  1275. if (netAddress)
  1276. {
  1277. StringBuffer x("Hardware/Computer[@netAddress=\"");
  1278. x.append(netAddress).append("\"]");
  1279. Owned<IConstEnvBase> cached = new CConstMachineInfo((CLocalEnvironment *) this, &it->query());
  1280. cache.setValue(x.str(), cached);
  1281. IpAddress ip;
  1282. ip.ipset(netAddress);
  1283. if (ip.isLocal())
  1284. cache.setValue("Hardware/Computer[@netAddress=\".\"]", cached);
  1285. }
  1286. numOfMachines++;
  1287. StringBuffer x("Hardware/Computer[@id=\"");
  1288. x.append(MACHINE_PREFIX).append(numOfMachines).append("\"]");
  1289. Owned<IConstEnvBase> cached = new CConstMachineInfo((CLocalEnvironment *) this, &it->query());
  1290. cache.setValue(x.str(), cached);
  1291. }
  1292. machineCacheBuilt = true;
  1293. }
  1294. }
  1295. void CLocalEnvironment::buildDropZoneCache() const
  1296. {
  1297. synchronized procedure(safeCache);
  1298. if (!dropZoneCacheBuilt)
  1299. {
  1300. Owned<IPropertyTreeIterator> it = p->getElements("Software/DropZone");
  1301. ForEach(*it)
  1302. {
  1303. const char *name = it->query().queryProp("@name");
  1304. if (name)
  1305. {
  1306. StringBuffer x("Software/DropZone[@name=\"");
  1307. x.append(name).append("\"]");
  1308. Owned<IConstEnvBase> cached = new CConstDropZoneInfo((CLocalEnvironment *) this, &it->query());
  1309. cache.setValue(x.str(), cached);
  1310. }
  1311. numOfDropZones++;
  1312. StringBuffer x("Software/DropZone[@id=\"");
  1313. x.append(DROPZONE_SUFFIX).append(numOfDropZones).append("\"]");
  1314. Owned<IConstEnvBase> cached = new CConstDropZoneInfo((CLocalEnvironment *) this, &it->query());
  1315. cache.setValue(x.str(), cached);
  1316. }
  1317. dropZoneCacheBuilt = true;
  1318. }
  1319. }
  1320. void CLocalEnvironment::buildDfuQueueCache() const
  1321. {
  1322. synchronized procedure(safeCache);
  1323. if (!dfuQueueCacheBuilt)
  1324. {
  1325. Owned<IPropertyTreeIterator> it = p->getElements("Software/DfuServerProcess");
  1326. ForEach(*it)
  1327. {
  1328. const char *qname = it->query().queryProp("@queue");
  1329. if (qname)
  1330. {
  1331. StringBuffer x("Software/DfuQueue[@qname=\"");
  1332. x.append(qname).append("\"]");
  1333. Owned<IConstEnvBase> cached = new CConstDfuQueueInfo((CLocalEnvironment *) this, &it->query());
  1334. cache.setValue(x.str(), cached);
  1335. }
  1336. numOfDfuQueues++;
  1337. StringBuffer x("Software/DfuQueue[@id=\"");
  1338. x.append(numOfDfuQueues).append("\"]");
  1339. Owned<IConstEnvBase> cached = new CConstDfuQueueInfo((CLocalEnvironment *) this, &it->query());
  1340. cache.setValue(x.str(), cached);
  1341. }
  1342. dfuQueueCacheBuilt = true;
  1343. }
  1344. }
  1345. IConstComputerTypeInfo * CLocalEnvironment::getComputerType(const char * name) const
  1346. {
  1347. if (!name)
  1348. return nullptr;
  1349. StringBuffer xpath;
  1350. xpath.appendf("Hardware/ComputerType[@name=\"%s\"]", name);
  1351. synchronized procedure(safeCache);
  1352. IConstEnvBase *cached = getCache(xpath.str());
  1353. if (!cached)
  1354. {
  1355. IPropertyTree *d = p->queryPropTree(xpath.str());
  1356. if (!d)
  1357. return nullptr;
  1358. cached = new CConstComputerTypeInfo((CLocalEnvironment *) this, d);
  1359. setCache(xpath.str(), cached);
  1360. }
  1361. return (CConstComputerTypeInfo *) cached;
  1362. }
  1363. IConstMachineInfo * CLocalEnvironment::getMachine(const char * name) const
  1364. {
  1365. if (!name)
  1366. return nullptr;
  1367. buildMachineCache();
  1368. StringBuffer xpath;
  1369. xpath.appendf("Hardware/Computer[@name=\"%s\"]", name);
  1370. synchronized procedure(safeCache);
  1371. IConstEnvBase *cached = getCache(xpath.str());
  1372. if (!cached)
  1373. {
  1374. IPropertyTree *d = p->queryPropTree(xpath.str());
  1375. if (!d)
  1376. return nullptr;
  1377. cached = new CConstMachineInfo((CLocalEnvironment *) this, d);
  1378. setCache(xpath.str(), cached);
  1379. }
  1380. return (CConstMachineInfo *) cached;
  1381. }
  1382. IConstMachineInfo * CLocalEnvironment::getMachineByAddress(const char * machineIp) const
  1383. {
  1384. if (!machineIp)
  1385. return nullptr;
  1386. buildMachineCache();
  1387. Owned<IPropertyTreeIterator> iter;
  1388. StringBuffer xpath;
  1389. xpath.appendf("Hardware/Computer[@netAddress=\"%s\"]", machineIp);
  1390. synchronized procedure(safeCache);
  1391. IConstEnvBase *cached = getCache(xpath.str());
  1392. if (!cached)
  1393. {
  1394. IPropertyTree *d = p->queryPropTree(xpath.str());
  1395. if (!d)
  1396. {
  1397. // I suspect not in the original spirit of this but look for resolved IP
  1398. Owned<IPropertyTreeIterator> iter = p->getElements("Hardware/Computer");
  1399. IpAddress ip;
  1400. ip.ipset(machineIp);
  1401. ForEach(*iter)
  1402. {
  1403. IPropertyTree &computer = iter->query();
  1404. IpAddress ip2;
  1405. const char *ips = computer.queryProp("@netAddress");
  1406. if (ips&&*ips)
  1407. {
  1408. ip2.ipset(ips);
  1409. if (ip.ipequals(ip2))
  1410. {
  1411. d = &computer;
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. }
  1417. if (!d)
  1418. return nullptr;
  1419. StringBuffer xpath1;
  1420. xpath1.appendf("Hardware/Computer[@name=\"%s\"]", d->queryProp("@name"));
  1421. cached = getCache(xpath1.str());
  1422. if (!cached)
  1423. {
  1424. cached = new CConstMachineInfo((CLocalEnvironment *) this, d);
  1425. setCache(xpath1.str(), cached);
  1426. setCache(xpath.str(), cached);
  1427. }
  1428. }
  1429. return (CConstMachineInfo *) cached;
  1430. }
  1431. IConstMachineInfo * CLocalEnvironment::getMachineByIndex(unsigned index) const
  1432. {
  1433. if (!numOfMachines || (index == 0))
  1434. return nullptr;
  1435. buildMachineCache();
  1436. if (index > numOfMachines)
  1437. return nullptr;
  1438. StringBuffer xpath("Hardware/Computer[@id=\"");
  1439. xpath.append(MACHINE_PREFIX).append(index).append("\"]");
  1440. synchronized procedure(safeCache);
  1441. return (IConstMachineInfo *) getCache(xpath.str());
  1442. }
  1443. IConstMachineInfo * CLocalEnvironment::getMachineForLocalHost() const
  1444. {
  1445. buildMachineCache();
  1446. synchronized procedure(safeCache);
  1447. return (CConstMachineInfo *) getCache("Hardware/Computer[@netAddress=\".\"]");
  1448. }
  1449. IConstDropZoneInfo * CLocalEnvironment::getDropZone(const char * name) const
  1450. {
  1451. if (!name)
  1452. return nullptr;
  1453. buildDropZoneCache();
  1454. VStringBuffer xpath("Software/DropZone[@name=\"%s\"]", name);
  1455. synchronized procedure(safeCache);
  1456. return (CConstDropZoneInfo *) getCache(xpath.str());
  1457. }
  1458. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByIndex(unsigned index) const
  1459. {
  1460. if (!numOfDropZones || (index == 0))
  1461. return nullptr;
  1462. buildDropZoneCache();
  1463. if (index > numOfDropZones)
  1464. return nullptr;
  1465. StringBuffer xpath("Software/DropZone[@id=\"");
  1466. xpath.append(DROPZONE_SUFFIX).append(index).append("\"]");
  1467. synchronized procedure(safeCache);
  1468. return (CConstDropZoneInfo *) getCache(xpath.str());
  1469. }
  1470. IConstDfuQueueInfo * CLocalEnvironment::getDfuQueueByIndex(unsigned index) const
  1471. {
  1472. if (!numOfDfuQueues || (index == 0))
  1473. return nullptr;
  1474. buildDfuQueueCache();
  1475. if (index > numOfDfuQueues)
  1476. return nullptr;
  1477. StringBuffer xpath("Software/DfuQueue[@id=\"");
  1478. xpath.append(index).append("\"]");
  1479. synchronized procedure(safeCache);
  1480. return (CConstDfuQueueInfo *) getCache(xpath.str());
  1481. }
  1482. IConstInstanceInfo * CLocalEnvironment::getInstance(const char *type, const char *version, const char *domain) const
  1483. {
  1484. StringBuffer xpath("Software/");
  1485. xpath.append(type);
  1486. if (version)
  1487. xpath.append("[@version='").append(version).append("']");
  1488. xpath.append("/Instance");
  1489. synchronized procedure(safeCache);
  1490. Owned<IPropertyTreeIterator> _it = p->getElements(xpath);
  1491. for (_it->first(); _it->isValid(); _it->next())
  1492. {
  1493. IPropertyTree *rp = &_it->query();
  1494. Owned<CConstInstanceInfo> inst = new CConstInstanceInfo(this, rp); // CConstInstanceInfo will link rp
  1495. Owned<IConstMachineInfo> m = inst->getMachine();
  1496. if (m)
  1497. {
  1498. Owned<IConstDomainInfo> dm = m->getDomain();
  1499. if (dm)
  1500. {
  1501. SCMStringBuffer thisdomain;
  1502. dm->getName(thisdomain);
  1503. if (thisdomain.length() && strcmp(domain, thisdomain.str())==0)
  1504. return inst.getClear();
  1505. }
  1506. }
  1507. }
  1508. return nullptr;
  1509. }
  1510. CConstInstanceInfo * CLocalEnvironment::getInstanceByIP(const char *type, const char *version, IpAddress &ip) const
  1511. {
  1512. StringBuffer xpath("Software/");
  1513. xpath.append(type);
  1514. if (version)
  1515. xpath.append("[@version='").append(version).append("']");
  1516. xpath.append("/Instance");
  1517. synchronized procedure(safeCache);
  1518. assertex(p);
  1519. Owned<IPropertyTreeIterator> _it = p->getElements(xpath);
  1520. assertex(_it);
  1521. for (_it->first(); _it->isValid(); _it->next())
  1522. {
  1523. IPropertyTree *rp = &_it->query();
  1524. assertex(rp);
  1525. Owned<CConstInstanceInfo> inst = new CConstInstanceInfo(this, rp); // CConstInstanceInfo will link rp
  1526. Owned<IConstMachineInfo> m = inst->getMachine();
  1527. if (m)
  1528. {
  1529. SCMStringBuffer eps;
  1530. m->getNetAddress(eps);
  1531. SocketEndpoint ep(eps.str());
  1532. if (ep.ipequals(ip))
  1533. return inst.getClear();
  1534. }
  1535. }
  1536. return nullptr;
  1537. }
  1538. void CLocalEnvironment::unlockRemote()
  1539. {
  1540. #if 0
  1541. conn->commit(true);
  1542. conn->changeMode(0, SDS_LOCK_TIMEOUT);
  1543. #else
  1544. if (conn)
  1545. {
  1546. synchronized procedure(safeCache);
  1547. p.clear();
  1548. conn.setown(querySDS().connect(xPath.str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT));
  1549. p.setown(conn->getRoot());
  1550. }
  1551. #endif
  1552. }
  1553. void CLocalEnvironment::preload()
  1554. {
  1555. synchronized procedure(safeCache);
  1556. p->queryBranch(".");
  1557. }
  1558. void CLocalEnvironment::setXML(const char *xml)
  1559. {
  1560. Owned<IPropertyTree> newRoot = createPTreeFromXMLString(xml, ipt_lowmem);
  1561. synchronized procedure(safeCache);
  1562. Owned<IPropertyTreeIterator> it = p->getElements("*");
  1563. ForEach(*it)
  1564. {
  1565. p->removeTree(&it->query());
  1566. }
  1567. it.setown(newRoot->getElements("*"));
  1568. ForEach(*it)
  1569. {
  1570. IPropertyTree *sub = &it->get();
  1571. p->addPropTree(sub->queryName(), sub);
  1572. }
  1573. }
  1574. bool CLocalEnvironment::getRunInfo(IStringVal & path, IStringVal & dir, const char * tag, const char * version, const char *machineaddr, const char *defprogname) const
  1575. {
  1576. try
  1577. {
  1578. // PrintLog("getExecutablePath %s %s %s", tag, version, machineaddr);
  1579. // first see if local machine with deployed on
  1580. SocketEndpoint ep(machineaddr);
  1581. Owned<CConstInstanceInfo> ipinstance = getInstanceByIP(tag, version, ep);
  1582. if (ipinstance)
  1583. {
  1584. StringAttr testpath;
  1585. StringAttrAdaptor teststrval(testpath);
  1586. if (ipinstance->doGetRunInfo(teststrval,dir,defprogname,false))
  1587. { // this returns full string
  1588. RemoteFilename rfn;
  1589. rfn.setRemotePath(testpath.get());
  1590. Owned<IFile> file = createIFile(rfn);
  1591. if (file->exists())
  1592. {
  1593. StringBuffer tmp;
  1594. rfn.getLocalPath(tmp);
  1595. path.set(tmp.str());
  1596. return true;
  1597. }
  1598. }
  1599. }
  1600. Owned<IConstMachineInfo> machine = getMachineByAddress(machineaddr);
  1601. if (!machine)
  1602. {
  1603. LOG(MCdebugInfo, unknownJob, "Unable to find machine for %s", machineaddr);
  1604. return false;
  1605. }
  1606. StringAttr targetdomain;
  1607. Owned<IConstDomainInfo> domain = machine->getDomain();
  1608. if (!domain)
  1609. {
  1610. LOG(MCdebugInfo, unknownJob, "Unable to find domain for %s", machineaddr);
  1611. return false;
  1612. }
  1613. //domain->getName(StringAttrAdaptor(targetdomain)); // confuses g++
  1614. StringAttrAdaptor strval(targetdomain);
  1615. domain->getName(strval);
  1616. Owned<IConstInstanceInfo> instance = getInstance(tag, version, targetdomain);
  1617. if (!instance)
  1618. {
  1619. LOG(MCdebugInfo, unknownJob, "Unable to find process %s for domain %s", tag, targetdomain.get());
  1620. return false;
  1621. }
  1622. return instance->getRunInfo(path,dir,defprogname);
  1623. }
  1624. catch (IException * e)
  1625. {
  1626. EXCLOG(e, "Extracting slave version");
  1627. e->Release();
  1628. return false;
  1629. }
  1630. }
  1631. void CLocalEnvironment::clearCache()
  1632. {
  1633. synchronized procedure(safeCache);
  1634. if (conn)
  1635. {
  1636. p.clear();
  1637. unsigned mode = 0;
  1638. try
  1639. {
  1640. conn->reload();
  1641. }
  1642. catch (IException *e)
  1643. {
  1644. EXCLOG(e, "Failed to reload connection");
  1645. e->Release();
  1646. mode = conn->queryMode();
  1647. conn.clear();
  1648. }
  1649. if (!conn)
  1650. conn.setown(querySDS().connect(xPath, myProcessSession(), mode, SDS_LOCK_TIMEOUT));
  1651. p.setown(conn->getRoot());
  1652. }
  1653. cache.kill();
  1654. keyGroupMap.clear();
  1655. keyPairMap.clear();
  1656. init();
  1657. resetPasswordsFromSDS();
  1658. }
  1659. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByAddressPath(const char * netaddress, const char *targetFilePath) const
  1660. {
  1661. IConstDropZoneInfo * dropZone = nullptr;
  1662. IpAddress targetIp(netaddress);
  1663. unsigned dropzonePathLen = _MAX_PATH + 1;
  1664. #ifdef _DEBUG
  1665. LOG(MCdebugInfo, unknownJob, "Netaddress: '%s', targetFilePath: '%s'", netaddress, targetFilePath);
  1666. #endif
  1667. // Check the directory path first
  1668. Owned<IConstDropZoneInfoIterator> zoneIt = getDropZoneIterator();
  1669. ForEach(*zoneIt)
  1670. {
  1671. SCMStringBuffer dropZoneDir;
  1672. zoneIt->query().getDirectory(dropZoneDir);
  1673. StringBuffer fullDropZoneDir(dropZoneDir.str());
  1674. addPathSepChar(fullDropZoneDir);
  1675. IConstDropZoneInfo * candidateDropZone = nullptr;
  1676. if (strncmp(fullDropZoneDir, targetFilePath, fullDropZoneDir.length()) == 0)
  1677. {
  1678. candidateDropZone = &zoneIt->query();
  1679. // The backward compatibility built in IConstDropZoneServerInfoIterator
  1680. Owned<IConstDropZoneServerInfoIterator> dropzoneServerListIt = candidateDropZone->getServers();
  1681. ForEach(*dropzoneServerListIt)
  1682. {
  1683. StringBuffer dropzoneServer;
  1684. dropzoneServerListIt->query().getServer(dropzoneServer);
  1685. // It can be a hostname or an IP -> get the IP
  1686. IpAddress serverIP(dropzoneServer.str());
  1687. #ifdef _DEBUG
  1688. StringBuffer serverIpString;
  1689. serverIP.getIpText(serverIpString);
  1690. LOG(MCdebugInfo, unknownJob, "Listed server: '%s', IP: '%s'", dropzoneServer.str(), serverIpString.str());
  1691. #endif
  1692. if (targetIp.ipequals(serverIP))
  1693. {
  1694. // OK the target is a valid machine in the server list we have a right drop zone candidate
  1695. // Keep this candidate drop zone if its directory path is shorter than we already have
  1696. if (dropzonePathLen > fullDropZoneDir.length())
  1697. {
  1698. dropzonePathLen = fullDropZoneDir.length();
  1699. dropZone = candidateDropZone;
  1700. }
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. }
  1706. return LINK(dropZone);
  1707. }
  1708. IConstDropZoneInfoIterator * CLocalEnvironment::getDropZoneIteratorByAddress(const char *addr) const
  1709. {
  1710. class CByAddrIter : public CSimpleInterfaceOf<IConstDropZoneInfoIterator>
  1711. {
  1712. IArrayOf<IConstDropZoneInfo> matches;
  1713. unsigned cur = NotFound;
  1714. public:
  1715. CByAddrIter(IConstDropZoneInfoIterator *baseIter, const char *addr)
  1716. {
  1717. IpAddress toMatch(addr);
  1718. ForEach(*baseIter)
  1719. {
  1720. IConstDropZoneInfo &dz = baseIter->query();
  1721. Owned<IConstDropZoneServerInfoIterator> serverIter = dz.getServers();
  1722. ForEach(*serverIter)
  1723. {
  1724. IConstDropZoneServerInfo &serverElem = serverIter->query();
  1725. StringBuffer serverName;
  1726. IpAddress serverIp(serverElem.getServer(serverName).str());
  1727. if (serverIp.ipequals(toMatch))
  1728. {
  1729. matches.append(*LINK(&dz));
  1730. break;
  1731. }
  1732. }
  1733. }
  1734. }
  1735. virtual bool first() override
  1736. {
  1737. if (0 == matches.ordinality())
  1738. {
  1739. cur = NotFound;
  1740. return false;
  1741. }
  1742. cur = 0;
  1743. return true;
  1744. }
  1745. virtual bool next() override
  1746. {
  1747. if (cur+1==matches.ordinality())
  1748. {
  1749. cur = NotFound;
  1750. return false;
  1751. }
  1752. ++cur;
  1753. return true;
  1754. }
  1755. virtual bool isValid() override
  1756. {
  1757. return NotFound != cur;
  1758. }
  1759. virtual IConstDropZoneInfo &query() override
  1760. {
  1761. assertex(NotFound != cur);
  1762. return matches.item(cur);
  1763. }
  1764. virtual unsigned count() const override
  1765. {
  1766. return matches.ordinality();
  1767. }
  1768. };
  1769. Owned<IConstDropZoneInfoIterator> baseIter = new CConstDropZoneInfoIterator();
  1770. return new CByAddrIter(baseIter, addr);
  1771. }
  1772. IConstDropZoneInfoIterator * CLocalEnvironment::getDropZoneIterator() const
  1773. {
  1774. return new CConstDropZoneInfoIterator();
  1775. }
  1776. IConstDfuQueueInfoIterator * CLocalEnvironment::getDfuQueueIterator() const
  1777. {
  1778. return new CConstDfuQueueInfoIterator();
  1779. }
  1780. bool CLocalEnvironment::isValidDfuQueueName(const char * queueName) const
  1781. {
  1782. bool retVal = false;
  1783. if (!isEmptyString(queueName))
  1784. {
  1785. Owned<IConstDfuQueueInfoIterator> queueIt = getDfuQueueIterator();
  1786. ForEach(*queueIt)
  1787. {
  1788. SCMStringBuffer _queueName;
  1789. queueIt->query().getDfuQueueName(_queueName);
  1790. retVal = streq(queueName, _queueName.str());
  1791. }
  1792. }
  1793. return retVal;
  1794. }
  1795. IConstMachineInfoIterator * CLocalEnvironment::getMachineIterator() const
  1796. {
  1797. return new CConstMachineInfoIterator();
  1798. }
  1799. bool CLocalEnvironment::isDropZoneRestrictionEnabled() const
  1800. {
  1801. if (!isDropZoneRestrictionLoaded)
  1802. {
  1803. dropZoneRestrictionEnabled = queryEnvironmentConf().getPropBool("useDropZoneRestriction", true);
  1804. isDropZoneRestrictionLoaded=true;
  1805. }
  1806. return dropZoneRestrictionEnabled;
  1807. }
  1808. IConstDaFileSrvInfo *CLocalEnvironment::getDaFileSrvGroupInfo(const char *name) const
  1809. {
  1810. if (!name)
  1811. return nullptr;
  1812. VStringBuffer xpath("Software/DafilesrvGroup[@name=\"%s\"]", name);
  1813. synchronized procedure(safeCache);
  1814. IConstEnvBase *cached = getCache(xpath.str());
  1815. if (!cached)
  1816. {
  1817. IPropertyTree *d = p->queryPropTree(xpath.str());
  1818. if (!d)
  1819. return nullptr;
  1820. cached = new CConstDaFileSrvInfo(this, d);
  1821. setCache(xpath.str(), cached);
  1822. }
  1823. return (IConstDaFileSrvInfo *) cached;
  1824. }
  1825. IConstSparkThorInfo *CLocalEnvironment::getSparkThor(const char *name) const
  1826. {
  1827. if (isEmptyString(name))
  1828. return nullptr;
  1829. buildSparkThorCache();
  1830. VStringBuffer xpath("Software/SparkThor[@name=\"%s\"]", name);
  1831. synchronized procedure(safeCache);
  1832. return (CConstSparkThorInfo *) getCache(xpath);
  1833. }
  1834. IConstSparkThorInfo *CLocalEnvironment::getSparkThorByIndex(unsigned index) const
  1835. {
  1836. if (index == 0)
  1837. return nullptr;
  1838. buildSparkThorCache();
  1839. if (index > numOfSparkThors)
  1840. return nullptr;
  1841. StringBuffer xpath("Software/SparkThor[@id=\"");
  1842. xpath.append(SPARKTHOR_SUFFIX).append(index).append("\"]");
  1843. synchronized procedure(safeCache);
  1844. return (CConstSparkThorInfo *) getCache(xpath);
  1845. }
  1846. IConstSparkThorInfoIterator *CLocalEnvironment::getSparkThorIterator() const
  1847. {
  1848. return new CConstSparkThorInfoIterator();
  1849. }
  1850. void CLocalEnvironment::buildSparkThorCache() const
  1851. {
  1852. synchronized procedure(safeCache);
  1853. if (sparkThorCacheBuilt)
  1854. return;
  1855. Owned<IPropertyTreeIterator> it = p->getElements("Software/SparkThorProcess");
  1856. ForEach(*it)
  1857. {
  1858. const char *name = it->query().queryProp("@name");
  1859. if (!isEmptyString(name))
  1860. {
  1861. StringBuffer x("Software/SparkThor[@name=\"");
  1862. x.append(name).append("\"]");
  1863. Owned<IConstEnvBase> cached = new CConstSparkThorInfo((CLocalEnvironment *) this, &it->query());
  1864. cache.setValue(x, cached);
  1865. }
  1866. numOfSparkThors++;
  1867. StringBuffer x("Software/SparkThor[@id=\"");
  1868. x.append(SPARKTHOR_SUFFIX).append(numOfSparkThors).append("\"]");
  1869. Owned<IConstEnvBase> cached = new CConstSparkThorInfo((CLocalEnvironment *) this, &it->query());
  1870. cache.setValue(x, cached);
  1871. }
  1872. sparkThorCacheBuilt = true;
  1873. }
  1874. //==========================================================================================
  1875. // Iterators implementation
  1876. CConstMachineInfoIterator::CConstMachineInfoIterator()
  1877. {
  1878. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  1879. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  1880. maxIndex = constEnv->getNumberOfMachines();
  1881. }
  1882. bool CConstMachineInfoIterator::first()
  1883. {
  1884. index = 1;
  1885. curr.setown(constEnv->getMachineByIndex(index));
  1886. return curr != nullptr;
  1887. }
  1888. bool CConstMachineInfoIterator::next()
  1889. {
  1890. if (index < maxIndex)
  1891. {
  1892. index++;
  1893. curr.setown(constEnv->getMachineByIndex(index));
  1894. }
  1895. else
  1896. curr.clear();
  1897. return curr != nullptr;
  1898. }
  1899. bool CConstMachineInfoIterator::isValid()
  1900. {
  1901. return curr != nullptr;
  1902. }
  1903. IConstMachineInfo & CConstMachineInfoIterator::query()
  1904. {
  1905. return *curr;
  1906. }
  1907. unsigned CConstMachineInfoIterator::count() const
  1908. {
  1909. return maxIndex;
  1910. }
  1911. CConstDropZoneServerInfoIterator::CConstDropZoneServerInfoIterator(const IConstDropZoneInfo * dropZone)
  1912. {
  1913. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  1914. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  1915. // For backward compatibility
  1916. SCMStringBuffer dropZoneMachineName;
  1917. // Returns dropzone '@computer' value if it is exists
  1918. dropZone->getComputerName(dropZoneMachineName);
  1919. if (0 != dropZoneMachineName.length())
  1920. {
  1921. // Create a ServerList for legacy element.
  1922. Owned<IPropertyTree> legacyServerList = createPTree(ipt_lowmem);
  1923. Owned<IConstMachineInfo> machineInfo = constEnv->getMachine(dropZoneMachineName.str());
  1924. if (machineInfo)
  1925. {
  1926. SCMStringBuffer dropZoneMachineNetAddress;
  1927. machineInfo->getNetAddress(dropZoneMachineNetAddress);
  1928. // Create a single ServerList record related to @computer
  1929. //<ServerList name="ServerList" server="<IP_of_@computer>"/>
  1930. IPropertyTree *newRecord = legacyServerList->addPropTree("ServerList");
  1931. newRecord->setProp("@name", "ServerList");
  1932. newRecord->setProp("@server", dropZoneMachineNetAddress.str());
  1933. maxIndex = 1;
  1934. }
  1935. else
  1936. {
  1937. // Something is terrible wrong because there is no matching machine for DropZone @computer
  1938. maxIndex = 0;
  1939. }
  1940. serverListIt.setown(legacyServerList->getElements("ServerList"));
  1941. }
  1942. else
  1943. {
  1944. Owned<IPropertyTree> pSrc = &dropZone->getPTree();
  1945. serverListIt.setown(pSrc->getElements("ServerList"));
  1946. maxIndex = pSrc->getCount("ServerList");
  1947. }
  1948. }
  1949. bool CConstDropZoneServerInfoIterator::first()
  1950. {
  1951. bool hasFirst = serverListIt->first();
  1952. if (hasFirst)
  1953. curr.setown(new CConstDropZoneServerInfo(constEnv, &serverListIt->query()));
  1954. else
  1955. curr.clear();
  1956. return hasFirst;
  1957. }
  1958. bool CConstDropZoneServerInfoIterator::next()
  1959. {
  1960. bool hasNext = serverListIt->next();
  1961. if (hasNext)
  1962. curr.setown(new CConstDropZoneServerInfo(constEnv, &serverListIt->query()));
  1963. else
  1964. curr.clear();
  1965. return hasNext;
  1966. }
  1967. bool CConstDropZoneServerInfoIterator::isValid()
  1968. {
  1969. return nullptr != curr;
  1970. }
  1971. IConstDropZoneServerInfo & CConstDropZoneServerInfoIterator::query()
  1972. {
  1973. return *curr;
  1974. }
  1975. unsigned CConstDropZoneServerInfoIterator::count() const
  1976. {
  1977. return maxIndex;
  1978. }
  1979. //--------------------------------------------------
  1980. CConstDropZoneInfoIterator::CConstDropZoneInfoIterator()
  1981. {
  1982. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  1983. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  1984. maxIndex = constEnv->getNumberOfDropZones();
  1985. }
  1986. bool CConstDropZoneInfoIterator::first()
  1987. {
  1988. index = 1;
  1989. curr.setown(constEnv->getDropZoneByIndex(index));
  1990. return curr != nullptr;
  1991. }
  1992. bool CConstDropZoneInfoIterator::next()
  1993. {
  1994. if (index < maxIndex)
  1995. {
  1996. index++;
  1997. curr.setown(constEnv->getDropZoneByIndex(index));
  1998. }
  1999. else
  2000. curr.clear();
  2001. return curr != nullptr;
  2002. }
  2003. bool CConstDropZoneInfoIterator::isValid()
  2004. {
  2005. return curr != nullptr;
  2006. }
  2007. IConstDropZoneInfo & CConstDropZoneInfoIterator::query()
  2008. {
  2009. return *curr;
  2010. }
  2011. unsigned CConstDropZoneInfoIterator::count() const
  2012. {
  2013. return maxIndex;
  2014. }
  2015. //--------------------------------------------------
  2016. CConstDfuQueueInfoIterator::CConstDfuQueueInfoIterator()
  2017. {
  2018. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  2019. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  2020. maxIndex = constEnv->getNumberOfDfuQueues();
  2021. }
  2022. bool CConstDfuQueueInfoIterator::first()
  2023. {
  2024. index = 1;
  2025. curr.setown(constEnv->getDfuQueueByIndex(index));
  2026. return curr != nullptr;
  2027. }
  2028. bool CConstDfuQueueInfoIterator::next()
  2029. {
  2030. if (index < maxIndex)
  2031. {
  2032. index++;
  2033. curr.setown(constEnv->getDfuQueueByIndex(index));
  2034. }
  2035. else
  2036. curr.clear();
  2037. return curr != nullptr;
  2038. }
  2039. bool CConstDfuQueueInfoIterator::isValid()
  2040. {
  2041. return curr != nullptr;
  2042. }
  2043. IConstDfuQueueInfo & CConstDfuQueueInfoIterator::query()
  2044. {
  2045. return *curr;
  2046. }
  2047. unsigned CConstDfuQueueInfoIterator::count() const
  2048. {
  2049. return maxIndex;
  2050. }
  2051. //--------------------------------------------------
  2052. CConstSparkThorInfoIterator::CConstSparkThorInfoIterator()
  2053. {
  2054. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  2055. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  2056. maxIndex = constEnv->getNumberOfSparkThors();
  2057. }
  2058. bool CConstSparkThorInfoIterator::first()
  2059. {
  2060. index = 1;
  2061. curr.setown(constEnv->getSparkThorByIndex(index));
  2062. return curr != nullptr;
  2063. }
  2064. bool CConstSparkThorInfoIterator::next()
  2065. {
  2066. if (index < maxIndex)
  2067. {
  2068. index++;
  2069. curr.setown(constEnv->getSparkThorByIndex(index));
  2070. }
  2071. else
  2072. curr.clear();
  2073. return curr != nullptr;
  2074. }
  2075. bool CConstSparkThorInfoIterator::isValid()
  2076. {
  2077. return curr != nullptr;
  2078. }
  2079. IConstSparkThorInfo &CConstSparkThorInfoIterator::query()
  2080. {
  2081. return *curr;
  2082. }
  2083. unsigned CConstSparkThorInfoIterator::count() const
  2084. {
  2085. return maxIndex;
  2086. }
  2087. //--------------------------------------------------
  2088. CConstInstanceInfoIterator::CConstInstanceInfoIterator(const CLocalEnvironment *env, IPropertyTreeIterator *itr)
  2089. : constEnv(env)
  2090. {
  2091. instanceItr.setown(itr);
  2092. maxIndex = 0;
  2093. ForEach(*instanceItr)
  2094. maxIndex++;
  2095. }
  2096. bool CConstInstanceInfoIterator::first()
  2097. {
  2098. index = 1;
  2099. instanceItr->first();
  2100. curr.setown(new CConstInstanceInfo(constEnv, &instanceItr->query()));
  2101. return curr != nullptr;
  2102. }
  2103. bool CConstInstanceInfoIterator::next()
  2104. {
  2105. if (index < maxIndex)
  2106. {
  2107. index++;
  2108. instanceItr->next();
  2109. curr.setown(new CConstInstanceInfo(constEnv, &instanceItr->query()));
  2110. }
  2111. else
  2112. curr.clear();
  2113. return curr != nullptr;
  2114. }
  2115. bool CConstInstanceInfoIterator::isValid()
  2116. {
  2117. return curr != nullptr;
  2118. }
  2119. IConstInstanceInfo &CConstInstanceInfoIterator::query()
  2120. {
  2121. return *curr;
  2122. }
  2123. unsigned CConstInstanceInfoIterator::count() const
  2124. {
  2125. return maxIndex;
  2126. }
  2127. //==========================================================================================
  2128. static CriticalSection getEnvSect;
  2129. extern ENVIRONMENT_API IEnvironmentFactory * getEnvironmentFactory(bool update)
  2130. {
  2131. CriticalBlock block(getEnvSect);
  2132. if (!factory)
  2133. {
  2134. factory = new CEnvironmentFactory();
  2135. addShutdownHook(*factory);
  2136. }
  2137. if (update)
  2138. factory->validateCache();
  2139. return LINK(factory);
  2140. }
  2141. extern ENVIRONMENT_API void closeEnvironment()
  2142. {
  2143. try
  2144. {
  2145. CEnvironmentFactory* pFactory;
  2146. {
  2147. //this method is not meant to be invoked by multiple
  2148. //threads concurrently but just in case...
  2149. CriticalBlock block(getEnvSect);
  2150. pFactory = factory;
  2151. factory = nullptr;
  2152. }
  2153. clearPasswordsFromSDS();
  2154. if (pFactory)
  2155. {
  2156. removeShutdownHook(*pFactory);
  2157. pFactory->close();
  2158. pFactory->Release();
  2159. }
  2160. }
  2161. catch (IException *e)
  2162. {
  2163. EXCLOG(e);
  2164. }
  2165. }
  2166. extern ENVIRONMENT_API unsigned long readSizeSetting(const char * sizeStr, const unsigned long defaultSize)
  2167. {
  2168. StringBuffer buf(sizeStr);
  2169. buf.trim();
  2170. if (buf.isEmpty())
  2171. return defaultSize;
  2172. const char* ptrStart = buf;
  2173. const char* ptrAfterDigit = ptrStart;
  2174. while (*ptrAfterDigit && isdigit(*ptrAfterDigit))
  2175. ptrAfterDigit++;
  2176. if (!*ptrAfterDigit)
  2177. return atol(buf);
  2178. const char* ptr = ptrAfterDigit;
  2179. while (*ptr && (ptr[0] == ' '))
  2180. ptr++;
  2181. char c = ptr[0];
  2182. buf.setLength(ptrAfterDigit - ptrStart);
  2183. unsigned long size = atol(buf);
  2184. switch (c)
  2185. {
  2186. case 'k':
  2187. case 'K':
  2188. size *= 1000;
  2189. break;
  2190. case 'm':
  2191. case 'M':
  2192. size *= 1000000;
  2193. break;
  2194. case 'g':
  2195. case 'G':
  2196. size *= 1000000000;
  2197. break;
  2198. case 't':
  2199. case 'T':
  2200. size *= 1000000000000;
  2201. break;
  2202. default:
  2203. break;
  2204. }
  2205. return size;
  2206. }
  2207. unsigned getAccessibleServiceURLList(const char *serviceType, std::vector<std::string> &list)
  2208. {
  2209. unsigned added = 0;
  2210. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  2211. Owned<IConstEnvironment> daliEnv = factory->openEnvironment();
  2212. Owned<IPropertyTree> env = &daliEnv->getPTree();
  2213. if (env.get())
  2214. {
  2215. StringBuffer fileMetaServiceUrl;
  2216. StringBuffer espInstanceComputerName;
  2217. StringBuffer bindingProtocol;
  2218. StringBuffer xpath;
  2219. StringBuffer instanceAddress;
  2220. StringBuffer espServiceType;
  2221. Owned<IPropertyTreeIterator> espProcessIter = env->getElements("Software/EspProcess");
  2222. ForEach(*espProcessIter)
  2223. {
  2224. Owned<IPropertyTreeIterator> espBindingIter = espProcessIter->query().getElements("EspBinding");
  2225. ForEach(*espBindingIter)
  2226. {
  2227. xpath.setf("Software/EspService[@name=\"%s\"]/Properties/@type", espBindingIter->query().queryProp("@service"));
  2228. if (strisame(env->queryProp(xpath), serviceType))
  2229. {
  2230. if (espBindingIter->query().getProp("@protocol", bindingProtocol.clear()))
  2231. {
  2232. Owned<IPropertyTreeIterator> espInstanceIter = espProcessIter->query().getElements("Instance");
  2233. ForEach(*espInstanceIter)
  2234. {
  2235. if (espInstanceIter->query().getProp("@computer", espInstanceComputerName.clear()))
  2236. {
  2237. xpath.setf("Hardware/Computer[@name=\"%s\"]/@netAddress", espInstanceComputerName.str());
  2238. if (env->getProp(xpath.str(), instanceAddress.clear()))
  2239. {
  2240. fileMetaServiceUrl.setf("%s://%s:%d", bindingProtocol.str(), instanceAddress.str(), espBindingIter->query().getPropInt("@port",8010));
  2241. list.push_back(fileMetaServiceUrl.str());
  2242. ++added;
  2243. }
  2244. }
  2245. }
  2246. }
  2247. }
  2248. }//ESPBinding
  2249. }//ESPProcess
  2250. }
  2251. return added;
  2252. }