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