Salome HOME
1378202917d8af99eabc251888748c5b8d285aba
[tools/libbatch.git] / src / SSH / Batch_BatchManager_eSSH.cxx
1 //  Copyright (C) 2007-2010  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_eSSH.cxx : emulation of SSH client
24  *
25  * Auteur : AndrĂ© RIBES - EDF R&D
26  * Date   : Octobre 2009
27  */
28
29 #include <iostream>
30 #include <fstream>
31 #include <sstream>
32 #include <sys/stat.h>
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <Batch_config.h>
37
38 #ifdef MSVC
39 #include <io.h>
40 #else
41 #include <libgen.h>
42 #endif
43
44 #include "Batch_BatchManager_eSSH.hxx"
45 #include "Batch_JobInfo_eSSH.hxx"
46
47 using namespace std;
48
49 namespace Batch {
50
51   BatchManager_eSSH::BatchManager_eSSH(const FactBatchManager * parent, const char * host,
52                                        CommunicationProtocolType protocolType, const char * mpiImpl)
53     : BatchManager_eClient(parent, host, protocolType, mpiImpl),
54       BatchManager_Local(parent, host, protocolType),
55       BatchManager(parent, host)
56   {
57     // Nothing to do
58   }
59
60   // Destructeur
61   BatchManager_eSSH::~BatchManager_eSSH()
62   {
63     // Nothing to do
64   }
65
66   // Methode pour le controle des jobs : soumet un job au gestionnaire
67   const JobId BatchManager_eSSH::submitJob(const Job & job)
68   {
69     // export input files on cluster
70     std::cerr << "BatchManager_eSSH::submitJob exportInputFiles" << std::endl;
71     Parametre param = job.getParametre();
72
73     // Input files copy
74     exportInputFiles(job);
75
76     // Launch job
77     // Patch until Local Manager is patched
78     std::string executable = param[EXECUTABLE].str();
79     std::string::size_type p1 = executable.find_last_of("/");
80     std::string fileNameToExecute = "./" + executable.substr(p1+1);
81     Parametre new_param(param);
82     new_param[INFILE].eraseAll(); 
83     new_param[OUTFILE].eraseAll();
84     new_param[EXECUTABLE] = fileNameToExecute;
85     new_param[EXECUTIONHOST] = _hostname;
86     Job * j = new Job(new_param);
87
88
89     std::cerr << "BatchManager_eSSH::submitJob Local submitJob" << std::endl;
90     JobId id = BatchManager_Local::submitJob(*j);
91     delete j;
92     return id;
93   }
94
95   // Methode pour le controle des jobs : retire un job du gestionnaire
96   void BatchManager_eSSH::deleteJob(const JobId & jobid)
97   {
98     BatchManager_Local::deleteJob(jobid);
99   }
100   
101   // Methode pour le controle des jobs : renvoie l'etat du job
102   JobInfo BatchManager_eSSH::queryJob(const JobId & jobid)
103   {
104     return BatchManager_Local::queryJob(jobid);
105   }
106
107   // Methode pour le controle des jobs : suspend un job en file d'attente
108   void BatchManager_eSSH::holdJob(const JobId & jobid)
109   {
110     BatchManager_Local::holdJob(jobid);
111   }
112
113   // Methode pour le controle des jobs : relache un job suspendu
114   void BatchManager_eSSH::releaseJob(const JobId & jobid)
115   {
116     BatchManager_Local::releaseJob(jobid);
117   }
118
119   // Methode pour le controle des jobs : modifie un job en file d'attente
120   void BatchManager_eSSH::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
121   {
122     BatchManager_Local::alterJob(jobid, param, env);
123   }
124
125   // Methode pour le controle des jobs : modifie un job en file d'attente
126   void BatchManager_eSSH::alterJob(const JobId & jobid, const Parametre & param)
127   {
128     BatchManager_Local::alterJob(jobid, param);
129   }
130
131   // Methode pour le controle des jobs : modifie un job en file d'attente
132   void BatchManager_eSSH::alterJob(const JobId & jobid, const Environnement & env)
133   {
134     BatchManager_Local::alterJob(jobid, env); 
135   }
136
137   void BatchManager_eSSH::buildBatchScript(const Job & job)
138   {
139     Parametre params = job.getParametre();
140     Environnement env = job.getEnvironnement();
141     const long nbproc = params[NBPROC];
142     const long edt = params[MAXWALLTIME];
143     const long mem = params[MAXRAMSIZE];
144     const string workDir = params[WORKDIR];
145     const std::string dirForTmpFiles = params[TMPDIR];
146     const string fileToExecute = params[EXECUTABLE];
147     const string home = params[HOMEDIR];
148     const std::string queue = params[QUEUE];
149     std::string rootNameToExecute;
150     std::string fileNameToExecute;
151     std::string filelogtemp;
152     if( fileToExecute.size() > 0 ){
153       string::size_type p1 = fileToExecute.find_last_of("/");
154       string::size_type p2 = fileToExecute.find_last_of(".");
155       rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
156
157 #ifdef MSVC
158       char fname[_MAX_FNAME];
159       char ext[_MAX_EXT];
160       _splitpath_s(fileToExecute.c_str(), NULL, 0, NULL, 0, fname, _MAX_FNAME, ext, _MAX_EXT);
161       string execBaseName = string(fname) + ext;
162 #else
163       char* basec=strdup(fileToExecute.c_str());
164       string execBaseName = string(basename(basec));
165       free(basec);
166 #endif
167
168       fileNameToExecute = "~/" + dirForTmpFiles + "/" + execBaseName;
169
170       int idx = dirForTmpFiles.find("Batch/");
171       filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
172     }
173     else{
174       rootNameToExecute = "command";
175     }
176
177     ofstream tempOutputFile;
178     std::string TmpFileName = createAndOpenTemporaryFile("SSH-script", tempOutputFile);
179
180     tempOutputFile << "#! /bin/sh -f" << endl;
181     if (queue != "")
182       tempOutputFile << "#BSUB -q " << queue << endl;
183     if( edt > 0 )
184       tempOutputFile << "#SSH -l walltime=" << edt*60 << endl ;
185     if( mem > 0 )
186       tempOutputFile << "#SSH -l mem=" << mem << "mb" << endl ;
187     if( fileToExecute.size() > 0 ){
188       tempOutputFile << "#SSH -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
189       tempOutputFile << "#SSH -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
190     }
191     else{
192       tempOutputFile << "#SSH -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
193       tempOutputFile << "#SSH -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
194     }
195     if( workDir.size() > 0 )
196       tempOutputFile << "cd " << workDir << endl ;
197     if( fileToExecute.size() > 0 ){
198       tempOutputFile << _mpiImpl->boot("${SSH_NODEFILE}",nbproc);
199       tempOutputFile << _mpiImpl->run("${SSH_NODEFILE}",nbproc,fileNameToExecute);
200       tempOutputFile << _mpiImpl->halt();
201     }
202     else{
203       tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
204       tempOutputFile << env["COMMAND"];
205     }
206
207     tempOutputFile.flush();
208     tempOutputFile.close();
209 #ifdef WIN32
210     _chmod(
211 #else
212     chmod(
213 #endif
214       TmpFileName.c_str(), 0x1ED);
215     cerr << TmpFileName.c_str() << endl;
216
217     int status = Batch::BatchManager_eClient::_protocol.copyFile(TmpFileName, "", "",
218                                     dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
219                                     _hostname, _username);
220     if (status)
221       throw EmulationException("Error of connection on remote host");
222
223     remove(TmpFileName.c_str());
224   }
225
226 }