Salome HOME
Copyright update 2020
[modules/kernel.git] / src / Launcher / Launcher_Job_SALOME.cxx
index 17e7e1766056bc5a68cc16a6a7d42fb2cdf4e5f2..47ec6f19acede6d9641b1e40bd7fd626c699cabe 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2017  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2009-2020  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -29,7 +29,9 @@
 #ifdef WIN32
 #include <io.h>
 #define _chmod chmod
+#include <process.h>
 #endif
+#include <sys/stat.h>
 
 #include <sstream>
 
@@ -66,8 +68,16 @@ Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
   // parameters
   std::string work_directory = params[Batch::WORKDIR].str();
   std::ostringstream str_pid;
+#ifdef WIN32
+  str_pid << _getpid();
+#else
   str_pid << ::getpid();
-  std::string launch_script = Kernel_Utils::GetTmpDir() + "runSalome_" + _job_file_name + "_" + _launch_date + "-" + str_pid.str() + ".sh";
+#endif
+  struct stat statbuf;
+  // true if APPLI holds a salome file launcher (in state of an application directory)
+  bool is_launcher_file=stat(getenv("APPLI"), &statbuf) ==0 &&  S_ISREG(statbuf.st_mode);
+  std::string launch_tmp_dir = Kernel_Utils::GetTmpDir();
+  std::string launch_script = launch_tmp_dir + "runSalome_" + _job_file_name + "_" + _launch_date + "-" + str_pid.str() + ".sh";
   std::ofstream launch_script_stream;
   launch_script_stream.open(launch_script.c_str(),
                             std::ofstream::out
@@ -92,38 +102,48 @@ Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
   launch_script_stream << "export SALOME_TMP_DIR=" << work_directory << "/logs" << std::endl;
 
   // -- Generates Catalog Resources
-  std::string resource_protocol = _resource_definition.getClusterInternalProtocolStr();
-  launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
-  launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
-  launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
-  launch_script_stream << "{" << std::endl;
-  launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'" << std::endl;
-  launch_script_stream << "echo '<resources>'" << std::endl;
-  launch_script_stream << "sort \"$LIBBATCH_NODEFILE\" | uniq -c | while read nbproc host"  << std::endl;
-  launch_script_stream << "do"                                                  << std::endl;
-  // Full name doesn't work. eg: sagittaire-7 instead of sagittaire-7.lyon.grid5000.fr
-  launch_script_stream << "host_basename=$(echo \"$host\" | cut -f1 -d.)"                                                  << std::endl;
-  launch_script_stream << "echo '<machine name='\\\"\"$host_basename\"\\\"" << std::endl;
-  launch_script_stream << "echo '         hostname='\\\"\"$host_basename\"\\\"" << std::endl;
-  launch_script_stream << "echo '         type=\"single_machine\"'" << std::endl;
-  launch_script_stream << "echo '         protocol=\"" << resource_protocol               << "\"'" << std::endl;
-  launch_script_stream << "echo '         userName=\"" << _resource_definition.UserName   << "\"'" << std::endl;
-  launch_script_stream << "echo '         appliPath=\"" << _resource_definition.AppliPath << "\"'" << std::endl;
-  launch_script_stream << "echo '         mpi=\"" << _resource_definition.getMpiImplTypeStr() << "\"'" << std::endl;
-  launch_script_stream << "echo '         nbOfNodes='\\\"\"$nbproc\"\\\"" << std::endl;
-  launch_script_stream << "echo '         nbOfProcPerNode=\"1\"'" << std::endl;
-  launch_script_stream << "echo '         canRunContainers=\"true\"'" << std::endl;
-  launch_script_stream << "echo '/>'" << std::endl;
-  launch_script_stream << "done"                                 << std::endl;
-  launch_script_stream << "echo '</resources>'" << std::endl;
-  launch_script_stream << "} > $CATALOG_FILE" << std::endl;
-  launch_script_stream << "fi" << std::endl;
-
+  // do not build a new catalog if the job is launched on localhost
+  if(_resource_required_params.name != "localhost")
+  {
+    std::string resource_protocol = _resource_definition.getClusterInternalProtocolStr();
+    launch_script_stream << "if [ \"x$LIBBATCH_NODEFILE\" != \"x\" ]; then " << std::endl;
+    launch_script_stream << "CATALOG_FILE=" << "CatalogResources_" << _launch_date << ".xml" << std::endl;
+    launch_script_stream << "export USER_CATALOG_RESOURCES_FILE=" << "$CATALOG_FILE" << std::endl;
+    launch_script_stream << "{" << std::endl;
+    launch_script_stream << "echo '<!DOCTYPE ResourcesCatalog>'" << std::endl;
+    launch_script_stream << "echo '<resources>'" << std::endl;
+    launch_script_stream << "sort \"$LIBBATCH_NODEFILE\" | uniq -c | while read nbproc host"  << std::endl;
+    launch_script_stream << "do"                                                  << std::endl;
+    // Full name doesn't work. eg: sagittaire-7 instead of sagittaire-7.lyon.grid5000.fr
+    launch_script_stream << "host_basename=$(echo \"$host\" | cut -f1 -d.)"   << std::endl;
+    launch_script_stream << "echo '<machine name='\\\"\"$host_basename\"\\\"" << std::endl;
+    launch_script_stream << "echo '         hostname='\\\"\"$host_basename\"\\\"" << std::endl;
+    launch_script_stream << "echo '         type=\"single_machine\"'" << std::endl;
+    launch_script_stream << "echo '         protocol=\"" << resource_protocol               << "\"'" << std::endl;
+    launch_script_stream << "echo '         userName=\"" << _resource_definition.UserName   << "\"'" << std::endl;
+    launch_script_stream << "echo '         appliPath=\"" << _resource_definition.AppliPath << "\"'" << std::endl;
+    launch_script_stream << "echo '         mpi=\"" << _resource_definition.getMpiImplTypeStr() << "\"'" << std::endl;
+    launch_script_stream << "echo '         nbOfNodes='\\\"\"$nbproc\"\\\"" << std::endl;
+    launch_script_stream << "echo '         nbOfProcPerNode=\"1\"'" << std::endl;
+    launch_script_stream << "echo '         canRunContainers=\"true\"'" << std::endl;
+    launch_script_stream << "echo '/>'" << std::endl;
+    launch_script_stream << "done"                                 << std::endl;
+    launch_script_stream << "echo '</resources>'" << std::endl;
+    launch_script_stream << "} > $CATALOG_FILE" << std::endl;
+    launch_script_stream << "fi" << std::endl;
+  }
   // Create file for ns-port-log
-  launch_script_stream << "NS_PORT_FILE_PATH=$(mktemp " << _resource_definition.AppliPath << "/USERS/nsport_XXXXXX) &&\n";
+  if (is_launcher_file)
+      // for a salome application file, we write NS_PORT_FILE_PATH in launch_tmp_dir
+      launch_script_stream << "NS_PORT_FILE_PATH=$(mktemp " << launch_tmp_dir << "nsport_XXXXXX) &&\n";
+  else
+      launch_script_stream << "NS_PORT_FILE_PATH=$(mktemp " << _resource_definition.AppliPath << "/USERS/nsport_XXXXXX) &&\n";
 
   // Launch SALOME with an appli
-  launch_script_stream << _resource_definition.AppliPath << "/salome start --terminal --ns-port-log=\"$NS_PORT_FILE_PATH\" --server-launch-mode=fork ";
+  if (is_launcher_file)
+      launch_script_stream << _resource_definition.AppliPath << " start --terminal --ns-port-log=\"$NS_PORT_FILE_PATH\" --server-launch-mode=fork ";
+  else
+      launch_script_stream << _resource_definition.AppliPath << "/salome start --terminal --ns-port-log=\"$NS_PORT_FILE_PATH\" --server-launch-mode=fork ";
   launch_script_stream << "> logs/salome_" << _launch_date << ".log 2>&1 &&" << std::endl;
   launch_script_stream << "current=0 &&\n"
                        << "stop=20 &&\n"
@@ -145,7 +165,10 @@ Launcher::Job_SALOME::buildSalomeScript(Batch::Parametre params)
   launch_script_stream << "echo $? > logs/exit_code.log" << std::endl;
 
   // End
-  launch_script_stream << _resource_definition.AppliPath << "/salome kill \"$appli_port\"" << std::endl;
+  if (is_launcher_file)
+      launch_script_stream << _resource_definition.AppliPath << " kill \"$appli_port\"" << std::endl;
+  else
+      launch_script_stream << _resource_definition.AppliPath << "/salome kill \"$appli_port\"" << std::endl;
 
   // Return
   launch_script_stream.flush();