Salome HOME
Merge from BR_LIBBATCH_2_0
[modules/kernel.git] / src / Launcher / Launcher_Job_SALOME.cxx
1 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // Author: AndrĂ© RIBES - EDF R&D
21 //
22 #include "Launcher_Job_SALOME.hxx"
23 #include "Basics_DirUtils.hxx"
24
25 #ifdef WITH_LIBBATCH
26 #include <libbatch/Constants.hxx>
27 #endif
28
29 #ifdef WNT
30 #include <io.h>
31 #define _chmod chmod
32 #endif
33
34 Launcher::Job_SALOME::Job_SALOME() {}
35
36 Launcher::Job_SALOME::~Job_SALOME() {}
37
38 void 
39 Launcher::Job_SALOME::setResourceDefinition(const ParserResourcesType & resource_definition)
40 {
41   // Check resource_definition
42   if (resource_definition.AppliPath == "")
43   {
44     std::string mess = "Resource definition must define an application path !, resource name is: " + resource_definition.Name;
45     throw LauncherException(mess);
46   }
47   Launcher::Job::setResourceDefinition(resource_definition);
48 }
49
50 void
51 Launcher::Job_SALOME::update_job()
52 {
53 #ifdef WITH_LIBBATCH
54   Batch::Parametre params = common_job_params();
55   params[Batch::EXECUTABLE] = buildSalomeScript(params);
56   params[Batch::EXCLUSIVE] = true;
57   _batch_job->setParametre(params);
58 #endif
59 }
60
61 #ifdef WITH_LIBBATCH
62 std::string 
63 Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
64 {
65   // parameters
66   std::string work_directory = params[Batch::WORKDIR].str();
67
68   std::string launch_script = Kernel_Utils::GetTmpDir() + "runSalome_" + _job_file_name + "_" + _launch_date + ".sh";
69   std::ofstream launch_script_stream;
70   launch_script_stream.open(launch_script.c_str(), 
71                             std::ofstream::out
72 #ifdef WIN32            
73  | std::ofstream::binary   //rnv: to avoid CL+RF end of line on windows
74 #endif
75                            );
76    
77   // Begin of script
78   launch_script_stream << "#!/bin/sh -f" << std::endl;
79   launch_script_stream << "cd " << work_directory << std::endl;
80   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
81   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
82   if (_env_file != "")
83   {
84     std::string::size_type last = _env_file.find_last_of("/");
85     launch_script_stream << ". " << _env_file.substr(last+1) << std::endl;
86   }
87   launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
88
89   // -- Generates Catalog Resources
90   std::string resource_protocol = _resource_definition.getClusterInternalProtocolStr();
91   launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
92   launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
93   launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
94   launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'  > $CATALOG_FILE" << std::endl;
95   launch_script_stream << "echo '<resources>'                 >> $CATALOG_FILE" << std::endl;   
96   launch_script_stream << "cat $LIBBATCH_NODEFILE | sort | uniq -c | while read nbproc host"  << std::endl;
97   launch_script_stream << "do"                                                  << std::endl;
98   launch_script_stream << "echo '<machine hostname='\\\"$host\\\"                                 >> $CATALOG_FILE" << std::endl;
99   launch_script_stream << "echo '         protocol=\"" << resource_protocol               << "\"' >> $CATALOG_FILE" << std::endl;
100   launch_script_stream << "echo '         userName=\"" << _resource_definition.UserName   << "\"' >> $CATALOG_FILE" << std::endl;
101   launch_script_stream << "echo '         appliPath=\"" << _resource_definition.AppliPath << "\"' >> $CATALOG_FILE" << std::endl;
102   launch_script_stream << "echo '         mpi=\"" << _resource_definition.getMpiImplTypeStr() << "\"' >> $CATALOG_FILE" << std::endl;
103   launch_script_stream << "echo '         nbOfNodes='\\\"$nbproc\\\" >> $CATALOG_FILE" << std::endl;
104   launch_script_stream << "echo '         nbOfProcPerNode=\"1\"' >> $CATALOG_FILE" << std::endl;
105   launch_script_stream << "echo '         can_run_containers=\"true\"' >> $CATALOG_FILE" << std::endl;
106   launch_script_stream << "echo '/>'                                                              >> $CATALOG_FILE" << std::endl;
107   launch_script_stream << "done"                                 << std::endl;
108   launch_script_stream << "echo '</resources>' >> $CATALOG_FILE" << std::endl;
109   launch_script_stream << "fi" << std::endl;
110
111   // Create file for ns-port-log
112   launch_script_stream << "NS_PORT_FILE_PATH=`mktemp " << _resource_definition.AppliPath << "/USERS/nsport_XXXXXX` &&\n";
113   launch_script_stream << "NS_PORT_FILE_NAME=`basename $NS_PORT_FILE_PATH` &&\n";
114
115   // Launch SALOME with an appli
116   launch_script_stream << _resource_definition.AppliPath << "/runAppli --terminal --ns-port-log=$NS_PORT_FILE_NAME --server-launch-mode=fork ";
117   launch_script_stream << "> logs/salome_" << _launch_date << ".log 2>&1 &&" << std::endl;
118   launch_script_stream << "current=0 &&\n"
119                        << "stop=20 &&\n"
120                        << "while ! test -s $NS_PORT_FILE_PATH\n"
121                        << "do\n"
122                        << "  sleep 2\n"
123                        << "  current=$((current+1))\n"
124                        << "  if [ \"$current\" -eq \"$stop\" ] ; then\n"
125                        << "    echo Error Naming Service failed ! >&2\n"
126                        << "    exit\n"
127                        << "  fi\n"
128                        << "done &&\n"
129                        << "appli_port=`cat $NS_PORT_FILE_PATH` &&\n"
130                        << "rm $NS_PORT_FILE_PATH &&\n";
131
132   // Call real job type
133   addJobTypeSpecificScript(launch_script_stream);
134
135   // End
136   launch_script_stream << _resource_definition.AppliPath << "/runSession -p $appli_port shutdownSalome.py" << std::endl;
137   launch_script_stream << "sleep 10" << std::endl;
138
139   // Return
140   launch_script_stream.flush();
141   launch_script_stream.close();
142   chmod(launch_script.c_str(), 0x1ED);
143   return launch_script;
144 }
145 #endif
146