environment.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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 "mpbase.hpp"
  21. #include "daclient.hpp"
  22. #include "dadfs.hpp"
  23. #include "dafdesc.hpp"
  24. #include "dasds.hpp"
  25. #include "dalienv.hpp"
  26. #define SDS_LOCK_TIMEOUT 30000
  27. #define DEFAULT_DROPZONE_INDEX 1
  28. #define DROPZONE_BY_MACHINE_SUFFIX "-dropzoneByMachine-"
  29. #define DROPZONE_SUFFIX "dropzone-"
  30. #define MACHINE_PREFIX "machine-"
  31. static int environmentTraceLevel = 1;
  32. static Owned <IConstEnvironment> cache;
  33. class CLocalEnvironment;
  34. class CConstMachineInfoIterator : public CSimpleInterfaceOf<IConstMachineInfoIterator>
  35. {
  36. public:
  37. CConstMachineInfoIterator();
  38. virtual bool first() override;
  39. virtual bool next() override;
  40. virtual bool isValid() override;
  41. virtual IConstMachineInfo & query() override;
  42. virtual unsigned count() const override;
  43. protected:
  44. IConstMachineInfo * curr = nullptr;
  45. Owned<CLocalEnvironment> constEnv;
  46. unsigned index = 1;
  47. unsigned maxIndex = 0;
  48. };
  49. class CConstDropZoneInfoIterator : public CSimpleInterfaceOf<IConstDropZoneInfoIterator>
  50. {
  51. public:
  52. CConstDropZoneInfoIterator();
  53. virtual bool first() override;
  54. virtual bool next() override;
  55. virtual bool isValid() override;
  56. virtual IConstDropZoneInfo & query() override;
  57. virtual unsigned count() const override;
  58. protected:
  59. IConstDropZoneInfo * curr = nullptr;
  60. Owned<CLocalEnvironment> constEnv;
  61. unsigned index = 1;
  62. unsigned maxIndex = 0;
  63. };
  64. class CConstDropZoneIteratorByComputer : public CSimpleInterfaceOf<IConstDropZoneInfoIterator>
  65. {
  66. public:
  67. CConstDropZoneIteratorByComputer(const char * computer);
  68. virtual bool first() override;
  69. virtual bool next() override;
  70. virtual bool isValid() override;
  71. virtual IConstDropZoneInfo & query() override;
  72. virtual unsigned count() const override;
  73. protected:
  74. StringBuffer computerName;
  75. IConstDropZoneInfo * curr = nullptr;
  76. Owned<CLocalEnvironment> constEnv;
  77. unsigned index = 1;
  78. unsigned maxIndex = 0;
  79. };
  80. //==========================================================================================
  81. class CConstInstanceInfo;
  82. class CLocalEnvironment : implements IConstEnvironment, public CInterface
  83. {
  84. private:
  85. // NOTE - order is important - we need to construct before p and (especially) destruct after p
  86. Owned<IRemoteConnection> conn;
  87. Owned<IPropertyTree> p;
  88. mutable MapStringToMyClass<IConstEnvBase> cache;
  89. mutable Mutex safeCache;
  90. mutable bool dropZoneCacheBuilt;
  91. mutable bool machineCacheBuilt;
  92. StringBuffer xPath;
  93. Owned<IPropertyTree> numOfDropzonesByComputer;
  94. mutable unsigned numOfMachines;
  95. mutable unsigned numOfDropZones;
  96. IConstEnvBase * getCache(const char *path) const;
  97. void setCache(const char *path, IConstEnvBase *value) const;
  98. void buildMachineCache() const;
  99. void buildDropZoneCache() const;
  100. void init();
  101. StringBuffer & createComputerNameXpath(const char * computer, StringBuffer &xpath) const
  102. {
  103. xpath.append("numberOfDropZones[@name=\"");
  104. xpath.append(computer).append("\"]/number");
  105. return xpath;
  106. }
  107. mutable bool isDropZoneRestrictionLoaded = false;
  108. mutable bool dropZoneRestrictionEnabled = true;
  109. public:
  110. IMPLEMENT_IINTERFACE;
  111. CLocalEnvironment(IRemoteConnection *_conn, IPropertyTree *x=nullptr, const char* path="Environment");
  112. CLocalEnvironment(const char* path="config.xml");
  113. virtual ~CLocalEnvironment();
  114. virtual IStringVal & getName(IStringVal & str) const;
  115. virtual IStringVal & getXML(IStringVal & str) const;
  116. virtual IPropertyTree & getPTree() const;
  117. virtual IEnvironment& lock() const;
  118. virtual IConstDomainInfo * getDomain(const char * name) const;
  119. virtual IConstMachineInfo * getMachine(const char * name) const;
  120. virtual IConstMachineInfo * getMachineByAddress(const char * machineIp) const;
  121. virtual IConstMachineInfo * getMachineForLocalHost() const;
  122. virtual IConstDropZoneInfo * getDropZone(const char * name) const;
  123. virtual IConstDropZoneInfo * getDropZoneByComputer(const char * computer) const;
  124. virtual IConstInstanceInfo * getInstance(const char * type, const char * version, const char *domain) const;
  125. virtual CConstInstanceInfo * getInstanceByIP(const char *type, const char *version, IpAddress &ip) const;
  126. virtual IConstComputerTypeInfo * getComputerType(const char * name) const;
  127. virtual bool getRunInfo(IStringVal & path, IStringVal & dir, const char *type, const char *version, const char *machineaddr, const char *defprogname) const;
  128. virtual void preload();
  129. virtual IRemoteConnection* getConnection() const { return conn.getLink(); }
  130. void setXML(const char * logicalName);
  131. const char* getPath() const { return xPath.str(); }
  132. void unlockRemote();
  133. virtual bool isConstEnvironment() const { return true; }
  134. virtual void clearCache();
  135. virtual IConstMachineInfoIterator * getMachineIterator() const;
  136. virtual IConstDropZoneInfo * getDropZoneByComputer(const char * computer, const char * dzname) const;
  137. virtual IConstDropZoneInfoIterator * getDropZoneIteratorByComputer(const char * computer) const;
  138. virtual IConstDropZoneInfo * getDropZoneByAddressPath(const char * netaddress, const char *targetPath) const;
  139. virtual IConstDropZoneInfoIterator * getDropZoneIterator() const;
  140. unsigned getNumberOfMachines() const { buildMachineCache(); return numOfMachines; }
  141. IConstMachineInfo * getMachineByIndex(unsigned index) const;
  142. unsigned getNumberOfDropZonesByComputer(const char * computer) const
  143. {
  144. buildDropZoneCache();
  145. StringBuffer xpath;
  146. createComputerNameXpath(computer, xpath);
  147. return numOfDropzonesByComputer->getPropInt(xpath);
  148. }
  149. IConstDropZoneInfo * getDropZoneByComputerByIndex(const char * computer, unsigned index) const;
  150. unsigned getNumberOfDropZones() const { buildDropZoneCache(); return numOfDropZones; }
  151. IConstDropZoneInfo * getDropZoneByIndex(unsigned index) const;
  152. bool isDropZoneRestrictionEnabled() const;
  153. };
  154. class CLockedEnvironment : implements IEnvironment, public CInterface
  155. {
  156. public:
  157. //note that order of construction/destruction is important
  158. Owned<CLocalEnvironment> c;
  159. Owned<CLocalEnvironment> env;
  160. Owned<CLocalEnvironment> constEnv;
  161. IMPLEMENT_IINTERFACE;
  162. CLockedEnvironment(CLocalEnvironment *_c)
  163. {
  164. Owned<IRemoteConnection> connection = _c->getConnection();
  165. if (connection)
  166. {
  167. constEnv.set(_c); //save original constant environment
  168. //we only wish to allow one party to allow updating the environment.
  169. //
  170. //create a new /NewEnvironment subtree, locked for read/write access for self and entire subtree; delete on disconnect
  171. //
  172. StringBuffer newName("/New");
  173. newName.append(constEnv->getPath());
  174. const unsigned int mode = RTM_CREATE | RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE |
  175. RTM_LOCK_SUB | RTM_DELETE_ON_DISCONNECT;
  176. Owned<IRemoteConnection> conn = querySDS().connect(newName.str(), myProcessSession(), mode, SDS_LOCK_TIMEOUT);
  177. if (conn == nullptr)
  178. {
  179. if (environmentTraceLevel > 0)
  180. PrintLog("Failed to create locked environment %s", newName.str());
  181. throw MakeStringException(-1, "Failed to get a lock on environment /%s", newName.str());
  182. }
  183. //save the locked environment
  184. env.setown(new CLocalEnvironment(conn, nullptr, newName.str()));
  185. //get a lock on the const environment
  186. const unsigned int mode2 = RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE | RTM_LOCK_SUB;
  187. Owned<IRemoteConnection> conn2 = querySDS().connect(constEnv->getPath(), myProcessSession(), mode2, SDS_LOCK_TIMEOUT);
  188. if (conn2 == nullptr)
  189. {
  190. if (environmentTraceLevel > 0)
  191. PrintLog("Failed to lock environment %s", constEnv->getPath());
  192. throw MakeStringException(-1, "Failed to get a lock on environment /%s", constEnv->getPath());
  193. }
  194. //copy const environment to our member environment
  195. Owned<IPropertyTree> pSrc = conn2->getRoot();
  196. c.setown( new CLocalEnvironment(nullptr, createPTreeFromIPT(pSrc)));
  197. conn2->rollback();
  198. }
  199. else
  200. {
  201. c.set(_c);
  202. }
  203. }
  204. virtual ~CLockedEnvironment()
  205. {
  206. }
  207. virtual IStringVal & getName(IStringVal & str) const
  208. { return c->getName(str); }
  209. virtual IStringVal & getXML(IStringVal & str) const
  210. { return c->getXML(str); }
  211. virtual IPropertyTree & getPTree() const
  212. { return c->getPTree(); }
  213. virtual IConstDomainInfo * getDomain(const char * name) const
  214. { return c->getDomain(name); }
  215. virtual IConstMachineInfo * getMachine(const char * name) const
  216. { return c->getMachine(name); }
  217. virtual IConstMachineInfo * getMachineByAddress(const char * machineIp) const
  218. { return c->getMachineByAddress(machineIp); }
  219. virtual IConstMachineInfo * getMachineForLocalHost() const
  220. { return c->getMachineForLocalHost(); }
  221. virtual IConstDropZoneInfo * getDropZone(const char * name) const
  222. { return c->getDropZone(name); }
  223. virtual IConstDropZoneInfo * getDropZoneByComputer(const char * computer) const
  224. { return c->getDropZoneByComputer(computer); }
  225. virtual IConstInstanceInfo * getInstance(const char *type, const char *version, const char *domain) const
  226. { return c->getInstance(type, version, domain); }
  227. virtual bool getRunInfo(IStringVal & path, IStringVal & dir, const char *type, const char *version, const char *machineaddr,const char *defprogname) const
  228. { return c->getRunInfo(path, dir, type, version, machineaddr, defprogname); }
  229. virtual IConstComputerTypeInfo * getComputerType(const char * name) const
  230. { return c->getComputerType(name); }
  231. virtual IEnvironment & lock() const
  232. { ((CInterface*)this)->Link(); return *(IEnvironment*)this; }
  233. virtual void commit();
  234. virtual void rollback();
  235. virtual void setXML(const char * pstr)
  236. { c->setXML(pstr); }
  237. virtual void preload()
  238. { c->preload(); }
  239. virtual bool isConstEnvironment() const { return false; }
  240. virtual void clearCache() { c->clearCache(); }
  241. virtual IConstMachineInfoIterator * getMachineIterator() const
  242. { return c->getMachineIterator(); }
  243. virtual IConstDropZoneInfo * getDropZoneByComputer(const char * computer, const char * dzname) const
  244. { return c->getDropZoneByComputer(computer, dzname); }
  245. virtual IConstDropZoneInfoIterator * getDropZoneIteratorByComputer(const char * computer) const
  246. { return c->getDropZoneIteratorByComputer(computer); }
  247. virtual IConstDropZoneInfo * getDropZoneByAddressPath(const char * netaddress, const char *targetPath) const
  248. { return c->getDropZoneByAddressPath(netaddress, targetPath); }
  249. virtual IConstDropZoneInfoIterator * getDropZoneIterator() const
  250. { return c->getDropZoneIterator(); }
  251. virtual bool isDropZoneRestrictionEnabled() const
  252. { return c->isDropZoneRestrictionEnabled(); }
  253. };
  254. void CLockedEnvironment::commit()
  255. {
  256. if (constEnv)
  257. {
  258. //get a lock on const environment momentarily
  259. const unsigned int mode2 = RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE | RTM_LOCK_SUB;
  260. Owned<IRemoteConnection> conn2 = querySDS().connect(constEnv->getPath(), myProcessSession(), mode2, SDS_LOCK_TIMEOUT);
  261. if (conn2 == nullptr)
  262. {
  263. if (environmentTraceLevel > 0)
  264. PrintLog("Failed to lock environment %s", constEnv->getPath());
  265. throw MakeStringException(-1, "Failed to get a lock on environment /%s", constEnv->getPath());
  266. }
  267. //copy locked environment to const environment
  268. Owned<IPropertyTree> pSrc = &getPTree();
  269. Owned<IPropertyTree> pDst = conn2->queryRoot()->getBranch(nullptr);
  270. // JCS - I think it could (and would be more efficient if it had kept the original read lock connection to Env
  271. // - instead of using NewEnv as lock point, still work on copy, then changeMode of original connect
  272. // - as opposed to current scheme, where it recoonects in write mode and has to lazy fetch original env to update.
  273. // ensures pDst is equal to pSrc, whilst minimizing changes to pDst
  274. try { synchronizePTree(pDst, pSrc); }
  275. catch (IException *) { conn2->rollback(); throw; }
  276. conn2->commit();
  277. }
  278. else
  279. {
  280. Owned<IRemoteConnection> conn = c->getConnection();
  281. conn->commit();
  282. }
  283. }
  284. void CLockedEnvironment::rollback()
  285. {
  286. if (constEnv)
  287. {
  288. //get a lock on const environment momentarily
  289. const unsigned int mode2 = RTM_CREATE_QUERY | RTM_LOCK_READ | RTM_LOCK_WRITE | RTM_LOCK_SUB;
  290. Owned<IRemoteConnection> conn2 = querySDS().connect(constEnv->getPath(), myProcessSession(), mode2, SDS_LOCK_TIMEOUT);
  291. if (conn2 == nullptr)
  292. {
  293. if (environmentTraceLevel > 0)
  294. PrintLog("Failed to lock environment %s", constEnv->getPath());
  295. throw MakeStringException(-1, "Failed to get a lock on environment /%s", constEnv->getPath());
  296. }
  297. //copy const environment to locked environment (as it stands now) again losing any changes we made
  298. Owned<IPropertyTree> pSrc = conn2->getRoot();
  299. Owned<IPropertyTree> pDst = &getPTree();
  300. pDst->removeTree( pDst->queryPropTree("Hardware") );
  301. pDst->removeTree( pDst->queryPropTree("Software") );
  302. pDst->removeTree( pDst->queryPropTree("Programs") );
  303. pDst->removeTree( pDst->queryPropTree("Data") );
  304. mergePTree(pDst, pSrc);
  305. conn2->rollback();
  306. }
  307. else
  308. {
  309. Owned<IRemoteConnection> conn = c->getConnection();
  310. conn->rollback();
  311. }
  312. }
  313. //==========================================================================================
  314. // the following class implements notification handler for subscription to dali for environment
  315. // updates by other clients and is used by environment factory below. This also serves as
  316. // a sample self-contained implementation that can be easily tailored for other purposes.
  317. //==========================================================================================
  318. class CSdsSubscription : implements ISDSSubscription, public CInterface
  319. {
  320. public:
  321. CSdsSubscription()
  322. {
  323. m_constEnvUpdated = false;
  324. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
  325. sub_id = envFactory->subscribe(this);
  326. }
  327. virtual ~CSdsSubscription()
  328. {
  329. /* note that ideally, we would make this class automatically
  330. unsubscribe in this destructor. However, underlying dali client
  331. layer (CDaliSubscriptionManagerStub) links to this object and so
  332. object would not get destroyed just by an application releasing it.
  333. The application either needs to explicitly unsubscribe or close
  334. the environment which unsubscribes during close down. */
  335. }
  336. void unsubscribe()
  337. {
  338. synchronized block(m_mutexEnv);
  339. if (sub_id)
  340. {
  341. Owned<IEnvironmentFactory> m_envFactory = getEnvironmentFactory();
  342. m_envFactory->unsubscribe(sub_id);
  343. sub_id = 0;
  344. }
  345. }
  346. IMPLEMENT_IINTERFACE;
  347. //another client (like configenv) may have updated the environment and we got notified
  348. //(thanks to our subscription) but don't just reload it yet since this notification is sent on
  349. //another thread asynchronously and we may be actively working with the old environment. Just
  350. //invoke handleEnvironmentChange() when we are ready to invalidate cache in environment factory.
  351. //
  352. void notify(SubscriptionId id, const char *xpath, SDSNotifyFlags flags, unsigned valueLen=0, const void *valueData=nullptr)
  353. {
  354. DBGLOG("Environment was updated by another client of Dali server. Invalidating cache.\n");
  355. synchronized block(m_mutexEnv);
  356. m_constEnvUpdated = true;
  357. }
  358. void handleEnvironmentChange()
  359. {
  360. synchronized block(m_mutexEnv);
  361. if (m_constEnvUpdated)
  362. {
  363. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory();
  364. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  365. constEnv->clearCache();
  366. m_constEnvUpdated = false;
  367. }
  368. }
  369. private:
  370. SubscriptionId sub_id;
  371. Mutex m_mutexEnv;
  372. bool m_constEnvUpdated;
  373. };
  374. //==========================================================================================
  375. class CEnvironmentFactory : public CInterface,
  376. implements IEnvironmentFactory, implements IDaliClientShutdown
  377. {
  378. public:
  379. IMPLEMENT_IINTERFACE;
  380. typedef ArrayOf<SubscriptionId> SubscriptionIDs;
  381. SubscriptionIDs subIDs;
  382. Mutex mutex;
  383. Owned<CSdsSubscription> subscription;
  384. CEnvironmentFactory()
  385. {
  386. }
  387. virtual void clientShutdown();
  388. virtual ~CEnvironmentFactory()
  389. {
  390. close(); //just in case it was not explicitly closed
  391. }
  392. virtual IConstEnvironment* openEnvironment()
  393. {
  394. synchronized procedure(mutex);
  395. if (!cache)
  396. {
  397. Owned<IRemoteConnection> conn = querySDS().connect("/Environment", myProcessSession(), 0, SDS_LOCK_TIMEOUT);
  398. if (conn)
  399. cache.setown(new CLocalEnvironment(conn));
  400. }
  401. return cache.getLink();
  402. }
  403. virtual IEnvironment* updateEnvironment()
  404. {
  405. Owned<IConstEnvironment> pConstEnv = openEnvironment();
  406. synchronized procedure(mutex);
  407. return &pConstEnv->lock();
  408. }
  409. virtual IEnvironment * loadLocalEnvironmentFile(const char * filename)
  410. {
  411. Owned<IPropertyTree> ptree = createPTreeFromXMLFile(filename);
  412. Owned<CLocalEnvironment> pLocalEnv = new CLocalEnvironment(nullptr, ptree);
  413. return new CLockedEnvironment(pLocalEnv);
  414. }
  415. virtual IEnvironment * loadLocalEnvironment(const char * xml)
  416. {
  417. Owned<IPropertyTree> ptree = createPTreeFromXMLString(xml);
  418. Owned<CLocalEnvironment> pLocalEnv = new CLocalEnvironment(nullptr, ptree);
  419. return new CLockedEnvironment(pLocalEnv);
  420. }
  421. void close()
  422. {
  423. SubscriptionIDs copySubIDs;
  424. {
  425. synchronized procedure(mutex);
  426. cache.clear();
  427. //save the active subscriptions in another array
  428. //so they can be unsubscribed without causing deadlock
  429. // since ~CSdsSubscription() would ask us to unsubscribe the
  430. //same requiring a mutex lock (copy is a little price for this
  431. //normally small/empty array).
  432. //
  433. ForEachItemIn(i, subIDs)
  434. copySubIDs.append(subIDs.item(i));
  435. subIDs.kill();
  436. }
  437. //now unsubscribe all outstanding subscriptions
  438. //
  439. subscription.clear();
  440. ForEachItemIn(i, copySubIDs)
  441. querySDS().unsubscribe( copySubIDs.item(i) );
  442. }
  443. virtual SubscriptionId subscribe(ISDSSubscription* pSubHandler)
  444. {
  445. SubscriptionId sub_id = querySDS().subscribe("/Environment", *pSubHandler);
  446. synchronized procedure(mutex);
  447. subIDs.append(sub_id);
  448. return sub_id;
  449. }
  450. virtual void unsubscribe(SubscriptionId sub_id)
  451. {
  452. synchronized procedure(mutex);
  453. aindex_t i = subIDs.find(sub_id);
  454. if (i != NotFound)
  455. {
  456. querySDS().unsubscribe(sub_id);
  457. subIDs.remove(i);
  458. }
  459. }
  460. virtual void validateCache()
  461. {
  462. if (!subscription)
  463. subscription.setown( new CSdsSubscription() );
  464. subscription->handleEnvironmentChange();
  465. }
  466. private:
  467. IRemoteConnection* connect(const char *xpath, unsigned flags)
  468. {
  469. return querySDS().connect(xpath, myProcessSession(), flags, SDS_LOCK_TIMEOUT);
  470. }
  471. };
  472. static CEnvironmentFactory *factory=nullptr;
  473. void CEnvironmentFactory::clientShutdown()
  474. {
  475. closeEnvironment();
  476. }
  477. MODULE_INIT(INIT_PRIORITY_ENV_ENVIRONMENT)
  478. {
  479. return true;
  480. }
  481. MODULE_EXIT()
  482. {
  483. ::Release(factory);
  484. }
  485. //==========================================================================================
  486. class CConstEnvBase : public CInterface
  487. {
  488. protected:
  489. const CLocalEnvironment* env; // Not linked - would be circular....
  490. // That could cause problems
  491. Linked<IPropertyTree> root;
  492. public:
  493. CConstEnvBase(const CLocalEnvironment* _env, IPropertyTree *_root)
  494. : env(_env), root(_root)
  495. {
  496. }
  497. IStringVal& getXML(IStringVal &str) const
  498. {
  499. StringBuffer x;
  500. toXML(root->queryBranch("."), x);
  501. str.set(x.str());
  502. return str;
  503. };
  504. IStringVal& getName(IStringVal &str) const
  505. {
  506. str.set(root->queryProp("@name"));
  507. return str;
  508. }
  509. IPropertyTree& getPTree() const
  510. {
  511. return *LINK(root);
  512. }
  513. };
  514. #define IMPLEMENT_ICONSTENVBASE \
  515. virtual IStringVal& getXML(IStringVal &str) const { return CConstEnvBase::getXML(str); } \
  516. virtual IStringVal& getName(IStringVal &str) const { return CConstEnvBase::getName(str); } \
  517. virtual IPropertyTree& getPTree() const { return CConstEnvBase::getPTree(); }
  518. //==========================================================================================
  519. class CConstDomainInfo : public CConstEnvBase, implements IConstDomainInfo
  520. {
  521. public:
  522. IMPLEMENT_IINTERFACE;
  523. IMPLEMENT_ICONSTENVBASE;
  524. CConstDomainInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  525. virtual void getAccountInfo(IStringVal &name, IStringVal &pw) const
  526. {
  527. if (root->hasProp("@username"))
  528. name.set(root->queryProp("@username"));
  529. else
  530. name.clear();
  531. if (root->hasProp("@password"))
  532. {
  533. StringBuffer pwd;
  534. decrypt(pwd, root->queryProp("@password"));
  535. pw.set(pwd.str());
  536. }
  537. else
  538. pw.clear();
  539. }
  540. virtual void getSnmpSecurityString(IStringVal & securityString) const
  541. {
  542. if (root->hasProp("@snmpSecurityString"))
  543. {
  544. StringBuffer sec_string;
  545. decrypt(sec_string, root->queryProp("@snmpSecurityString"));
  546. securityString.set(sec_string.str());
  547. }
  548. else
  549. securityString.set("");
  550. }
  551. virtual void getSSHAccountInfo(IStringVal &name, IStringVal &sshKeyFile, IStringVal& sshKeyPassphrase) const
  552. {
  553. if (root->hasProp("@username"))
  554. name.set(root->queryProp("@username"));
  555. else
  556. name.clear();
  557. if (root->hasProp("@sshKeyFile"))
  558. sshKeyFile.set(root->queryProp("@sshKeyFile"));
  559. else
  560. sshKeyFile.clear();
  561. if (root->hasProp("@sshKeyPassphrase"))
  562. sshKeyPassphrase.set(root->queryProp("@sshKeyPassphrase"));
  563. else
  564. sshKeyPassphrase.clear();
  565. }
  566. };
  567. //==========================================================================================
  568. struct mapOsEnums { EnvMachineOS val; const char *str; };
  569. static EnvMachineOS getEnum(IPropertyTree *p, const char *propname, mapOsEnums *map)
  570. {
  571. const char *v = p->queryProp(propname);
  572. if (v && *v)
  573. {
  574. while (map->str)
  575. {
  576. if (stricmp(v, map->str)==0)
  577. return map->val;
  578. map++;
  579. }
  580. throw MakeStringException(0, "Unknown operating system: \"%s\"", v);
  581. }
  582. return MachineOsUnknown;
  583. }
  584. struct mapStateEnums { EnvMachineState val; const char *str; };
  585. static EnvMachineState getEnum(IPropertyTree *p, const char *propname, mapStateEnums *map)
  586. {
  587. const char *v = p->queryProp(propname);
  588. if (v && *v)
  589. {
  590. while (map->str)
  591. {
  592. if (stricmp(v, map->str)==0)
  593. return map->val;
  594. map++;
  595. }
  596. assertex(!"Unexpected value in getEnum");
  597. }
  598. return MachineStateUnknown;
  599. }
  600. mapOsEnums OperatingSystems[] = {
  601. { MachineOsW2K, "W2K" },
  602. { MachineOsSolaris, "solaris" },
  603. { MachineOsLinux, "linux" },
  604. { MachineOsSize, nullptr }
  605. };
  606. mapStateEnums MachineStates[] = {
  607. { MachineStateAvailable, "Available" },
  608. { MachineStateUnavailable, "Unavailable" },
  609. { MachineStateUnknown, "Unknown" }
  610. };
  611. //==========================================================================================
  612. class CConstMachineInfo : public CConstEnvBase, implements IConstMachineInfo
  613. {
  614. public:
  615. IMPLEMENT_IINTERFACE;
  616. IMPLEMENT_ICONSTENVBASE;
  617. CConstMachineInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  618. virtual IConstDomainInfo* getDomain() const
  619. {
  620. return env->getDomain(root->queryProp("@domain"));
  621. }
  622. virtual IStringVal& getNetAddress(IStringVal &str) const
  623. {
  624. str.set(root->queryProp("@netAddress"));
  625. return str;
  626. }
  627. virtual IStringVal& getDescription(IStringVal &str) const
  628. {
  629. UNIMPLEMENTED;
  630. }
  631. virtual unsigned getNicSpeedMbitSec() const
  632. {
  633. const char * v = root->queryProp("@nicSpeed");
  634. if (v && *v)
  635. return atoi(v);
  636. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  637. if (type)
  638. return type->getNicSpeedMbitSec();
  639. return 0;
  640. }
  641. virtual EnvMachineOS getOS() const
  642. {
  643. EnvMachineOS os = getEnum(root, "@opSys", OperatingSystems);
  644. if (os != MachineOsUnknown)
  645. return os;
  646. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  647. if (type)
  648. return type->getOS();
  649. return MachineOsUnknown;
  650. }
  651. virtual EnvMachineState getState() const
  652. {
  653. return getEnum(root, "@state", MachineStates);
  654. }
  655. };
  656. //==========================================================================================
  657. class CConstComputerTypeInfo : public CConstEnvBase, implements IConstComputerTypeInfo
  658. {
  659. public:
  660. IMPLEMENT_IINTERFACE;
  661. IMPLEMENT_ICONSTENVBASE;
  662. CConstComputerTypeInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  663. virtual EnvMachineOS getOS() const
  664. {
  665. EnvMachineOS os = getEnum(root, "@opSys", OperatingSystems);
  666. if (os != MachineOsUnknown)
  667. return os;
  668. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  669. if (type && (type.get() != this))
  670. return type->getOS();
  671. return MachineOsUnknown;
  672. }
  673. virtual unsigned getNicSpeedMbitSec() const
  674. {
  675. const char * v = root->queryProp("@nicSpeed");
  676. if (v && *v)
  677. return atoi(v);
  678. Owned<IConstComputerTypeInfo> type = env->getComputerType(root->queryProp("@computerType"));
  679. if (type && (type.get() != this))
  680. return type->getNicSpeedMbitSec();
  681. return 0;
  682. }
  683. };
  684. //==========================================================================================
  685. class CConstInstanceInfo : public CConstEnvBase, implements IConstInstanceInfo
  686. {
  687. public:
  688. IMPLEMENT_IINTERFACE;
  689. IMPLEMENT_ICONSTENVBASE;
  690. CConstInstanceInfo(const CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root)
  691. {
  692. }
  693. virtual IConstMachineInfo * getMachine() const
  694. {
  695. return env->getMachine(root->queryProp("@computer"));
  696. }
  697. virtual IStringVal & getEndPoint(IStringVal & str) const
  698. {
  699. SCMStringBuffer ep;
  700. Owned<IConstMachineInfo> machine = getMachine();
  701. if (machine)
  702. {
  703. machine->getNetAddress(ep);
  704. const char *port = root->queryProp("@port");
  705. if (port)
  706. ep.s.append(':').append(port);
  707. }
  708. str.set(ep.str());
  709. return str;
  710. }
  711. virtual IStringVal & getExecutableDirectory(IStringVal & str) const
  712. {
  713. // this is the deploy directory so uses local path separators (I suspect this call is LEGACY now)
  714. SCMStringBuffer ep;
  715. Owned<IConstMachineInfo> machine = getMachine();
  716. if (machine)
  717. {
  718. machine->getNetAddress(ep);
  719. ep.s.insert(0, PATHSEPSTR PATHSEPSTR);
  720. }
  721. ep.s.append(PATHSEPCHAR).append(root->queryProp("@directory"));
  722. str.set(ep.str());
  723. return str;
  724. }
  725. virtual bool doGetRunInfo(IStringVal & progpath, IStringVal & workdir, const char *defprogname, bool useprog) const
  726. {
  727. // this is remote path i.e. path should match *target* nodes format
  728. Owned<IConstMachineInfo> machine = getMachine();
  729. if (!machine)
  730. return false;
  731. char psep;
  732. bool appendexe;
  733. switch (machine->getOS())
  734. {
  735. case MachineOsSolaris:
  736. case MachineOsLinux:
  737. psep = '/';
  738. appendexe = false;
  739. break;
  740. default:
  741. psep = '\\';
  742. appendexe = true;
  743. }
  744. StringBuffer tmp;
  745. const char *program = useprog?root->queryProp("@program"):nullptr; // if program specified assume absolute
  746. if (!program||!*program)
  747. {
  748. SCMStringBuffer ep;
  749. machine->getNetAddress(ep);
  750. const char *dir = root->queryProp("@directory");
  751. if (dir)
  752. {
  753. if (isPathSepChar(*dir))
  754. dir++;
  755. if (!*dir)
  756. return false;
  757. tmp.append(psep).append(psep).append(ep.s).append(psep);
  758. do {
  759. if (isPathSepChar(*dir))
  760. tmp.append(psep);
  761. else
  762. tmp.append(*dir);
  763. dir++;
  764. } while (*dir);
  765. if (!isPathSepChar(tmp.charAt(tmp.length()-1)))
  766. tmp.append(psep);
  767. tmp.append(defprogname);
  768. size32_t l = strlen(defprogname);
  769. if (appendexe&&((l<5)||(stricmp(defprogname+l-4,".exe")!=0)))
  770. tmp.append(".exe");
  771. }
  772. program = tmp.str();
  773. }
  774. progpath.set(program);
  775. const char *workd = root->queryProp("@workdir"); // if program specified assume absolute
  776. workdir.set(workd?workd:"");
  777. return true;
  778. }
  779. virtual bool getRunInfo(IStringVal & progpath, IStringVal & workdir, const char *defprogname) const
  780. {
  781. return doGetRunInfo(progpath,workdir,defprogname,true);
  782. }
  783. virtual unsigned getPort() const
  784. {
  785. return root->getPropInt("@port", 0);
  786. }
  787. };
  788. class CConstDropZoneInfo : public CConstEnvBase, implements IConstDropZoneInfo
  789. {
  790. public:
  791. IMPLEMENT_IINTERFACE;
  792. IMPLEMENT_ICONSTENVBASE;
  793. CConstDropZoneInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root) {}
  794. virtual IStringVal& getComputerName(IStringVal &str) const
  795. {
  796. str.set(root->queryProp("@computer"));
  797. return str;
  798. }
  799. virtual IStringVal& getDescription(IStringVal &str) const
  800. {
  801. str.set(root->queryProp("@description"));
  802. return str;
  803. }
  804. virtual IStringVal& getDirectory(IStringVal &str) const
  805. {
  806. str.set(root->queryProp("@directory"));
  807. return str;
  808. }
  809. virtual IStringVal& getUMask(IStringVal &str) const
  810. {
  811. if (root->hasProp("@umask"))
  812. str.set(root->queryProp("@umask"));
  813. return str;
  814. }
  815. virtual bool isECLWatchVisible() const
  816. {
  817. return root->getPropBool("@ECLWatchVisible", true);
  818. }
  819. };
  820. #if 0
  821. //==========================================================================================
  822. class CConstProcessInfo : public CConstEnvBase, implements IConstProcessInfo
  823. {
  824. IArrayOf<IConstInstanceInfo> w;
  825. CArrayIteratorOf<IInterface, IIterator> it;
  826. public:
  827. IMPLEMENT_IINTERFACE;
  828. IMPLEMENT_ICONSTENVBASE;
  829. CConstProcessInfo(CLocalEnvironment *env, IPropertyTree *root) : CConstEnvBase(env, root), it(w)
  830. {
  831. Owned<IPropertyTreeIterator> _it = root->getElements("*"); // MORE - should be instance
  832. for (_it->first(); _it->isValid(); _it->next())
  833. {
  834. IPropertyTree *rp = &_it->query();
  835. w.append(*new CConstInstanceInfo(env, rp)); // CConstInstanceInfo will link rp
  836. }
  837. }
  838. bool first() { return it.first(); }
  839. bool isValid() { return it.isValid(); }
  840. bool next() { return it.next(); }
  841. IConstInstanceInfo & query() { return (IConstInstanceInfo &) it.query();}
  842. virtual IConstInstanceInfo * getInstance(const char *domain)
  843. {
  844. for (int pass=0; pass<2; pass++)
  845. {
  846. ForEachItemIn(idx, w)
  847. {
  848. Owned<IConstMachineInfo> m = w.item(idx).getMachine();
  849. if (m)
  850. {
  851. Owned<IConstDomainInfo> dm = m->getDomain();
  852. if (dm)
  853. {
  854. StringBuffer thisdomain;
  855. //dm->getName(StringBufferAdaptor(thisdomain)); // confuses g++
  856. StringBufferAdaptor strval(thisdomain);
  857. dm->getName(strval);
  858. if (thisdomain.length() && strcmp(domain, thisdomain.str())==0)
  859. return LINK(&w.item(idx));
  860. }
  861. }
  862. }
  863. }
  864. return nullptr;
  865. }
  866. };
  867. #endif
  868. //==========================================================================================
  869. CLocalEnvironment::CLocalEnvironment(const char* environmentFile)
  870. {
  871. if (environmentFile && *environmentFile)
  872. {
  873. IPropertyTree* root = createPTreeFromXMLFile(environmentFile);
  874. if (root)
  875. p.set(root);
  876. }
  877. init();
  878. }
  879. CLocalEnvironment::CLocalEnvironment(IRemoteConnection *_conn, IPropertyTree* root/*=nullptr*/,
  880. const char* path/*="/Environment"*/)
  881. : xPath(path)
  882. {
  883. conn.set(_conn);
  884. if (root)
  885. p.set(root);
  886. else
  887. p.setown(conn->getRoot());
  888. init();
  889. }
  890. void CLocalEnvironment::init()
  891. {
  892. machineCacheBuilt = false;
  893. dropZoneCacheBuilt = false;
  894. numOfMachines = 0;
  895. numOfDropZones = 0;
  896. numOfDropzonesByComputer.setown(createPTree("computers"));
  897. isDropZoneRestrictionLoaded = false;
  898. }
  899. CLocalEnvironment::~CLocalEnvironment()
  900. {
  901. if (conn)
  902. conn->rollback();
  903. }
  904. IEnvironment& CLocalEnvironment::lock() const
  905. {
  906. return *new CLockedEnvironment((CLocalEnvironment*)this);
  907. }
  908. IStringVal & CLocalEnvironment::getName(IStringVal & str) const
  909. {
  910. synchronized procedure(safeCache);
  911. str.set(p->queryProp("@name"));
  912. return str;
  913. }
  914. IStringVal & CLocalEnvironment::getXML(IStringVal & str) const
  915. {
  916. StringBuffer xml;
  917. {
  918. synchronized procedure(safeCache);
  919. toXML(p->queryBranch("."), xml);
  920. }
  921. str.set(xml.str());
  922. return str;
  923. }
  924. IPropertyTree & CLocalEnvironment::getPTree() const
  925. {
  926. synchronized procedure(safeCache);
  927. return *LINK(p);
  928. }
  929. IConstEnvBase * CLocalEnvironment::getCache(const char *path) const
  930. {
  931. IConstEnvBase * ret = cache.getValue(path);
  932. ::Link(ret);
  933. return ret;
  934. }
  935. void CLocalEnvironment::setCache(const char *path, IConstEnvBase *value) const
  936. {
  937. cache.setValue(path, value);
  938. }
  939. IConstDomainInfo * CLocalEnvironment::getDomain(const char * name) const
  940. {
  941. if (!name)
  942. return nullptr;
  943. StringBuffer xpath;
  944. xpath.appendf("Hardware/Domain[@name=\"%s\"]", name);
  945. synchronized procedure(safeCache);
  946. IConstEnvBase *cached = getCache(xpath.str());
  947. if (!cached)
  948. {
  949. IPropertyTree *d = p->queryPropTree(xpath.str());
  950. if (!d)
  951. return nullptr;
  952. cached = new CConstDomainInfo((CLocalEnvironment *) this, d);
  953. setCache(xpath.str(), cached);
  954. }
  955. return (IConstDomainInfo *) cached;
  956. }
  957. void CLocalEnvironment::buildMachineCache() const
  958. {
  959. synchronized procedure(safeCache);
  960. if (!machineCacheBuilt)
  961. {
  962. Owned<IPropertyTreeIterator> it = p->getElements("Hardware/Computer");
  963. ForEach(*it)
  964. {
  965. const char *name = it->query().queryProp("@name");
  966. if (name)
  967. {
  968. StringBuffer x("Hardware/Computer[@name=\"");
  969. x.append(name).append("\"]");
  970. Owned<IConstEnvBase> cached = new CConstMachineInfo((CLocalEnvironment *) this, &it->query());
  971. cache.setValue(x.str(), cached);
  972. }
  973. const char * netAddress = it->query().queryProp("@netAddress");
  974. if (netAddress)
  975. {
  976. StringBuffer x("Hardware/Computer[@netAddress=\"");
  977. x.append(netAddress).append("\"]");
  978. Owned<IConstEnvBase> cached = new CConstMachineInfo((CLocalEnvironment *) this, &it->query());
  979. cache.setValue(x.str(), cached);
  980. IpAddress ip;
  981. ip.ipset(netAddress);
  982. if (ip.isLocal())
  983. cache.setValue("Hardware/Computer[@netAddress=\".\"]", cached);
  984. }
  985. numOfMachines++;
  986. StringBuffer x("Hardware/Computer[@id=\"");
  987. x.append(MACHINE_PREFIX).append(numOfMachines).append("\"]");
  988. Owned<IConstEnvBase> cached = new CConstMachineInfo((CLocalEnvironment *) this, &it->query());
  989. cache.setValue(x.str(), cached);
  990. }
  991. machineCacheBuilt = true;
  992. }
  993. }
  994. void CLocalEnvironment::buildDropZoneCache() const
  995. {
  996. synchronized procedure(safeCache);
  997. if (!dropZoneCacheBuilt)
  998. {
  999. Owned<IPropertyTreeIterator> it = p->getElements("Software/DropZone");
  1000. ForEach(*it)
  1001. {
  1002. const char *name = it->query().queryProp("@name");
  1003. if (name)
  1004. {
  1005. StringBuffer x("Software/DropZone[@name=\"");
  1006. x.append(name).append("\"]");
  1007. Owned<IConstEnvBase> cached = new CConstDropZoneInfo((CLocalEnvironment *) this, &it->query());
  1008. cache.setValue(x.str(), cached);
  1009. }
  1010. const char * computer = it->query().queryProp("@computer");
  1011. if (computer)
  1012. {
  1013. StringBuffer xpath;
  1014. createComputerNameXpath(computer, xpath);
  1015. unsigned numOfDropZoneByComputer = numOfDropzonesByComputer->getPropInt(xpath.str(), -1);
  1016. if (numOfDropZoneByComputer == -1)
  1017. {
  1018. numOfDropZoneByComputer = 0;
  1019. IPropertyTree * val = createPTree("numberOfDropZones");
  1020. val->addPropInt("@number",numOfDropZoneByComputer);
  1021. val->addProp("@name", computer);
  1022. numOfDropzonesByComputer->addPropTree("numberOfDropZones",val);
  1023. }
  1024. numOfDropZoneByComputer++;
  1025. numOfDropzonesByComputer->setPropInt(xpath.str(), numOfDropZoneByComputer);
  1026. StringBuffer x("Software/DropZone[@computer=\"");
  1027. x.append(computer);
  1028. x.append(DROPZONE_BY_MACHINE_SUFFIX).append(numOfDropZoneByComputer);
  1029. x.append("\"]");
  1030. Owned<IConstEnvBase> cached = new CConstDropZoneInfo((CLocalEnvironment *) this, &it->query());
  1031. cache.setValue(x.str(), cached);
  1032. }
  1033. numOfDropZones++;
  1034. StringBuffer x("Software/DropZone[@id=\"");
  1035. x.append(DROPZONE_SUFFIX).append(numOfDropZones).append("\"]");
  1036. Owned<IConstEnvBase> cached = new CConstDropZoneInfo((CLocalEnvironment *) this, &it->query());
  1037. cache.setValue(x.str(), cached);
  1038. }
  1039. dropZoneCacheBuilt = true;
  1040. }
  1041. }
  1042. IConstComputerTypeInfo * CLocalEnvironment::getComputerType(const char * name) const
  1043. {
  1044. if (!name)
  1045. return nullptr;
  1046. StringBuffer xpath;
  1047. xpath.appendf("Hardware/ComputerType[@name=\"%s\"]", name);
  1048. synchronized procedure(safeCache);
  1049. IConstEnvBase *cached = getCache(xpath.str());
  1050. if (!cached)
  1051. {
  1052. IPropertyTree *d = p->queryPropTree(xpath.str());
  1053. if (!d)
  1054. return nullptr;
  1055. cached = new CConstComputerTypeInfo((CLocalEnvironment *) this, d);
  1056. setCache(xpath.str(), cached);
  1057. }
  1058. return (CConstComputerTypeInfo *) cached;
  1059. }
  1060. IConstMachineInfo * CLocalEnvironment::getMachine(const char * name) const
  1061. {
  1062. if (!name)
  1063. return nullptr;
  1064. buildMachineCache();
  1065. StringBuffer xpath;
  1066. xpath.appendf("Hardware/Computer[@name=\"%s\"]", name);
  1067. synchronized procedure(safeCache);
  1068. IConstEnvBase *cached = getCache(xpath.str());
  1069. if (!cached)
  1070. {
  1071. IPropertyTree *d = p->queryPropTree(xpath.str());
  1072. if (!d)
  1073. return nullptr;
  1074. cached = new CConstMachineInfo((CLocalEnvironment *) this, d);
  1075. setCache(xpath.str(), cached);
  1076. }
  1077. return (CConstMachineInfo *) cached;
  1078. }
  1079. IConstMachineInfo * CLocalEnvironment::getMachineByAddress(const char * machineIp) const
  1080. {
  1081. if (!machineIp)
  1082. return nullptr;
  1083. buildMachineCache();
  1084. Owned<IPropertyTreeIterator> iter;
  1085. StringBuffer xpath;
  1086. xpath.appendf("Hardware/Computer[@netAddress=\"%s\"]", machineIp);
  1087. synchronized procedure(safeCache);
  1088. IConstEnvBase *cached = getCache(xpath.str());
  1089. if (!cached)
  1090. {
  1091. IPropertyTree *d = p->queryPropTree(xpath.str());
  1092. if (!d)
  1093. {
  1094. // I suspect not in the original spirit of this but look for resolved IP
  1095. Owned<IPropertyTreeIterator> iter = p->getElements("Hardware/Computer");
  1096. IpAddress ip;
  1097. ip.ipset(machineIp);
  1098. ForEach(*iter)
  1099. {
  1100. IPropertyTree &computer = iter->query();
  1101. IpAddress ip2;
  1102. const char *ips = computer.queryProp("@netAddress");
  1103. if (ips&&*ips)
  1104. {
  1105. ip2.ipset(ips);
  1106. if (ip.ipequals(ip2))
  1107. {
  1108. d = &computer;
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. }
  1114. if (!d)
  1115. return nullptr;
  1116. StringBuffer xpath1;
  1117. xpath1.appendf("Hardware/Computer[@name=\"%s\"]", d->queryProp("@name"));
  1118. cached = getCache(xpath1.str());
  1119. if (!cached)
  1120. {
  1121. cached = new CConstMachineInfo((CLocalEnvironment *) this, d);
  1122. setCache(xpath1.str(), cached);
  1123. setCache(xpath.str(), cached);
  1124. }
  1125. }
  1126. return (CConstMachineInfo *) cached;
  1127. }
  1128. IConstMachineInfo * CLocalEnvironment::getMachineByIndex(unsigned index) const
  1129. {
  1130. if (!numOfMachines || (index == 0))
  1131. return nullptr;
  1132. buildMachineCache();
  1133. if (index > numOfMachines)
  1134. return nullptr;
  1135. StringBuffer xpath("Hardware/Computer[@id=\"");
  1136. xpath.append(MACHINE_PREFIX).append(index).append("\"]");
  1137. synchronized procedure(safeCache);
  1138. return (IConstMachineInfo *) getCache(xpath.str());
  1139. }
  1140. IConstMachineInfo * CLocalEnvironment::getMachineForLocalHost() const
  1141. {
  1142. buildMachineCache();
  1143. synchronized procedure(safeCache);
  1144. return (CConstMachineInfo *) getCache("Hardware/Computer[@netAddress=\".\"]");
  1145. }
  1146. IConstDropZoneInfo * CLocalEnvironment::getDropZone(const char * name) const
  1147. {
  1148. if (!name)
  1149. return nullptr;
  1150. buildDropZoneCache();
  1151. VStringBuffer xpath("Software/DropZone[@name=\"%s\"]", name);
  1152. synchronized procedure(safeCache);
  1153. return (CConstDropZoneInfo *) getCache(xpath.str());
  1154. }
  1155. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByComputer(const char * computer) const
  1156. {
  1157. if (!computer)
  1158. return nullptr;
  1159. buildDropZoneCache();
  1160. StringBuffer x;
  1161. createComputerNameXpath(computer, x);
  1162. unsigned numOfDropZoneByComputer = numOfDropzonesByComputer->getPropInt(x, -1);
  1163. if (numOfDropZoneByComputer == -1)
  1164. return nullptr;
  1165. StringBuffer xpath("Software/DropZone[@computer=\"");
  1166. xpath.append(computer);
  1167. xpath.append(DROPZONE_BY_MACHINE_SUFFIX).append(DEFAULT_DROPZONE_INDEX);
  1168. xpath.append("\"]");
  1169. synchronized procedure(safeCache);
  1170. return (CConstDropZoneInfo *) getCache(xpath.str());
  1171. }
  1172. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByComputer(const char * computer, const char * dzname) const
  1173. {
  1174. IConstDropZoneInfo * dzInfo = getDropZone(dzname);
  1175. if (!dzInfo)
  1176. return nullptr;
  1177. SCMStringBuffer cachedComputer;
  1178. dzInfo->getComputerName(cachedComputer);
  1179. if (!streq(computer, cachedComputer.str()))
  1180. return nullptr;
  1181. return dzInfo;
  1182. }
  1183. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByComputerByIndex(const char * computer, unsigned index) const
  1184. {
  1185. if (!computer || (index == 0))
  1186. return nullptr;
  1187. buildDropZoneCache();
  1188. if (index > getNumberOfDropZonesByComputer(computer))
  1189. return nullptr;
  1190. StringBuffer xpath("Software/DropZone[@computer=\"");
  1191. xpath.append(computer).append(DROPZONE_BY_MACHINE_SUFFIX).append(index).append("\"]");
  1192. synchronized procedure(safeCache);
  1193. return (CConstDropZoneInfo *) getCache(xpath.str());
  1194. }
  1195. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByIndex(unsigned index) const
  1196. {
  1197. if (!numOfDropZones || (index == 0))
  1198. return nullptr;
  1199. buildDropZoneCache();
  1200. if (index > numOfDropZones)
  1201. return nullptr;
  1202. StringBuffer xpath("Software/DropZone[@id=\"");
  1203. xpath.append(DROPZONE_SUFFIX).append(index).append("\"]");
  1204. synchronized procedure(safeCache);
  1205. return (CConstDropZoneInfo *) getCache(xpath.str());
  1206. }
  1207. IConstInstanceInfo * CLocalEnvironment::getInstance(const char *type, const char *version, const char *domain) const
  1208. {
  1209. StringBuffer xpath("Software/");
  1210. xpath.append(type);
  1211. if (version)
  1212. xpath.append("[@version='").append(version).append("']");
  1213. xpath.append("/Instance");
  1214. synchronized procedure(safeCache);
  1215. Owned<IPropertyTreeIterator> _it = p->getElements(xpath);
  1216. for (_it->first(); _it->isValid(); _it->next())
  1217. {
  1218. IPropertyTree *rp = &_it->query();
  1219. Owned<CConstInstanceInfo> inst = new CConstInstanceInfo(this, rp); // CConstInstanceInfo will link rp
  1220. Owned<IConstMachineInfo> m = inst->getMachine();
  1221. if (m)
  1222. {
  1223. Owned<IConstDomainInfo> dm = m->getDomain();
  1224. if (dm)
  1225. {
  1226. SCMStringBuffer thisdomain;
  1227. dm->getName(thisdomain);
  1228. if (thisdomain.length() && strcmp(domain, thisdomain.str())==0)
  1229. return inst.getClear();
  1230. }
  1231. }
  1232. }
  1233. return nullptr;
  1234. }
  1235. CConstInstanceInfo * CLocalEnvironment::getInstanceByIP(const char *type, const char *version, IpAddress &ip) const
  1236. {
  1237. StringBuffer xpath("Software/");
  1238. xpath.append(type);
  1239. if (version)
  1240. xpath.append("[@version='").append(version).append("']");
  1241. xpath.append("/Instance");
  1242. synchronized procedure(safeCache);
  1243. assertex(p);
  1244. Owned<IPropertyTreeIterator> _it = p->getElements(xpath);
  1245. assertex(_it);
  1246. for (_it->first(); _it->isValid(); _it->next())
  1247. {
  1248. IPropertyTree *rp = &_it->query();
  1249. assertex(rp);
  1250. Owned<CConstInstanceInfo> inst = new CConstInstanceInfo(this, rp); // CConstInstanceInfo will link rp
  1251. Owned<IConstMachineInfo> m = inst->getMachine();
  1252. if (m)
  1253. {
  1254. SCMStringBuffer eps;
  1255. m->getNetAddress(eps);
  1256. SocketEndpoint ep(eps.str());
  1257. if (ep.ipequals(ip))
  1258. return inst.getClear();
  1259. }
  1260. }
  1261. return nullptr;
  1262. }
  1263. void CLocalEnvironment::unlockRemote()
  1264. {
  1265. #if 0
  1266. conn->commit(true);
  1267. conn->changeMode(0, SDS_LOCK_TIMEOUT);
  1268. #else
  1269. if (conn)
  1270. {
  1271. synchronized procedure(safeCache);
  1272. p.clear();
  1273. conn.setown(querySDS().connect(xPath.str(), myProcessSession(), 0, SDS_LOCK_TIMEOUT));
  1274. p.setown(conn->getRoot());
  1275. }
  1276. #endif
  1277. }
  1278. void CLocalEnvironment::preload()
  1279. {
  1280. synchronized procedure(safeCache);
  1281. p->queryBranch(".");
  1282. }
  1283. void CLocalEnvironment::setXML(const char *xml)
  1284. {
  1285. Owned<IPropertyTree> newRoot = createPTreeFromXMLString(xml);
  1286. synchronized procedure(safeCache);
  1287. Owned<IPropertyTreeIterator> it = p->getElements("*");
  1288. ForEach(*it)
  1289. {
  1290. p->removeTree(&it->query());
  1291. }
  1292. it.setown(newRoot->getElements("*"));
  1293. ForEach(*it)
  1294. {
  1295. IPropertyTree *sub = &it->get();
  1296. p->addPropTree(sub->queryName(), sub);
  1297. }
  1298. }
  1299. bool CLocalEnvironment::getRunInfo(IStringVal & path, IStringVal & dir, const char * tag, const char * version, const char *machineaddr, const char *defprogname) const
  1300. {
  1301. try
  1302. {
  1303. // PrintLog("getExecutablePath %s %s %s", tag, version, machineaddr);
  1304. // first see if local machine with deployed on
  1305. SocketEndpoint ep(machineaddr);
  1306. Owned<CConstInstanceInfo> ipinstance = getInstanceByIP(tag, version, ep);
  1307. if (ipinstance)
  1308. {
  1309. StringAttr testpath;
  1310. StringAttrAdaptor teststrval(testpath);
  1311. if (ipinstance->doGetRunInfo(teststrval,dir,defprogname,false))
  1312. { // this returns full string
  1313. RemoteFilename rfn;
  1314. rfn.setRemotePath(testpath.get());
  1315. Owned<IFile> file = createIFile(rfn);
  1316. if (file->exists())
  1317. {
  1318. StringBuffer tmp;
  1319. rfn.getLocalPath(tmp);
  1320. path.set(tmp.str());
  1321. return true;
  1322. }
  1323. }
  1324. }
  1325. Owned<IConstMachineInfo> machine = getMachineByAddress(machineaddr);
  1326. if (!machine)
  1327. {
  1328. LOG(MCdebugInfo, unknownJob, "Unable to find machine for %s", machineaddr);
  1329. return false;
  1330. }
  1331. StringAttr targetdomain;
  1332. Owned<IConstDomainInfo> domain = machine->getDomain();
  1333. if (!domain)
  1334. {
  1335. LOG(MCdebugInfo, unknownJob, "Unable to find domain for %s", machineaddr);
  1336. return false;
  1337. }
  1338. //domain->getName(StringAttrAdaptor(targetdomain)); // confuses g++
  1339. StringAttrAdaptor strval(targetdomain);
  1340. domain->getName(strval);
  1341. Owned<IConstInstanceInfo> instance = getInstance(tag, version, targetdomain);
  1342. if (!instance)
  1343. {
  1344. LOG(MCdebugInfo, unknownJob, "Unable to find process %s for domain %s", tag, targetdomain.get());
  1345. return false;
  1346. }
  1347. return instance->getRunInfo(path,dir,defprogname);
  1348. }
  1349. catch (IException * e)
  1350. {
  1351. EXCLOG(e, "Extracting slave version");
  1352. e->Release();
  1353. return false;
  1354. }
  1355. }
  1356. void CLocalEnvironment::clearCache()
  1357. {
  1358. synchronized procedure(safeCache);
  1359. if (conn)
  1360. {
  1361. p.clear();
  1362. unsigned mode = 0;
  1363. try
  1364. {
  1365. conn->reload();
  1366. }
  1367. catch (IException *e)
  1368. {
  1369. EXCLOG(e, "Failed to reload connection");
  1370. e->Release();
  1371. mode = conn->queryMode();
  1372. conn.clear();
  1373. }
  1374. if (!conn)
  1375. conn.setown(querySDS().connect(xPath, myProcessSession(), mode, SDS_LOCK_TIMEOUT));
  1376. p.setown(conn->getRoot());
  1377. }
  1378. cache.kill();
  1379. numOfDropzonesByComputer.clear();
  1380. init();
  1381. resetPasswordsFromSDS();
  1382. }
  1383. IConstDropZoneInfo * CLocalEnvironment::getDropZoneByAddressPath(const char * netaddress, const char *targetFilePath) const
  1384. {
  1385. IConstDropZoneInfo * retVal = nullptr;
  1386. Owned<IConstMachineInfo> machineInfo = getMachineByAddress(netaddress);
  1387. if (!machineInfo)
  1388. return retVal;
  1389. SCMStringBuffer machineName;
  1390. machineInfo->getName(machineName);
  1391. const char * pmachineName = machineName.str();
  1392. Owned<IConstDropZoneInfoIterator> zoneIt= getDropZoneIteratorByComputer(pmachineName);
  1393. #ifdef _DEBUG
  1394. LOG(MCdebugInfo, unknownJob, "Check remote drop zones (%d) by iterator on '%s':", zoneIt->count(), pmachineName);
  1395. #endif
  1396. SCMStringBuffer dropzonePath;
  1397. const char * pdropzonePath = nullptr;
  1398. unsigned dropzonePathLen = _MAX_PATH + 1;
  1399. ForEach(*zoneIt)
  1400. {
  1401. SCMStringBuffer dropZoneDir;
  1402. zoneIt->query().getDirectory(dropZoneDir);
  1403. StringBuffer fullDropZoneDir(dropZoneDir.str());
  1404. addPathSepChar(fullDropZoneDir);
  1405. const char * pdropzoneDir = fullDropZoneDir.str();
  1406. // Check target file path starts with this Drop zone path
  1407. // the drop zone paths can be nested (nothing forbids it) like
  1408. // dz1: /a/b/c/d
  1409. // dz2: /a/b/c
  1410. // dz3: /a/b
  1411. // check all and select the shortest
  1412. if (strncmp(pdropzoneDir, targetFilePath, strlen(pdropzoneDir)) == 0)
  1413. {
  1414. if (dropzonePathLen > strlen(pdropzoneDir))
  1415. {
  1416. dropzonePathLen = strlen(pdropzoneDir);
  1417. dropzonePath.set(pdropzoneDir);
  1418. pdropzonePath = dropzonePath.str();
  1419. retVal = &zoneIt->query();
  1420. }
  1421. }
  1422. }
  1423. return retVal;
  1424. }
  1425. IConstDropZoneInfoIterator * CLocalEnvironment::getDropZoneIteratorByComputer(const char * computer) const
  1426. {
  1427. return new CConstDropZoneIteratorByComputer(computer);
  1428. }
  1429. IConstDropZoneInfoIterator * CLocalEnvironment::getDropZoneIterator() const
  1430. {
  1431. return new CConstDropZoneInfoIterator();
  1432. }
  1433. IConstMachineInfoIterator * CLocalEnvironment::getMachineIterator() const
  1434. {
  1435. return new CConstMachineInfoIterator();
  1436. }
  1437. bool CLocalEnvironment::isDropZoneRestrictionEnabled() const
  1438. {
  1439. if (!isDropZoneRestrictionLoaded)
  1440. {
  1441. dropZoneRestrictionEnabled = queryEnvironmentConf().getPropBool("useDropZoneRestriction", true);
  1442. isDropZoneRestrictionLoaded=true;
  1443. }
  1444. return dropZoneRestrictionEnabled;
  1445. }
  1446. //==========================================================================================
  1447. // Iterators implementation
  1448. CConstMachineInfoIterator::CConstMachineInfoIterator()
  1449. {
  1450. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  1451. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  1452. maxIndex = constEnv->getNumberOfMachines();
  1453. }
  1454. bool CConstMachineInfoIterator::first()
  1455. {
  1456. index = 1;
  1457. curr = constEnv->getMachineByIndex(index);
  1458. return (curr != nullptr);
  1459. }
  1460. bool CConstMachineInfoIterator::next()
  1461. {
  1462. if (index < maxIndex)
  1463. {
  1464. index++;
  1465. curr = constEnv->getMachineByIndex(index);
  1466. }
  1467. else
  1468. curr = nullptr;
  1469. return (curr ? true : false);
  1470. }
  1471. bool CConstMachineInfoIterator::isValid()
  1472. {
  1473. return curr != nullptr;
  1474. }
  1475. IConstMachineInfo & CConstMachineInfoIterator::query()
  1476. {
  1477. return *curr;
  1478. }
  1479. unsigned CConstMachineInfoIterator::count() const
  1480. {
  1481. return maxIndex;
  1482. }
  1483. //--------------------------------------------------
  1484. CConstDropZoneIteratorByComputer::CConstDropZoneIteratorByComputer(const char * computer)
  1485. {
  1486. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  1487. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  1488. computerName.set(computer);
  1489. maxIndex = constEnv->getNumberOfDropZonesByComputer(computer);
  1490. }
  1491. bool CConstDropZoneIteratorByComputer::first()
  1492. {
  1493. index = 1;
  1494. curr = constEnv->getDropZoneByComputerByIndex(computerName.str(), index);
  1495. return (curr != nullptr);
  1496. }
  1497. bool CConstDropZoneIteratorByComputer::next()
  1498. {
  1499. if (index < maxIndex)
  1500. {
  1501. index++;
  1502. curr = constEnv->getDropZoneByComputerByIndex(computerName.str(), index);
  1503. }
  1504. else
  1505. curr = nullptr;
  1506. return (curr ? true : false);
  1507. }
  1508. bool CConstDropZoneIteratorByComputer::isValid()
  1509. {
  1510. return curr != nullptr;
  1511. }
  1512. IConstDropZoneInfo & CConstDropZoneIteratorByComputer::query()
  1513. {
  1514. return *curr;
  1515. }
  1516. unsigned CConstDropZoneIteratorByComputer::count() const
  1517. {
  1518. return maxIndex;
  1519. }
  1520. //--------------------------------------------------
  1521. CConstDropZoneInfoIterator::CConstDropZoneInfoIterator()
  1522. {
  1523. Owned<IEnvironmentFactory> factory = getEnvironmentFactory();
  1524. constEnv.setown((CLocalEnvironment *)factory->openEnvironment());
  1525. maxIndex = constEnv->getNumberOfDropZones();
  1526. }
  1527. bool CConstDropZoneInfoIterator::first()
  1528. {
  1529. index = 1;
  1530. curr = constEnv->getDropZoneByIndex(index);
  1531. return (curr != nullptr);
  1532. }
  1533. bool CConstDropZoneInfoIterator::next()
  1534. {
  1535. if (index < maxIndex)
  1536. {
  1537. index++;
  1538. curr = constEnv->getDropZoneByIndex(index);
  1539. }
  1540. else
  1541. curr = nullptr;
  1542. return (curr ? true : false);
  1543. }
  1544. bool CConstDropZoneInfoIterator::isValid()
  1545. {
  1546. return curr != nullptr;
  1547. }
  1548. IConstDropZoneInfo & CConstDropZoneInfoIterator::query()
  1549. {
  1550. return *curr;
  1551. }
  1552. unsigned CConstDropZoneInfoIterator::count() const
  1553. {
  1554. return maxIndex;
  1555. }
  1556. //==========================================================================================
  1557. static CriticalSection getEnvSect;
  1558. extern ENVIRONMENT_API IEnvironmentFactory * getEnvironmentFactory()
  1559. {
  1560. CriticalBlock block(getEnvSect);
  1561. if (!factory)
  1562. {
  1563. factory = new CEnvironmentFactory();
  1564. addShutdownHook(*factory);
  1565. }
  1566. return LINK(factory);
  1567. }
  1568. extern ENVIRONMENT_API void closeEnvironment()
  1569. {
  1570. try
  1571. {
  1572. CEnvironmentFactory* pFactory;
  1573. {
  1574. //this method is not meant to be invoked by multiple
  1575. //threads concurrently but just in case...
  1576. CriticalBlock block(getEnvSect);
  1577. pFactory = factory;
  1578. factory = nullptr;
  1579. }
  1580. clearPasswordsFromSDS();
  1581. if (pFactory)
  1582. {
  1583. removeShutdownHook(*pFactory);
  1584. pFactory->close();
  1585. pFactory->Release();
  1586. }
  1587. }
  1588. catch (IException *e)
  1589. {
  1590. EXCLOG(e);
  1591. }
  1592. }