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