Salome HOME
[EDF29576] : Move to python implementation of driver and integrate monitoring into...
[modules/kernel.git] / src / Container / SALOME_ContainerManager.hxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef __SALOME_CONTAINERMANAGER_HXX__
24 #define __SALOME_CONTAINERMANAGER_HXX__
25
26 #include "SALOME_Container.hxx"
27
28 #include <SALOMEconfig.h>
29 #include CORBA_CLIENT_HEADER(SALOME_Component)
30 #include CORBA_CLIENT_HEADER(SALOME_ContainerManager)
31 #include "SALOME_ResourcesCatalog_Parser.hxx"
32
33 #include "Utils_Mutex.hxx"
34
35 #include <vector>
36 #include <string>
37 #include <utility>
38 #include <functional>
39 #include <set>
40
41 class SALOME_NamingService_Abstract;
42 class SALOME_ResourcesManager_Client;
43
44 class CONTAINER_EXPORT SALOME_ContainerManager : public POA_Engines::ContainerManager
45 {
46
47 public:
48   SALOME_ContainerManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService_Abstract *ns);
49   ~SALOME_ContainerManager();
50
51   // Corba Methods
52   Engines::Container_ptr GiveContainer(const Engines::ContainerParameters& params) override;
53
54   void ShutdownContainers() override;
55
56   void ShutdownContainersNow() override;
57
58   void SetOverrideEnvForContainers(const Engines::KeyValDict& env) override;
59
60   Engines::KeyValDict *GetOverrideEnvForContainers() override;
61
62   void SetCodeOnContainerStartUp(const char *code) override;
63
64   // C++ Methods
65   void Shutdown();
66
67   void DeclareUsingSalomeSession() { _isSSL = false; }
68
69   CORBA::Long GetTimeOutToLaunchServerInSecond() override;
70
71   void SetTimeOutToLaunchServerInSecond(CORBA::Long timeInSecond) override;
72
73   CORBA::Long GetDeltaTimeBetweenNSLookupAtLaunchTimeInMilliSecond() override;
74
75   void SetDeltaTimeBetweenNSLookupAtLaunchTimeInMilliSecond(CORBA::Long timeInMS) override;
76
77   CORBA::Long GetDeltaTimeBetweenCPUMemMeasureInMilliSecond() override;
78
79   void SetDeltaTimeBetweenCPUMemMeasureInMilliSecond(CORBA::Long timeInMS) override;
80
81   static const char *_ContainerManagerNameInNS;
82
83 private:
84
85   void ShutdownContainersGeneric(std::function<void(Engines::Container_ptr)> funcToBeCalledOnContainer);
86
87 protected:
88   // C++ methods
89   Engines::Container_ptr
90   FindContainer(const Engines::ContainerParameters& params,
91                 const Engines::ResourceList& possibleResources);
92
93   Engines::Container_ptr
94   FindContainer(const Engines::ContainerParameters& params,
95                 const std::string& resource);
96
97   std::string GetCppBinaryOfKernelContainer() const;
98   
99   std::string GetRunRemoteExecutableScript() const;
100
101   std::string BuildCommandToLaunchRemoteContainer(const std::string & resource_name,
102                                                   const Engines::ContainerParameters& params,
103                                                   const std::string& container_exe="SALOME_Container") const;
104
105   std::string BuildCommandToLaunchLocalContainer(const Engines::ContainerParameters& params,
106                                                  const std::string& machinesFile,
107                                                  const std::string& container_exe,//"SALOME_Container"
108                                                  std::string& tmpFileName) const;
109
110   std::string BuildTempFileToLaunchRemoteContainer(const std::string& resource_name,
111                                                    const Engines::ContainerParameters& params, std::string& tmpFileName) const;
112
113   static void RmTmpFile(std::string& tmpFile);
114
115   void AddOmninamesParams(std::string& command) const;
116
117   void AddOmninamesParams(std::ostream& fileStream) const;
118
119   static std::string BuildTemporaryFileName();
120
121   std::string GetMPIZeroNode(const std::string machine, const std::string machinesFile) const;
122
123   std::string machinesFile(const int nbproc);
124   
125   std::string getCommandToRunRemoteProcessCommon(const std::string& templateName, const std::string& remoteScript, 
126                                           AccessProtocolType protocol, 
127                                           const std::string & hostname, 
128                                           const std::string & username,
129                                           const std::string & applipath,
130                                           const std::string & workdir) const;
131
132   std::string getCommandToRunRemoteProcess(AccessProtocolType protocol, 
133                                           const std::string & hostname, 
134                                           const std::string & username,
135                                           const std::string & applipath,
136                                           const std::string & workdir="") const;
137                                           
138   std::string getCommandToRunRemoteProcessNoAppli(AccessProtocolType protocol, 
139                                           const std::string & hostname, 
140                                           const std::string & username,
141                                           const std::string & applipath,
142                                           const std::string & workdir="") const;
143
144   Engines::Container_ptr
145   LaunchContainer(const Engines::ContainerParameters& params,
146                   const std::string & resource_selected,
147                   const std::string & hostname,
148                   const std::string & machFile,
149                   const std::string & containerNameInNS);
150
151   CORBA::ORB_var _orb;
152   PortableServer::POA_var _poa;
153
154   SALOME_ResourcesManager_Client *_resManager;
155   SALOME_NamingService_Abstract *_NS;
156
157   //! different behaviour if $APPLI exists (SALOME Application)
158   bool _isAppliSalomeDefined;
159
160   //! attribute that contains the number of processes used in batch mode by MPI containers
161   int _nbprocUsed;
162
163   //! attribute that specifies the launch mode.
164   bool _isSSL = true;
165
166   static omni_mutex _numInstanceMutex ; // lib and instance protection
167
168   //! attributes to allow concurrency for // GiveContainer
169   Utils_Mutex _giveContainerMutex1;
170
171   pid_t _pid_mpiServer;
172
173   // Begin of PacO++ Parallel extension
174   typedef std::vector<std::string> actual_launch_machine_t;
175
176   bool checkPaCOParameters(Engines::ContainerParameters & params, std::string resource_selected);
177
178   Engines::Container_ptr
179   StartPaCOPPContainer(const Engines::ContainerParameters& params, std::string resource_selected);
180
181
182
183   std::string BuildCommandToLaunchPaCOProxyContainer(const Engines::ContainerParameters& params,
184                                                      std::string machine_file_name,
185                                                      std::string & proxy_hostname);
186
187   std::string BuildCommandToLaunchPaCONodeContainer(const Engines::ContainerParameters& params,
188                                                     const std::string & machine_file_name,
189                                                     SALOME_ContainerManager::actual_launch_machine_t & vect_machine,
190                                                     const std::string & proxy_hostname);
191
192   void LogConfiguration(const std::string & log_type,
193                         const std::string & exe_type,
194                         const std::string & container_name,
195                         const std::string & hostname,
196                         std::string & begin,
197                         std::string & end);
198
199   CORBA::Object_ptr
200   LaunchPaCOProxyContainer(const std::string& command,
201                            const Engines::ContainerParameters& params,
202                            const std::string& hostname);
203
204   bool
205   LaunchPaCONodeContainer(const std::string& command,
206                           const Engines::ContainerParameters& params,
207                           const std::string& name,
208                           SALOME_ContainerManager::actual_launch_machine_t & vect_machine);
209   // End of PaCO++ Parallel extension
210 public:
211   static char *GetenvThreadSafe(const char *name);
212   static std::string GetenvThreadSafeAsString(const char *name);
213   static int SystemThreadSafe(const char *command);
214   static long SystemWithPIDThreadSafe(const std::vector<std::string>& command);
215   static void AddOmninamesParams(std::ostream& fileStream, SALOME_NamingService_Abstract *ns);
216   static void MakeTheCommandToBeLaunchedASync(std::string& command);
217   static int GetTimeOutToLoaunchServer();
218   static void SleepInSecond(int ellapseTimeInSecond);
219  private:
220   static const int TIME_OUT_TO_LAUNCH_CONT;
221   static const int DFT_DELTA_TIME_NS_LOOKUP_IN_MS;
222   static Utils_Mutex _getenvMutex;
223   static Utils_Mutex _systemMutex;
224 private:
225   std::vector< std::pair<std::string, std::string> > _override_env;
226   int _time_out_in_second;
227   int _delta_time_ns_lookup_in_ms;
228   int _delta_time_measure_in_ms;
229   std::string _code_to_exe_on_startup;
230 };
231 #endif