Salome HOME
Merging from V4_1_0_maintainance for porting on Win32 Platform
[modules/kernel.git] / src / Batch / Batch_BatchManager_eClient.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21 * BatchManager_eLSF.cxx : emulation of LSF client
22 *
23 * Auteur : Bernard SECHER - CEA DEN
24 * Mail   : mailto:bernard.secher@cea.fr
25 * Date   : Thu Apr 24 10:17:22 2008
26 * Projet : PAL Salome 
27 *
28 */
29
30 #include "Batch_BatchManager_eClient.hxx"
31 #include "Basics_DirUtils.hxx"
32
33 #include <iostream>
34 #include <fstream>
35 #include <sstream>
36 #include <sys/stat.h>
37
38 using namespace std;
39
40
41 namespace Batch {
42
43   BatchManager_eClient::BatchManager_eClient(const Batch::FactBatchManager * parent, const char* host, const char* protocol, const char* mpiImpl) : BatchManager(parent, host), _protocol(protocol), _username("")
44   {
45     // instanciation of mpi implementation needed to launch executable in batch script
46     _mpiImpl = FactoryMpiImpl(mpiImpl);
47   }
48
49   // Destructeur
50   BatchManager_eClient::~BatchManager_eClient()
51   {
52     // Nothing to do
53     delete _mpiImpl;
54   }
55
56   void BatchManager_eClient::exportInputFiles(const Job& job) throw(EmulationException)
57   {
58     int status;
59     Parametre params = job.getParametre();
60     Versatile V = params[INFILE];
61     Versatile::iterator Vit;
62     string command;
63     string copy_command;
64     _username = string(params[USER]);
65
66     // Test protocol
67     if( _protocol == "rsh" )
68       copy_command = "rcp ";
69     else if( _protocol == "ssh" )
70       copy_command = "scp ";
71     else
72       throw EmulationException("Unknown protocol : only rsh and ssh are known !");
73
74     // First step : creating batch tmp files directory
75     command = _protocol;
76     command += " ";
77     if(_username != ""){
78       command += _username;
79       command += "@";
80     }
81     command += _hostname;
82     command += " \"mkdir -p ";
83     command += string(params[TMPDIR]);
84     command += "\"" ;
85     cerr << command.c_str() << endl;
86     status = system(command.c_str());
87     if(status) {
88       std::ostringstream oss;
89       oss << status;
90       std::string ex_mess("Error of connection on remote host ! status = ");
91       ex_mess += oss.str();
92       throw EmulationException(ex_mess.c_str());
93     }
94
95     // Second step : copy fileToExecute into
96     // batch tmp files directory
97     command = copy_command;
98     command += string(params[EXECUTABLE]);
99     command += " ";
100     if(_username != ""){
101       command += _username;
102       command += "@";
103     }
104     command += _hostname;
105     command += ":";
106     command += string(params[TMPDIR]);
107     cerr << command.c_str() << endl;
108     status = system(command.c_str());
109     if(status) {
110       std::ostringstream oss;
111       oss << status;
112       std::string ex_mess("Error of connection on remote host ! status = ");
113       ex_mess += oss.str();
114       throw EmulationException(ex_mess.c_str());
115     }
116
117     // Third step : copy filesToExportList into
118     // batch tmp files directory
119     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
120       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
121       Couple inputFile = cpt;
122       command = copy_command;
123       command += inputFile.getLocal();
124       command += " ";
125       if(_username != ""){
126         command += _username;
127         command += "@";
128       }
129       command += _hostname;
130       command += ":";
131       command += inputFile.getRemote();
132       cerr << command.c_str() << endl;
133       status = system(command.c_str());
134       if(status) {
135         std::ostringstream oss;
136         oss << status;
137         std::string ex_mess("Error of connection on remote host ! status = ");
138         ex_mess += oss.str();
139         throw EmulationException(ex_mess.c_str());
140       }
141     }
142
143   }
144
145   void BatchManager_eClient::importOutputFiles( const Job & job, const string directory ) throw(EmulationException)
146   {
147     string command;
148     int status;
149
150     Parametre params = job.getParametre();
151     Versatile V = params[OUTFILE];
152     Versatile::iterator Vit;
153
154     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
155       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
156       Couple outputFile = cpt;
157       if( _protocol == "rsh" )
158         command = "rcp ";
159       else if( _protocol == "ssh" )
160         command = "scp ";
161       else
162         throw EmulationException("Unknown protocol");
163
164       if (_username != ""){
165         command += _username;
166         command += "@";
167       }
168       command += _hostname;
169       command += ":";
170       command += outputFile.getRemote();
171       command += " ";
172       command += directory;
173       cerr << command.c_str() << endl;
174       status = system(command.c_str());
175       if(status) 
176       {
177         // Try to get what we can (logs files)
178         // throw BatchException("Error of connection on remote host");    
179         std::string mess("Copy command failed ! status is :");
180         ostringstream status_str;
181         status_str << status;
182         mess += status_str.str();
183         cerr << mess << endl;
184       }
185     }
186
187   }
188
189   MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl) throw(EmulationException)
190   {
191     if(mpiImpl == "lam")
192       return new MpiImpl_LAM();
193     else if(mpiImpl == "mpich1")
194       return new MpiImpl_MPICH1();
195     else if(mpiImpl == "mpich2")
196       return new MpiImpl_MPICH2();
197     else if(mpiImpl == "openmpi")
198       return new MpiImpl_OPENMPI();
199     else if(mpiImpl == "slurm")
200       return new MpiImpl_SLURM();
201     else{
202       ostringstream oss;
203       oss << mpiImpl << " : not yet implemented";
204       throw EmulationException(oss.str().c_str());
205     }
206   }
207
208   string BatchManager_eClient::BuildTemporaryFileName() const
209   {
210     //build more complex file name to support multiple salome session
211     string aFileName = Kernel_Utils::GetTmpFileName();
212 #ifndef WIN32
213     aFileName += ".sh";
214 #else
215     aFileName += ".bat";
216 #endif
217     return aFileName;
218   }
219
220   void BatchManager_eClient::RmTmpFile(std::string & TemporaryFileName)
221   {
222 #ifdef WIN32
223     string command = "del /F ";
224 #else
225     string command = "rm ";
226 #endif
227     command += TemporaryFileName;
228     char *temp = strdup(command.c_str());
229     int lgthTemp = strlen(temp);
230     temp[lgthTemp - 3] = '*';
231     temp[lgthTemp - 2] = '\0';
232     system(temp);
233     free(temp);
234   }
235 }