1 // Copyright (C) 2009-2021 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author: André RIBES - EDF R&D
22 #include "Launcher_Job_SALOME.hxx"
23 #include "Basics_DirUtils.hxx"
26 #include <libbatch/Constants.hxx>
38 Launcher::Job_SALOME::Job_SALOME(bool activateSession)
39 : _activateSession(activateSession){}
41 Launcher::Job_SALOME::~Job_SALOME() {}
44 Launcher::Job_SALOME::setResourceDefinition(const ParserResourcesType & resource_definition)
46 // Check resource_definition
47 if (resource_definition.AppliPath == "")
49 std::string mess = "Resource definition must define an application path !, resource name is: " + resource_definition.Name;
50 throw LauncherException(mess);
52 Launcher::Job::setResourceDefinition(resource_definition);
56 Launcher::Job_SALOME::update_job()
59 Batch::Parametre params = common_job_params();
60 params[Batch::EXECUTABLE] = buildSalomeScript(params);
61 _batch_job->setParametre(params);
67 Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
70 std::string work_directory = params[Batch::WORKDIR].str();
71 std::ostringstream str_pid;
75 str_pid << ::getpid();
78 // true if APPLI holds a salome file launcher (in state of an application directory)
79 bool is_launcher_file=stat(getenv("APPLI"), &statbuf) ==0 && S_ISREG(statbuf.st_mode);
80 std::string launch_tmp_dir = Kernel_Utils::GetTmpDir();
81 std::string launch_script = launch_tmp_dir + "runSalome_" + _job_file_name + "_" + _launch_date + "-" + str_pid.str() + ".sh";
82 std::ofstream launch_script_stream;
83 launch_script_stream.open(launch_script.c_str(),
86 | std::ofstream::binary //rnv: to avoid CL+RF end of line on windows
91 launch_script_stream << "#!/bin/sh -f" << std::endl;
92 launch_script_stream << "cd " << work_directory << std::endl;
93 // remove the exit code from any previous execution
94 launch_script_stream << "rm -f logs/exit_code.log" << std::endl;
96 launch_script_stream << "export PYTHONPATH=" << work_directory << ":$PYTHONPATH" << std::endl;
97 launch_script_stream << "export PATH=" << work_directory << ":$PATH" << std::endl;
100 std::string::size_type last = _env_file.find_last_of("/");
101 launch_script_stream << ". " << _env_file.substr(last+1) << std::endl;
103 launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
105 // -- Generates Catalog Resources
106 // do not build a new catalog if the job is launched on localhost
107 if(_resource_required_params.name != "localhost")
109 std::string resource_protocol = _resource_definition.getClusterInternalProtocolStr();
110 launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
111 launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
112 launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
113 launch_script_stream << "{" << std::endl;
114 launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'" << std::endl;
115 launch_script_stream << "echo '<resources>'" << std::endl;
116 launch_script_stream << "sort \"$LIBBATCH_NODEFILE\" | uniq -c | while read nbproc host" << std::endl;
117 launch_script_stream << "do" << std::endl;
118 // Full name doesn't work. eg: sagittaire-7 instead of sagittaire-7.lyon.grid5000.fr
119 launch_script_stream << "host_basename=$(echo \"$host\" | cut -f1 -d.)" << std::endl;
120 launch_script_stream << "echo '<machine name='\\\"\"$host_basename\"\\\"" << std::endl;
121 launch_script_stream << "echo ' hostname='\\\"\"$host_basename\"\\\"" << std::endl;
122 launch_script_stream << "echo ' type=\"single_machine\"'" << std::endl;
123 launch_script_stream << "echo ' protocol=\"" << resource_protocol << "\"'" << std::endl;
124 launch_script_stream << "echo ' userName=\"" << _resource_definition.UserName << "\"'" << std::endl;
125 launch_script_stream << "echo ' appliPath=\"" << _resource_definition.AppliPath << "\"'" << std::endl;
126 launch_script_stream << "echo ' mpi=\"" << _resource_definition.getMpiImplTypeStr() << "\"'" << std::endl;
127 launch_script_stream << "echo ' nbOfNodes='\\\"\"$nbproc\"\\\"" << std::endl;
128 launch_script_stream << "echo ' nbOfProcPerNode=\"1\"'" << std::endl;
129 launch_script_stream << "echo ' canRunContainers=\"true\"'" << std::endl;
130 launch_script_stream << "echo '/>'" << std::endl;
131 launch_script_stream << "done" << std::endl;
132 launch_script_stream << "echo '</resources>'" << std::endl;
133 launch_script_stream << "} > $CATALOG_FILE" << std::endl;
134 launch_script_stream << "fi" << std::endl;
137 // Call real job type
138 addJobTypeSpecificScript(launch_script_stream);
140 launch_script_stream << "echo $? > logs/exit_code.log" << std::endl;
143 launch_script_stream.flush();
144 launch_script_stream.close();
145 chmod(launch_script.c_str(), 0x1ED);
146 return launch_script;