ws_fsService.cpp 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651
  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. #pragma warning (disable : 4786)
  14. #pragma warning (disable : 4129)
  15. #include <math.h>
  16. #include "jsocket.hpp"
  17. #include "dasds.hpp"
  18. #include "dadfs.hpp"
  19. #include "dautils.hpp"
  20. #include "daclient.hpp"
  21. #include "wshelpers.hpp"
  22. #include "dfuwu.hpp"
  23. #include "workunit.hpp"
  24. #include "ws_fsService.hpp"
  25. #ifdef _WIN32
  26. #include "windows.h"
  27. #endif
  28. #include "jlog.hpp"
  29. #include "dalienv.hpp"
  30. #include "dfuutil.hpp"
  31. #include "portlist.h"
  32. #include "sacmd.hpp"
  33. #include "exception_util.hpp"
  34. #include "LogicFileWrapper.hpp"
  35. #include "dameta.hpp"
  36. #include "daqueue.hpp"
  37. #define DFU_WU_URL "DfuWorkunitsAccess"
  38. #define DFU_EX_URL "DfuExceptionsAccess"
  39. #define FILE_SPRAY_URL "FileSprayAccess"
  40. #define FILE_DESPRAY_URL "FileDesprayAccess"
  41. #define WUDETAILS_REFRESH_MINS 1
  42. const unsigned dropZoneFileSearchMaxFiles = 1000;
  43. void SetResp(StringBuffer &resp, IConstDFUWorkUnit * wu, bool array);
  44. int Schedule::run()
  45. {
  46. PROGLOG("DfuWorkunit WUSchedule Thread started.");
  47. while(!stopping)
  48. {
  49. unsigned int waitTimeMillies = 1000*60;
  50. if (!detached)
  51. {
  52. try
  53. {
  54. if (waitTimeMillies == (unsigned)-1)
  55. {
  56. PROGLOG("WS_FS WUSchedule Thread Re-started.");
  57. waitTimeMillies = 1000*60;
  58. }
  59. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  60. Owned<IConstDFUWorkUnitIterator> itr = factory->getWorkUnitsByState(DFUstate_scheduled);
  61. itr->first();
  62. while(itr->isValid())
  63. {
  64. Owned<IConstDFUWorkUnit> wu = itr->get();
  65. CDateTime dt, now;
  66. now.setNow();
  67. try
  68. {
  69. wu->getTimeScheduled(dt);
  70. if (now.compare(dt) > 0)
  71. {
  72. StringAttr wuid(wu->queryId());
  73. wu.clear();
  74. submitDFUWorkUnit(wuid.get());
  75. }
  76. }
  77. catch(IException *e)
  78. {
  79. StringBuffer msg;
  80. IERRLOG("Exception %d:%s in WsWorkunits Schedule::run", e->errorCode(), e->errorMessage(msg).str());
  81. e->Release();
  82. }
  83. itr->next();
  84. }
  85. }
  86. catch(IException *e)
  87. {
  88. StringBuffer msg;
  89. IERRLOG("Exception %d:%s in WS_FS Schedule::run", e->errorCode(), e->errorMessage(msg).str());
  90. e->Release();
  91. }
  92. catch(...)
  93. {
  94. IERRLOG("Unknown exception in WS_FS Schedule::run");
  95. }
  96. }
  97. else
  98. {
  99. OWARNLOG("Detached from DALI, WS_FS schedule interrupted");
  100. waitTimeMillies = (unsigned)-1;
  101. }
  102. semSchedule.wait(waitTimeMillies);
  103. }
  104. return 0;
  105. }
  106. void CFileSprayEx::init(IPropertyTree *cfg, const char *process, const char *service)
  107. {
  108. #ifndef _CONTAINERIZED
  109. StringBuffer xpath;
  110. xpath.clear().appendf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/QueueLabel", process, service);
  111. cfg->getProp(xpath.str(), m_QueueLabel);
  112. StringArray qlist;
  113. getDFUServerQueueNames(qlist, nullptr);
  114. if (qlist.ordinality())
  115. {
  116. if (!m_QueueLabel.length())
  117. m_QueueLabel.append(qlist.item(0));
  118. else
  119. {
  120. bool found = false;
  121. ForEachItemIn(i, qlist)
  122. {
  123. const char* qname = qlist.item(i);
  124. if (qname && strieq(qname, m_QueueLabel.str()))
  125. {
  126. found = true;
  127. break;
  128. }
  129. }
  130. if (!found)
  131. throw MakeStringException(-1, "Invalid DFU Queue Label %s in configuration file", m_QueueLabel.str());
  132. }
  133. }
  134. xpath.setf("Software/EspProcess[@name=\"%s\"]/@PageCacheTimeoutSeconds", process);
  135. if (cfg->hasProp(xpath.str()))
  136. setPageCacheTimeoutMilliSeconds(cfg->getPropInt(xpath.str()));
  137. xpath.setf("Software/EspProcess[@name=\"%s\"]/@MaxPageCacheItems", process);
  138. if (cfg->hasProp(xpath.str()))
  139. setMaxPageCacheItems(cfg->getPropInt(xpath.str()));
  140. xpath.setf("Software/EspProcess[@name=\"%s\"]/EspService[@name=\"%s\"]/MonitorQueueLabel", process, service);
  141. cfg->getProp(xpath.str(), m_MonitorQueueLabel);
  142. directories.set(cfg->queryPropTree("Software/Directories"));
  143. #else
  144. // Using the first queue for now.
  145. // TODO: Re-design support for multiple queues
  146. Owned<IPropertyTreeIterator> dfuQueues = getComponentConfigSP()->getElements("dfuQueues");
  147. ForEach(*dfuQueues)
  148. {
  149. IPropertyTree & dfuQueue = dfuQueues->query();
  150. const char * dfuName = dfuQueue.queryProp("@name");
  151. if (!isEmptyString(dfuName))
  152. {
  153. getDfuQueueName(m_QueueLabel, dfuName);
  154. getDfuMonitorQueueName(m_MonitorQueueLabel, dfuName);
  155. break;
  156. }
  157. }
  158. #endif
  159. DBGLOG("queueLabel=%s", m_QueueLabel.str());
  160. DBGLOG("monitorQueueLabel=%s", m_MonitorQueueLabel.str());
  161. if (!daliClientActive())
  162. {
  163. OERRLOG("No Dali Connection Active.");
  164. throw MakeStringException(-1, "No Dali Connection Active. Please Specify a Dali to connect to in your configuration file");
  165. }
  166. m_sched.start();
  167. }
  168. StringBuffer& CFileSprayEx::getAcceptLanguage(IEspContext& context, StringBuffer& acceptLanguage)
  169. {
  170. context.getAcceptLanguage(acceptLanguage);
  171. if (!acceptLanguage.length())
  172. {
  173. acceptLanguage.set("en");
  174. return acceptLanguage;
  175. }
  176. acceptLanguage.setLength(2);
  177. VStringBuffer languageFile("%ssmc_xslt/nls/%s/hpcc.xml", getCFD(), acceptLanguage.str());
  178. if (!checkFileExists(languageFile.str()))
  179. acceptLanguage.set("en");
  180. return acceptLanguage;
  181. }
  182. void ParsePath(const char * fullPath, StringBuffer &ip, StringBuffer &filePath, StringBuffer &title)
  183. {
  184. ip.clear();
  185. filePath.clear();
  186. title.clear();
  187. if(fullPath == NULL || *fullPath == '\0')
  188. return;
  189. const char* ptr = fullPath;
  190. if(*ptr == '\\' && *(ptr+1) == '\\')
  191. {
  192. ptr += 2;
  193. while(*ptr != '\0' && *ptr != '\\')
  194. ptr++;
  195. ip.append(ptr - fullPath - 2, fullPath + 2);
  196. }
  197. filePath.append(ptr);
  198. ptr = fullPath + strlen(fullPath) - 1;
  199. while(ptr > fullPath && *ptr != '\\')
  200. ptr--;
  201. title.append(ptr + 1);
  202. }
  203. const char * const NODATETIME="1970-01-01T00:00:00Z";
  204. // Assign from a dfuwu workunit structure to an esp request workunit structure.
  205. static void DeepAssign(IEspContext &context, IConstDFUWorkUnit *src, IEspDFUWorkunit &dest)
  206. {
  207. if(src == NULL)
  208. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "'Source DFU workunit' doesn't exist.");
  209. StringBuffer tmp;
  210. double version = context.getClientVersion();
  211. dest.setID(src->queryId());
  212. #ifndef _CONTAINERIZED
  213. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  214. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  215. Owned<IPropertyTree> root = &constEnv->getPTree();
  216. if (src->getClusterName(tmp.clear()).length()!=0)
  217. {
  218. char *clusterName = (char *)tmp.str();
  219. if (clusterName && *clusterName)
  220. {
  221. StringBuffer clusterNameForDisplay(clusterName);
  222. Owned<IPropertyTreeIterator> clusters= root->getElements("Software/Topology/Cluster");
  223. if (clusters->first())
  224. {
  225. do {
  226. IPropertyTree &cluster = clusters->query();
  227. const char* name = cluster.queryProp("@name");
  228. if (!name || !*name)
  229. continue;
  230. Owned<IPropertyTreeIterator> thorClusters= cluster.getElements(eqThorCluster);
  231. Owned<IPropertyTreeIterator> roxieClusters= cluster.getElements(eqRoxieCluster);
  232. if (thorClusters->first() || roxieClusters->first())
  233. {
  234. if (thorClusters->first())
  235. {
  236. IPropertyTree &thorCluster = thorClusters->query();
  237. const char* process = thorCluster.queryProp("@process");
  238. if (process && *process)
  239. {
  240. if (clusterName && !stricmp(clusterName, process))
  241. {
  242. clusterNameForDisplay.clear().append(name);
  243. break;
  244. }
  245. }
  246. }
  247. if (roxieClusters->first())
  248. {
  249. IPropertyTree &roxieCluster = roxieClusters->query();
  250. const char* process = roxieCluster.queryProp("@process");
  251. if (process && *process)
  252. {
  253. if (clusterName && !stricmp(clusterName, name))
  254. {
  255. clusterNameForDisplay.clear().append(name);
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. } while (clusters->next());
  262. }
  263. dest.setClusterName(clusterNameForDisplay.str());
  264. }
  265. }
  266. #else
  267. src->getClusterName(tmp.clear());
  268. dest.setClusterName(tmp.str());
  269. #endif
  270. if ((version > 1.05) && src->getDFUServerName(tmp.clear()).length())
  271. dest.setDFUServerName(tmp.str());
  272. if (src->getJobName(tmp.clear()).length()!=0)
  273. dest.setJobName(tmp.str());
  274. else
  275. dest.setJobName("");
  276. if (src->getQueue(tmp.clear()).length()!=0)
  277. dest.setQueue(tmp.str());
  278. if (src->getUser(tmp.clear()).length()!=0)
  279. dest.setUser(tmp.str());
  280. dest.setIsProtected(src->isProtected());
  281. dest.setCommand(src->getCommand());
  282. IConstDFUprogress *prog = src->queryProgress();
  283. if (prog != NULL)
  284. {
  285. DFUstate state = prog->getState();
  286. dest.setState(state);
  287. StringBuffer statemsg;
  288. encodeDFUstate(state,statemsg);
  289. dest.setStateMessage(statemsg.str());
  290. CDateTime startAt;
  291. CDateTime stoppAt;
  292. prog->getTimeStarted(startAt);
  293. prog->getTimeStopped(stoppAt);
  294. StringBuffer tmpstr;
  295. startAt.getDateString(tmpstr);
  296. tmpstr.append(" ");
  297. startAt.getTimeString(tmpstr);
  298. dest.setTimeStarted(tmpstr.str());
  299. tmpstr.clear();
  300. stoppAt.getDateString(tmpstr);
  301. tmpstr.append(" ");
  302. stoppAt.getTimeString(tmpstr);
  303. dest.setTimeStopped(tmpstr.str());
  304. StringBuffer prgmsg;
  305. prog->formatProgressMessage(prgmsg);
  306. dest.setProgressMessage(prgmsg.str());
  307. prog->formatSummaryMessage(prgmsg.clear());
  308. dest.setSummaryMessage(prgmsg.str());
  309. unsigned secs = prog->getSecsLeft();
  310. if(secs > 0)
  311. dest.setSecsLeft(secs);
  312. dest.setPercentDone(prog->getPercentDone());
  313. }
  314. IConstDFUoptions *options = src->queryOptions();
  315. if(options)
  316. {
  317. dest.setReplicate(options->getReplicate());
  318. dest.setOverwrite(options->getOverwrite());
  319. }
  320. IConstDFUfileSpec * file = src->querySource();
  321. if (file != NULL)
  322. {
  323. //if (file->getTitle(tmp.clear()).length()!=0)
  324. // dest.setSourceTitle(tmp.str());
  325. StringBuffer lfn;
  326. file->getLogicalName(lfn);
  327. if (lfn.length() != 0)
  328. {
  329. dest.setSourceLogicalName(lfn.str());
  330. }
  331. else
  332. dest.setSourceFormat(file->getFormat());
  333. if (file->getRawDirectory(tmp.clear()).length()!=0)
  334. dest.setSourceDirectory(tmp.str());
  335. SocketEndpoint srcdali;
  336. StringBuffer srcdaliip;
  337. file->getForeignDali(srcdali);
  338. srcdali.getIpText(srcdaliip);
  339. if(srcdaliip.length() > 0 && strcmp(srcdaliip.str(), "0.0.0.0") != 0)
  340. dest.setSourceDali(srcdaliip.str());
  341. StringBuffer diffkeyname;
  342. file->getDiffKey(diffkeyname);
  343. if(diffkeyname.length() > 0)
  344. dest.setSourceDiffKeyName(diffkeyname.str());
  345. StringBuffer socket, dir, title;
  346. unsigned np = file->getNumParts(0); // should handle multiple clusters?
  347. if (lfn.length() == 0) { // no logical name
  348. if (np == 1)
  349. {
  350. Owned<IFileDescriptor> info;
  351. try
  352. {
  353. info.setown(file->getFileDescriptor());
  354. if(info)
  355. {
  356. Owned<INode> node = info->getNode(0);
  357. if (node)
  358. {
  359. node->endpoint().getIpText(socket);
  360. dest.setSourceIP(socket.str());
  361. }
  362. const char *defaultdir = info->queryDefaultDir();
  363. if (defaultdir&&*defaultdir)
  364. addPathSepChar(dir.append(defaultdir));
  365. file->getRawFileMask(dir);
  366. dest.setSourceFilePath(dir.str());
  367. }
  368. }
  369. catch(IException *e)
  370. {
  371. EXCLOG(e,"DeepAssign getFileDescriptor");
  372. e->Release();
  373. }
  374. }
  375. }
  376. if (np)
  377. dest.setSourceNumParts(np);
  378. unsigned rs = file->getRecordSize();
  379. if (rs)
  380. dest.setSourceRecordSize(rs);
  381. StringBuffer rowtag;
  382. file->getRowTag(rowtag);
  383. if(rowtag.length() > 0)
  384. dest.setRowTag(rowtag.str());
  385. if (version >= 1.04 && (file->getFormat() == DFUff_csv))
  386. {
  387. StringBuffer separate, terminate, quote, escape;
  388. bool quotedTerminator;
  389. file->getCsvOptions(separate,terminate,quote, escape, quotedTerminator);
  390. if(separate.length() > 0)
  391. dest.setSourceCsvSeparate(separate.str());
  392. if(terminate.length() > 0)
  393. dest.setSourceCsvTerminate(terminate.str());
  394. if(quote.length() > 0)
  395. dest.setSourceCsvQuote(quote.str());
  396. if((version >= 1.05) && (escape.length() > 0))
  397. dest.setSourceCsvEscape(escape.str());
  398. if(version >=1.10)
  399. dest.setQuotedTerminator(quotedTerminator);
  400. }
  401. }
  402. file = src->queryDestination();
  403. if (file != NULL)
  404. {
  405. StringBuffer lfn;
  406. file->getLogicalName(lfn);
  407. if (lfn.length() != 0)
  408. dest.setDestLogicalName(lfn.str());
  409. else
  410. dest.setDestFormat(file->getFormat());
  411. if (file->getRawDirectory(tmp.clear()).length()!=0)
  412. dest.setDestDirectory(tmp.str());
  413. if (file->getGroupName(0,tmp.clear()).length()!=0) // should handle multiple clusters?
  414. {
  415. char *clusterName = (char *)tmp.str();
  416. if (clusterName)
  417. dest.setDestGroupName(clusterName);
  418. }
  419. if (version >= 1.21)
  420. dest.setPreserveFileParts(file->getWrap());
  421. StringBuffer socket, dir, title;
  422. unsigned np = file->getNumParts(0); // should handle multiple clusters?
  423. if (lfn.length() == 0) { // no logical name
  424. if (np == 1)
  425. {
  426. Owned<IFileDescriptor> info;
  427. try
  428. {
  429. info.setown(file->getFileDescriptor());
  430. if(info)
  431. {
  432. Owned<INode> node = info->getNode(0);
  433. if (node)
  434. {
  435. node->endpoint().getIpText(socket);
  436. dest.setDestIP(socket.str());
  437. }
  438. const char *defaultdir = info->queryDefaultDir();
  439. if (defaultdir&&*defaultdir)
  440. addPathSepChar(dir.append(defaultdir));
  441. file->getRawFileMask(dir);
  442. dest.setDestFilePath(dir.str());
  443. }
  444. }
  445. catch(IException *e)
  446. {
  447. EXCLOG(e,"DeepAssign getFileDescriptor dest");
  448. e->Release();
  449. }
  450. }
  451. }
  452. if (np)
  453. dest.setDestNumParts(np);
  454. unsigned rs = file->getRecordSize();
  455. if (rs)
  456. dest.setDestRecordSize(rs);
  457. dest.setCompress(file->isCompressed());
  458. }
  459. // monitor stuff
  460. IConstDFUmonitor *monitor = src->queryMonitor();
  461. if (monitor) {
  462. monitor->getEventName(tmp.clear());
  463. if (tmp.length())
  464. dest.setMonitorEventName(tmp.str());
  465. bool sub = monitor->getSub();
  466. dest.setMonitorSub(sub);
  467. unsigned sl = monitor->getShotLimit();
  468. if (sl)
  469. dest.setMonitorShotLimit(sl);
  470. }
  471. }
  472. bool CFileSprayEx::ParseLogicalPath(const char * pLogicalPath, const char* groupName, const char* cluster,
  473. StringBuffer &folder, StringBuffer &title, StringBuffer &defaultFolder, StringBuffer &defaultReplicateFolder)
  474. {
  475. if(!pLogicalPath || !*pLogicalPath)
  476. return false;
  477. folder.clear();
  478. title.clear();
  479. defaultFolder.clear();
  480. defaultReplicateFolder.clear();
  481. DFD_OS os = DFD_OSdefault;
  482. if(groupName != NULL && *groupName != '\0')
  483. {
  484. #ifdef _CONTAINERIZED
  485. Owned<IPropertyTree> plane = getStoragePlane(groupName);
  486. if (plane)
  487. defaultFolder.append(plane->queryProp("@prefix"));
  488. #else
  489. StringBuffer basedir;
  490. GroupType groupType;
  491. Owned<IGroup> group = queryNamedGroupStore().lookup(groupName, basedir, groupType);
  492. if (group) {
  493. switch (queryOS(group->queryNode(0).endpoint())) {
  494. case MachineOsW2K:
  495. os = DFD_OSwindows; break;
  496. case MachineOsSolaris:
  497. case MachineOsLinux:
  498. os = DFD_OSunix; break;
  499. }
  500. if (directories.get())
  501. {
  502. switch (groupType)
  503. {
  504. case grp_roxie:
  505. getConfigurationDirectory(directories, "data", "roxie", cluster, defaultFolder);
  506. getConfigurationDirectory(directories, "data2", "roxie", cluster, defaultReplicateFolder);
  507. // MORE - should extend to systems with higher redundancy
  508. break;
  509. case grp_hthor:
  510. getConfigurationDirectory(directories, "data", "eclagent", cluster, defaultFolder);
  511. break;
  512. case grp_thor:
  513. default:
  514. getConfigurationDirectory(directories, "data", "thor", cluster, defaultFolder);
  515. getConfigurationDirectory(directories, "mirror", "thor", cluster, defaultReplicateFolder);
  516. }
  517. }
  518. }
  519. else
  520. {
  521. // Error here?
  522. }
  523. #endif
  524. }
  525. makePhysicalPartName(pLogicalPath,0,0,folder,false,os,defaultFolder.str());
  526. const char *n = pLogicalPath;
  527. const char* p;
  528. do {
  529. p = strstr(n,"::");
  530. if(p)
  531. n = p+2;
  532. } while(p);
  533. title.append(n);
  534. return true;
  535. }
  536. bool CFileSprayEx::ParseLogicalPath(const char * pLogicalPath, StringBuffer &title)
  537. {
  538. if(!pLogicalPath || !*pLogicalPath)
  539. return false;
  540. title.clear();
  541. const char *n = pLogicalPath;
  542. const char* p;
  543. do {
  544. p = strstr(n,"::");
  545. if(p)
  546. n = p+2;
  547. } while(p);
  548. title.append(n);
  549. return true;
  550. }
  551. void setRoxieClusterPartDiskMapping(const char *clusterName, const char *defaultFolder, const char *defaultReplicateFolder,
  552. bool supercopy, IDFUfileSpec *wuFSpecDest, IDFUoptions *wuOptions)
  553. {
  554. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  555. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  556. VStringBuffer dirxpath("Software/RoxieCluster[@name=\"%s\"]",clusterName);
  557. Owned<IPropertyTree> pEnvRoot = &constEnv->getPTree();
  558. Owned<IPropertyTreeIterator> processes = pEnvRoot->getElements(dirxpath);
  559. if (!processes->first())
  560. {
  561. DBGLOG("Failed to get RoxieCluster settings");
  562. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "Failed to get RoxieCluster settings. The workunit will not be created.");
  563. }
  564. IPropertyTree &process = processes->query();
  565. const char *slaveConfig = process.queryProp("@slaveConfig");
  566. if (!slaveConfig || !*slaveConfig)
  567. {
  568. DBGLOG("Failed to get RoxieCluster settings");
  569. throw MakeStringException(ECLWATCH_INVALID_CLUSTER_INFO, "Failed to get RoxieCluster settings. The workunit will not be created.");
  570. }
  571. bool replicate = false;
  572. unsigned redundancy = 0; // Number of "spare" copies of the data
  573. unsigned channelsPerNode = 1; // Overloaded and cyclic modes
  574. int replicateOffset = 1; // Used In cyclic mode only
  575. unsigned numDataCopies = process.getPropInt("@numDataCopies", 1);
  576. ClusterPartDiskMapSpec spec;
  577. spec.setDefaultBaseDir(defaultFolder);
  578. if (strieq(slaveConfig, "overloaded"))
  579. {
  580. channelsPerNode = process.getPropInt("@channelsPernode", 1);
  581. spec.setDefaultReplicateDir(defaultReplicateFolder);
  582. }
  583. else if (strieq(slaveConfig, "full redundancy"))
  584. {
  585. redundancy = numDataCopies-1;
  586. replicateOffset = 0;
  587. replicate = true;
  588. }
  589. else if (strieq(slaveConfig, "cyclic redundancy"))
  590. {
  591. redundancy = numDataCopies-1;
  592. channelsPerNode = numDataCopies;
  593. replicateOffset = process.getPropInt("@cyclicOffset", 1);
  594. spec.setDefaultReplicateDir(defaultReplicateFolder);
  595. replicate = true;
  596. }
  597. spec.setRoxie (redundancy, channelsPerNode, replicateOffset);
  598. if (!supercopy)
  599. spec.setRepeatedCopies(CPDMSRP_lastRepeated,false);
  600. wuFSpecDest->setClusterPartDiskMapSpec(clusterName,spec);
  601. wuOptions->setReplicate(replicate);
  602. }
  603. StringBuffer& constructFileMask(const char* filename, StringBuffer& filemask)
  604. {
  605. filemask.clear().append(filename).toLowerCase().append("._$P$_of_$N$");
  606. return filemask;
  607. }
  608. bool CFileSprayEx::onDFUWUSearch(IEspContext &context, IEspDFUWUSearchRequest & req, IEspDFUWUSearchResponse & resp)
  609. {
  610. try
  611. {
  612. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Read, ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  613. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  614. Owned<IConstEnvironment> environment = factory->openEnvironment();
  615. Owned<IPropertyTree> root = &environment->getPTree();
  616. StringArray dfuclusters;
  617. Owned<IPropertyTreeIterator> clusterIterator = root->getElements("Software/Topology/Cluster");
  618. if (clusterIterator->first())
  619. {
  620. do {
  621. IPropertyTree &cluster = clusterIterator->query();
  622. const char *clusterName = cluster.queryProp("@name");
  623. if (!clusterName || !*clusterName)
  624. continue;
  625. dfuclusters.append(clusterName);
  626. } while (clusterIterator->next());
  627. }
  628. resp.setClusterNames(dfuclusters);
  629. }
  630. catch(IException* e)
  631. {
  632. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  633. }
  634. return true;
  635. }
  636. int readFromCommaSeparatedString(const char *commaSeparatedString, StringBuffer* output)
  637. {
  638. int numOfItems = 0;
  639. if (commaSeparatedString && *commaSeparatedString)
  640. {
  641. char *pStr = (char *) commaSeparatedString;
  642. while (pStr)
  643. {
  644. char item[1024];
  645. bool bFoundComma = false;
  646. int len = strlen(pStr);
  647. for (int i = 0; i < len; i++)
  648. {
  649. char *pStr1 = pStr + i;
  650. if (pStr1[0] != ',')
  651. continue;
  652. strncpy(item, pStr, pStr1 - pStr);
  653. item[pStr1 - pStr] = 0;
  654. bFoundComma = true;
  655. if (i < len - 1)
  656. pStr = pStr1 + 1;
  657. else
  658. pStr = NULL;
  659. break;
  660. }
  661. if (!bFoundComma && len > 0)
  662. {
  663. strcpy(item, pStr);
  664. pStr = NULL;
  665. }
  666. output[numOfItems] = item;
  667. numOfItems++;
  668. }
  669. }
  670. return numOfItems;
  671. }
  672. bool CFileSprayEx::GetArchivedDFUWorkunits(IEspContext &context, IEspGetDFUWorkunits &req, IEspGetDFUWorkunitsResponse &resp)
  673. {
  674. StringBuffer user;
  675. context.getUserID(user);
  676. SocketEndpoint ep;
  677. getSashaServiceEP(ep, "sasha-dfuwu-archiver", true);
  678. Owned<INode> sashaserver = createINode(ep);
  679. __int64 count=req.getPageSize();
  680. if(count < 1)
  681. count=100;
  682. __int64 begin=req.getPageStartFrom();
  683. if (begin < 0)
  684. begin = 0;
  685. Owned<ISashaCommand> cmd = createSashaCommand();
  686. cmd->setAction(SCA_LIST);
  687. cmd->setOnline(false);
  688. cmd->setArchived(true);
  689. cmd->setDFU(true);
  690. cmd->setLimit((int) count+1);
  691. cmd->setStart((int)begin);
  692. if(req.getCluster() && *req.getCluster())
  693. cmd->setCluster(req.getCluster());
  694. if(req.getOwner() && *req.getOwner())
  695. cmd->setOwner(req.getOwner());
  696. if(req.getJobname() && *req.getJobname())
  697. cmd->setJobName(req.getJobname());
  698. if(req.getStateReq() && *req.getStateReq())
  699. cmd->setState(req.getStateReq());
  700. cmd->setOutputFormat("owner,jobname,cluster,state,command");//date range/owner/jobname/state*/
  701. if (!cmd->send(sashaserver))
  702. {
  703. StringBuffer url;
  704. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,
  705. "Sasha (%s) took too long to respond from: Get archived workUnits.",
  706. ep.getUrlStr(url).str());
  707. }
  708. IArrayOf<IEspDFUWorkunit> results;
  709. __int64 actualCount = cmd->numIds();
  710. StringBuffer s;
  711. for (unsigned j=0;j<actualCount;j++)
  712. {
  713. const char *wuidStr = cmd->queryId(j);
  714. if (!wuidStr)
  715. continue;
  716. StringBuffer strArray[6];
  717. readFromCommaSeparatedString(wuidStr, strArray);
  718. //skip any workunits without access
  719. Owned<IEspDFUWorkunit> resultWU = createDFUWorkunit("", "");
  720. resultWU->setArchived(true);
  721. if (strArray[0].length() > 0)
  722. resultWU->setID(strArray[0].str());
  723. if (strArray[1].length() > 0)
  724. resultWU->setUser(strArray[1].str());
  725. if (strArray[2].length() > 0)
  726. resultWU->setJobName(strArray[2].str());
  727. if (strArray[3].length() > 0)
  728. resultWU->setClusterName(strArray[3].str());
  729. if (strArray[4].length() > 0)
  730. resultWU->setStateMessage(strArray[4].str());
  731. if (strArray[5].length() > 0)
  732. resultWU->setCommand(atoi(strArray[5].str()));
  733. results.append(*resultWU.getLink());
  734. }
  735. resp.setPageStartFrom(begin+1);
  736. resp.setNextPage(-1);
  737. if(count < actualCount)
  738. {
  739. if (results.length() > count)
  740. {
  741. results.pop();
  742. }
  743. resp.setNextPage(begin + count);
  744. resp.setPageEndAt(begin + count);
  745. }
  746. else
  747. {
  748. resp.setPageEndAt(begin + actualCount);
  749. }
  750. if(begin > 0)
  751. {
  752. resp.setFirst(false);
  753. if (begin - count > 0)
  754. resp.setPrevPage(begin - count);
  755. else
  756. resp.setPrevPage(0);
  757. }
  758. resp.setPageSize(count);
  759. resp.setResults(results);
  760. StringBuffer basicQuery;
  761. if (req.getStateReq() && *req.getStateReq())
  762. {
  763. resp.setStateReq(req.getStateReq());
  764. addToQueryString(basicQuery, "StateReq", req.getStateReq());
  765. }
  766. if (req.getCluster() && *req.getCluster())
  767. {
  768. resp.setCluster(req.getCluster());
  769. addToQueryString(basicQuery, "Cluster", req.getCluster());
  770. }
  771. if (req.getOwner() && *req.getOwner())
  772. {
  773. resp.setOwner(req.getOwner());
  774. addToQueryString(basicQuery, "Owner", req.getOwner());
  775. }
  776. if (req.getType() && *req.getType())
  777. {
  778. resp.setType(req.getType());
  779. addToQueryString(basicQuery, "Type", req.getType());
  780. }
  781. resp.setFilters(basicQuery.str());
  782. resp.setBasicQuery(basicQuery.str());
  783. return true;
  784. }
  785. bool CFileSprayEx::getOneDFUWorkunit(IEspContext& context, const char* wuid, IEspGetDFUWorkunitsResponse& resp)
  786. {
  787. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  788. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(wuid, false);
  789. if (!wu)
  790. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Dfu workunit %s not found.", wuid);
  791. Owned<IEspDFUWorkunit> resultWU = createDFUWorkunit();
  792. resultWU->setID(wuid);
  793. resultWU->setCommand(wu->getCommand());
  794. resultWU->setIsProtected(wu->isProtected());
  795. StringBuffer jobname, user, cluster;
  796. resultWU->setJobName(wu->getJobName(jobname).str());
  797. resultWU->setUser(wu->getUser(user).str());
  798. const char* clusterName = wu->getClusterName(cluster).str();
  799. if (clusterName && *clusterName)
  800. {
  801. #ifdef _CONTAINERIZED
  802. Owned<IStringIterator> targets = getContainerTargetClusters(nullptr, clusterName);
  803. #else
  804. Owned<IStringIterator> targets = getTargetClusters(nullptr, clusterName);
  805. #endif
  806. if (!targets->first())
  807. resultWU->setClusterName(clusterName);
  808. else
  809. {
  810. SCMStringBuffer targetCluster;
  811. targets->str(targetCluster);
  812. resultWU->setClusterName(targetCluster.str());
  813. }
  814. }
  815. IConstDFUprogress* prog = wu->queryProgress();
  816. if (prog)
  817. {
  818. StringBuffer statemsg;
  819. DFUstate state = prog->getState();
  820. encodeDFUstate(state, statemsg);
  821. resultWU->setState(state);
  822. resultWU->setStateMessage(statemsg.str());
  823. resultWU->setPercentDone(prog->getPercentDone());
  824. }
  825. IArrayOf<IEspDFUWorkunit> result;
  826. result.append(*resultWU.getClear());
  827. resp.setResults(result);
  828. return true;
  829. }
  830. bool CFileSprayEx::onGetDFUWorkunits(IEspContext &context, IEspGetDFUWorkunits &req, IEspGetDFUWorkunitsResponse &resp)
  831. {
  832. try
  833. {
  834. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Read, ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  835. StringBuffer wuidStr(req.getWuid());
  836. const char* wuid = wuidStr.trim().str();
  837. if (wuid && *wuid && looksLikeAWuid(wuid, 'D'))
  838. return getOneDFUWorkunit(context, wuid, resp);
  839. double version = context.getClientVersion();
  840. if (version > 1.02)
  841. {
  842. const char *type = req.getType();
  843. if (type && *type && !stricmp(type, "archived workunits"))
  844. {
  845. return GetArchivedDFUWorkunits(context, req, resp);
  846. }
  847. }
  848. StringBuffer clusterReq;
  849. const char *clusterName = req.getCluster();
  850. if(clusterName && *clusterName)
  851. {
  852. clusterReq.append(clusterName);
  853. }
  854. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  855. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  856. Owned<IPropertyTree> root = &constEnv->getPTree();
  857. StringArray targetClusters, clusterProcesses;
  858. Owned<IPropertyTreeIterator> clusters= root->getElements("Software/Topology/Cluster");
  859. if (clusters->first())
  860. {
  861. do {
  862. IPropertyTree &cluster = clusters->query();
  863. const char* name = cluster.queryProp("@name");
  864. if (!name || !*name)
  865. continue;
  866. Owned<IPropertyTreeIterator> thorClusters= cluster.getElements(eqThorCluster);
  867. Owned<IPropertyTreeIterator> roxieClusters= cluster.getElements(eqRoxieCluster);
  868. if (thorClusters->first() || roxieClusters->first())
  869. {
  870. bool bFound = false;
  871. if (thorClusters->first())
  872. {
  873. IPropertyTree &thorCluster = thorClusters->query();
  874. const char* process = thorCluster.queryProp("@process");
  875. if (process && *process)
  876. {
  877. targetClusters.append(name);
  878. clusterProcesses.append(process);
  879. if (clusterName && !stricmp(clusterName, name))
  880. {
  881. clusterReq.clear().append(process);
  882. }
  883. }
  884. }
  885. if (!bFound && roxieClusters->first())
  886. {
  887. IPropertyTree &roxieCluster = roxieClusters->query();
  888. const char* process = roxieCluster.queryProp("@process");
  889. if (process && *process)
  890. {
  891. targetClusters.append(name);
  892. clusterProcesses.append(process);
  893. if (clusterName && !stricmp(clusterName, name))
  894. {
  895. clusterReq.clear().append(process);
  896. }
  897. }
  898. }
  899. }
  900. } while (clusters->next());
  901. }
  902. __int64 pagesize = req.getPageSize();
  903. __int64 pagefrom = req.getPageStartFrom();
  904. __int64 displayFrom = 0;
  905. if (pagesize < 1)
  906. {
  907. pagesize = 100;
  908. }
  909. if (pagefrom > 0)
  910. {
  911. displayFrom = pagefrom;
  912. }
  913. DFUsortfield sortorder[2] = {DFUsf_wuid, DFUsf_term};
  914. sortorder[0] = (DFUsortfield) (DFUsf_wuid + DFUsf_reverse);
  915. if(req.getSortby() && *req.getSortby())
  916. {
  917. const char *sortby = req.getSortby();
  918. if (!stricmp(sortby, "Owner"))
  919. sortorder[0] = DFUsf_user;
  920. else if (!stricmp(sortby, "JobName"))
  921. sortorder[0] = DFUsf_job;
  922. else if (!stricmp(sortby, "Cluster"))
  923. sortorder[0] = DFUsf_cluster;
  924. else if (!stricmp(sortby, "State"))
  925. sortorder[0] = DFUsf_state;
  926. else if (!stricmp(sortby, "Type"))
  927. sortorder[0] = DFUsf_command;
  928. else if (!stricmp(sortby, "Protected"))
  929. sortorder[0] = DFUsf_protected;
  930. else if (!stricmp(sortby, "PCTDone"))
  931. sortorder[0] = (DFUsortfield) (DFUsf_pcdone | DFUsf_numeric);
  932. else
  933. sortorder[0] = DFUsf_wuid;
  934. bool descending = req.getDescending();
  935. if (descending)
  936. sortorder[0] = (DFUsortfield) (sortorder[0] | DFUsf_reverse);
  937. }
  938. DFUsortfield filters[10];
  939. unsigned short filterCount = 0;
  940. MemoryBuffer filterbuf;
  941. if(req.getStateReq() && *req.getStateReq())
  942. {
  943. filters[filterCount] = DFUsf_state;
  944. filterCount++;
  945. if (stricmp(req.getStateReq(), "unknown") != 0)
  946. filterbuf.append(req.getStateReq());
  947. else
  948. filterbuf.append("");
  949. }
  950. if(wuid && *wuid)
  951. {
  952. filters[filterCount] = DFUsf_wildwuid;
  953. filterCount++;
  954. filterbuf.append(wuid);
  955. }
  956. if(clusterName && *clusterName)
  957. {
  958. filters[filterCount] = DFUsf_cluster;
  959. filterCount++;
  960. filterbuf.append(clusterReq.str());
  961. }
  962. if(req.getOwner() && *req.getOwner())
  963. {
  964. filters[filterCount] = DFUsortfield (DFUsf_user | DFUsf_nocase);
  965. filterCount++;
  966. filterbuf.append(req.getOwner());
  967. }
  968. if(req.getJobname() && *req.getJobname())
  969. {
  970. filters[filterCount] = DFUsortfield (DFUsf_job | DFUsf_nocase);
  971. filterCount++;
  972. filterbuf.append(req.getJobname());
  973. }
  974. filters[filterCount] = DFUsf_term;
  975. __int64 cacheHint = req.getCacheHint();
  976. if (cacheHint < 0) //Not set yet
  977. cacheHint = 0;
  978. IArrayOf<IEspDFUWorkunit> result;
  979. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  980. unsigned numWUs;
  981. PROGLOG("GetDFUWorkunits: getWorkUnitsSorted");
  982. Owned<IConstDFUWorkUnitIterator> itr = factory->getWorkUnitsSorted(sortorder, filters, filterbuf.bufferBase(), (int) displayFrom, (int) pagesize+1, req.getOwner(), &cacheHint, &numWUs);
  983. if (version >= 1.07)
  984. resp.setCacheHint(cacheHint);
  985. PROGLOG("GetDFUWorkunits: getWorkUnitsSorted done");
  986. //unsigned actualCount = 0;
  987. itr->first();
  988. while(itr->isValid())
  989. {
  990. Owned<IConstDFUWorkUnit> wu = itr->get();
  991. //actualCount++;
  992. Owned<IEspDFUWorkunit> resultWU = createDFUWorkunit("", "");
  993. resultWU->setID(wu->queryId());
  994. StringBuffer jobname, user, cluster;
  995. resultWU->setJobName(wu->getJobName(jobname).str());
  996. resultWU->setCommand(wu->getCommand());
  997. resultWU->setUser(wu->getUser(user).str());
  998. const char* clusterName = wu->getClusterName(cluster).str();
  999. if (clusterName)
  1000. {
  1001. StringBuffer clusterForDisplay(clusterName);
  1002. if (clusterProcesses.ordinality())
  1003. {
  1004. for (unsigned i = 0; i < clusterProcesses.length(); i++)
  1005. {
  1006. const char* clusterProcessName = clusterProcesses.item(i);
  1007. if (!stricmp(clusterProcessName, clusterName))
  1008. {
  1009. clusterForDisplay.clear().append(targetClusters.item(i));
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. resultWU->setClusterName(clusterForDisplay.str());
  1015. }
  1016. resultWU->setIsProtected(wu->isProtected());
  1017. IConstDFUprogress *prog = wu->queryProgress();
  1018. if (prog != NULL)
  1019. {
  1020. DFUstate state = prog->getState();
  1021. resultWU->setState(state);
  1022. StringBuffer statemsg;
  1023. encodeDFUstate(state,statemsg);
  1024. resultWU->setStateMessage(statemsg.str());
  1025. resultWU->setPercentDone(prog->getPercentDone());
  1026. }
  1027. result.append(*resultWU.getLink());
  1028. itr->next();
  1029. }
  1030. if (result.length() > pagesize)
  1031. result.pop();
  1032. resp.setPageSize(pagesize);
  1033. resp.setNumWUs(numWUs);
  1034. resp.setPageStartFrom(displayFrom + 1);
  1035. if(displayFrom + pagesize < numWUs)
  1036. {
  1037. resp.setNextPage(displayFrom + pagesize);
  1038. resp.setPageEndAt(pagefrom + pagesize);
  1039. __int64 last = displayFrom + pagesize;
  1040. while (last + pagesize < numWUs)
  1041. {
  1042. last += pagesize;
  1043. }
  1044. resp.setLastPage(last);
  1045. }
  1046. else
  1047. {
  1048. resp.setNextPage(-1);
  1049. resp.setPageEndAt(numWUs);
  1050. }
  1051. if(displayFrom > 0)
  1052. {
  1053. resp.setFirst(false);
  1054. if (displayFrom - pagesize > 0)
  1055. resp.setPrevPage(displayFrom - pagesize);
  1056. else
  1057. resp.setPrevPage(0);
  1058. }
  1059. StringBuffer basicQuery;
  1060. if (req.getStateReq() && *req.getStateReq())
  1061. {
  1062. resp.setStateReq(req.getStateReq());
  1063. addToQueryString(basicQuery, "StateReq", req.getStateReq());
  1064. }
  1065. if (req.getCluster() && *req.getCluster())
  1066. {
  1067. resp.setCluster(req.getCluster());
  1068. addToQueryString(basicQuery, "Cluster", req.getCluster());
  1069. }
  1070. if (req.getOwner() && *req.getOwner())
  1071. {
  1072. resp.setOwner(req.getOwner());
  1073. addToQueryString(basicQuery, "Owner", req.getOwner());
  1074. }
  1075. resp.setFilters(basicQuery.str());
  1076. if (req.getSortby() && *req.getSortby())
  1077. {
  1078. resp.setSortby(req.getSortby());
  1079. if (req.getDescending())
  1080. resp.setDescending(req.getDescending());
  1081. StringBuffer strbuf(req.getSortby());
  1082. strbuf.append("=");
  1083. String str1(strbuf.str());
  1084. String str(basicQuery.str());
  1085. if (str.indexOf(str1) < 0)
  1086. {
  1087. addToQueryString(basicQuery, "Sortby", req.getSortby());
  1088. if (req.getDescending())
  1089. addToQueryString(basicQuery, "Descending", "1");
  1090. }
  1091. }
  1092. resp.setBasicQuery(basicQuery.str());
  1093. resp.setResults(result);
  1094. }
  1095. catch(IException* e)
  1096. {
  1097. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1098. }
  1099. return true;
  1100. }
  1101. void CFileSprayEx::addToQueryString(StringBuffer &queryString, const char *name, const char *value)
  1102. {
  1103. if (queryString.length() > 0)
  1104. {
  1105. queryString.append("&amp;");
  1106. }
  1107. queryString.append(name);
  1108. queryString.append("=");
  1109. queryString.append(value);
  1110. }
  1111. void CFileSprayEx::getInfoFromSasha(IEspContext &context, const char *sashaServer, const char* wuid, IEspDFUWorkunit *info)
  1112. {
  1113. Owned<ISashaCommand> cmd = createSashaCommand();
  1114. cmd->addId(wuid);
  1115. cmd->setAction(SCA_GET);
  1116. cmd->setArchived(true);
  1117. cmd->setDFU(true);
  1118. SocketEndpoint ep(sashaServer);
  1119. Owned<INode> node = createINode(ep);
  1120. if (!cmd->send(node,1*60*1000))
  1121. {
  1122. StringBuffer url;
  1123. throw MakeStringException(ECLWATCH_CANNOT_CONNECT_ARCHIVE_SERVER,
  1124. "Sasha (%s) took too long to respond from: Get information for %s.",
  1125. ep.getUrlStr(url).str(), wuid);
  1126. }
  1127. if (cmd->numIds()==0)
  1128. {
  1129. DBGLOG("Could not read archived %s",wuid);
  1130. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT,"Cannot read workunit %s.",wuid);
  1131. }
  1132. unsigned num = cmd->numResults();
  1133. if (num < 1)
  1134. return;
  1135. StringBuffer res;
  1136. cmd->getResult(0,res);
  1137. if(res.length() < 1)
  1138. return;
  1139. Owned<IPropertyTree> wu = createPTreeFromXMLString(res.str());
  1140. if (!wu)
  1141. return;
  1142. const char * command = wu->queryProp("@command");
  1143. const char * submitID = wu->queryProp("@submitID");
  1144. const char * cluster = wu->queryProp("@clusterName");
  1145. const char * queue = wu->queryProp("@queue");
  1146. const char * jobName = wu->queryProp("@jobName");
  1147. const char * protectedWU = wu->queryProp("@protected");
  1148. info->setID(wuid);
  1149. info->setArchived(true);
  1150. if (command && *command)
  1151. info->setCommandMessage(command);
  1152. if (cluster && *cluster)
  1153. info->setClusterName(cluster);
  1154. if (submitID && *submitID)
  1155. info->setUser(submitID);
  1156. if (queue && *queue)
  1157. info->setQueue(queue);
  1158. if (jobName && *jobName)
  1159. info->setJobName(jobName);
  1160. if (protectedWU && stricmp(protectedWU, "0"))
  1161. info->setIsProtected(true);
  1162. else
  1163. info->setIsProtected(false);
  1164. IPropertyTree *source = wu->queryPropTree("Source");
  1165. if(source)
  1166. {
  1167. const char * directory = source->queryProp("@directory");
  1168. const char * name = source->queryProp("@name");
  1169. if (directory && *directory)
  1170. info->setSourceDirectory(directory);
  1171. if (name && *name)
  1172. info->setSourceLogicalName(name);
  1173. }
  1174. IPropertyTree *dest = wu->queryPropTree("Destination");
  1175. if(dest)
  1176. {
  1177. const char * directory = dest->queryProp("@directory");
  1178. int numParts = dest->getPropInt("@numparts", -1);
  1179. if (directory && *directory)
  1180. info->setDestDirectory(directory);
  1181. if (numParts > 0)
  1182. info->setDestNumParts(numParts);
  1183. }
  1184. IPropertyTree *progress = wu->queryPropTree("Progress");
  1185. if(progress)
  1186. {
  1187. const char * state = progress->queryProp("@state");
  1188. const char * timeStarted = progress->queryProp("@timestarted");
  1189. const char * timeStopped = progress->queryProp("@timestopped");
  1190. if (state && *state)
  1191. info->setStateMessage(state);
  1192. if (timeStarted && *timeStarted)
  1193. {
  1194. StringBuffer startStr(timeStarted);
  1195. startStr.replace('T', ' ');
  1196. info->setTimeStarted(startStr.str());
  1197. }
  1198. if (timeStopped && *timeStopped)
  1199. {
  1200. StringBuffer stopStr(timeStopped);
  1201. stopStr.replace('T', ' ');
  1202. info->setTimeStopped(stopStr.str());
  1203. }
  1204. }
  1205. return;
  1206. }
  1207. bool CFileSprayEx::getArchivedWUInfo(IEspContext &context, IEspGetDFUWorkunit &req, IEspGetDFUWorkunitResponse &resp)
  1208. {
  1209. const char *wuid = req.getWuid();
  1210. if (wuid && *wuid)
  1211. {
  1212. StringBuffer serviceEndpoint;
  1213. getSashaService(serviceEndpoint, "sasha-dfuwu-archiver", true);
  1214. getInfoFromSasha(context, serviceEndpoint, wuid, &resp.updateResult());
  1215. return true;
  1216. }
  1217. return false;
  1218. }
  1219. bool CFileSprayEx::onGetDFUWorkunit(IEspContext &context, IEspGetDFUWorkunit &req, IEspGetDFUWorkunitResponse &resp)
  1220. {
  1221. try
  1222. {
  1223. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Read, ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  1224. const char* wuid = req.getWuid();
  1225. if (!wuid || !*wuid)
  1226. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Dfu workunit ID not specified.");
  1227. bool found = false;
  1228. double version = context.getClientVersion();
  1229. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1230. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(wuid, false);
  1231. if(wu)
  1232. {
  1233. IEspDFUWorkunit &result = resp.updateResult();
  1234. PROGLOG("GetDFUWorkunit: %s", wuid);
  1235. DeepAssign(context, wu, result);
  1236. int n = resp.getResult().getState();
  1237. if (n == DFUstate_scheduled || n == DFUstate_queued || n == DFUstate_started)
  1238. {
  1239. resp.setAutoRefresh(WUDETAILS_REFRESH_MINS);
  1240. }
  1241. found = true;
  1242. }
  1243. else if ((version > 1.02) && getArchivedWUInfo(context, req, resp))
  1244. {
  1245. found = true;
  1246. }
  1247. if (!found)
  1248. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1249. }
  1250. catch(IException* e)
  1251. {
  1252. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1253. }
  1254. return true;
  1255. }
  1256. bool CFileSprayEx::onGetDFUProgress(IEspContext &context, IEspProgressRequest &req, IEspProgressResponse &resp)
  1257. {
  1258. try
  1259. {
  1260. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Read, ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  1261. const char* wuid = req.getWuid();
  1262. if(!wuid || !*wuid)
  1263. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Workunit ID not specified.");
  1264. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1265. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(req.getWuid(), false);
  1266. if(!wu)
  1267. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1268. resp.setWuid(req.getWuid());
  1269. PROGLOG("GetDFUProgress: %s", wuid);
  1270. IConstDFUprogress *prog = wu->queryProgress();
  1271. if (prog)
  1272. {
  1273. resp.setPercentDone(prog->getPercentDone());
  1274. resp.setKbPerSec(prog->getKbPerSec());
  1275. resp.setKbPerSecAve(prog->getKbPerSecAve());
  1276. resp.setSecsLeft(prog->getSecsLeft());
  1277. StringBuffer statestr;
  1278. encodeDFUstate(prog->getState(), statestr);
  1279. resp.setState(statestr.str());
  1280. resp.setSlavesDone(prog->getSlavesDone());
  1281. StringBuffer msg;
  1282. prog->formatProgressMessage(msg);
  1283. resp.setProgressMessage(msg.str());
  1284. prog->formatSummaryMessage(msg.clear());
  1285. resp.setSummaryMessage(msg.str());
  1286. prog->getTimeTaken(msg.clear());
  1287. resp.setTimeTaken(msg.str());
  1288. }
  1289. }
  1290. catch(IException* e)
  1291. {
  1292. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1293. }
  1294. return true;
  1295. }
  1296. bool CFileSprayEx::onCreateDFUWorkunit(IEspContext &context, IEspCreateDFUWorkunit &req, IEspCreateDFUWorkunitResponse &resp)
  1297. {
  1298. try
  1299. {
  1300. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to create DFU workunit. Permission denied.");
  1301. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1302. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1303. setDFUServerQueueReq(req.getDFUServerQueue(), wu);
  1304. setUserAuth(context, wu);
  1305. wu->commit();
  1306. const char * d = wu->queryId();
  1307. IEspDFUWorkunit &result = resp.updateResult();
  1308. DeepAssign(context, wu, result);
  1309. result.setOverwrite(false);
  1310. result.setReplicate(true);
  1311. }
  1312. catch(IException* e)
  1313. {
  1314. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1315. }
  1316. return true;
  1317. }
  1318. bool CFileSprayEx::onUpdateDFUWorkunit(IEspContext &context, IEspUpdateDFUWorkunit &req, IEspUpdateDFUWorkunitResponse &resp)
  1319. {
  1320. try
  1321. {
  1322. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to update DFU workunit. Permission denied.");
  1323. IConstDFUWorkunit & reqWU = req.getWu();
  1324. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1325. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(reqWU.getID());
  1326. if(!wu)
  1327. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Dfu workunit %s not found.", reqWU.getID());
  1328. PROGLOG("UpdateDFUWorkunit: %s", reqWU.getID());
  1329. IDFUprogress *prog = wu->queryUpdateProgress();
  1330. if (prog && req.getStateOrig() != reqWU.getState())
  1331. {
  1332. if (prog->getState() != req.getStateOrig())
  1333. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT,"Cannot update DFU workunit %s because its state has been changed internally. Please refresh the page and try again.",reqWU.getID());
  1334. prog->setState((enum DFUstate)reqWU.getState());
  1335. }
  1336. const char* clusterOrig = req.getClusterOrig();
  1337. const char* cluster = reqWU.getClusterName();
  1338. if(cluster && (!clusterOrig || stricmp(clusterOrig, cluster)))
  1339. {
  1340. wu->setClusterName(reqWU.getClusterName());
  1341. }
  1342. const char* jobNameOrig = req.getJobNameOrig();
  1343. const char* jobName = reqWU.getJobName();
  1344. if(jobName && (!jobNameOrig || stricmp(jobNameOrig, jobName)))
  1345. {
  1346. wu->setJobName(jobName);
  1347. }
  1348. if (reqWU.getIsProtected() != req.getIsProtectedOrig())
  1349. wu->protect(reqWU.getIsProtected());
  1350. wu->commit();
  1351. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(reqWU.getID()).str());
  1352. }
  1353. catch(IException* e)
  1354. {
  1355. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1356. }
  1357. return true;
  1358. }
  1359. bool markWUFailed(IDFUWorkUnitFactory *f, const char *wuid)
  1360. {
  1361. Owned<IDFUWorkUnit> wu = f->updateWorkUnit(wuid);
  1362. if(!wu)
  1363. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Dfu workunit %s not found.", wuid);
  1364. IDFUprogress *prog = wu->queryUpdateProgress();
  1365. if(!prog)
  1366. throw MakeStringException(ECLWATCH_PROGRESS_INFO_NOT_FOUND, "progress information not found for workunit %s.", wuid);
  1367. else if(prog->getState() == DFUstate_started)
  1368. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT, "Cannot delete workunit %s because its state is Started.", wuid);
  1369. else
  1370. {
  1371. prog->setState(DFUstate_failed);
  1372. return true;
  1373. }
  1374. return false;
  1375. }
  1376. static unsigned NumOfDFUWUActionNames = 6;
  1377. static const char *DFUWUActionNames[] = { "Delete", "Protect" , "Unprotect" , "Restore" , "SetToFailed", "Archive" };
  1378. bool CFileSprayEx::onDFUWorkunitsAction(IEspContext &context, IEspDFUWorkunitsActionRequest &req, IEspDFUWorkunitsActionResponse &resp)
  1379. {
  1380. try
  1381. {
  1382. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to update DFU workunit. Permission denied.");
  1383. CDFUWUActions action = req.getType();
  1384. if (action == DFUWUActions_Undefined)
  1385. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Action not defined.");
  1386. const char* actionStr = (action < NumOfDFUWUActionNames) ? DFUWUActionNames[action] : "Unknown";
  1387. StringArray& wuids = req.getWuids();
  1388. if (!wuids.ordinality())
  1389. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Workunit not defined.");
  1390. if ((action == CDFUWUActions_Restore) || (action == CDFUWUActions_Archive))
  1391. {
  1392. StringBuffer msg;
  1393. ForEachItemIn(i, wuids)
  1394. {
  1395. StringBuffer wuidStr(wuids.item(i));
  1396. const char* wuid = wuidStr.trim().str();
  1397. if (isEmptyString(wuid))
  1398. msg.appendf("Empty Workunit ID at %u. ", i);
  1399. }
  1400. if (!msg.isEmpty())
  1401. throw makeStringException(ECLWATCH_INVALID_INPUT, msg);
  1402. Owned<ISashaCommand> cmd = archiveOrRestoreWorkunits(wuids, nullptr, action == CDFUWUActions_Archive, true);
  1403. IArrayOf<IEspDFUActionResult> results;
  1404. ForEachItemIn(x, wuids)
  1405. {
  1406. Owned<IEspDFUActionResult> res = createDFUActionResult();
  1407. res->setID(wuids.item(x));
  1408. res->setAction(actionStr);
  1409. StringBuffer reply;
  1410. if (action == CDFUWUActions_Restore)
  1411. reply.set("Restore ID: ");
  1412. else
  1413. reply.set("Archive ID: ");
  1414. if (cmd->getId(x, reply))
  1415. res->setResult(reply.str());
  1416. else
  1417. res->setResult("Failed to get Archive/restore ID.");
  1418. results.append(*res.getLink());
  1419. }
  1420. resp.setDFUActionResults(results);
  1421. return true;
  1422. }
  1423. IArrayOf<IEspDFUActionResult> results;
  1424. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1425. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1426. {
  1427. const char* wuid = wuids.item(i);
  1428. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  1429. res->setID(wuid);
  1430. res->setAction(actionStr);
  1431. try
  1432. {
  1433. PROGLOG("%s %s", actionStr, wuid);
  1434. switch (action)
  1435. {
  1436. case CDFUWUActions_Delete:
  1437. if (!markWUFailed(factory, wuid))
  1438. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT, "Failed to mark workunit failed.");
  1439. if (!factory->deleteWorkUnit(wuid))
  1440. throw MakeStringException(ECLWATCH_CANNOT_DELETE_WORKUNIT, "Failed in deleting workunit.");
  1441. res->setResult("Success");
  1442. break;
  1443. case CDFUWUActions_Protect:
  1444. case CDFUWUActions_Unprotect:
  1445. case CDFUWUActions_SetToFailed:
  1446. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(wuid);
  1447. if(!wu.get())
  1448. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Failed in calling updateWorkUnit().");
  1449. switch (action)
  1450. {
  1451. case CDFUWUActions_Protect:
  1452. wu->protect(true);
  1453. break;
  1454. case CDFUWUActions_Unprotect:
  1455. wu->protect(false);
  1456. break;
  1457. case CDFUWUActions_SetToFailed:
  1458. IDFUprogress *prog = wu->queryUpdateProgress();
  1459. if (!prog)
  1460. throw MakeStringException(ECLWATCH_CANNOT_UPDATE_WORKUNIT, "Failed in calling queryUpdateProgress().");
  1461. prog->setState(DFUstate_failed);
  1462. break;
  1463. }
  1464. wu->commit();
  1465. res->setResult("Success");
  1466. break;
  1467. }
  1468. PROGLOG("%s %s done", actionStr, wuid);
  1469. }
  1470. catch (IException *e)
  1471. {
  1472. StringBuffer eMsg, failedMsg("Failed: ");
  1473. res->setResult(failedMsg.append(e->errorMessage(eMsg)).str());
  1474. e->Release();
  1475. }
  1476. results.append(*res.getLink());
  1477. }
  1478. resp.setDFUActionResults(results);
  1479. }
  1480. catch(IException* e)
  1481. {
  1482. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1483. }
  1484. return true;
  1485. }
  1486. bool CFileSprayEx::onDeleteDFUWorkunits(IEspContext &context, IEspDeleteDFUWorkunits &req, IEspDeleteDFUWorkunitsResponse &resp)
  1487. {
  1488. try
  1489. {
  1490. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to delete DFU workunit. Permission denied.");
  1491. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1492. StringArray & wuids = req.getWuids();
  1493. for(unsigned i = 0; i < wuids.ordinality(); ++i)
  1494. {
  1495. const char* wuid = wuids.item(i);
  1496. if (markWUFailed(factory, wuid))
  1497. {
  1498. factory->deleteWorkUnit(wuid);
  1499. PROGLOG("DeleteDFUWorkunits: %s deleted", wuid);
  1500. }
  1501. }
  1502. resp.setRedirectUrl("/FileSpray/GetDFUWorkunits");
  1503. }
  1504. catch(IException* e)
  1505. {
  1506. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1507. }
  1508. return true;
  1509. }
  1510. bool CFileSprayEx::onDeleteDFUWorkunit(IEspContext &context, IEspDeleteDFUWorkunit &req, IEspDeleteDFUWorkunitResponse &resp)
  1511. {
  1512. try
  1513. {
  1514. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to delete DFU workunit. Permission denied.");
  1515. const char* wuid = req.getWuid();
  1516. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1517. if (markWUFailed(factory, wuid))
  1518. {
  1519. resp.setResult(factory->deleteWorkUnit(wuid));
  1520. PROGLOG("DeleteDFUWorkunit: %s deleted", wuid);
  1521. }
  1522. else
  1523. resp.setResult(false);
  1524. resp.setRedirectUrl("/FileSpray/GetDFUWorkunits");
  1525. }
  1526. catch(IException* e)
  1527. {
  1528. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1529. }
  1530. return true;
  1531. }
  1532. bool CFileSprayEx::onSubmitDFUWorkunit(IEspContext &context, IEspSubmitDFUWorkunit &req, IEspSubmitDFUWorkunitResponse &resp)
  1533. {
  1534. try
  1535. {
  1536. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to submit DFU workunit. Permission denied.");
  1537. if (!req.getWuid() || !*req.getWuid())
  1538. throw MakeStringException(ECLWATCH_MISSING_PARAMS, "Workunit ID required");
  1539. PROGLOG("SubmitDFUWorkunit: %s", req.getWuid());
  1540. submitDFUWorkUnit(req.getWuid());
  1541. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(req.getWuid()).str());
  1542. }
  1543. catch(IException* e)
  1544. {
  1545. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1546. }
  1547. return true;
  1548. }
  1549. bool CFileSprayEx::onAbortDFUWorkunit(IEspContext &context, IEspAbortDFUWorkunit &req, IEspAbortDFUWorkunitResponse &resp)
  1550. {
  1551. try
  1552. {
  1553. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Write, ECLWATCH_DFU_WU_ACCESS_DENIED, "Failed to abort DFU workunit. Permission denied.");
  1554. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1555. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(req.getWuid());
  1556. if(!wu)
  1557. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1558. PROGLOG("AbortDFUWorkunit: %s", req.getWuid());
  1559. wu->requestAbort();
  1560. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(req.getWuid()).str());
  1561. }
  1562. catch(IException* e)
  1563. {
  1564. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1565. }
  1566. return true;
  1567. }
  1568. bool CFileSprayEx::onGetDFUExceptions(IEspContext &context, IEspGetDFUExceptions &req, IEspGetDFUExceptionsResponse &resp)
  1569. {
  1570. try
  1571. {
  1572. context.ensureFeatureAccess(DFU_EX_URL, SecAccess_Read, ECLWATCH_DFU_EX_ACCESS_DENIED, "Failed to get DFU Exceptions. Permission denied.");
  1573. IArrayOf<IEspDFUException> result;
  1574. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1575. Owned<IDFUWorkUnit> wu = factory->updateWorkUnit(req.getWuid());
  1576. if(!wu)
  1577. throw MakeStringException(ECLWATCH_CANNOT_GET_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  1578. PROGLOG("GetDFUExceptions: %s", req.getWuid());
  1579. Owned<IExceptionIterator> itr = wu->getExceptionIterator();
  1580. itr->first();
  1581. while(itr->isValid())
  1582. {
  1583. Owned<IEspDFUException> resultE = createDFUException("", "");
  1584. IException &e = itr->query();
  1585. resultE->setCode(e.errorCode());
  1586. StringBuffer msg;
  1587. resultE->setMessage(e.errorMessage(msg).str());
  1588. result.append(*resultE.getLink());
  1589. itr->next();
  1590. }
  1591. resp.setResult(result);
  1592. }
  1593. catch(IException* e)
  1594. {
  1595. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1596. }
  1597. return true;
  1598. }
  1599. void CFileSprayEx::readAndCheckSpraySourceReq(MemoryBuffer& srcxml, const char* srcIP, const char* srcPath, const char* srcPlane,
  1600. StringBuffer& sourceIPReq, StringBuffer& sourcePathReq)
  1601. {
  1602. StringBuffer sourcePath;
  1603. if(srcxml.length() == 0)
  1604. {
  1605. if (!isEmptyString(srcPlane))
  1606. {
  1607. Owned<IPropertyTree> dropZone = getDropZonePlane(srcPlane);
  1608. if (!dropZone)
  1609. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Unknown landing zone: %s", srcPlane);
  1610. const char * dropZonePlanePath = dropZone->queryProp("@prefix");
  1611. if (isAbsolutePath(srcPath))
  1612. {
  1613. if (!startsWith(srcPath,dropZonePlanePath))
  1614. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid source path");
  1615. sourcePath.append(srcPath).trim();
  1616. }
  1617. else
  1618. {
  1619. sourcePath.append(dropZonePlanePath);
  1620. addNonEmptyPathSepChar(sourcePath);
  1621. sourcePath.append(srcPath).trim();
  1622. }
  1623. const char * hostGroup = dropZone->queryProp("@hostGroup");
  1624. if (hostGroup)
  1625. {
  1626. Owned<IPropertyTree> match = getHostGroup(hostGroup,true);
  1627. if (!isEmptyString(srcIP))
  1628. {
  1629. // Already have srcIP. Just need to check that the ip is valid for storage plane.
  1630. bool ipAddressMatches = false;
  1631. Owned<IPropertyTreeIterator> hostIter = match->getElements("hosts");
  1632. ForEach (*hostIter)
  1633. {
  1634. const char *knownIP = hostIter->query().queryProp(nullptr);
  1635. if (strcmp(knownIP, srcIP)==0)
  1636. {
  1637. ipAddressMatches=true;
  1638. break;
  1639. }
  1640. }
  1641. if (!ipAddressMatches)
  1642. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "srcip %s is not valid storage plane %s", srcIP, srcPlane);
  1643. sourceIPReq.set(srcIP);
  1644. }
  1645. else
  1646. sourceIPReq.set(match->queryProp("hosts[1]"));
  1647. }
  1648. else
  1649. {
  1650. sourceIPReq.set("localhost"); // storage plane will be mounted when not using hostgroup
  1651. }
  1652. }
  1653. else
  1654. {
  1655. sourcePath.append(srcPath).trim();
  1656. sourceIPReq.set(srcIP).trim();
  1657. }
  1658. if (sourceIPReq.isEmpty())
  1659. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Source network IP not specified.");
  1660. if (sourcePath.isEmpty())
  1661. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Source path not specified.");
  1662. }
  1663. getStandardPosixPath(sourcePathReq, sourcePath.str());
  1664. }
  1665. static void checkValidDfuQueue(const char * dfuQueue)
  1666. {
  1667. if (isEmptyString(dfuQueue))
  1668. return;
  1669. #ifndef _CONTAINERIZED
  1670. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  1671. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  1672. if (!constEnv->isValidDfuQueueName(dfuQueue))
  1673. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid DFU server queue name:'%s'", dfuQueue);
  1674. #else
  1675. bool isValidDfuQueueName = false;
  1676. Owned<IPropertyTreeIterator> dfuServers = getComponentConfigSP()->getElements("dfuQueues");
  1677. ForEach(*dfuServers)
  1678. {
  1679. IPropertyTree & dfuServer = dfuServers->query();
  1680. const char * dfuServerName = dfuServer.queryProp("@name");
  1681. StringBuffer knownDfuQueueName;
  1682. getDfuQueueName(knownDfuQueueName, dfuServerName);
  1683. if (streq(dfuQueue, knownDfuQueueName))
  1684. {
  1685. isValidDfuQueueName = true;
  1686. break;
  1687. }
  1688. }
  1689. if (!isValidDfuQueueName)
  1690. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Invalid DFU server queue name: '%s'", dfuQueue);
  1691. #endif
  1692. }
  1693. bool CFileSprayEx::onSprayFixed(IEspContext &context, IEspSprayFixed &req, IEspSprayFixedResponse &resp)
  1694. {
  1695. try
  1696. {
  1697. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Spray. Permission denied.");
  1698. StringBuffer destFolder, destTitle, defaultFolder, defaultReplicateFolder;
  1699. const char* destNodeGroup = req.getDestGroup();
  1700. if (isEmptyString(destNodeGroup))
  1701. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Destination node group not specified.");
  1702. MemoryBuffer& srcxml = (MemoryBuffer&)req.getSrcxml();
  1703. StringBuffer sourceIPReq, sourcePathReq;
  1704. readAndCheckSpraySourceReq(srcxml, req.getSourceIP(), req.getSourcePath(), req.getSourcePlane(), sourceIPReq, sourcePathReq);
  1705. const char* srcip = sourceIPReq.str();
  1706. const char* srcfile = sourcePathReq.str();
  1707. const char* destname = req.getDestLogicalName();
  1708. if(isEmptyString(destname))
  1709. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Destination file not specified.");
  1710. CDfsLogicalFileName lfn;
  1711. if (!lfn.setValidate(destname))
  1712. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Invalid destination filename:'%s'", destname);
  1713. destname = lfn.get();
  1714. PROGLOG("SprayFixed: DestLogicalName %s, DestGroup %s", destname, destNodeGroup);
  1715. StringBuffer gName, ipAddr;
  1716. const char *pTr = strchr(destNodeGroup, ' ');
  1717. if (pTr)
  1718. {
  1719. gName.append(pTr - destNodeGroup, destNodeGroup);
  1720. ipAddr.append(pTr+1);
  1721. }
  1722. else
  1723. gName.append(destNodeGroup);
  1724. if (ipAddr.length() > 0)
  1725. ParseLogicalPath(destname, ipAddr.str(), NULL, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1726. else
  1727. ParseLogicalPath(destname, destNodeGroup, NULL, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1728. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1729. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1730. wu->setClusterName(gName.str());
  1731. wu->setJobName(destTitle.str());
  1732. const char * dfuQueue = req.getDFUServerQueue();
  1733. checkValidDfuQueue(dfuQueue);
  1734. setDFUServerQueueReq(dfuQueue, wu);
  1735. setUserAuth(context, wu);
  1736. wu->setCommand(DFUcmd_import);
  1737. IDFUfileSpec *source = wu->queryUpdateSource();
  1738. if(srcxml.length() == 0)
  1739. {
  1740. RemoteMultiFilename rmfn;
  1741. SocketEndpoint ep(srcip);
  1742. if (ep.isNull())
  1743. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "SprayFixed to %s: cannot resolve source network IP from %s.", destname, srcip);
  1744. rmfn.setEp(ep);
  1745. StringBuffer fnamebuf(srcfile);
  1746. fnamebuf.trim();
  1747. rmfn.append(fnamebuf.str()); // handles comma separated files
  1748. source->setMultiFilename(rmfn);
  1749. }
  1750. else
  1751. {
  1752. srcxml.append('\0');
  1753. source->setFromXML((const char*)srcxml.toByteArray());
  1754. }
  1755. IDFUfileSpec *destination = wu->queryUpdateDestination();
  1756. bool nosplit = req.getNosplit();
  1757. int recordsize = req.getSourceRecordSize();
  1758. const char* format = req.getSourceFormat();
  1759. if ((recordsize == RECFMVB_RECSIZE_ESCAPE) || (format && strieq(format, "recfmvb")))
  1760. {//recordsize may be set by dfuplus; format may be set by EclWatch
  1761. source->setFormat(DFUff_recfmvb);
  1762. destination->setFormat(DFUff_variable);
  1763. }
  1764. else if ((recordsize == RECFMV_RECSIZE_ESCAPE) || (format && strieq(format, "recfmv")))
  1765. {
  1766. source->setFormat(DFUff_recfmv);
  1767. destination->setFormat(DFUff_variable);
  1768. }
  1769. else if ((recordsize == PREFIX_VARIABLE_RECSIZE_ESCAPE) || (format && strieq(format, "variable")))
  1770. {
  1771. source->setFormat(DFUff_variable);
  1772. destination->setFormat(DFUff_variable);
  1773. }
  1774. else if((recordsize == PREFIX_VARIABLE_BIGENDIAN_RECSIZE_ESCAPE) || (format && strieq(format, "variablebigendian")))
  1775. {
  1776. source->setFormat(DFUff_variablebigendian);
  1777. destination->setFormat(DFUff_variable);
  1778. }
  1779. else if(recordsize == 0 && !nosplit) // -ve record sizes for blocked
  1780. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid record size");
  1781. else
  1782. source->setRecordSize(recordsize);
  1783. destination->setLogicalName(destname);
  1784. destination->setDirectory(destFolder.str());
  1785. StringBuffer fileMask;
  1786. constructFileMask(destTitle.str(), fileMask);
  1787. destination->setFileMask(fileMask.str());
  1788. destination->setGroupName(gName.str());
  1789. const char * encryptkey = req.getEncrypt();
  1790. if(req.getCompress()||(encryptkey&&*encryptkey))
  1791. destination->setCompressed(true);
  1792. ClusterPartDiskMapSpec mspec;
  1793. destination->getClusterPartDiskMapSpec(gName.str(), mspec);
  1794. mspec.setDefaultBaseDir(defaultFolder.str());
  1795. mspec.setDefaultReplicateDir(defaultReplicateFolder.str());
  1796. destination->setClusterPartDiskMapSpec(gName.str(), mspec);
  1797. int repo = req.getReplicateOffset();
  1798. bool isNull = req.getReplicateOffset_isNull();
  1799. if (!isNull && (repo!=1))
  1800. destination->setReplicateOffset(repo);
  1801. if (req.getWrap())
  1802. destination->setWrap(true);
  1803. IDFUoptions *options = wu->queryUpdateOptions();
  1804. const char * decryptkey = req.getDecrypt();
  1805. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  1806. options->setEncDec(encryptkey,decryptkey);
  1807. options->setReplicate(req.getReplicate());
  1808. options->setOverwrite(req.getOverwrite()); // needed if target already exists
  1809. const char* prefix = req.getPrefix();
  1810. if(prefix && *prefix)
  1811. options->setLengthPrefix(prefix);
  1812. if(req.getNosplit())
  1813. options->setNoSplit(true);
  1814. if(req.getNorecover())
  1815. options->setNoRecover(true);
  1816. if(req.getMaxConnections() > 0)
  1817. options->setmaxConnections(req.getMaxConnections());
  1818. if(req.getThrottle() > 0)
  1819. options->setThrottle(req.getThrottle());
  1820. if(req.getTransferBufferSize() > 0)
  1821. options->setTransferBufferSize(req.getTransferBufferSize());
  1822. if (req.getPull())
  1823. options->setPull(true);
  1824. if (req.getPush())
  1825. options->setPush(true);
  1826. if (!req.getNoCommon_isNull())
  1827. options->setNoCommon(req.getNoCommon());
  1828. if (req.getFailIfNoSourceFile())
  1829. options->setFailIfNoSourceFile(true);
  1830. if (req.getRecordStructurePresent())
  1831. options->setRecordStructurePresent(true);
  1832. if (!req.getExpireDays_isNull())
  1833. options->setExpireDays(req.getExpireDays());
  1834. resp.setWuid(wu->queryId());
  1835. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  1836. submitDFUWorkUnit(wu.getClear());
  1837. }
  1838. catch(IException* e)
  1839. {
  1840. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1841. }
  1842. return true;
  1843. }
  1844. bool CFileSprayEx::onSprayVariable(IEspContext &context, IEspSprayVariable &req, IEspSprayResponse &resp)
  1845. {
  1846. try
  1847. {
  1848. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Spray. Permission denied.");
  1849. StringBuffer destFolder, destTitle, defaultFolder, defaultReplicateFolder;
  1850. const char* destNodeGroup = req.getDestGroup();
  1851. if (isEmptyString(destNodeGroup))
  1852. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Destination node group not specified.");
  1853. StringBuffer gName, ipAddr;
  1854. const char *pTr = strchr(destNodeGroup, ' ');
  1855. if (pTr)
  1856. {
  1857. gName.append(pTr - destNodeGroup, destNodeGroup);
  1858. ipAddr.append(pTr+1);
  1859. }
  1860. else
  1861. gName.append(destNodeGroup);
  1862. MemoryBuffer& srcxml = (MemoryBuffer&)req.getSrcxml();
  1863. StringBuffer sourceIPReq, sourcePathReq;
  1864. readAndCheckSpraySourceReq(srcxml, req.getSourceIP(), req.getSourcePath(), req.getSourcePlane(), sourceIPReq, sourcePathReq);
  1865. const char* srcip = sourceIPReq.str();
  1866. const char* srcfile = sourcePathReq.str();
  1867. const char* destname = req.getDestLogicalName();
  1868. if(isEmptyString(destname))
  1869. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Destination file not specified.");
  1870. CDfsLogicalFileName lfn;
  1871. if (!lfn.setValidate(destname))
  1872. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Invalid destination filename:'%s'", destname);
  1873. destname = lfn.get();
  1874. PROGLOG("SprayVariable: DestLogicalName %s, DestGroup %s", destname, destNodeGroup);
  1875. if (ipAddr.length() > 0)
  1876. ParseLogicalPath(destname, ipAddr.str(), NULL, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1877. else
  1878. ParseLogicalPath(destname, destNodeGroup, NULL, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  1879. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  1880. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  1881. wu->setClusterName(gName.str());
  1882. wu->setJobName(destTitle.str());
  1883. const char * dfuQueue = req.getDFUServerQueue();
  1884. checkValidDfuQueue(dfuQueue);
  1885. setDFUServerQueueReq(dfuQueue, wu);
  1886. setUserAuth(context, wu);
  1887. wu->setCommand(DFUcmd_import);
  1888. IDFUfileSpec *source = wu->queryUpdateSource();
  1889. IDFUfileSpec *destination = wu->queryUpdateDestination();
  1890. IDFUoptions *options = wu->queryUpdateOptions();
  1891. if(srcxml.length() == 0)
  1892. {
  1893. RemoteMultiFilename rmfn;
  1894. SocketEndpoint ep(srcip);
  1895. if (ep.isNull())
  1896. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "SprayVariable to %s: cannot resolve source network IP from %s.", destname, srcip);
  1897. rmfn.setEp(ep);
  1898. StringBuffer fnamebuf(srcfile);
  1899. fnamebuf.trim();
  1900. rmfn.append(fnamebuf.str()); // handles comma separated files
  1901. source->setMultiFilename(rmfn);
  1902. }
  1903. else
  1904. {
  1905. srcxml.append('\0');
  1906. source->setFromXML((const char*)srcxml.toByteArray());
  1907. }
  1908. source->setMaxRecordSize(req.getSourceMaxRecordSize());
  1909. source->setFormat((DFUfileformat)req.getSourceFormat());
  1910. StringBuffer rowtag;
  1911. if (req.getIsJSON())
  1912. {
  1913. const char *srcRowPath = req.getSourceRowPath();
  1914. if (!srcRowPath || *srcRowPath != '/')
  1915. rowtag.append("/");
  1916. rowtag.append(srcRowPath);
  1917. }
  1918. else
  1919. rowtag.append(req.getSourceRowTag());
  1920. // if rowTag specified, it means it's xml or json format, otherwise it's csv
  1921. if(rowtag.length())
  1922. {
  1923. source->setRowTag(rowtag);
  1924. options->setKeepHeader(true);
  1925. }
  1926. else
  1927. {
  1928. const char* cs = req.getSourceCsvSeparate();
  1929. if (req.getNoSourceCsvSeparator())
  1930. {
  1931. cs = "";
  1932. }
  1933. else if(cs == NULL || *cs == '\0')
  1934. cs = "\\,";
  1935. const char* ct = req.getSourceCsvTerminate();
  1936. if(ct == NULL || *ct == '\0')
  1937. ct = "\\n,\\r\\n";
  1938. const char* cq = req.getSourceCsvQuote();
  1939. if(cq== NULL)
  1940. cq = "\"";
  1941. source->setCsvOptions(cs, ct, cq, req.getSourceCsvEscape(), req.getQuotedTerminator());
  1942. options->setQuotedTerminator(req.getQuotedTerminator());
  1943. }
  1944. destination->setLogicalName(destname);
  1945. destination->setDirectory(destFolder.str());
  1946. StringBuffer fileMask;
  1947. constructFileMask(destTitle.str(), fileMask);
  1948. destination->setFileMask(fileMask.str());
  1949. destination->setGroupName(gName.str());
  1950. ClusterPartDiskMapSpec mspec;
  1951. destination->getClusterPartDiskMapSpec(gName.str(), mspec);
  1952. mspec.setDefaultBaseDir(defaultFolder.str());
  1953. mspec.setDefaultReplicateDir(defaultReplicateFolder.str());
  1954. destination->setClusterPartDiskMapSpec(gName.str(), mspec);
  1955. const char * encryptkey = req.getEncrypt();
  1956. if(req.getCompress()||(encryptkey&&*encryptkey))
  1957. destination->setCompressed(true);
  1958. const char * decryptkey = req.getDecrypt();
  1959. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  1960. options->setEncDec(encryptkey,decryptkey);
  1961. int repo = req.getReplicateOffset();
  1962. bool isNull = req.getReplicateOffset_isNull();
  1963. if (!isNull && (repo!=1))
  1964. destination->setReplicateOffset(repo);
  1965. options->setReplicate(req.getReplicate());
  1966. options->setOverwrite(req.getOverwrite()); // needed if target already exists
  1967. const char* prefix = req.getPrefix();
  1968. if(prefix && *prefix)
  1969. options->setLengthPrefix(prefix);
  1970. if(req.getNosplit())
  1971. options->setNoSplit(true);
  1972. if(req.getNorecover())
  1973. options->setNoRecover(true);
  1974. if(req.getMaxConnections() > 0)
  1975. options->setmaxConnections(req.getMaxConnections());
  1976. if(req.getThrottle() > 0)
  1977. options->setThrottle(req.getThrottle());
  1978. if(req.getTransferBufferSize() > 0)
  1979. options->setTransferBufferSize(req.getTransferBufferSize());
  1980. if (req.getPull())
  1981. options->setPull(true);
  1982. if (req.getPush())
  1983. options->setPush(true);
  1984. if (req.getFailIfNoSourceFile())
  1985. options->setFailIfNoSourceFile(true);
  1986. if (req.getRecordStructurePresent())
  1987. options->setRecordStructurePresent(true);
  1988. if (!req.getExpireDays_isNull())
  1989. options->setExpireDays(req.getExpireDays());
  1990. resp.setWuid(wu->queryId());
  1991. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  1992. submitDFUWorkUnit(wu.getClear());
  1993. }
  1994. catch(IException* e)
  1995. {
  1996. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  1997. }
  1998. return true;
  1999. }
  2000. bool CFileSprayEx::onReplicate(IEspContext &context, IEspReplicate &req, IEspReplicateResponse &resp)
  2001. {
  2002. try
  2003. {
  2004. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Replicate. Permission denied.");
  2005. const char* srcname = req.getSourceLogicalName();
  2006. if(!srcname || !*srcname)
  2007. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  2008. PROGLOG("Replicate %s", srcname);
  2009. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2010. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2011. StringBuffer jobname("Replicate: ");
  2012. jobname.append(srcname);
  2013. wu->setJobName(jobname.str());
  2014. setDFUServerQueueReq(req.getDFUServerQueue(), wu);
  2015. setUserAuth(context, wu);
  2016. wu->setCommand(DFUcmd_replicate);
  2017. IDFUfileSpec *source = wu->queryUpdateSource();
  2018. if (source)
  2019. {
  2020. source->setLogicalName(srcname);
  2021. int repo = req.getReplicateOffset();
  2022. if (repo!=1)
  2023. source->setReplicateOffset(repo);
  2024. }
  2025. const char* cluster = req.getCluster();
  2026. if(cluster && *cluster)
  2027. {
  2028. IDFUoptions *opt = wu->queryUpdateOptions();
  2029. opt->setReplicateMode(DFURMmissing,cluster,req.getRepeatLast(),req.getOnlyRepeated());
  2030. }
  2031. resp.setWuid(wu->queryId());
  2032. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2033. submitDFUWorkUnit(wu.getClear());
  2034. }
  2035. catch(IException* e)
  2036. {
  2037. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2038. }
  2039. return true;
  2040. }
  2041. void CFileSprayEx::getDropZoneInfoByDestPlane(double clientVersion, const char* destPlane, const char* destFileIn, StringBuffer& destFileOut, StringBuffer& umask, StringBuffer & hostip)
  2042. {
  2043. Owned<IPropertyTree> dropZone = getDropZonePlane(destPlane);
  2044. if (!dropZone)
  2045. throw makeStringExceptionV(ECLWATCH_DROP_ZONE_NOT_FOUND, "Unknown landing zone %s", destPlane);
  2046. StringBuffer fullDropZoneDir(dropZone->queryProp("@prefix"));
  2047. addPathSepChar(fullDropZoneDir);
  2048. if (isAbsolutePath(destFileIn))
  2049. {
  2050. if (!startsWith(destFileIn, fullDropZoneDir))
  2051. throw makeStringExceptionV(ECLWATCH_DROP_ZONE_NOT_FOUND, "No landing zone configured for %s:%s", destPlane, destFileIn);
  2052. }
  2053. else
  2054. {
  2055. destFileOut.append(fullDropZoneDir);
  2056. addNonEmptyPathSepChar(destFileOut);
  2057. }
  2058. destFileOut.append(destFileIn).trim();
  2059. dropZone->getProp("@umask", umask);
  2060. const char * hostGroup = dropZone->queryProp("@hostGroup");
  2061. if (hostGroup)
  2062. {
  2063. Owned<IPropertyTree> match = getHostGroup(hostGroup,true);
  2064. if (!hostip.isEmpty())
  2065. {
  2066. // Already have hostip. Just need to check that the ip is valid for storage plane.
  2067. bool ipAddressMatches = false;
  2068. Owned<IPropertyTreeIterator> hostIter = match->getElements("hosts");
  2069. ForEach (*hostIter)
  2070. {
  2071. const char *knownIP = hostIter->query().queryProp(nullptr);
  2072. if (strcmp(knownIP, hostip)==0)
  2073. {
  2074. ipAddressMatches=true;
  2075. break;
  2076. }
  2077. }
  2078. if (!ipAddressMatches)
  2079. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "destip %s is not valid storage plane %s", hostip.str(), destPlane);
  2080. }
  2081. else
  2082. hostip.set(match->queryProp("hosts[1]"));
  2083. }
  2084. else
  2085. {
  2086. hostip.set("localhost"); // storage plane will be mounted when not using hostgroup
  2087. }
  2088. }
  2089. void CFileSprayEx::getDropZoneInfoByIP(double clientVersion, const char* ip, const char* destFileIn, StringBuffer& destFileOut, StringBuffer& umask)
  2090. {
  2091. #ifndef _CONTAINERIZED
  2092. if (destFileIn && *destFileIn)
  2093. destFileOut.set(destFileIn);
  2094. if (!ip || !*ip)
  2095. throw MakeStringExceptionDirect(ECLWATCH_INVALID_IP, "Network address must be specified for a drop zone!");
  2096. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  2097. Owned<IConstEnvironment> constEnv = factory->openEnvironment();
  2098. StringBuffer destFile;
  2099. if (isAbsolutePath(destFileIn))
  2100. {
  2101. destFile.set(destFileIn);
  2102. Owned<IConstDropZoneInfo> dropZone = constEnv->getDropZoneByAddressPath(ip, destFile.str());
  2103. if (!dropZone)
  2104. {
  2105. if (constEnv->isDropZoneRestrictionEnabled())
  2106. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "No drop zone configured for '%s' and '%s'. Check your system drop zone configuration.", ip, destFile.str());
  2107. else
  2108. {
  2109. LOG(MCdebugInfo, unknownJob, "No drop zone configured for '%s' and '%s'. Check your system drop zone configuration.", ip, destFile.str());
  2110. return;
  2111. }
  2112. }
  2113. SCMStringBuffer directory, maskBuf;
  2114. dropZone->getDirectory(directory);
  2115. destFileOut.set(destFile.str());
  2116. dropZone->getUMask(maskBuf);
  2117. if (maskBuf.length())
  2118. umask.set(maskBuf.str());
  2119. return;
  2120. }
  2121. Owned<IConstDropZoneInfoIterator> dropZoneItr = constEnv->getDropZoneIteratorByAddress(ip);
  2122. if (dropZoneItr->count() < 1)
  2123. {
  2124. if (constEnv->isDropZoneRestrictionEnabled())
  2125. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Drop zone not found for network address '%s'. Check your system drop zone configuration.", ip);
  2126. else
  2127. {
  2128. LOG(MCdebugInfo, unknownJob, "Drop zone not found for network address '%s'. Check your system drop zone configuration.", ip);
  2129. return;
  2130. }
  2131. }
  2132. bool dzFound = false;
  2133. ForEach(*dropZoneItr)
  2134. {
  2135. IConstDropZoneInfo& dropZoneInfo = dropZoneItr->query();
  2136. SCMStringBuffer dropZoneDirectory, dropZoneUMask;
  2137. dropZoneInfo.getDirectory(dropZoneDirectory);
  2138. dropZoneInfo.getUMask(dropZoneUMask);
  2139. if (!dropZoneDirectory.length())
  2140. continue;
  2141. if (!dzFound)
  2142. {
  2143. dzFound = true;
  2144. destFileOut.set(dropZoneDirectory.str());
  2145. addPathSepChar(destFileOut);
  2146. destFileOut.append(destFileIn);
  2147. if (dropZoneUMask.length())
  2148. umask.set(dropZoneUMask.str());
  2149. }
  2150. else
  2151. {
  2152. if (constEnv->isDropZoneRestrictionEnabled())
  2153. throw MakeStringException(ECLWATCH_INVALID_INPUT, "> 1 drop zones found for network address '%s'.", ip);
  2154. else
  2155. {
  2156. LOG(MCdebugInfo, unknownJob, "> 1 drop zones found for network address '%s'.", ip);
  2157. return;
  2158. }
  2159. }
  2160. }
  2161. if (!dzFound)
  2162. {
  2163. if (constEnv->isDropZoneRestrictionEnabled())
  2164. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "No valid drop zone found for network address '%s'. Check your system drop zone configuration.", ip);
  2165. else
  2166. LOG(MCdebugInfo, unknownJob, "No valid drop zone found for network address '%s'. Check your system drop zone configuration.", ip);
  2167. }
  2168. #else
  2169. throw makeStringException(-1, "Internal error: CFileSprayEx::getDropZoneInfoByIP should not be called in containerized environment");
  2170. #endif
  2171. }
  2172. static StringBuffer & expandLogicalAsPhysical(StringBuffer & target, const char * name, const char * separator)
  2173. {
  2174. const char * cur = name;
  2175. for (;;)
  2176. {
  2177. const char * colon = strstr(cur, "::");
  2178. if (!colon)
  2179. break;
  2180. //MORE: Process special characters?
  2181. target.append(colon - cur, cur);
  2182. target.append(separator);
  2183. cur = colon + 2;
  2184. }
  2185. return target.append(cur);
  2186. }
  2187. bool CFileSprayEx::onDespray(IEspContext &context, IEspDespray &req, IEspDesprayResponse &resp)
  2188. {
  2189. try
  2190. {
  2191. context.ensureFeatureAccess(FILE_DESPRAY_URL, SecAccess_Write, ECLWATCH_FILE_DESPRAY_ACCESS_DENIED, "Failed to do Despray. Permission denied.");
  2192. const char* srcname = req.getSourceLogicalName();
  2193. if(!srcname || !*srcname)
  2194. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  2195. PROGLOG("Despray %s", srcname);
  2196. double version = context.getClientVersion();
  2197. StringBuffer destip(req.getDestIP());
  2198. const char* destPlane = req.getDestPlane();
  2199. #ifdef _CONTAINERIZED
  2200. if (isEmptyString(destPlane))
  2201. destPlane = req.getDestGroup(); // allow eclwatch to continue providing storage plane as 'destgroup' field
  2202. #endif
  2203. StringBuffer destPath;
  2204. StringBuffer implicitDestFile;
  2205. const char* destfile = getStandardPosixPath(destPath, req.getDestPath()).str();
  2206. MemoryBuffer& dstxml = (MemoryBuffer&)req.getDstxml();
  2207. if(dstxml.length() == 0)
  2208. {
  2209. if(isEmptyString(destPlane) && destip.isEmpty())
  2210. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination network IP/storage plane not specified.");
  2211. //If the destination filename is not provided, calculate a relative filename from the logical filename
  2212. if(!destfile || !*destfile)
  2213. {
  2214. expandLogicalAsPhysical(implicitDestFile, srcname, "/");
  2215. destfile = implicitDestFile;
  2216. }
  2217. }
  2218. StringBuffer srcTitle;
  2219. ParseLogicalPath(srcname, srcTitle);
  2220. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2221. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2222. wu->setJobName(srcTitle.str());
  2223. setDFUServerQueueReq(req.getDFUServerQueue(), wu);
  2224. setUserAuth(context, wu);
  2225. wu->setCommand(DFUcmd_export);
  2226. IDFUfileSpec *source = wu->queryUpdateSource();
  2227. IDFUfileSpec *destination = wu->queryUpdateDestination();
  2228. IDFUoptions *options = wu->queryUpdateOptions();
  2229. bool preserveFileParts = req.getWrap();
  2230. source->setLogicalName(srcname);
  2231. if(dstxml.length() == 0)
  2232. {
  2233. StringBuffer destfileWithPath, umask;
  2234. if (!isEmptyString(destPlane))
  2235. getDropZoneInfoByDestPlane(version, destPlane, destfile, destfileWithPath, umask, destip);
  2236. else
  2237. getDropZoneInfoByIP(version, destip, destfile, destfileWithPath, umask);
  2238. RemoteFilename rfn;
  2239. SocketEndpoint ep(destip.str());
  2240. if (ep.isNull())
  2241. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Despray %s: cannot resolve destination network IP from %s.", srcname, destip.str());
  2242. //Ensure the filename is dependent on the file part if parts are being preserved
  2243. if (preserveFileParts && !strstr(destfileWithPath, "$P$"))
  2244. destfileWithPath.append("._$P$_of_$N$");
  2245. rfn.setPath(ep, destfileWithPath.str());
  2246. if (umask.length())
  2247. options->setUMask(umask.str());
  2248. destination->setSingleFilename(rfn);
  2249. }
  2250. else
  2251. {
  2252. dstxml.append('\0');
  2253. destination->setFromXML((const char*)dstxml.toByteArray());
  2254. }
  2255. destination->setTitle(srcTitle.str());
  2256. options->setKeepHeader(true);
  2257. options->setOverwrite(req.getOverwrite()); // needed if target already exists
  2258. const char* splitprefix = req.getSplitprefix();
  2259. if(splitprefix && *splitprefix)
  2260. options->setSplitPrefix(splitprefix);
  2261. if (version > 1.01)
  2262. {
  2263. if(req.getMaxConnections() > 0)
  2264. options->setmaxConnections(req.getMaxConnections());
  2265. else if(req.getSingleConnection())
  2266. options->setmaxConnections(1);
  2267. }
  2268. else
  2269. {
  2270. if(req.getMaxConnections() > 0)
  2271. options->setmaxConnections(req.getMaxConnections());
  2272. }
  2273. if(req.getThrottle() > 0)
  2274. options->setThrottle(req.getThrottle());
  2275. if(req.getTransferBufferSize() > 0)
  2276. options->setTransferBufferSize(req.getTransferBufferSize());
  2277. if(req.getNorecover())
  2278. options->setNoRecover(true);
  2279. if (preserveFileParts) {
  2280. options->setPush(); // I think needed for a despray
  2281. destination->setWrap(true);
  2282. }
  2283. if (req.getMultiCopy())
  2284. destination->setMultiCopy(true);
  2285. const char * encryptkey = req.getEncrypt();
  2286. if(req.getCompress()||(encryptkey&&*encryptkey))
  2287. destination->setCompressed(true);
  2288. const char * decryptkey = req.getDecrypt();
  2289. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  2290. options->setEncDec(encryptkey,decryptkey);
  2291. resp.setWuid(wu->queryId());
  2292. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2293. submitDFUWorkUnit(wu.getClear());
  2294. }
  2295. catch(IException* e)
  2296. {
  2297. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2298. }
  2299. return true;
  2300. }
  2301. bool CFileSprayEx::onCopy(IEspContext &context, IEspCopy &req, IEspCopyResponse &resp)
  2302. {
  2303. try
  2304. {
  2305. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Copy. Permission denied.");
  2306. const char* srcname = req.getSourceLogicalName();
  2307. const char* dstname = req.getDestLogicalName();
  2308. if(!srcname || !*srcname)
  2309. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  2310. if(!dstname || !*dstname)
  2311. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination logical file not specified.");
  2312. PROGLOG("Copy from %s to %s", srcname, dstname);
  2313. StringBuffer destFolder, destTitle, defaultFolder, defaultReplicateFolder;
  2314. StringBuffer srcNodeGroup, destNodeGroup;
  2315. bool bRoxie = false;
  2316. const char* destNodeGroupReq = req.getDestGroup();
  2317. if(!destNodeGroupReq || !*destNodeGroupReq)
  2318. {
  2319. getNodeGroupFromLFN(context, srcname, destNodeGroup);
  2320. DBGLOG("Destination node group not specified, using source node group %s", destNodeGroup.str());
  2321. }
  2322. else
  2323. {
  2324. destNodeGroup = destNodeGroupReq;
  2325. const char* destRoxie = req.getDestGroupRoxie();
  2326. if (destRoxie && !stricmp(destRoxie, "Yes"))
  2327. {
  2328. bRoxie = true;
  2329. }
  2330. }
  2331. CDfsLogicalFileName lfn; // NOTE: must not be moved into block below, or dstname will point to invalid memory
  2332. if (!bRoxie)
  2333. {
  2334. if (!lfn.setValidate(dstname))
  2335. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid destination filename:'%s'", dstname);
  2336. dstname = lfn.get();
  2337. }
  2338. ParseLogicalPath(dstname, destNodeGroup.str(), NULL, destFolder, destTitle, defaultFolder, defaultReplicateFolder);
  2339. StringBuffer fileMask;
  2340. constructFileMask(destTitle.str(), fileMask);
  2341. Owned<IUserDescriptor> udesc=createUserDescriptor();
  2342. const char* srcDali = req.getSourceDali();
  2343. const char* srcu = req.getSrcusername();
  2344. if (!isEmptyString(srcDali) && !isEmptyString(srcu))
  2345. {
  2346. udesc->set(srcu, req.getSrcpassword());
  2347. }
  2348. else
  2349. {
  2350. StringBuffer user, passwd;
  2351. context.getUserID(user);
  2352. context.getPassword(passwd);
  2353. udesc->set(user, passwd);
  2354. }
  2355. CDfsLogicalFileName logicalName;
  2356. logicalName.set(srcname);
  2357. if (!isEmptyString(srcDali))
  2358. {
  2359. SocketEndpoint ep(srcDali);
  2360. if (ep.isNull())
  2361. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Copy %s: cannot resolve SourceDali network IP from %s.", srcname, srcDali);
  2362. logicalName.setForeign(ep,false);
  2363. }
  2364. Owned<IDistributedFile> file = queryDistributedFileDirectory().lookup(logicalName, udesc, false, false, false, nullptr, defaultPrivilegedUser);
  2365. if (!file)
  2366. throw MakeStringException(ECLWATCH_FILE_NOT_EXIST, "Failed to find file: %s", logicalName.get());
  2367. bool supercopy = req.getSuperCopy();
  2368. if (supercopy)
  2369. {
  2370. if (!file->querySuperFile())
  2371. supercopy = false;
  2372. }
  2373. else if (file->querySuperFile() && (file->querySuperFile()->numSubFiles() > 1) && isFileKey(file))
  2374. supercopy = true;
  2375. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2376. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2377. wu->setJobName(dstname);
  2378. setDFUServerQueueReq(req.getDFUServerQueue(), wu);
  2379. setUserAuth(context, wu);
  2380. if(destNodeGroup.length() > 0)
  2381. wu->setClusterName(destNodeGroup.str());
  2382. if (supercopy)
  2383. wu->setCommand(DFUcmd_supercopy);
  2384. else
  2385. wu->setCommand(DFUcmd_copy);
  2386. IDFUfileSpec *wuFSpecSource = wu->queryUpdateSource();
  2387. IDFUfileSpec *wuFSpecDest = wu->queryUpdateDestination();
  2388. IDFUoptions *wuOptions = wu->queryUpdateOptions();
  2389. wuFSpecSource->setLogicalName(srcname);
  2390. if(srcDali && *srcDali)
  2391. {
  2392. SocketEndpoint ep(srcDali);
  2393. wuFSpecSource->setForeignDali(ep);
  2394. const char* srcusername = req.getSrcusername();
  2395. if(srcusername && *srcusername)
  2396. {
  2397. const char* srcpasswd = req.getSrcpassword();
  2398. wuFSpecSource->setForeignUser(srcusername, srcpasswd);
  2399. }
  2400. }
  2401. wuFSpecDest->setLogicalName(dstname);
  2402. wuFSpecDest->setFileMask(fileMask.str());
  2403. wuOptions->setOverwrite(req.getOverwrite());
  2404. wuOptions->setPreserveCompression(req.getPreserveCompression());
  2405. if (!req.getExpireDays_isNull())
  2406. wuOptions->setExpireDays(req.getExpireDays());
  2407. if(req.getNosplit())
  2408. wuOptions->setNoSplit(true);
  2409. if (!req.getNoCommon_isNull())
  2410. wuOptions->setNoCommon(req.getNoCommon());
  2411. if (bRoxie)
  2412. {
  2413. setRoxieClusterPartDiskMapping(destNodeGroup.str(), defaultFolder.str(), defaultReplicateFolder.str(), supercopy, wuFSpecDest, wuOptions);
  2414. wuFSpecDest->setWrap(true); // roxie always wraps
  2415. if(req.getCompress())
  2416. wuFSpecDest->setCompressed(true);
  2417. if (!supercopy)
  2418. wuOptions->setSuppressNonKeyRepeats(true); // **** only repeat last part when src kind = key
  2419. }
  2420. else
  2421. {
  2422. const char* srcDiffKeyName = req.getSourceDiffKeyName();
  2423. const char* destDiffKeyName = req.getDestDiffKeyName();
  2424. if (srcDiffKeyName&&*srcDiffKeyName)
  2425. wuFSpecSource->setDiffKey(srcDiffKeyName);
  2426. if (destDiffKeyName&&*destDiffKeyName)
  2427. wuFSpecDest->setDiffKey(destDiffKeyName);
  2428. wuFSpecDest->setDirectory(destFolder.str());
  2429. wuFSpecDest->setGroupName(destNodeGroup.str());
  2430. wuFSpecDest->setWrap(req.getWrap());
  2431. const char * encryptkey = req.getEncrypt();
  2432. if(req.getCompress()||(encryptkey&&*encryptkey))
  2433. wuFSpecDest->setCompressed(true);
  2434. wuOptions->setReplicate(req.getReplicate());
  2435. const char * decryptkey = req.getDecrypt();
  2436. if ((encryptkey&&*encryptkey)||(decryptkey&&*decryptkey))
  2437. wuOptions->setEncDec(encryptkey,decryptkey);
  2438. if(req.getNorecover())
  2439. wuOptions->setNoRecover(true);
  2440. if(!req.getNosplit_isNull())
  2441. wuOptions->setNoSplit(req.getNosplit());
  2442. if(req.getMaxConnections() > 0)
  2443. wuOptions->setmaxConnections(req.getMaxConnections());
  2444. if(req.getThrottle() > 0)
  2445. wuOptions->setThrottle(req.getThrottle());
  2446. if(req.getTransferBufferSize() > 0)
  2447. wuOptions->setTransferBufferSize(req.getTransferBufferSize());
  2448. if (req.getPull())
  2449. wuOptions->setPull(true);
  2450. if (req.getPush())
  2451. wuOptions->setPush(true);
  2452. if (req.getIfnewer())
  2453. wuOptions->setIfNewer(true);
  2454. if (req.getNosplit())
  2455. wuOptions->setNoSplit(true);
  2456. ClusterPartDiskMapSpec mspec;
  2457. wuFSpecDest->getClusterPartDiskMapSpec(destNodeGroup.str(), mspec);
  2458. mspec.setDefaultBaseDir(defaultFolder.str());
  2459. mspec.setDefaultReplicateDir(defaultReplicateFolder.str());
  2460. wuFSpecDest->setClusterPartDiskMapSpec(destNodeGroup.str(), mspec);
  2461. }
  2462. resp.setResult(wu->queryId());
  2463. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2464. submitDFUWorkUnit(wu.getClear());
  2465. }
  2466. catch(IException* e)
  2467. {
  2468. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2469. }
  2470. return true;
  2471. }
  2472. bool CFileSprayEx::onRename(IEspContext &context, IEspRename &req, IEspRenameResponse &resp)
  2473. {
  2474. try
  2475. {
  2476. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Write, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do Rename. Permission denied.");
  2477. const char* srcname = req.getSrcname();
  2478. const char* dstname = req.getDstname();
  2479. if(!srcname || !*srcname)
  2480. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Source logical file not specified.");
  2481. if(!dstname || !*dstname)
  2482. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Destination logical file not specified.");
  2483. PROGLOG("Rename from %s to %s", srcname, dstname);
  2484. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2485. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2486. StringBuffer destTitle;
  2487. ParseLogicalPath(req.getDstname(), destTitle);
  2488. wu->setJobName(destTitle.str());
  2489. setDFUServerQueueReq(req.getDFUServerQueue(), wu);
  2490. setUserAuth(context, wu);
  2491. wu->setCommand(DFUcmd_rename);
  2492. #if 0 // TBD - Handling for multiple clusters? the cluster should be specified by user if needed
  2493. Owned<IUserDescriptor> udesc;
  2494. if(user.length() > 0)
  2495. {
  2496. const char* passwd = context.queryPassword();
  2497. udesc.setown(createUserDescriptor());
  2498. udesc->set(user.str(), passwd);
  2499. Owned<IDistributedFile> df = queryDistributedFileDirectory().lookup(srcname, udesc);
  2500. if(df)
  2501. {
  2502. StringBuffer cluster0;
  2503. df->getClusterName(0,cluster0); // TBD - Handling for multiple clusters?
  2504. if (cluster0.length()!=0)
  2505. {
  2506. wu->setClusterName(cluster0.str());
  2507. }
  2508. else
  2509. {
  2510. const char *cluster = df->queryAttributes().queryProp("@group");
  2511. if (cluster && *cluster)
  2512. {
  2513. wu->setClusterName(cluster);
  2514. }
  2515. }
  2516. }
  2517. }
  2518. #endif
  2519. IDFUfileSpec *source = wu->queryUpdateSource();
  2520. source->setLogicalName(srcname);
  2521. IDFUfileSpec *destination = wu->queryUpdateDestination();
  2522. destination->setLogicalName(dstname);
  2523. IDFUoptions *options = wu->queryUpdateOptions();
  2524. options->setOverwrite(req.getOverwrite());
  2525. resp.setWuid(wu->queryId());
  2526. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2527. submitDFUWorkUnit(wu.getClear());
  2528. }
  2529. catch(IException* e)
  2530. {
  2531. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2532. }
  2533. return true;
  2534. }
  2535. bool CFileSprayEx::onDFUWUFile(IEspContext &context, IEspDFUWUFileRequest &req, IEspDFUWUFileResponse &resp)
  2536. {
  2537. try
  2538. {
  2539. context.ensureFeatureAccess(DFU_WU_URL, SecAccess_Read, ECLWATCH_DFU_WU_ACCESS_DENIED, "Access to DFU workunit is denied.");
  2540. if (*req.getWuid())
  2541. {
  2542. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2543. Owned<IConstDFUWorkUnit> wu = factory->openWorkUnit(req.getWuid(), false);
  2544. if(!wu)
  2545. throw MakeStringException(ECLWATCH_CANNOT_OPEN_WORKUNIT, "Dfu workunit %s not found.", req.getWuid());
  2546. PROGLOG("DFUWUFile: %s", req.getWuid());
  2547. StringBuffer xmlbuf;
  2548. xmlbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  2549. const char* plainText = req.getPlainText();
  2550. if (plainText && (!stricmp(plainText, "yes")))
  2551. {
  2552. wu->toXML(xmlbuf);
  2553. resp.setFile(xmlbuf.str());
  2554. resp.setFile_mimetype(HTTP_TYPE_TEXT_PLAIN);
  2555. }
  2556. else
  2557. {
  2558. xmlbuf.append("<?xml-stylesheet href=\"../esp/xslt/xmlformatter.xsl\" type=\"text/xsl\"?>");
  2559. wu->toXML(xmlbuf);
  2560. resp.setFile(xmlbuf.str());
  2561. resp.setFile_mimetype(HTTP_TYPE_APPLICATION_XML);
  2562. }
  2563. }
  2564. }
  2565. catch(IException* e)
  2566. {
  2567. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2568. }
  2569. return true;
  2570. }
  2571. bool CFileSprayEx::onFileList(IEspContext &context, IEspFileListRequest &req, IEspFileListResponse &resp)
  2572. {
  2573. try
  2574. {
  2575. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do FileList. Permission denied.");
  2576. const char* path = req.getPath();
  2577. if (!path || !*path)
  2578. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Path not specified.");
  2579. double version = context.getClientVersion();
  2580. const char* netaddr = req.getNetaddr();
  2581. if (!netaddr || !*netaddr)
  2582. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Network address not specified.");
  2583. const char* fileNameMask = req.getMask();
  2584. bool directoryOnly = req.getDirectoryOnly();
  2585. PROGLOG("FileList: Netaddr %s, Path %s", netaddr, path);
  2586. StringBuffer sPath(path);
  2587. const char* osStr = req.getOS();
  2588. if (osStr && *osStr)
  2589. {
  2590. int os = atoi(osStr);
  2591. const char pathSep = (os == OS_WINDOWS) ? '\\' : '/';
  2592. sPath.replace(pathSep=='\\'?'/':'\\', pathSep);
  2593. if (*(sPath.str() + sPath.length() -1) != pathSep)
  2594. sPath.append( pathSep );
  2595. }
  2596. if (!isEmptyString(fileNameMask))
  2597. {
  2598. const char* ext = pathExtension(sPath.str());
  2599. if (ext && !strieq(ext, "cfg") && !strieq(ext, "log"))
  2600. throw MakeStringException(ECLWATCH_ACCESS_TO_FILE_DENIED, "Only cfg or log file allowed.");
  2601. }
  2602. RemoteFilename rfn;
  2603. SocketEndpoint ep;
  2604. #ifdef MACHINE_IP
  2605. ep.set(MACHINE_IP);
  2606. #else
  2607. ep.set(netaddr);
  2608. if (ep.isNull())
  2609. throw MakeStringException(ECLWATCH_INVALID_INPUT, "FileList: cannot resolve network IP from %s.", netaddr);
  2610. #endif
  2611. rfn.setPath(ep, sPath.str());
  2612. Owned<IFile> f = createIFile(rfn);
  2613. if (f->isDirectory()!=fileBool::foundYes)
  2614. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", path);
  2615. IArrayOf<IEspPhysicalFileStruct> files;
  2616. Owned<IDirectoryIterator> di = f->directoryFiles(NULL, false, true);
  2617. if(di.get() != NULL)
  2618. {
  2619. ForEach(*di)
  2620. {
  2621. StringBuffer fname;
  2622. di->getName(fname);
  2623. if (fname.length() == 0 || (directoryOnly && !di->isDir()) || (!di->isDir() && !isEmptyString(fileNameMask) && !WildMatch(fname.str(), fileNameMask, true)))
  2624. continue;
  2625. Owned<IEspPhysicalFileStruct> onefile = createPhysicalFileStruct();
  2626. onefile->setName(fname.str());
  2627. onefile->setIsDir(di->isDir());
  2628. onefile->setFilesize(di->getFileSize());
  2629. CDateTime modtime;
  2630. StringBuffer timestr;
  2631. di->getModifiedTime(modtime);
  2632. unsigned y,m,d,h,min,sec,nsec;
  2633. modtime.getDate(y,m,d,true);
  2634. modtime.getTime(h,min,sec,nsec,true);
  2635. timestr.appendf("%04d-%02d-%02d %02d:%02d:%02d", y,m,d,h,min,sec);
  2636. onefile->setModifiedtime(timestr.str());
  2637. files.append(*onefile.getLink());
  2638. }
  2639. }
  2640. sPath.replace('\\', '/');//XSLT cannot handle backslashes
  2641. resp.setPath(sPath);
  2642. resp.setFiles(files);
  2643. resp.setNetaddr(netaddr);
  2644. if (osStr && *osStr)
  2645. {
  2646. int os = atoi(osStr);
  2647. resp.setOS(os);
  2648. }
  2649. if (!isEmptyString(fileNameMask))
  2650. resp.setMask(fileNameMask);
  2651. if (version >= 1.10)
  2652. {
  2653. StringBuffer acceptLanguage;
  2654. resp.setAcceptLanguage(getAcceptLanguage(context, acceptLanguage).str());
  2655. }
  2656. resp.setDirectoryOnly(directoryOnly);
  2657. }
  2658. catch(IException* e)
  2659. {
  2660. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2661. }
  2662. return true;
  2663. }
  2664. bool CFileSprayEx::checkDropZoneIPAndPath(double clientVersion, const char* dropZoneName, const char* netAddr, const char* path)
  2665. {
  2666. if (isEmptyString(netAddr) || isEmptyString(path))
  2667. throw MakeStringException(ECLWATCH_INVALID_INPUT, "NetworkAddress or Path not defined.");
  2668. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  2669. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  2670. Owned<IConstDropZoneInfoIterator> dropZoneItr = constEnv->getDropZoneIteratorByAddress(netAddr);
  2671. ForEach(*dropZoneItr)
  2672. {
  2673. SCMStringBuffer directory, name;
  2674. IConstDropZoneInfo& dropZoneInfo = dropZoneItr->query();
  2675. dropZoneInfo.getDirectory(directory);
  2676. if (directory.length() && (strnicmp(path, directory.str(), directory.length()) == 0))
  2677. {
  2678. if (isEmptyString(dropZoneName))
  2679. return true;
  2680. dropZoneInfo.getName(name);
  2681. if (strieq(name.str(), dropZoneName))
  2682. return true;
  2683. }
  2684. }
  2685. return false;
  2686. }
  2687. void CFileSprayEx::addDropZoneFile(IEspContext& context, IDirectoryIterator* di, const char* name, const char pathSep, IArrayOf<IEspPhysicalFileStruct>& files)
  2688. {
  2689. Owned<IEspPhysicalFileStruct> aFile = createPhysicalFileStruct();
  2690. const char* pName = strrchr(name, pathSep);
  2691. if (!pName)
  2692. aFile->setName(name);
  2693. else
  2694. {
  2695. StringBuffer sPath;
  2696. sPath.append(pName - name, name);
  2697. aFile->setPath(sPath.str());
  2698. pName++; //skip the PathSepChar
  2699. aFile->setName(pName);
  2700. }
  2701. aFile->setIsDir(di->isDir());
  2702. CDateTime modtime;
  2703. StringBuffer timestr;
  2704. di->getModifiedTime(modtime);
  2705. unsigned y,m,d,h,min,sec,nsec;
  2706. modtime.getDate(y,m,d,true);
  2707. modtime.getTime(h,min,sec,nsec,true);
  2708. timestr.appendf("%04d-%02d-%02d %02d:%02d:%02d", y,m,d,h,min,sec);
  2709. aFile->setModifiedtime(timestr.str());
  2710. aFile->setFilesize(di->getFileSize());
  2711. files.append(*aFile.getLink());
  2712. }
  2713. void CFileSprayEx::searchDropZoneFiles(IEspContext& context, IpAddress& ip, const char* dir, const char* nameFilter, IArrayOf<IEspPhysicalFileStruct>& files, unsigned& filesFound)
  2714. {
  2715. RemoteFilename rfn;
  2716. SocketEndpoint ep;
  2717. ep.ipset(ip);
  2718. rfn.setPath(ep, dir);
  2719. Owned<IFile> f = createIFile(rfn);
  2720. if(f->isDirectory()!=fileBool::foundYes)
  2721. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", dir);
  2722. const char pathSep = getPathSepChar(dir);
  2723. Owned<IDirectoryIterator> di = f->directoryFiles(nameFilter, true, true);
  2724. ForEach(*di)
  2725. {
  2726. StringBuffer fname;
  2727. di->getName(fname);
  2728. if (!fname.length())
  2729. continue;
  2730. filesFound++;
  2731. if (filesFound > dropZoneFileSearchMaxFiles)
  2732. break;
  2733. addDropZoneFile(context, di, fname.str(), pathSep, files);
  2734. }
  2735. }
  2736. bool CFileSprayEx::onDropZoneFileSearch(IEspContext &context, IEspDropZoneFileSearchRequest &req, IEspDropZoneFileSearchResponse &resp)
  2737. {
  2738. try
  2739. {
  2740. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Access, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do FileList. Permission denied.");
  2741. const char* dropZoneName = req.getDropZoneName();
  2742. if (isEmptyString(dropZoneName))
  2743. throw MakeStringException(ECLWATCH_INVALID_INPUT, "DropZone not specified.");
  2744. const char* dropZoneServerReq = req.getServer(); //IP or hostname
  2745. if (isEmptyString(dropZoneServerReq))
  2746. throw MakeStringException(ECLWATCH_INVALID_INPUT, "DropZone server not specified.");
  2747. const char* nameFilter = req.getNameFilter();
  2748. if (isEmptyString(nameFilter))
  2749. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Name Filter not specified.");
  2750. bool validNameFilter = false;
  2751. const char* pNameFilter = nameFilter;
  2752. while (!isEmptyString(pNameFilter))
  2753. {
  2754. if (*pNameFilter != '*')
  2755. {
  2756. validNameFilter = true;
  2757. break;
  2758. }
  2759. pNameFilter++;
  2760. }
  2761. if (!validNameFilter)
  2762. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid Name Filter '*'");
  2763. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  2764. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  2765. Owned<IConstDropZoneInfo> dropZoneInfo = constEnv->getDropZone(dropZoneName);
  2766. if (!dropZoneInfo || (req.getECLWatchVisibleOnly() && !dropZoneInfo->isECLWatchVisible()))
  2767. throw MakeStringException(ECLWATCH_INVALID_INPUT, "DropZone %s not found.", dropZoneName);
  2768. SCMStringBuffer directory, computer;
  2769. dropZoneInfo->getDirectory(directory);
  2770. if (!directory.length())
  2771. throw MakeStringException(ECLWATCH_INVALID_INPUT, "DropZone Directory not found for %s.", dropZoneName);
  2772. IpAddress ipAddress;
  2773. ipAddress.ipset(dropZoneServerReq);
  2774. if (ipAddress.isNull())
  2775. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid server %s specified.", dropZoneServerReq);
  2776. double version = context.getClientVersion();
  2777. bool serverFound = false;
  2778. unsigned filesFound = 0;
  2779. IArrayOf<IEspPhysicalFileStruct> files;
  2780. Owned<IConstDropZoneServerInfoIterator> dropZoneServerItr = dropZoneInfo->getServers();
  2781. ForEach(*dropZoneServerItr)
  2782. {
  2783. StringBuffer server, networkAddress;
  2784. IConstDropZoneServerInfo& dropZoneServer = dropZoneServerItr->query();
  2785. dropZoneServer.getServer(server);
  2786. if (server.isEmpty())
  2787. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Invalid server for dropzone %s.", dropZoneName);
  2788. //Do string compare here because the server could be a pseudo-host name.
  2789. if (strieq(dropZoneServerReq, server))
  2790. {
  2791. serverFound = true;
  2792. searchDropZoneFiles(context, ipAddress, directory.str(), nameFilter, files, filesFound);
  2793. break;
  2794. }
  2795. }
  2796. if (!serverFound)
  2797. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Server %s not found in dropzone %s.", dropZoneServerReq, dropZoneName);
  2798. if ((version >= 1.16) && (filesFound > dropZoneFileSearchMaxFiles))
  2799. {
  2800. VStringBuffer msg("More than %u files are found. Only %u files are returned.", dropZoneFileSearchMaxFiles, dropZoneFileSearchMaxFiles);
  2801. resp.setWarning(msg.str());
  2802. }
  2803. resp.setFiles(files);
  2804. }
  2805. catch(IException* e)
  2806. {
  2807. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2808. }
  2809. return true;
  2810. }
  2811. bool CFileSprayEx::onDfuMonitor(IEspContext &context, IEspDfuMonitorRequest &req, IEspDfuMonitorResponse &resp)
  2812. {
  2813. try
  2814. {
  2815. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Failed to do DfuMonitor. Permission denied.");
  2816. Owned<IDFUWorkUnitFactory> factory = getDFUWorkUnitFactory();
  2817. Owned<IDFUWorkUnit> wu = factory->createWorkUnit();
  2818. wu->setQueue(m_MonitorQueueLabel.str());
  2819. StringBuffer user, passwd;
  2820. wu->setUser(context.getUserID(user).str());
  2821. wu->setPassword(context.getPassword(passwd).str());
  2822. wu->setCommand(DFUcmd_monitor);
  2823. IDFUmonitor *monitor = wu->queryUpdateMonitor();
  2824. IDFUfileSpec *source = wu->queryUpdateSource();
  2825. const char *eventname = req.getEventName();
  2826. const char *lname = req.getLogicalName();
  2827. if (lname&&*lname)
  2828. source->setLogicalName(lname);
  2829. else {
  2830. const char *ip = req.getIp();
  2831. const char *filename = req.getFilename();
  2832. if (filename&&*filename) {
  2833. RemoteFilename rfn;
  2834. if (ip&&*ip) {
  2835. SocketEndpoint ep(ip);
  2836. if (ep.isNull())
  2837. throw MakeStringException(ECLWATCH_INVALID_INPUT, "DfuMonitor: cannot resolve network IP from %s.", ip);
  2838. rfn.setPath(ep,filename);
  2839. }
  2840. else
  2841. rfn.setRemotePath(filename);
  2842. source->setSingleFilename(rfn);
  2843. }
  2844. else
  2845. throw MakeStringException(ECLWATCH_INVALID_INPUT, "Neither logical name nor network ip/file specified for monitor.");
  2846. }
  2847. if (eventname)
  2848. monitor->setEventName(eventname);
  2849. monitor->setShotLimit(req.getShotLimit());
  2850. monitor->setSub(req.getSub());
  2851. resp.setWuid(wu->queryId());
  2852. resp.setRedirectUrl(StringBuffer("/FileSpray/GetDFUWorkunit?wuid=").append(wu->queryId()).str());
  2853. submitDFUWorkUnit(wu.getClear());
  2854. }
  2855. catch(IException* e)
  2856. {
  2857. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2858. }
  2859. return true;
  2860. }
  2861. bool CFileSprayEx::onOpenSave(IEspContext &context, IEspOpenSaveRequest &req, IEspOpenSaveResponse &resp)
  2862. {
  2863. try
  2864. {
  2865. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  2866. const char* location = req.getLocation();
  2867. const char* path = req.getPath();
  2868. const char* name = req.getName();
  2869. const char* type = req.getType();
  2870. const char* dateTime = req.getDateTime();
  2871. if (location && *location)
  2872. resp.setLocation(location);
  2873. if (path && *path)
  2874. resp.setPath(path);
  2875. if (name && *name)
  2876. resp.setName(name);
  2877. if (type && *type)
  2878. resp.setType(type);
  2879. if (dateTime && *dateTime)
  2880. resp.setDateTime(dateTime);
  2881. if (req.getBinaryFile())
  2882. resp.setViewable(false);
  2883. }
  2884. catch(IException* e)
  2885. {
  2886. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  2887. }
  2888. return true;
  2889. }
  2890. bool CFileSprayEx::getDropZoneFiles(IEspContext &context, const char* dropZone, const char* netaddr, const char* path,
  2891. IEspDropZoneFilesRequest &req, IEspDropZoneFilesResponse &resp)
  2892. {
  2893. if (!checkDropZoneIPAndPath(context.getClientVersion(), dropZone, netaddr, path))
  2894. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Dropzone is not found in the environment settings.");
  2895. bool directoryOnly = req.getDirectoryOnly();
  2896. RemoteFilename rfn;
  2897. SocketEndpoint ep;
  2898. #ifdef MACHINE_IP
  2899. ep.set(MACHINE_IP);
  2900. #else
  2901. ep.set(netaddr);
  2902. if (ep.isNull())
  2903. throw MakeStringException(ECLWATCH_INVALID_INPUT, "CFileSprayEx::getDropZoneFiles: cannot resolve network IP from %s.", netaddr);
  2904. #endif
  2905. rfn.setPath(ep, path);
  2906. Owned<IFile> f = createIFile(rfn);
  2907. if(f->isDirectory()!=fileBool::foundYes)
  2908. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", path);
  2909. IArrayOf<IEspPhysicalFileStruct> files;
  2910. Owned<IDirectoryIterator> di = f->directoryFiles(NULL, false, true);
  2911. if(di.get() != NULL)
  2912. {
  2913. ForEach(*di)
  2914. {
  2915. StringBuffer fname;
  2916. di->getName(fname);
  2917. if (fname.length() == 0 || (directoryOnly && !di->isDir()))
  2918. continue;
  2919. Owned<IEspPhysicalFileStruct> onefile = createPhysicalFileStruct();
  2920. onefile->setName(fname.str());
  2921. onefile->setIsDir(di->isDir());
  2922. onefile->setFilesize(di->getFileSize());
  2923. CDateTime modtime;
  2924. StringBuffer timestr;
  2925. di->getModifiedTime(modtime);
  2926. unsigned y,m,d,h,min,sec,nsec;
  2927. modtime.getDate(y,m,d,true);
  2928. modtime.getTime(h,min,sec,nsec,true);
  2929. timestr.appendf("%04d-%02d-%02d %02d:%02d:%02d", y,m,d,h,min,sec);
  2930. onefile->setModifiedtime(timestr.str());
  2931. files.append(*onefile.getLink());
  2932. }
  2933. }
  2934. resp.setFiles(files);
  2935. return true;
  2936. }
  2937. //This method returns all dropzones and, if NetAddress and Path specified, returns filtered list of files.
  2938. bool CFileSprayEx::onDropZoneFiles(IEspContext &context, IEspDropZoneFilesRequest &req, IEspDropZoneFilesResponse &resp)
  2939. {
  2940. try
  2941. {
  2942. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  2943. const char* netAddress = req.getNetAddress();
  2944. if (!isEmptyString(netAddress))
  2945. {
  2946. IpAddress ipToCheck;
  2947. ipToCheck.ipset(netAddress);
  2948. if (ipToCheck.isNull())
  2949. throw makeStringExceptionV(ECLWATCH_INVALID_INPUT, "Invalid server %s specified.", netAddress);
  2950. }
  2951. bool filesFromALinux = false;
  2952. IArrayOf<IEspDropZone> dropZoneList;
  2953. bool ECLWatchVisibleOnly = req.getECLWatchVisibleOnly();
  2954. Owned<IEnvironmentFactory> envFactory = getEnvironmentFactory(true);
  2955. Owned<IConstEnvironment> constEnv = envFactory->openEnvironment();
  2956. Owned<IConstDropZoneInfoIterator> dropZoneItr = constEnv->getDropZoneIterator();
  2957. ForEach(*dropZoneItr)
  2958. {
  2959. IConstDropZoneInfo& dropZoneInfo = dropZoneItr->query();
  2960. if (ECLWatchVisibleOnly && !dropZoneInfo.isECLWatchVisible())
  2961. continue;
  2962. SCMStringBuffer dropZoneName, directory, computerName;
  2963. dropZoneInfo.getName(dropZoneName);
  2964. dropZoneInfo.getDirectory(directory);
  2965. dropZoneInfo.getComputerName(computerName); //legacy env
  2966. if (!dropZoneName.length() || !directory.length())
  2967. continue;
  2968. bool isLinux = getPathSepChar(directory.str()) == '/' ? true : false;
  2969. Owned<IConstDropZoneServerInfoIterator> dropZoneServerItr = dropZoneInfo.getServers();
  2970. ForEach(*dropZoneServerItr)
  2971. {
  2972. IConstDropZoneServerInfo& dropZoneServer = dropZoneServerItr->query();
  2973. StringBuffer name, server, networkAddress;
  2974. dropZoneServer.getName(name);
  2975. dropZoneServer.getServer(server);
  2976. if (name.isEmpty() || server.isEmpty())
  2977. continue;
  2978. Owned<IEspDropZone> aDropZone = createDropZone();
  2979. aDropZone->setName(dropZoneName.str());
  2980. aDropZone->setComputer(name.str());
  2981. aDropZone->setNetAddress(server);
  2982. aDropZone->setPath(directory.str());
  2983. if (isLinux)
  2984. aDropZone->setLinux("true");
  2985. if (!isEmptyString(netAddress) && strieq(netAddress, server))
  2986. filesFromALinux = isLinux;
  2987. dropZoneList.append(*aDropZone.getClear());
  2988. }
  2989. }
  2990. if (dropZoneList.ordinality())
  2991. resp.setDropZones(dropZoneList);
  2992. const char* dzName = req.getDropZoneName();
  2993. const char* directory = req.getPath();
  2994. const char* subfolder = req.getSubfolder();
  2995. if (isEmptyString(netAddress) || (isEmptyString(directory) && isEmptyString(subfolder)))
  2996. return true;
  2997. StringBuffer netAddressStr, directoryStr, osStr;
  2998. netAddressStr.set(netAddress);
  2999. if (!isEmptyString(directory))
  3000. directoryStr.set(directory);
  3001. if (!isEmptyString(subfolder))
  3002. {
  3003. if (directoryStr.length())
  3004. addPathSepChar(directoryStr);
  3005. directoryStr.append(subfolder);
  3006. }
  3007. addPathSepChar(directoryStr);
  3008. getDropZoneFiles(context, dzName, netAddress, directoryStr.str(), req, resp);
  3009. resp.setDropZoneName(dzName);
  3010. resp.setNetAddress(netAddress);
  3011. resp.setPath(directoryStr.str());
  3012. resp.setOS(filesFromALinux);
  3013. resp.setECLWatchVisibleOnly(ECLWatchVisibleOnly);
  3014. }
  3015. catch(IException* e)
  3016. {
  3017. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3018. }
  3019. return true;
  3020. }
  3021. bool CFileSprayEx::onDeleteDropZoneFiles(IEspContext &context, IEspDeleteDropZoneFilesRequest &req, IEspDFUWorkunitsActionResponse &resp)
  3022. {
  3023. try
  3024. {
  3025. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Full, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  3026. double version = context.getClientVersion();
  3027. const char* dzName = req.getDropZoneName();
  3028. const char* netAddress = req.getNetAddress();
  3029. const char* directory = req.getPath();
  3030. const char* osStr = req.getOS();
  3031. StringArray & files = req.getNames();
  3032. if (!files.ordinality())
  3033. throw MakeStringException(ECLWATCH_INVALID_INPUT, "File not specified.");
  3034. StringBuffer path(directory);
  3035. if (!isEmptyString(osStr))
  3036. {
  3037. char pathSep = (atoi(osStr) == OS_WINDOWS) ? '\\' : '/';
  3038. path.replace(pathSep=='\\' ? '/' : '\\', pathSep);
  3039. }
  3040. addPathSepChar(path, getPathSepChar(path.str()));
  3041. if (!checkDropZoneIPAndPath(version, dzName, netAddress, path.str()))
  3042. throw MakeStringException(ECLWATCH_DROP_ZONE_NOT_FOUND, "Dropzone is not found in the environment settings.");
  3043. RemoteFilename rfn;
  3044. SocketEndpoint ep(netAddress);
  3045. if (ep.isNull())
  3046. throw MakeStringException(ECLWATCH_INVALID_INPUT, "DeleteDropZoneFiles: cannot resolve network IP from %s.", netAddress);
  3047. rfn.setPath(ep, path.str());
  3048. Owned<IFile> f = createIFile(rfn);
  3049. if(f->isDirectory()!=fileBool::foundYes)
  3050. throw MakeStringException(ECLWATCH_INVALID_DIRECTORY, "%s is not a directory.", directory);
  3051. bool bAllSuccess = true;
  3052. IArrayOf<IEspDFUActionResult> results;
  3053. for(unsigned i = 0; i < files.ordinality(); ++i)
  3054. {
  3055. const char* file = files.item(i);
  3056. if (!file || !*file)
  3057. continue;
  3058. PROGLOG("DeleteDropZoneFiles: netAddress %s, path %s, file %s", netAddress, directory, file);
  3059. Owned<IEspDFUActionResult> res = createDFUActionResult("", "");
  3060. res->setID(files.item(i));
  3061. res->setAction("Delete");
  3062. res->setResult("Success");
  3063. try
  3064. {
  3065. StringBuffer fileToDelete(path);
  3066. fileToDelete.append(file);
  3067. rfn.setPath(ep, fileToDelete.str());
  3068. Owned<IFile> rFile = createIFile(rfn);
  3069. if (!rFile->exists())
  3070. res->setResult("Warning: this file does not exist.");
  3071. else
  3072. rFile->remove();
  3073. }
  3074. catch (IException *e)
  3075. {
  3076. bAllSuccess = false;
  3077. StringBuffer eMsg;
  3078. eMsg = e->errorMessage(eMsg);
  3079. e->Release();
  3080. StringBuffer failedMsg("Failed: ");
  3081. failedMsg.append(eMsg);
  3082. res->setResult(failedMsg.str());
  3083. }
  3084. results.append(*res.getLink());
  3085. }
  3086. resp.setFirstColumn("File");
  3087. resp.setDFUActionResults(results);
  3088. }
  3089. catch(IException* e)
  3090. {
  3091. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3092. }
  3093. return true;
  3094. }
  3095. void CFileSprayEx::appendGroupNode(IArrayOf<IEspGroupNode>& groupNodes, const char* nodeName, const char* clusterType,
  3096. bool replicateOutputs)
  3097. {
  3098. Owned<IEspGroupNode> node = createGroupNode();
  3099. node->setName(nodeName);
  3100. node->setClusterType(clusterType);
  3101. if (replicateOutputs)
  3102. node->setReplicateOutputs(replicateOutputs);
  3103. groupNodes.append(*node.getClear());
  3104. }
  3105. bool CFileSprayEx::onGetSprayTargets(IEspContext &context, IEspGetSprayTargetsRequest &req, IEspGetSprayTargetsResponse &resp)
  3106. {
  3107. try
  3108. {
  3109. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  3110. #ifdef _CONTAINERIZED
  3111. IArrayOf<IEspGroupNode> sprayTargets;
  3112. Owned<IPropertyTreeIterator> dataPlanes = getGlobalConfigSP()->getElements("storage/planes[labels='data']");
  3113. ForEach(*dataPlanes)
  3114. {
  3115. IPropertyTree & plane = dataPlanes->query();
  3116. const char * name = plane.queryProp("@name");
  3117. appendGroupNode(sprayTargets, name, "storage plane", false /*replicate outputs*/);
  3118. }
  3119. resp.setGroupNodes(sprayTargets);
  3120. #else
  3121. Owned<IEnvironmentFactory> factory = getEnvironmentFactory(true);
  3122. Owned<IConstEnvironment> environment = factory->openEnvironment();
  3123. Owned<IPropertyTree> root = &environment->getPTree();
  3124. IArrayOf<IEspGroupNode> sprayTargets;
  3125. //Fetch all the group names for all the thor instances (and dedup them)
  3126. BoolHash uniqueThorClusterGroupNames;
  3127. Owned<IPropertyTreeIterator> it = root->getElements("Software/ThorCluster");
  3128. ForEach(*it)
  3129. {
  3130. IPropertyTree& cluster = it->query();
  3131. StringBuffer thorClusterGroupName;
  3132. getClusterGroupName(cluster, thorClusterGroupName);
  3133. if (!thorClusterGroupName.length())
  3134. continue;
  3135. bool* found = uniqueThorClusterGroupNames.getValue(thorClusterGroupName.str());
  3136. if (!found || !*found)
  3137. appendGroupNode(sprayTargets, thorClusterGroupName.str(), "thor", cluster.getPropBool("@replicateOutputs", false));
  3138. }
  3139. //Fetch all the group names for all the hthor instances
  3140. it.setown(root->getElements("Software/EclAgentProcess"));
  3141. ForEach(*it)
  3142. {
  3143. IPropertyTree &cluster = it->query();
  3144. const char* name = cluster.queryProp("@name");
  3145. if (!name || !*name)
  3146. continue;
  3147. unsigned ins = 0;
  3148. Owned<IPropertyTreeIterator> insts = cluster.getElements("Instance");
  3149. ForEach(*insts)
  3150. {
  3151. const char *na = insts->query().queryProp("@netAddress");
  3152. if (!na || !*na)
  3153. continue;
  3154. SocketEndpoint ep(na);
  3155. if (ep.isNull())
  3156. continue;
  3157. ins++;
  3158. VStringBuffer gname("hthor__%s", name);
  3159. if (ins>1)
  3160. gname.append('_').append(ins);
  3161. appendGroupNode(sprayTargets, gname.str(), "hthor", false);
  3162. }
  3163. }
  3164. resp.setGroupNodes(sprayTargets);
  3165. #endif
  3166. }
  3167. catch(IException* e)
  3168. {
  3169. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3170. }
  3171. return true;
  3172. }
  3173. void CFileSprayEx::setDFUServerQueueReq(const char* dfuServerQueue, IDFUWorkUnit* wu)
  3174. {
  3175. wu->setQueue((dfuServerQueue && *dfuServerQueue) ? dfuServerQueue : m_QueueLabel.str());
  3176. }
  3177. void CFileSprayEx::setUserAuth(IEspContext &context, IDFUWorkUnit* wu)
  3178. {
  3179. StringBuffer user, passwd;
  3180. wu->setUser(context.getUserID(user).str());
  3181. wu->setPassword(context.getPassword(passwd).str());
  3182. }
  3183. bool CFileSprayEx::onGetDFUServerQueues(IEspContext &context, IEspGetDFUServerQueuesRequest &req, IEspGetDFUServerQueuesResponse &resp)
  3184. {
  3185. try
  3186. {
  3187. context.ensureFeatureAccess(FILE_SPRAY_URL, SecAccess_Read, ECLWATCH_FILE_SPRAY_ACCESS_DENIED, "Permission denied.");
  3188. StringArray qlist;
  3189. getDFUServerQueueNames(qlist, req.getDFUServerName());
  3190. resp.setNames(qlist);
  3191. }
  3192. catch(IException* e)
  3193. {
  3194. FORWARDEXCEPTION(context, e, ECLWATCH_INTERNAL_ERROR);
  3195. }
  3196. return true;
  3197. }