DeploymentEngine.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #ifndef DEPLOYMENTENGINE_HPP_INCL
  14. #define DEPLOYMENTENGINE_HPP_INCL
  15. #include "jiface.hpp"
  16. #include "jptree.hpp"
  17. #include "jstring.hpp"
  18. #include "deploy.hpp"
  19. #include "environment.hpp"
  20. #include "xslprocessor.hpp"
  21. #include <map>
  22. #include <set>
  23. #include <string>
  24. #include <vector>
  25. #if defined(_DEBUG) && defined(_WIN32) && !defined(USING_MPATROL)
  26. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  27. #endif
  28. #define SEISINT_NAMESPACE "http://seisint.com"
  29. interface IPropertyTree;
  30. interface IXslProcessor;
  31. interface IXslTransform;
  32. using namespace std;
  33. struct CInstallFile : public CInterface
  34. {
  35. CInstallFile(const char* method, const char* srcPath, const char* destPath, bool bCacheable=false)
  36. : m_method(method), m_srcPath(srcPath), m_destPath(destPath), m_bCacheable(bCacheable),
  37. m_szSrc(0), m_crcSrc(0), m_sizeInitialized(false), m_crcInitialized(false)
  38. {
  39. }
  40. virtual ~CInstallFile()
  41. {
  42. }
  43. IMPLEMENT_IINTERFACE;
  44. const string& getMethod () const { return m_method; }
  45. const string& getSrcPath () const { return m_srcPath; }
  46. const string& getDestPath() const { return m_destPath;}
  47. const string& getParams() const { return m_params; }//only supported for dynamically added files
  48. // This function could throw exception
  49. offset_t getSrcSize()
  50. {
  51. if (!m_sizeInitialized)
  52. {
  53. m_szSrc = filesize(m_srcPath.c_str());
  54. m_sizeInitialized = true;
  55. }
  56. return m_szSrc;
  57. }
  58. // This function could throw exception
  59. unsigned getSrcCRC()
  60. {
  61. if (!m_crcInitialized)
  62. {
  63. m_crcSrc = getFileCRC(m_srcPath.c_str());
  64. m_crcInitialized = true;
  65. }
  66. return m_crcSrc;
  67. }
  68. bool isCacheable() const { return m_bCacheable;}
  69. void setParams(const char* params){ m_params = params;}//only supported for dynamically added files
  70. void setMethod(const char* method){ m_method = method;}
  71. void setSrcPath(const char* path) { m_srcPath = path; }
  72. //void setSrcSize(offset_t sz) { m_szSrc = sz; }
  73. //void setSrcCRC(unsigned crc) { m_crcSrc = crc; }
  74. void setCacheable(bool val=true) { m_bCacheable = val; }
  75. bool isDuplicateSrcFile(const char* filename) const
  76. {
  77. return m_duplicateSrcFiles.find(filename) != m_duplicateSrcFiles.end();
  78. }
  79. void addDuplicateSrcFile(const char* filename)
  80. {
  81. m_duplicateSrcFiles.insert(filename);
  82. }
  83. private:
  84. string m_method;
  85. string m_srcPath;
  86. string m_destPath;
  87. string m_params; //only supported for dynamically added files
  88. offset_t m_szSrc;
  89. unsigned m_crcSrc;
  90. bool m_sizeInitialized;
  91. bool m_crcInitialized;
  92. bool m_bCacheable;
  93. set<string> m_duplicateSrcFiles;
  94. };
  95. //define a case insensitive comparator class for std::string to be used for
  96. //implementing a case insensitive multimap below
  97. struct iless_string : std::binary_function<string, string, bool> {
  98. bool operator()(const string& _X, const string& _Y) const
  99. {return stricmp(_X.c_str(), _Y.c_str()) < 0; }
  100. };
  101. typedef Linked<CInstallFile> LinkedFilePtr;
  102. class CInstallFileMap : public std::multimap<std::string, LinkedFilePtr, iless_string>
  103. {
  104. public:
  105. CInstallFileMap()
  106. : m_pDepEngine(NULL)
  107. {
  108. }
  109. virtual ~CInstallFileMap()
  110. {
  111. }
  112. void setDeploymentEngine(IDeploymentEngine& depEngine)
  113. {
  114. m_pDepEngine = &depEngine;
  115. }
  116. bool resolveConflicts(IPropertyTree& processNode, const char* method, const char* srcPath, const char* destPath,
  117. const char* compName, const char* instanceName, const char* params);
  118. private:
  119. CInstallFileMap(const CInstallFileMap&);
  120. IDeploymentEngine* m_pDepEngine;
  121. };
  122. typedef vector<Linked<CInstallFile> > CInstallFileList;
  123. struct CInstallFiles
  124. {
  125. private:
  126. IDeploymentEngine* m_pDepEngine;
  127. CInstallFileList m_list;
  128. CInstallFileMap m_map; // multimap of destFilePath -> std::pair<method, sourceFilePath>
  129. CInstallFiles(const CInstallFiles&);//disallow compiler from generating copy constructor
  130. public:
  131. CInstallFiles()
  132. {
  133. m_pDepEngine = NULL;
  134. }
  135. virtual ~CInstallFiles()
  136. {
  137. }
  138. void setDeploymentEngine(IDeploymentEngine& depEngine)
  139. {
  140. m_pDepEngine = &depEngine;
  141. m_map.setDeploymentEngine(depEngine);
  142. }
  143. CInstallFile* addInstallFile(const char* method, const char* srcPath, const char* destPath, bool bCacheable, const char* params)
  144. {
  145. LinkedFilePtr pFile = new CInstallFile(method, srcPath, destPath, bCacheable);
  146. if (params)
  147. pFile->setParams(params);
  148. m_map.insert(CInstallFileMap::value_type(destPath, pFile));
  149. m_list.push_back(pFile);
  150. pFile->Release();
  151. return pFile;
  152. }
  153. void clear()
  154. {
  155. m_list.clear();
  156. m_map.clear();
  157. }
  158. CInstallFile* findInstallFile(const char* destPath, string& method, string& srcPath)
  159. {
  160. CInstallFileMap::const_iterator it = m_map.find(destPath);
  161. return it != m_map.end() ? (*it).second : NULL;
  162. }
  163. bool resolveConflicts(IPropertyTree& processNode, const char* method, const char* srcPath, const char* destPath,
  164. const char* compName, const char* instanceName, const char* params)
  165. {
  166. return m_map.resolveConflicts(processNode, method, srcPath, destPath, compName, instanceName, params);
  167. }
  168. const CInstallFileList& getInstallFileList() const { return m_list; }
  169. };
  170. //---------------------------------------------------------------------------
  171. // CDeploymentEngine
  172. //---------------------------------------------------------------------------
  173. class CDeploymentEngine : public CInterface,
  174. implements IDeploymentEngine,
  175. implements IExceptionHandler
  176. {
  177. public:
  178. IMPLEMENT_IINTERFACE;
  179. CDeploymentEngine(IEnvDeploymentEngine& envDepEngine,
  180. IDeploymentCallback& callback, IPropertyTree& process,
  181. const char* instanceType=NULL, bool createIni=false);
  182. virtual ~CDeploymentEngine();
  183. public: // IDeploymentEngine implementation
  184. virtual void setXsl(IXslProcessor* processor, IXslTransform* transform);
  185. virtual void start();
  186. virtual void stop();
  187. virtual void check();
  188. virtual void compare(unsigned flags);
  189. virtual void deploy(unsigned flags, bool useTempDir);
  190. virtual void renameDirs();
  191. virtual void backupDirs();
  192. virtual void abort();
  193. virtual void resetInstances() { m_instances.kill(); }
  194. virtual void addInstance(const char* tagName, const char* name);
  195. virtual const IArrayOf<IPropertyTree>& getInstances() const { return m_instances; }
  196. static bool stripTrailingDirsFromUNCPath(const char* uncPath, StringBuffer& netPath);
  197. virtual int getInstallFileCount();
  198. virtual offset_t getInstallFileSize();
  199. protected:
  200. void checkAbort(IDeployTask* task=NULL) const;
  201. void checkBuild() const;
  202. virtual void checkInstance(IPropertyTree& node) const;
  203. virtual void deployInstance(IPropertyTree& node, bool useTempDir);
  204. virtual void stopInstance(IPropertyTree& node, const char* fileName="stop");
  205. virtual void startInstance(IPropertyTree& node, const char* fileName="startup");
  206. virtual void _deploy(bool useTempDir);
  207. virtual void xslTransform(const char *xsl, const char *outputFile, const char *instanceName,
  208. EnvMachineOS os=MachineOsUnknown, const char* processName=NULL,bool isEspModuleOrPlugin=false);
  209. virtual void processCustomMethod(const char *method, const char *source, const char *outputFile,
  210. const char *instanceName, EnvMachineOS os);
  211. #ifdef _USE_OPENSSL
  212. virtual void siteCertificate(IPropertyTree& process, const char *instanceName, const char *outputFile);
  213. #endif
  214. StringBuffer &getHostRoot(StringBuffer &hostRoot, const char* computer, const char* dir, bool bIgnoreDepToFolder=false) const;
  215. StringBuffer &getHostDir(StringBuffer &hostDir, IPropertyTree& node, bool bIgnoreDepToFolder=false);
  216. StringBuffer &getDeployDir(StringBuffer &deployDir, IPropertyTree& node);
  217. StringBuffer &getLocalDir(StringBuffer &localDir, IPropertyTree& node) const;
  218. const char *queryDirectory(IPropertyTree& node, StringBuffer& dir) const;
  219. void connectToHost(IPropertyTree& node, const char* dir=NULL);
  220. void disconnectHost(const char* uncPath);
  221. void connectToNetworkPath(const char* uncPath, const char* user, const char* pswd);
  222. void ensurePath(const char* filespec) const;
  223. void renameDir(const char* from, const char* to, EnvMachineOS os);
  224. void backupDir(const char* from);
  225. void writeFile(const char* filename, const char* str, EnvMachineOS os=MachineOsUnknown);
  226. void deleteFile(const char* target, const char* instanceName, EnvMachineOS os);
  227. virtual int determineInstallFiles(IPropertyTree& node, CInstallFiles& installFiles) const;
  228. virtual void copyInstallFiles(IPropertyTree& instanceNode, const char* destPath);
  229. virtual void beforeDeploy();
  230. virtual void afterDeploy() {}
  231. virtual void beforeDeployInstance(IPropertyTree& instanceNode, const char* destPath);
  232. virtual void afterDeployInstance(IPropertyTree& instanceNode, const char* destPath) {}
  233. bool checkFileExists(const char* filename) const;
  234. IPropertyTree *lookupProcess(const char* type, const char *name) const;
  235. IPropertyTree* lookupTable(IPropertyTree* modelTree, const char* table) const;
  236. StringBuffer& getEndPoints(const char* path, const char* delimiter, StringBuffer &endPoints) const;
  237. StringBuffer& getDaliServers(StringBuffer &daliServers) const;
  238. StringBuffer& getHMonIP(StringBuffer &hmon) const;
  239. void copyAttributes(IPropertyTree *dst, IPropertyTree *src, const char** begin, const char** end);
  240. void copyUnknownAttributes(IPropertyTree *dst, IPropertyTree *src, const char** begin, const char** end);
  241. void compareFiles(EnvMachineOS os);
  242. virtual const char* setCompare(const char *filename);
  243. const CInstallFiles& getInstallFiles() const { return m_installFiles; }
  244. virtual bool processInstallFile(IPropertyTree& processNode, const char* instanceName, const char* method,
  245. const char* source, const char* dest,
  246. EnvMachineOS os, bool bCacheable,
  247. const char* params=NULL);
  248. static void addDeploymentFile(StringBuffer &ret, const char *in, IXslTransform*);
  249. #ifdef _USE_OPENSSL
  250. static void siteCertificateFunction(StringBuffer &ret, const char *in, IXslTransform*);
  251. #endif
  252. virtual IEnvDeploymentEngine& getEnvDepEngine() const { return m_envDepEngine; }
  253. virtual IDeploymentCallback& getCallback() const { return *m_pCallback; }
  254. void copyInstallFiles(const char* instanceName, int instanceIndex, const char* destPath, EnvMachineOS os);
  255. //override for IExceptionHandler
  256. bool fireException(IException *e);
  257. private:
  258. virtual void compareFiles(const char *newFile, const char *oldFile, EnvMachineOS os);
  259. void createIniFile(const char* destPath, EnvMachineOS os);
  260. IPropertyTree* queryBuildSetNode(IPropertyTree& processNode, IPropertyTree*& buildNode) const;
  261. IPropertyTree* getDeployMapNode(IPropertyTree* buildNode, IPropertyTree* buildSetNode) const;
  262. bool searchDeployMap(const char* fileName, const char* optionalFileExt) const;
  263. void getBackupDirName(const char* from, StringBuffer& to);
  264. bool checkSSHFileExists(const char* dir) const;
  265. void setSSHVars(IPropertyTree& instance);
  266. void clearSSHVars();
  267. protected:
  268. Owned<IThreadPool> m_threadPool;
  269. Owned<IDeploymentCallback> m_pCallback;
  270. IEnvDeploymentEngine& m_envDepEngine;
  271. Owned<IXslFunction> m_externalFunction;
  272. Owned<IXslFunction> m_externalFunction2;
  273. IConstEnvironment& m_environment;
  274. Owned<IPropertyTree> m_rootNode;
  275. IPropertyTree& m_process;
  276. IArrayOf<IPropertyTree> m_instances;
  277. set<string> m_connections;
  278. IArrayOf<IDeployTask> m_renameDirList;
  279. CInstallFiles m_installFiles;
  280. static CInstallFileList s_dynamicFileList;//additional files like plugin files determined at deployment time
  281. IXslProcessor* m_processor;
  282. IXslTransform* m_transform;
  283. const char* m_curInstance;
  284. CriticalSection m_critSec;
  285. static CDeploymentEngine* s_xsltDepEngine;//deployment engine context for XSLT
  286. static bool s_bCacheableDynFile;
  287. enum ThreeState {no, yes, unknown};
  288. mutable ThreeState m_startable;
  289. mutable ThreeState m_stoppable;
  290. StringAttr m_name;
  291. StringAttr m_instanceType;
  292. StringAttr m_compareOld;
  293. StringAttr m_compareNew;
  294. StringAttr m_iniFile;
  295. StringAttr m_cachePath;
  296. StringAttr m_curSSHKeyFile;
  297. StringAttr m_curSSHUser;
  298. StringAttr m_curSSHKeyPassphrase;
  299. unsigned m_deployFlags;
  300. bool m_compare;
  301. bool m_createIni;
  302. bool m_abort;
  303. bool m_useSSHIfDefined;
  304. bool m_instanceCheck;
  305. };
  306. //---------------------------------------------------------------------------
  307. #endif // DEPLOYMENTENGINE_HPP_INCL