1 // Copyright (C) 2012-2020 INRIA
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
23 #include <Constants.hxx>
27 #include "BatchManager_COORM.hxx"
28 #include "JobInfo_COORM.hxx"
34 BatchManager_COORM::BatchManager_COORM(const FactBatchManager * parent, const char * host,
35 const char * username,
36 CommunicationProtocolType protocolType, const char * mpiImpl)
37 : BatchManager(parent, host, username, protocolType, mpiImpl)
41 BatchManager_COORM::~BatchManager_COORM()
45 // Soumet un job au gestionnaire
46 const JobId BatchManager_COORM::runJob(const Job & job)
48 Parametre params = job.getParametre();
49 const string workDir = params[WORKDIR];
50 const string fileToExecute = params[EXECUTABLE];
53 const string launcherArgs = params[LAUNCHER_ARGS];
54 const string launcherFile = params[LAUNCHER_FILE];
56 const string jobName = params[NAME];
58 string::size_type p1 = fileToExecute.find_last_of("/");
59 string::size_type p2 = fileToExecute.find_last_of(".");
60 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
63 p1 = launcherFile.find_last_of("/");
64 std::string fileNameToLaunch = launcherFile.substr(p1+1);
66 // build batch script for job
67 string scriptFile = buildBatchScript(job);
69 // Get REMOTE_COORM_PATH environment variable
70 const char * remote_coorm_path = getenv("REMOTE_COORM_PATH");
71 if (remote_coorm_path == NULL)
73 throw RunTimeException("Unable to get REMOTE_COORM_PATH environment variable");
76 // We need omniORB to execute launcher.py
77 const string set_env_cmd = "source " + string(remote_coorm_path) + "/coorm_prerequis.sh;";
80 // define command to submit batch
81 string subCommand = set_env_cmd + "python " + workDir + "/" + fileNameToLaunch + " --name="+ jobName +
82 " --workdir=" + workDir + " --outputs=" + workDir + "/logs/outputs.log" +
83 " --errors=" + workDir + "/logs/errors.log" +
84 " --executable=" + scriptFile + " " + launcherArgs;
85 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
91 int status = Utils::getCommandOutput(command, output);
93 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
95 // read id of submitted job in output
96 istringstream logfile(output);
97 string sline, idline, id;
101 while (getline(logfile, sline) && sline != "")
106 vector<string> tokens;
107 JobInfo::Tokenize(idline, tokens, "=");
112 throw RunTimeException("Error in the submission of the job on the remote host");
115 JobId jobid(this, (string) id);
119 // retire un job du gestionnaire
120 void BatchManager_COORM::deleteJob(const JobId & jobid)
122 // Get REMOTE_COORM_PATH environment variable
123 const char * remote_coorm_path = getenv("REMOTE_COORM_PATH");
124 if (remote_coorm_path == NULL)
126 throw RunTimeException("Unable to get REMOTE_COORM_PATH environment variable");
129 // We need omniORB to execute launcher.py
130 const string set_env_cmd = "source " + string(remote_coorm_path) + "/coorm_prerequis.sh;";
132 // define command to delete job
133 string subCommand = set_env_cmd + "python " + string(remote_coorm_path) + "/coormdel.py --jobID=" + jobid.getReference();
134 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
137 int status = system(command.c_str());
139 throw RunTimeException("Can't delete job " + jobid.getReference());
141 LOG("job " << jobid.getReference() << " killed");
144 // Renvoie l'etat du job
145 JobInfo BatchManager_COORM::queryJob(const JobId & jobid)
147 // Get REMOTE_COORM_PATH environment variable
148 const char * remote_coorm_path = getenv("REMOTE_COORM_PATH");
149 if (remote_coorm_path == NULL)
151 throw RunTimeException("Unable to get REMOTE_COORM_PATH environment variable");
154 // We need omniORB to execute launcher.py
155 const string set_env_cmd = "source " + string(remote_coorm_path) + "/coorm_prerequis.sh;";
157 // define command to query batch
158 string subCommand = set_env_cmd + "python " + string(remote_coorm_path) + "/coormstat.py --jobID=" + jobid.getReference();
159 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
162 int status = Utils::getCommandOutput(command, output);
163 if(status && status != 153 && status != 256*153)
164 throw RunTimeException("Error of connection on remote host");
166 JobInfo_COORM jobinfo = JobInfo_COORM(jobid.getReference(), output);
170 string BatchManager_COORM::buildBatchScript(const Job & job)
172 Parametre params = job.getParametre();
176 string fileToExecute = "";
182 LIBBATCH_UNUSED(nbproc);
183 LIBBATCH_UNUSED(edt);
184 LIBBATCH_UNUSED(mem);
186 // Mandatory parameters
187 if (params.find(WORKDIR) != params.end())
188 workDir = params[WORKDIR].str();
190 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
191 if (params.find(EXECUTABLE) != params.end())
192 fileToExecute = params[EXECUTABLE].str();
194 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
196 string::size_type p1 = fileToExecute.find_last_of("/");
197 string::size_type p2 = fileToExecute.find_last_of(".");
198 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
199 string fileNameToExecute = fileToExecute.substr(p1+1);
201 // Create batch submit file
202 ofstream tempOutputFile;
203 string tmpFileName = Utils::createAndOpenTemporaryFile("COORM-script", tempOutputFile);
205 tempOutputFile << "#!/bin/sh -f" << endl;
206 tempOutputFile << "export LIBBATCH_NODEFILE=$COORM_NODEFILE" << endl;
207 // Launch the executable
208 tempOutputFile << "cd " << tmpDir << endl;
209 tempOutputFile << "./" + fileNameToExecute << endl;
210 tempOutputFile.flush();
211 tempOutputFile.close();
213 Utils::chmod(tmpFileName.c_str(), 0x1ED);
214 LOG("Batch script file generated is: " << tmpFileName);
216 string remoteFileName = rootNameToExecute + "_Batch.sh";
217 int status = _protocol.copyFile(tmpFileName, "", "",
218 workDir + "/" + remoteFileName,
219 _hostname, _username);
221 throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
223 return remoteFileName;
226 void BatchManager_COORM::exportInputFiles(const Job & job)
228 BatchManager::exportInputFiles(job);
231 Parametre params = job.getParametre();
233 string launcherFile = params[LAUNCHER_FILE];
235 if (launcherFile.size() != 0)
237 // Copy launcherFile into batch working directory
238 status = _protocol.copyFile(launcherFile, "", "",
239 params[WORKDIR], _hostname, _username);
241 std::ostringstream oss;
242 oss << "Cannot copy file " << launcherFile << " on host " << _hostname;
243 oss << ". Return status is " << status;
244 throw RunTimeException(oss.str());
247 string remoteLauncher = launcherFile;
248 remoteLauncher = remoteLauncher.substr(remoteLauncher.rfind("/") + 1, remoteLauncher.length());
249 remoteLauncher = string(params[WORKDIR]) + "/" + remoteLauncher;
251 string subCommand = string("chmod u+x ") + remoteLauncher;
252 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
254 status = system(command.c_str());
256 std::ostringstream oss;
257 oss << "Cannot change permissions of file " << remoteLauncher << " on host " << _hostname;
258 oss << ". Return status is " << status;
259 throw RunTimeException(oss.str());