Salome HOME
merge from branch BR_For40_DSC tag mergeto_V4_1_0_maintainance_29may08
[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 <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <sys/stat.h>
34 #include "Batch_BatchManager_eClient.hxx"
35
36 namespace Batch {
37
38   BatchManager_eClient::BatchManager_eClient(const Batch::FactBatchManager * parent, const char* host, const char* protocol, const char* mpiImpl) : BatchManager(parent, host), _protocol(protocol), _username("")
39   {
40     // instanciation of mpi implementation needed to launch executable in batch script
41     _mpiImpl = FactoryMpiImpl(mpiImpl);
42   }
43
44   // Destructeur
45   BatchManager_eClient::~BatchManager_eClient()
46   {
47     // Nothing to do
48     delete _mpiImpl;
49   }
50
51   void BatchManager_eClient::exportInputFiles(const Job& job) throw(EmulationException)
52   {
53     int status;
54     Parametre params = job.getParametre();
55     Versatile V = params[INFILE];
56     Versatile::iterator Vit;
57     string command;
58     string copy_command;
59     _username = string(params[USER]);
60
61     // Test protocol
62     if( _protocol == "rsh" )
63       copy_command = "rcp ";
64     else if( _protocol == "ssh" )
65       copy_command = "scp ";
66     else
67       throw EmulationException("Unknown protocol : only rsh and ssh are known !");
68     
69     // First step : creating batch tmp files directory
70     command = _protocol;
71     command += " ";
72     if(_username != ""){
73       command += _username;
74       command += "@";
75     }
76     command += _hostname;
77     command += " \"mkdir -p ";
78     command += string(params[TMPDIR]);
79     command += "\"" ;
80     cerr << command.c_str() << endl;
81     status = system(command.c_str());
82     if(status) {
83       std::ostringstream oss;
84       oss << status;
85       std::string ex_mess("Error of connection on remote host ! status = ");
86       ex_mess += oss.str();
87       throw EmulationException(ex_mess.c_str());
88     }
89
90     // Second step : copy fileToExecute into
91     // batch tmp files directory
92     command = copy_command;
93     command += string(params[EXECUTABLE]);
94     command += " ";
95     if(_username != ""){
96       command += _username;
97       command += "@";
98     }
99     command += _hostname;
100     command += ":";
101     command += string(params[TMPDIR]);
102     cerr << command.c_str() << endl;
103     status = system(command.c_str());
104     if(status) {
105       std::ostringstream oss;
106       oss << status;
107       std::string ex_mess("Error of connection on remote host ! status = ");
108       ex_mess += oss.str();
109       throw EmulationException(ex_mess.c_str());
110     }
111     
112     // Third step : copy filesToExportList into
113     // batch tmp files directory
114     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
115       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
116       Couple inputFile = cpt;
117       command = copy_command;
118       command += inputFile.getLocal();
119       command += " ";
120       if(_username != ""){
121         command += _username;
122         command += "@";
123       }
124       command += _hostname;
125       command += ":";
126       command += inputFile.getRemote();
127       cerr << command.c_str() << endl;
128       status = system(command.c_str());
129       if(status) {
130         std::ostringstream oss;
131         oss << status;
132         std::string ex_mess("Error of connection on remote host ! status = ");
133         ex_mess += oss.str();
134         throw EmulationException(ex_mess.c_str());
135       }
136     }
137
138   }
139
140   void BatchManager_eClient::importOutputFiles( const Job & job, const string directory ) throw(EmulationException)
141   {
142     string command;
143     int status;
144
145     Parametre params = job.getParametre();
146     Versatile V = params[OUTFILE];
147     Versatile::iterator Vit;
148  
149     for(Vit=V.begin(); Vit!=V.end(); Vit++) {
150       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
151       Couple outputFile = cpt;
152       if( _protocol == "rsh" )
153         command = "rcp ";
154       else if( _protocol == "ssh" )
155         command = "scp ";
156       else
157         throw EmulationException("Unknown protocol");
158
159       if (_username != ""){
160         command += _username;
161         command += "@";
162       }
163       command += _hostname;
164       command += ":";
165       command += outputFile.getRemote();
166       command += " ";
167       command += directory;
168       cerr << command.c_str() << endl;
169       status = system(command.c_str());
170       if(status) 
171       {
172         // Try to get what we can (logs files)
173         // throw BatchException("Error of connection on remote host");    
174         std::string mess("Copy command failed ! status is :");
175         ostringstream status_str;
176         status_str << status;
177         mess += status_str.str();
178         cerr << mess << endl;
179       }
180     }
181
182   }
183
184   MpiImpl *BatchManager_eClient::FactoryMpiImpl(string mpiImpl) throw(EmulationException)
185   {
186     if(mpiImpl == "lam")
187       return new MpiImpl_LAM();
188     else if(mpiImpl == "mpich1")
189       return new MpiImpl_MPICH1();
190     else if(mpiImpl == "mpich2")
191       return new MpiImpl_MPICH2();
192     else if(mpiImpl == "openmpi")
193       return new MpiImpl_OPENMPI();
194     else if(mpiImpl == "slurm")
195       return new MpiImpl_SLURM();
196     else{
197       ostringstream oss;
198       oss << mpiImpl << " : not yet implemented";
199       throw EmulationException(oss.str().c_str());
200     }
201   }
202
203   string BatchManager_eClient::BuildTemporaryFileName() const
204   {
205     //build more complex file name to support multiple salome session
206     char *temp = new char[19];
207     strcpy(temp, "/tmp/command");
208     strcat(temp, "XXXXXX");
209 #ifndef WNT
210     mkstemp(temp);
211 #else
212     char aPID[80];
213     itoa(getpid(), aPID, 10);
214     strcat(temp, aPID);
215 #endif
216
217     string command(temp);
218     delete [] temp;
219     command += ".sh";
220     return command;
221   }
222
223   void BatchManager_eClient::RmTmpFile(std::string & TemporaryFileName)
224   {
225     string command = "rm ";
226     command += TemporaryFileName;
227     char *temp = strdup(command.c_str());
228     int lgthTemp = strlen(temp);
229     temp[lgthTemp - 3] = '*';
230     temp[lgthTemp - 2] = '\0';
231     system(temp);
232     free(temp);
233   }
234
235 }