Salome HOME
Moved username initialization to the constructor in BatchManager_eClients
[tools/libbatch.git] / src / LSF / Batch_BatchManager_eLSF.cxx
index e48690c47da107c228e7dab07eab8d198ed59d2f..5b992b8b4bf5bebbe82440ca74d9f51cf7b382f2 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
  *
  */
 
+#include <stdlib.h>
+#include <string.h>
+
 #include <iostream>
 #include <fstream>
 #include <sstream>
+#include <string>
 #include <sys/stat.h>
-#include <string.h>
+
 #include <stdlib.h>
+#include <string.h>
 
-#include "Batch_BatchManager_eLSF.hxx"
 #ifdef WIN32
-# include <time.h>
-# include <io.h>
+#include <io.h>
 #else
-# include <libgen.h>
+#include <libgen.h>
 #endif
 
+#include "Batch_BatchManager_eLSF.hxx"
+#include "Batch_JobInfo_eLSF.hxx"
+
 using namespace std;
 
 namespace Batch {
 
-  BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host, const char * protocol, const char * mpiImpl) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_eClient(parent,host,protocol,mpiImpl)
+  BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host,
+                                       const char * username,
+                                       CommunicationProtocolType protocolType, const char * mpiImpl)
+  : BatchManager_eClient(parent, host, username, protocolType, mpiImpl),
+    BatchManager(parent, host)
   {
     // Nothing to do
   }
@@ -64,56 +74,42 @@ namespace Batch {
   {
     int status;
     Parametre params = job.getParametre();
-    const std::string dirForTmpFiles = params[TMPDIR];
+    const std::string workDir = params[WORKDIR];
     const string fileToExecute = params[EXECUTABLE];
-    std::string fileNameToExecute;
-    if( fileToExecute.size() > 0 ){
-      string::size_type p1 = fileToExecute.find_last_of("/");
-      string::size_type p2 = fileToExecute.find_last_of(".");
-      fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
-    }
-    else
-      fileNameToExecute = "command";
+    string::size_type p1 = fileToExecute.find_last_of("/");
+    string::size_type p2 = fileToExecute.find_last_of(".");
+    std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
 
     // export input files on cluster
+    cerr << "Export des fichiers en entree" << endl;
     exportInputFiles(job);
 
     // build batch script for job
+    cerr << "Construction du script de batch" << endl;
     buildBatchScript(job);
+    cerr << "Script envoye" << endl;
 
-    // define name of log file
-    string logFile="/tmp/logs/";
-    logFile += getenv("USER");
-    logFile += "/batchSalome_";
-    srand ( time(NULL) );
-    int ir = rand();
-    ostringstream oss;
-    oss << ir;
-    logFile += oss.str();
-    logFile += ".log";
-
-    string command;
+    // define name of log file (local)
+    string logFile = generateTemporaryFileName("LSF-submitlog");
 
     // define command to submit batch
-    command = _protocol;
-    command += " ";
-
-    if(_username != ""){
-      command += _username;
-      command += "@";
-    }
-
-    command += _hostname;
-    command += " \"cd " ;
-    command += dirForTmpFiles ;
-    command += "; bsub < " ;
-    command += fileNameToExecute ;
-    command += "_Batch.sh\" > ";
+    string subCommand = string("cd ") + workDir + "; bsub < " + fileNameToExecute + "_Batch.sh";
+    string command = _protocol.getExecCommand(subCommand, _hostname, _username);
+    command += " > ";
     command += logFile;
+    command += " 2>&1";
     cerr << command.c_str() << endl;
     status = system(command.c_str());
     if(status)
-      throw EmulationException("Error of connection on remote host");
+    {
+      ifstream error_message(logFile.c_str());
+      std::string mess;
+      std::string temp;
+      while(std::getline(error_message, temp))
+       mess += temp;
+      error_message.close();
+      throw EmulationException("Error of connection on remote host, error was: " + mess);
+    }
 
     // read id of submitted job in log file
     char line[128];
@@ -130,6 +126,13 @@ namespace Batch {
     return id;
   }
 
+  // Ce manager permet de faire de la reprise
+  const Batch::JobId
+  BatchManager_eLSF::addJob(const Batch::Job & job, const std::string reference)
+  {
+    return JobId(this, reference);
+  }
+
   // Methode pour le controle des jobs : retire un job du gestionnaire
   void BatchManager_eLSF::deleteJob(const JobId & jobid)
   {
@@ -138,23 +141,12 @@ namespace Batch {
     istringstream iss(jobid.getReference());
     iss >> ref;
 
-    // define command to submit batch
-    string command;
-    command = _protocol;
-    command += " ";
-
-    if (_username != ""){
-      command += _username;
-      command += "@";
-    }
-
-    command += _hostname;
-    command += " \"bkill " ;
-    command += iss.str();
-    command += "\"";
+    // define command to delete batch
+    string subCommand = string("bkill ") + iss.str();
+    string command = _protocol.getExecCommand(subCommand, _hostname, _username);
     cerr << command.c_str() << endl;
     status = system(command.c_str());
-    if(status)
+    if (status)
       throw EmulationException("Error of connection on remote host");
 
     cerr << "jobId = " << ref << "killed" << endl;
@@ -198,38 +190,17 @@ namespace Batch {
     istringstream iss(jobid.getReference());
     iss >> id;
 
-    // define name of log file
-    string logFile="/tmp/logs/";
-    logFile += getenv("USER");
-    logFile += "/batchSalome_";
-
-    srand ( time(NULL) );
-    int ir = rand();
-    ostringstream oss;
-    oss << ir;
-    logFile += oss.str();
-    logFile += ".log";
-
-    string command;
-    int status;
-
-    // define command to submit batch
-    command = _protocol;
-    command += " ";
-
-    if (_username != ""){
-      command += _username;
-      command += "@";
-    }
+    // define name of log file (local)
+    string logFile = generateTemporaryFileName(string("LSF-querylog-id") + jobid.getReference());
 
-    command += _hostname;
-    command += " \"bjobs " ;
-    command += iss.str();
-    command += "\" > ";
+    // define command to query batch
+    string subCommand = string("bjobs ") + iss.str();
+    string command = _protocol.getExecCommand(subCommand, _hostname, _username);
+    command += " > ";
     command += logFile;
     cerr << command.c_str() << endl;
-    status = system(command.c_str());
-    if(status)
+    int status = system(command.c_str());
+    if (status)
       throw EmulationException("Error of connection on remote host");
 
     JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
@@ -247,37 +218,44 @@ namespace Batch {
   void BatchManager_eLSF::buildBatchScript(const Job & job)
   {
 #ifndef WIN32 //TODO: need for porting on Windows
-    int status;
     Parametre params = job.getParametre();
-    Environnement env = job.getEnvironnement();
-    const int nbproc = params[NBPROC];
-    const long edt = params[MAXWALLTIME];
-    const long mem = params[MAXRAMSIZE];
-    const string workDir = params[WORKDIR];
-    const std::string dirForTmpFiles = params[TMPDIR];
-    const string fileToExecute = params[EXECUTABLE];
-    const string home = params[HOMEDIR];
-    const std::string queue = params[QUEUE];
-    std::string rootNameToExecute;
-    std::string fileNameToExecute;
-    std::string filelogtemp;
-    if( fileToExecute.size() > 0 ){
-      string::size_type p1 = fileToExecute.find_last_of("/");
-      string::size_type p2 = fileToExecute.find_last_of(".");
-      rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
-      char* basec=strdup(fileToExecute.c_str());
-      fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
-      free(basec);
-
-      int idx = dirForTmpFiles.find("Batch/");
-      filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
-    }
-    else{
-      rootNameToExecute = "command";
-    }
 
+    // Job Parameters
+    string workDir       = "";
+    string fileToExecute = "";
+    int nbproc          = 0;
+    int edt             = 0;
+    int mem              = 0;
+    string queue         = "";
+
+    // Mandatory parameters
+    if (params.find(WORKDIR) != params.end()) 
+      workDir = params[WORKDIR].str();
+    else 
+      throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
+    if (params.find(EXECUTABLE) != params.end()) 
+      fileToExecute = params[EXECUTABLE].str();
+    else 
+      throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
+
+    // Optional parameters
+    if (params.find(NBPROC) != params.end()) 
+      nbproc = params[NBPROC];
+    if (params.find(MAXWALLTIME) != params.end()) 
+      edt = params[MAXWALLTIME];
+    if (params.find(MAXRAMSIZE) != params.end()) 
+      mem = params[MAXRAMSIZE];
+    if (params.find(QUEUE) != params.end()) 
+      queue = params[QUEUE].str();
+
+    string::size_type p1 = fileToExecute.find_last_of("/");
+    string::size_type p2 = fileToExecute.find_last_of(".");
+    string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
+    string fileNameToExecute = fileToExecute.substr(p1+1);
+    // Create batch submit file
     ofstream tempOutputFile;
-    std::string TmpFileName = createAndOpenTemporaryFile(tempOutputFile);
+    std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
 
     tempOutputFile << "#! /bin/sh -f" << endl ;
     if (queue != "")
@@ -287,61 +265,58 @@ namespace Batch {
     if( mem > 0 )
       tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
     tempOutputFile << "#BSUB -n " << nbproc << endl ;
-    if( fileToExecute.size() > 0 ){
-      tempOutputFile << "#BSUB -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
-      tempOutputFile << "#BSUB -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
-    }
-    else{
-      tempOutputFile << "#BSUB -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
-      tempOutputFile << "#BSUB -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
-    }
-    if( workDir.size() > 0 )
-      tempOutputFile << "cd " << workDir << endl ;
-    if( fileToExecute.size() > 0 ){
-      tempOutputFile << _mpiImpl->boot("",nbproc);
-      tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
-      tempOutputFile << _mpiImpl->halt();
-    }
+    size_t pos = workDir.find("$HOME");
+    string baseDir;
+    if( pos != string::npos )
+      baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
     else{
-      tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
-      tempOutputFile << env["COMMAND"];
+      pos = workDir.find("~");
+      if( pos != string::npos )
+       baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
+      else
+       baseDir = workDir;
     }
+    tempOutputFile << "#BSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
+    tempOutputFile << "#BSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
+
+    tempOutputFile << "cd " << workDir << endl ;
+
+    // generate nodes file
+    tempOutputFile << "NODEFILE=`mktemp nodefile-XXXXXXXXXX` || exit 1" << endl;
+    tempOutputFile << "bool=0" << endl;
+    tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
+    tempOutputFile << "  if test $bool = 0; then" << endl;
+    tempOutputFile << "    n=$i" << endl;
+    tempOutputFile << "    bool=1" << endl;
+    tempOutputFile << "  else" << endl;
+    tempOutputFile << "    for ((j=0;j<$i;j++)); do" << endl;
+    tempOutputFile << "      echo $n >> $NODEFILE" << endl;
+    tempOutputFile << "    done" << endl;
+    tempOutputFile << "    bool=0" << endl;
+    tempOutputFile << "  fi" << endl;
+    tempOutputFile << "done" << endl;
+
+    // Abstraction of PBS_NODEFILE - TODO
+    tempOutputFile << "export LIBBATCH_NODEFILE=$NODEFILE" << endl;
+
+    // Launch the executable
+    tempOutputFile << "./" + fileNameToExecute << endl;
+
+    // Remove the node file
+    tempOutputFile << "rm $NODEFILE" << endl;
 
     tempOutputFile.flush();
     tempOutputFile.close();
-#ifdef WIN32
-    _chmod(
-#else
-    chmod(
-#endif
-      TmpFileName.c_str(), 0x1ED);
-    cerr << TmpFileName.c_str() << endl;
-
-    string command;
-    if( _protocol == "rsh" )
-      command = "rcp ";
-    else if( _protocol == "ssh" )
-      command = "scp ";
-    else
-      throw EmulationException("Unknown protocol");
-    command += TmpFileName;
-    command += " ";
-    if(_username != ""){
-      command +=  _username;
-      command += "@";
-    }
-    command += _hostname;
-    command += ":";
-    command += dirForTmpFiles ;
-    command += "/" ;
-    command += rootNameToExecute ;
-    command += "_Batch.sh" ;
-    cerr << command.c_str() << endl;
-    status = system(command.c_str());
-    if(status)
+
+    BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
+    cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
+
+    int status = _protocol.copyFile(TmpFileName, "", "",
+                                    workDir + "/" + rootNameToExecute + "_Batch.sh",
+                                    _hostname, _username);
+    if (status)
       throw EmulationException("Error of connection on remote host");
 
-    remove(TmpFileName.c_str());
 #endif
 
   }
@@ -359,4 +334,24 @@ namespace Batch {
     return oss.str();
   }
 
+  std::string BatchManager_eLSF::getHomeDir(std::string tmpdir)
+  {
+    std::string home;
+    int idx = tmpdir.find("Batch/");
+    std::string filelogtemp = tmpdir.substr(idx+6, tmpdir.length());
+    filelogtemp = "/tmp/logs" + filelogtemp + "_home";
+
+    string subCommand = string("echo $HOME");
+    string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
+    cerr << command.c_str() << endl;
+    int status = system(command.c_str());
+    if (status)
+      throw EmulationException("Error of launching home command on remote host");
+
+    std::ifstream file_home(filelogtemp.c_str());
+    std::getline(file_home, home);
+    file_home.close();
+    return home;
+  }
+
 }