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