Salome HOME
Added Windows implementation for local submission based on SSH and PBS emulation...
[tools/libbatch.git] / src / Local / Batch_BatchManager_Local_SSH.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_Local_SSH.cxx :
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Thu Nov  6 10:17:22 2003
28  * Projet : Salome 2
29  *
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #  include <SALOMEconfig.h>
34 #endif
35
36 #include <iostream>
37 #include <fstream>
38 #include <sstream>
39 #include <cstdlib>
40 #include <sys/types.h>
41 #ifndef WIN32
42 #include <sys/wait.h>
43 #include <unistd.h>
44 #endif
45 #include <ctime>
46
47 #include <pthread.h>
48 #include <signal.h>
49 #include <errno.h>
50 #include <string.h>
51 #include "Batch_IOMutex.hxx"
52 #include "Batch_BatchManager_Local_SSH.hxx"
53
54 #include "Batch_config.h"
55
56 #ifndef RM
57 #error "RM undefined. You must set RM to a valid path to a rm-like command."
58 #endif
59
60 #ifndef SCP
61 #error "SCP undefined. You must set SCP to a valid path to a scp-like command."
62 #endif
63
64 #ifndef SSH
65 #error "SSH undefined. You must set SSH to a valid path to a ssh-like command."
66 #endif
67
68 using namespace std;
69
70 namespace Batch {
71
72
73   // Constructeur
74   BatchManager_Local_SSH::BatchManager_Local_SSH(const FactBatchManager * parent, const char * host) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_Local(parent, host)
75   {
76   }
77
78   // Destructeur
79   BatchManager_Local_SSH::~BatchManager_Local_SSH()
80   {
81   }
82
83
84   // Methode abstraite qui renvoie la commande de copie du fichier source en destination
85   string BatchManager_Local_SSH::copy_command(const std::string & user_source,
86                                               const std::string & host_source,
87                                               const std::string & source,
88                                               const std::string & user_destination,
89                                               const std::string & host_destination,
90                                               const std::string & destination) const
91   {
92     ostringstream fullsource;
93     if (host_source.size() != 0) {
94       if (user_source.size() != 0) {
95         fullsource << user_source << "@";
96       }
97       fullsource << host_source << ":";
98     }
99     fullsource << source;
100
101     ostringstream fulldestination;
102     if (host_destination.size() != 0) {
103       if (user_destination.size() != 0) {
104         fulldestination << user_destination << "@";
105       }
106       fulldestination << host_destination << ":";
107     }
108     fulldestination << destination;
109
110     ostringstream copy_cmd;
111     // Option -p is used to keep the same permissions for the destination file (particularly useful to keep scripts
112     // executable when copying them)
113     copy_cmd << "\"" << SCP << "\" -p " << fullsource.str() << " " << fulldestination.str();
114     return copy_cmd.str();
115   }
116
117   // Methode abstraite qui renvoie la commande a executer
118   string BatchManager_Local_SSH::exec_command(Parametre & param) const
119   {
120     ostringstream exec_sub_cmd;
121 #ifdef WIN32
122     exec_sub_cmd << "\"";
123 #endif
124     exec_sub_cmd << "cd " << param[WORKDIR] << " && " << param[EXECUTABLE];
125
126     if (param.find(ARGUMENTS) != param.end()) {
127       Versatile V = param[ARGUMENTS];
128       for(Versatile::const_iterator it=V.begin(); it!=V.end(); it++) {
129         StringType argt = * static_cast<StringType *>(*it);
130         string     arg  = argt;
131         exec_sub_cmd << " " << arg;
132       }
133     }
134 #ifdef WIN32
135     exec_sub_cmd << "\"";
136 #endif
137
138
139     Versatile new_arguments;
140     new_arguments.setMaxSize(0);
141     new_arguments = string(param[EXECUTIONHOST]);
142
143
144     if (param.find(USER) != param.end()) {
145       new_arguments += "-l";
146       new_arguments += string(param[USER]);
147     }
148
149     new_arguments += exec_sub_cmd.str();
150
151     param[ARGUMENTS] = new_arguments;
152
153     // Sous Linux on est oblige de modifier ces deux parametres pour faire fonctionner la commande rsh
154     param[EXECUTABLE] = SSH;
155     param.erase(NAME);
156
157     return SSH;
158   }
159
160   // Methode qui renvoie la commande d'effacement du fichier
161   string BatchManager_Local_SSH::remove_command(const std::string & user_destination,
162                                                 const std::string & host_destination,
163                                                 const std::string & destination) const
164   {
165     string fulldestination = (host_destination.size()) ? host_destination : "localhost";
166     if (user_destination.size() != 0) {
167       fulldestination += " -l " + user_destination;
168     }
169
170     // We consider here that the remote system is UNIX-like and has a "rm" command. Using the
171     // RM macro would be pointless here since the remote system is different from the local one.
172     ostringstream remove_cmd;
173     remove_cmd << "\"" << SSH << "\" " << fulldestination << " \"rm " << destination << "\"";
174     return remove_cmd.str();
175   }
176 }