Salome HOME
Copyright update 2020
[modules/kernel.git] / src / Launcher / Launcher_Job_SALOME.cxx
1 // Copyright (C) 2009-2020  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, or (at your option) any later version.
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 WIN32
30 #include <io.h>
31 #define _chmod chmod
32 #include <process.h>
33 #endif
34 #include <sys/stat.h>
35
36 #include <sstream>
37
38 Launcher::Job_SALOME::Job_SALOME() {}
39
40 Launcher::Job_SALOME::~Job_SALOME() {}
41
42 void
43 Launcher::Job_SALOME::setResourceDefinition(const ParserResourcesType & resource_definition)
44 {
45   // Check resource_definition
46   if (resource_definition.AppliPath == "")
47   {
48     std::string mess = "Resource definition must define an application path !, resource name is: " + resource_definition.Name;
49     throw LauncherException(mess);
50   }
51   Launcher::Job::setResourceDefinition(resource_definition);
52 }
53
54 void
55 Launcher::Job_SALOME::update_job()
56 {
57 #ifdef WITH_LIBBATCH
58   Batch::Parametre params = common_job_params();
59   params[Batch::EXECUTABLE] = buildSalomeScript(params);
60   _batch_job->setParametre(params);
61 #endif
62 }
63
64 #ifdef WITH_LIBBATCH
65 std::string
66 Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
67 {
68   // parameters
69   std::string work_directory = params[Batch::WORKDIR].str();
70   std::ostringstream str_pid;
71 #ifdef WIN32
72   str_pid << _getpid();
73 #else
74   str_pid << ::getpid();
75 #endif
76   struct stat statbuf;
77   // true if APPLI holds a salome file launcher (in state of an application directory)
78   bool is_launcher_file=stat(getenv("APPLI"), &statbuf) ==0 &&  S_ISREG(statbuf.st_mode);
79   std::string launch_tmp_dir = Kernel_Utils::GetTmpDir();
80   std::string launch_script = launch_tmp_dir + "runSalome_" + _job_file_name + "_" + _launch_date + "-" + str_pid.str() + ".sh";
81   std::ofstream launch_script_stream;
82   launch_script_stream.open(launch_script.c_str(),
83                             std::ofstream::out
84 #ifdef WIN32
85  | std::ofstream::binary   //rnv: to avoid CL+RF end of line on windows
86 #endif
87                            );
88
89   // Begin of script
90   launch_script_stream << "#!/bin/sh -f" << std::endl;
91   launch_script_stream << "cd " << work_directory << std::endl;
92   // remove the exit code from any previous execution
93   launch_script_stream << "rm -f logs/exit_code.log" << std::endl;
94
95   launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
96   launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
97   if (_env_file != "")
98   {
99     std::string::size_type last = _env_file.find_last_of("/");
100     launch_script_stream << ". " << _env_file.substr(last+1) << std::endl;
101   }
102   launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
103
104   // -- Generates Catalog Resources
105   // do not build a new catalog if the job is launched on localhost
106   if(_resource_required_params.name != "localhost")
107   {
108     std::string resource_protocol = _resource_definition.getClusterInternalProtocolStr();
109     launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
110     launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
111     launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
112     launch_script_stream << "{" << std::endl;
113     launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'" << std::endl;
114     launch_script_stream << "echo '<resources>'" << std::endl;
115     launch_script_stream << "sort \"$LIBBATCH_NODEFILE\" | uniq -c | while read nbproc host"  << std::endl;
116     launch_script_stream << "do"                                                  << std::endl;
117     // Full name doesn't work. eg: sagittaire-7 instead of sagittaire-7.lyon.grid5000.fr
118     launch_script_stream << "host_basename=$(echo \"$host\" | cut -f1 -d.)"   << std::endl;
119     launch_script_stream << "echo '<machine name='\\\"\"$host_basename\"\\\"" << std::endl;
120     launch_script_stream << "echo '         hostname='\\\"\"$host_basename\"\\\"" << std::endl;
121     launch_script_stream << "echo '         type=\"single_machine\"'" << std::endl;
122     launch_script_stream << "echo '         protocol=\"" << resource_protocol               << "\"'" << std::endl;
123     launch_script_stream << "echo '         userName=\"" << _resource_definition.UserName   << "\"'" << std::endl;
124     launch_script_stream << "echo '         appliPath=\"" << _resource_definition.AppliPath << "\"'" << std::endl;
125     launch_script_stream << "echo '         mpi=\"" << _resource_definition.getMpiImplTypeStr() << "\"'" << std::endl;
126     launch_script_stream << "echo '         nbOfNodes='\\\"\"$nbproc\"\\\"" << std::endl;
127     launch_script_stream << "echo '         nbOfProcPerNode=\"1\"'" << std::endl;
128     launch_script_stream << "echo '         canRunContainers=\"true\"'" << std::endl;
129     launch_script_stream << "echo '/>'" << std::endl;
130     launch_script_stream << "done"                                 << std::endl;
131     launch_script_stream << "echo '</resources>'" << std::endl;
132     launch_script_stream << "} > $CATALOG_FILE" << std::endl;
133     launch_script_stream << "fi" << std::endl;
134   }
135   // Create file for ns-port-log
136   if (is_launcher_file)
137       // for a salome application file, we write NS_PORT_FILE_PATH in launch_tmp_dir
138       launch_script_stream << "NS_PORT_FILE_PATH=$(mktemp " << launch_tmp_dir << "nsport_XXXXXX) &&\n";
139   else
140       launch_script_stream << "NS_PORT_FILE_PATH=$(mktemp " << _resource_definition.AppliPath << "/USERS/nsport_XXXXXX) &&\n";
141
142   // Launch SALOME with an appli
143   if (is_launcher_file)
144       launch_script_stream << _resource_definition.AppliPath << " start --terminal --ns-port-log=\"$NS_PORT_FILE_PATH\" --server-launch-mode=fork ";
145   else
146       launch_script_stream << _resource_definition.AppliPath << "/salome start --terminal --ns-port-log=\"$NS_PORT_FILE_PATH\" --server-launch-mode=fork ";
147   launch_script_stream << "> logs/salome_" << _launch_date << ".log 2>&1 &&" << std::endl;
148   launch_script_stream << "current=0 &&\n"
149                        << "stop=20 &&\n"
150                        << "while ! test -s \"$NS_PORT_FILE_PATH\"\n"
151                        << "do\n"
152                        << "  sleep 2\n"
153                        << "  current=$((current+1))\n"
154                        << "  if [ \"$current\" -eq \"$stop\" ] ; then\n"
155                        << "    echo Error Naming Service failed ! >&2\n"
156                        << "    exit\n"
157                        << "  fi\n"
158                        << "done &&\n"
159                        << "appli_port=$(cat \"$NS_PORT_FILE_PATH\") &&\n"
160                        << "rm \"$NS_PORT_FILE_PATH\" &&\n";
161
162   // Call real job type
163   addJobTypeSpecificScript(launch_script_stream);
164   // log the exit code
165   launch_script_stream << "echo $? > logs/exit_code.log" << std::endl;
166
167   // End
168   if (is_launcher_file)
169       launch_script_stream << _resource_definition.AppliPath << " kill \"$appli_port\"" << std::endl;
170   else
171       launch_script_stream << _resource_definition.AppliPath << "/salome kill \"$appli_port\"" << std::endl;
172
173   // Return
174   launch_script_stream.flush();
175   launch_script_stream.close();
176   chmod(launch_script.c_str(), 0x1ED);
177   return launch_script;
178 }
179 #endif