1 // Copyright (C) 2012-2013 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.
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::submitJob(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);
67 // export input files on cluster
68 exportInputFiles(job);
70 // build batch script for job
71 string scriptFile = buildBatchScript(job);
73 // Get REMOTE_COORM_PATH environment variable
74 const char * remote_coorm_path = getenv("REMOTE_COORM_PATH");
75 if (remote_coorm_path == NULL)
77 throw RunTimeException("Unable to get REMOTE_COORM_PATH environment variable");
80 // We need omniORB to execute launcher.py
81 const string set_env_cmd = "source " + string(remote_coorm_path) + "/coorm_prerequis.sh;";
84 // define command to submit batch
85 string subCommand = set_env_cmd + "python " + workDir + "/" + fileNameToLaunch + " --name="+ jobName +
86 " --workdir=" + workDir + " --outputs=" + workDir + "/logs/outputs.log" +
87 " --errors=" + workDir + "/logs/errors.log" +
88 " --executable=" + scriptFile + " " + launcherArgs;
89 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
95 int status = Utils::getCommandOutput(command, output);
97 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
99 // read id of submitted job in output
100 istringstream logfile(output);
101 string sline, idline, id;
105 while (getline(logfile, sline) && sline != "")
110 vector<string> tokens;
111 JobInfo::Tokenize(idline, tokens, "=");
116 throw RunTimeException("Error in the submission of the job on the remote host");
119 JobId jobid(this, (string) id);
123 // retire un job du gestionnaire
124 void BatchManager_COORM::deleteJob(const JobId & jobid)
126 // Get REMOTE_COORM_PATH environment variable
127 const char * remote_coorm_path = getenv("REMOTE_COORM_PATH");
128 if (remote_coorm_path == NULL)
130 throw RunTimeException("Unable to get REMOTE_COORM_PATH environment variable");
133 // We need omniORB to execute launcher.py
134 const string set_env_cmd = "source " + string(remote_coorm_path) + "/coorm_prerequis.sh;";
136 // define command to delete job
137 string subCommand = set_env_cmd + "python " + string(remote_coorm_path) + "/coormdel.py --jobID=" + jobid.getReference();
138 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
141 int status = system(command.c_str());
143 throw RunTimeException("Can't delete job " + jobid.getReference());
145 LOG("job " << jobid.getReference() << " killed");
148 // Renvoie l'etat du job
149 JobInfo BatchManager_COORM::queryJob(const JobId & jobid)
151 // Get REMOTE_COORM_PATH environment variable
152 const char * remote_coorm_path = getenv("REMOTE_COORM_PATH");
153 if (remote_coorm_path == NULL)
155 throw RunTimeException("Unable to get REMOTE_COORM_PATH environment variable");
158 // We need omniORB to execute launcher.py
159 const string set_env_cmd = "source " + string(remote_coorm_path) + "/coorm_prerequis.sh;";
161 // define command to query batch
162 string subCommand = set_env_cmd + "python " + string(remote_coorm_path) + "/coormstat.py --jobID=" + jobid.getReference();
163 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
166 int status = Utils::getCommandOutput(command, output);
167 if(status && status != 153 && status != 256*153)
168 throw RunTimeException("Error of connection on remote host");
170 JobInfo_COORM jobinfo = JobInfo_COORM(jobid.getReference(), output);
174 string BatchManager_COORM::buildBatchScript(const Job & job)
176 Parametre params = job.getParametre();
180 string fileToExecute = "";
187 // Mandatory parameters
188 if (params.find(WORKDIR) != params.end())
189 workDir = params[WORKDIR].str();
191 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
192 if (params.find(EXECUTABLE) != params.end())
193 fileToExecute = params[EXECUTABLE].str();
195 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
197 string::size_type p1 = fileToExecute.find_last_of("/");
198 string::size_type p2 = fileToExecute.find_last_of(".");
199 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
200 string fileNameToExecute = fileToExecute.substr(p1+1);
202 // Create batch submit file
203 ofstream tempOutputFile;
204 string tmpFileName = Utils::createAndOpenTemporaryFile("COORM-script", tempOutputFile);
206 tempOutputFile << "#!/bin/sh -f" << endl;
207 tempOutputFile << "export LIBBATCH_NODEFILE=$COORM_NODEFILE" << endl;
208 // Launch the executable
209 tempOutputFile << "cd " << tmpDir << endl;
210 tempOutputFile << "./" + fileNameToExecute << endl;
211 tempOutputFile.flush();
212 tempOutputFile.close();
214 Utils::chmod(tmpFileName.c_str(), 0x1ED);
215 LOG("Batch script file generated is: " << tmpFileName);
217 string remoteFileName = rootNameToExecute + "_Batch.sh";
218 int status = _protocol.copyFile(tmpFileName, "", "",
219 workDir + "/" + remoteFileName,
220 _hostname, _username);
222 throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
224 return remoteFileName;
227 void BatchManager_COORM::exportInputFiles(const Job & job)
229 BatchManager::exportInputFiles(job);
232 Parametre params = job.getParametre();
234 string launcherFile = params[LAUNCHER_FILE];
236 if (launcherFile.size() != 0)
238 // Copy launcherFile into batch working directory
239 status = _protocol.copyFile(launcherFile, "", "",
240 params[WORKDIR], _hostname, _username);
242 std::ostringstream oss;
243 oss << "Cannot copy file " << launcherFile << " on host " << _hostname;
244 oss << ". Return status is " << status;
245 throw RunTimeException(oss.str());
248 string remoteLauncher = launcherFile;
249 remoteLauncher = remoteLauncher.substr(remoteLauncher.rfind("/") + 1, remoteLauncher.length());
250 remoteLauncher = string(params[WORKDIR]) + "/" + remoteLauncher;
252 string subCommand = string("chmod u+x ") + remoteLauncher;
253 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
255 status = system(command.c_str());
257 std::ostringstream oss;
258 oss << "Cannot change permissions of file " << remoteLauncher << " on host " << _hostname;
259 oss << ". Return status is " << status;
260 throw RunTimeException(oss.str());