Salome HOME
Merge branch 'master' of https://codev-tuleap.cea.fr/plugins/git/salome/kernel
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SALOME_Launcher.hxx"
24 #include "BatchTest.hxx"
25 #include "OpUtil.hxx"
26 #include "SALOME_ContainerManager.hxx"
27 #include "SALOME_NamingService.hxx"
28 #include "SALOME_ResourcesManager.hxx"
29 #include "Utils_CorbaException.hxx"
30
31
32 #include "Launcher_Job_Command.hxx"
33 #include "Launcher_Job_YACSFile.hxx"
34 #include "Launcher_Job_PythonSALOME.hxx"
35 #include "Launcher_Job_CommandSALOME.hxx"
36
37 #include "utilities.h"
38
39 #ifdef WIN32
40 # include <process.h>
41 #else
42 # include <unistd.h>
43 #endif
44 #include <sys/types.h>
45 #include <vector>
46 #include <list>
47
48 #include <stdio.h>
49 #include <sstream>
50
51 using namespace std;
52
53 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
54
55 //=============================================================================
56 /*!
57  *  Constructor
58  *  \param orb
59  */
60 //=============================================================================
61 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
62 {
63   MESSAGE("SALOME_Launcher constructor");
64   _NS = new SALOME_NamingService(orb);
65   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
66   _l.SetResourcesManager(_ResManager->GetImpl());
67   _ContManager = new SALOME_ContainerManager(orb,poa,_NS);
68   _ResManager->_remove_ref();
69   _ContManager->_remove_ref();
70
71   _orb = CORBA::ORB::_duplicate(orb) ;
72   _poa = PortableServer::POA::_duplicate(poa) ;
73   PortableServer::ObjectId_var id = _poa->activate_object(this);
74   CORBA::Object_var obj = _poa->id_to_reference(id);
75   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
76
77   _NS->Register(refContMan,_LauncherNameInNS);
78   MESSAGE("SALOME_Launcher constructor end");
79 }
80
81 //=============================================================================
82 /*!
83  * destructor
84  */
85 //=============================================================================
86 SALOME_Launcher::~SALOME_Launcher()
87 {
88   MESSAGE("SALOME_Launcher destructor");
89   delete _NS;
90   MESSAGE("SALOME_Launcher destructor end");
91 }
92
93
94 CORBA::Long
95 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
96 {
97   JobParameters_cpp cpp_parameters = JobParameters_CORBA2CPP(job_parameters);
98   CORBA::Long jobNumber = -1;
99   try
100   {
101     jobNumber = _l.createJob(cpp_parameters);
102     std::ostringstream job_id;
103     job_id << jobNumber;
104     notifyObservers("NEW_JOB", job_id.str());
105   }
106   catch(const LauncherException &ex)
107   {
108     INFOS(ex.msg.c_str());
109     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
110   }
111   return jobNumber;
112 }
113
114 void
115 SALOME_Launcher::launchJob(CORBA::Long job_id)
116 {
117   try
118   {
119     _l.launchJob(job_id);
120   }
121   catch(const LauncherException &ex)
122   {
123     INFOS(ex.msg.c_str());
124     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
125   }
126 }
127
128 char *
129 SALOME_Launcher::getJobState(CORBA::Long job_id)
130 {
131   std::string result;
132   try
133   {
134     result = _l.getJobState(job_id);
135   }
136   catch(const LauncherException &ex)
137   {
138     INFOS(ex.msg.c_str());
139     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
140   }
141   return CORBA::string_dup(result.c_str());
142 }
143
144 // Get names or ids of hosts assigned to the job
145 char *
146 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
147 {
148   std::string result;
149   try
150   {
151     result = _l.getAssignedHostnames(job_id);
152   }
153   catch(const LauncherException &ex)
154   {
155     INFOS(ex.msg.c_str());
156     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
157   }
158   return CORBA::string_dup(result.c_str());
159 }
160
161 void
162 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
163 {
164   try
165   {
166     _l.getJobResults(job_id, directory);
167   }
168   catch(const LauncherException &ex)
169   {
170     INFOS(ex.msg.c_str());
171     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
172   }
173 }
174
175 void
176 SALOME_Launcher::clearJobWorkingDir(CORBA::Long job_id)
177 {
178   try
179   {
180     _l.clearJobWorkingDir(job_id);
181   }
182   catch(const LauncherException &ex)
183   {
184     INFOS(ex.msg.c_str());
185     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
186   }
187 }
188
189 CORBA::Boolean
190 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
191 {
192   CORBA::Boolean rtn = false;
193   try
194   {
195     rtn = _l.getJobDumpState(job_id, directory);
196   }
197   catch(const LauncherException &ex)
198   {
199     INFOS(ex.msg.c_str());
200     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
201   }
202   return rtn;
203 }
204
205 CORBA::Boolean
206 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
207 {
208   CORBA::Boolean rtn = false;
209   try
210   {
211     rtn = _l.getJobWorkFile(job_id, work_file, directory);
212   }
213   catch(const LauncherException &ex)
214   {
215     INFOS(ex.msg.c_str());
216     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
217   }
218   return rtn;
219 }
220
221 void
222 SALOME_Launcher::removeJob(CORBA::Long job_id)
223 {
224   try
225   {
226     _l.removeJob(job_id);
227     std::ostringstream job_id_str;
228     job_id_str << job_id;
229     notifyObservers("REMOVE_JOB", job_id_str.str());
230   }
231   catch(const LauncherException &ex)
232   {
233     INFOS(ex.msg.c_str());
234     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
235   }
236 }
237
238 void
239 SALOME_Launcher::stopJob(CORBA::Long job_id)
240 {
241   try
242   {
243     _l.stopJob(job_id);
244     std::ostringstream job_id_str;
245     job_id_str << job_id;
246     notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
247   }
248   catch(const LauncherException &ex)
249   {
250     INFOS(ex.msg.c_str());
251     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
252   }
253 }
254
255 char *
256 SALOME_Launcher::dumpJob(CORBA::Long job_id)
257 {
258   std::string result;
259   try
260   {
261     result = _l.dumpJob(job_id);
262   }
263   catch(const LauncherException &ex)
264   {
265     INFOS(ex.msg.c_str());
266     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
267   }
268   return CORBA::string_dup(result.c_str());
269 }
270
271 CORBA::Long
272 SALOME_Launcher::restoreJob(const char * dumpedJob)
273 {
274   CORBA::Long jobId;
275   try{
276     jobId = _l.restoreJob(dumpedJob);
277     if(jobId >= 0)
278     {
279       std::ostringstream job_str;
280       job_str << jobId;
281       notifyObservers("NEW_JOB", job_str.str());
282     }
283   }
284   catch(const LauncherException &ex){
285     INFOS(ex.msg.c_str());
286     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
287   }
288
289   return jobId;
290 }
291
292 //=============================================================================
293 /*! CORBA Method:
294  *  Create a job in the launcher with a file
295  *  \param xmlExecuteFile     : .xml to parse that contains job description
296  *  \param clusterName        : machine chosen
297  */
298 //=============================================================================
299 CORBA::Long
300 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
301                                    const char * clusterName)
302 {
303   CORBA::Long jobId;
304   try{
305     jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
306   }
307   catch(const LauncherException &ex){
308     INFOS(ex.msg.c_str());
309     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
310   }
311
312   return jobId;
313 }
314
315 //=============================================================================
316 /*! CORBA Method:
317  *  the test batch configuration
318  *  \param params             : The batch cluster
319  */
320 //=============================================================================
321 CORBA::Boolean
322 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
323 {
324   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
325   CORBA::Boolean rtn = false;
326   try
327   {
328     // Consider only resources that can run batch jobs
329     Engines::ResourceParameters new_params(params);
330     new_params.can_launch_batch_jobs = true;
331
332     // find a resource matching the required parameters
333     Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
334     if (aMachineList->length() == 0)
335       throw SALOME_Exception("No resources have been found with your parameters");
336
337     const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
338         std::string resource_name(p->name);
339     INFOS("Choose resource for test: " <<  resource_name);
340
341     BatchTest t(*p);
342     if (t.test())
343     {
344       rtn = true;
345     }
346   }
347   catch(const LauncherException &ex){
348     INFOS(ex.msg.c_str());
349     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
350   }
351   return rtn;
352 }
353
354 //=============================================================================
355 /*! CORBA method:
356  *  shutdown all the containers, then the ContainerManager servant
357  */
358 //=============================================================================
359 void SALOME_Launcher::Shutdown()
360 {
361   MESSAGE("Shutdown");
362   _NS->Destroy_Name(_LauncherNameInNS);
363   _ContManager->Shutdown();
364   _ResManager->Shutdown();
365   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
366   _poa->deactivate_object(oid);
367   if(!CORBA::is_nil(_orb))
368     _orb->shutdown(0);
369 }
370
371 //=============================================================================
372 /*! CORBA Method:
373  *  Returns the PID of the process
374  */
375 //=============================================================================
376 CORBA::Long SALOME_Launcher::getPID()
377 {
378   return
379 #ifndef WIN32
380     (CORBA::Long)getpid();
381 #else
382     (CORBA::Long)_getpid();
383 #endif
384 }
385
386 //=============================================================================
387 /*! CORBA Method:
388  *  Returns current launcher jobs list
389  */
390 //=============================================================================
391 Engines::JobsList *
392 SALOME_Launcher::getJobsList()
393 {
394   Engines::JobsList_var jobs_list = new Engines::JobsList();
395   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
396   std::map<int, Launcher::Job *>::const_iterator it_job;
397   int list_id = 0;
398   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
399   {
400     int number          = it_job->first;
401     try
402     {
403       // Prepare CORBA job description
404       Engines::JobDescription_var job_descr = new Engines::JobDescription();
405       Engines::JobParameters_var job_parameters = getJobParameters(number);
406       job_descr->job_id = number;
407       job_descr->job_parameters = job_parameters;
408
409       // Add job description to the sequence
410       jobs_list->length(list_id + 1);
411       jobs_list[list_id] = job_descr;
412       list_id++;
413     }
414     catch (...) {}
415   }
416   return jobs_list._retn();
417 }
418
419 //=============================================================================
420 /*! CORBA Method:
421  * Returns the job description
422  */
423 //=============================================================================
424 Engines::JobParameters *
425 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
426 {
427   Engines::JobParameters_var job_parameters;
428   try
429   {
430     JobParameters_cpp cpp_parameters = _l.getJobParameters(job_id);
431     job_parameters = JobParameters_CPP2CORBA(cpp_parameters);
432   }
433   catch(const LauncherException &ex)
434   {
435     INFOS(ex.msg.c_str());
436     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
437   }
438
439   return job_parameters._retn();
440 }
441
442 //=============================================================================
443 /*! CORBA Method:
444  *  Loads jobs saved in jobs_file
445  */
446 //=============================================================================
447 void
448 SALOME_Launcher::loadJobs(const char* jobs_file)
449 {
450   list<int> new_jobs_id_list;
451   try
452   {
453     // Load the jobs in Launcher
454     new_jobs_id_list = _l.loadJobs(jobs_file);
455   }
456   catch (const LauncherException & ex)
457   {
458     INFOS(ex.msg.c_str());
459     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
460   }
461
462   // Notify observers of the new jobs
463   list<int>::const_iterator it_jobs_id;
464   for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
465   {
466     ostringstream job_id_sstr;
467     job_id_sstr << *it_jobs_id;
468     notifyObservers("NEW_JOB", job_id_sstr.str());
469   }
470   notifyObservers("LOAD_JOBS", jobs_file);
471 }
472
473 //=============================================================================
474 /*! CORBA Method:
475  *  Save jobs of Launcher (in any steps) in file jobs_file
476  */
477 //=============================================================================
478 void
479 SALOME_Launcher::saveJobs(const char* jobs_file)
480 {
481   _l.saveJobs(jobs_file);
482   notifyObservers("SAVE_JOBS", jobs_file);
483 }
484
485 //=============================================================================
486 /*! CORBA Method:
487  *  Add a new observer to the launcher
488  */
489 //=============================================================================
490 void
491 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
492 {
493   bool new_observer = true;
494   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
495   while(iter != _observers.end())
496   {
497     if (std::string(_orb->object_to_string(*iter)) ==
498         std::string(_orb->object_to_string(observer)))
499     {
500       new_observer = false;
501       break;
502     }
503     iter++;
504   }
505   if (new_observer)
506     _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
507
508   // We notify the new observer with all jobs that are currently in the Launcher
509   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
510   std::map<int, Launcher::Job *>::const_iterator it_job;
511   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
512   {
513     int number = it_job->first;
514     std::ostringstream job_id;
515     job_id << number;
516     try
517     {
518       observer->notify("NEW_JOB", job_id.str().c_str());
519     }
520     catch (...)
521     {
522        MESSAGE("Notify Observer, exception catch");
523     }
524
525   }
526 }
527
528 //=============================================================================
529 /*! CORBA Method:
530  *  Add a new observer to the launcher
531  */
532 //=============================================================================
533 void
534 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
535 {
536   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
537   while(iter != _observers.end())
538   {
539     if (std::string(_orb->object_to_string(*iter)) ==
540         std::string(_orb->object_to_string(observer)))
541     {
542       // Observer found
543       iter =_observers.erase(iter++);
544     }
545     else
546     {
547       iter++;
548     }
549   }
550 }
551
552 //=============================================================================
553 /*! Internal Method:
554  *  Notify observers on a new event
555  */
556 //=============================================================================
557 void
558 SALOME_Launcher::notifyObservers(const std::string & event_name,
559                                  const std::string & event_data)
560 {
561   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
562   while(iter != _observers.end())
563   {
564     try
565     {
566       (*iter)->notify(CORBA::string_dup(event_name.c_str()),
567                       CORBA::string_dup(event_data.c_str()));
568     }
569     catch (...)
570     {
571        MESSAGE("Notify Observer, exception catch");
572     }
573     iter++;
574   }
575
576 }
577
578 JobParameters_cpp
579 SALOME_Launcher::JobParameters_CORBA2CPP(
580                                    const Engines::JobParameters& job_parameters)
581 {
582   JobParameters_cpp result;
583
584   result.job_name = job_parameters.job_name.in();
585   result.job_type = job_parameters.job_type.in();
586   result.job_file = job_parameters.job_file.in();
587   result.pre_command = job_parameters.pre_command.in();
588   result.env_file = job_parameters.env_file.in();
589
590   result.in_files.clear();
591   for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
592     result.in_files.push_back(job_parameters.in_files[i].in());
593   result.out_files.clear();
594   for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
595     result.out_files.push_back(job_parameters.out_files[i].in());
596
597   result.work_directory = job_parameters.work_directory.in();
598   result.local_directory = job_parameters.local_directory.in();
599   result.result_directory = job_parameters.result_directory.in();
600   result.maximum_duration = job_parameters.maximum_duration.in();
601
602   result.resource_required = resourceParameters_CORBAtoCPP(job_parameters.resource_required);
603
604   result.queue = job_parameters.queue.in();
605   result.partition = job_parameters.partition.in();
606   result.exclusive = job_parameters.exclusive;
607   result.mem_per_cpu = job_parameters.mem_per_cpu;
608   result.wckey = job_parameters.wckey.in();
609   result.extra_params = job_parameters.extra_params.in();
610
611   result.specific_parameters.clear();
612   for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
613     result.specific_parameters[job_parameters.specific_parameters[i].name.in()]
614                              = job_parameters.specific_parameters[i].value.in();
615
616   result.launcher_file = job_parameters.launcher_file.in();
617   result.launcher_args = job_parameters.launcher_args.in();
618   return result;
619 }
620
621 Engines::JobParameters_var
622 SALOME_Launcher::JobParameters_CPP2CORBA(const JobParameters_cpp& job_parameters)
623 {
624   Engines::JobParameters_var result = new Engines::JobParameters;
625   result->job_name = CORBA::string_dup(job_parameters.job_name.c_str());
626   result->job_type = CORBA::string_dup(job_parameters.job_type.c_str());
627   result->job_file = CORBA::string_dup(job_parameters.job_file.c_str());
628   result->pre_command = CORBA::string_dup(job_parameters.pre_command.c_str());
629   result->env_file = CORBA::string_dup(job_parameters.env_file.c_str());
630   result->in_files.length(job_parameters.in_files.size());
631
632   int i = 0;
633   for(const std::string& it : job_parameters.in_files)
634   {
635     result->in_files[i] = CORBA::string_dup(it.c_str());
636     i++;
637   }
638   result->out_files.length(job_parameters.out_files.size());
639   i = 0;
640   for(const std::string& it : job_parameters.out_files)
641   {
642     result->out_files[i] = CORBA::string_dup(it.c_str());
643     i++;
644   }
645
646   result->work_directory = CORBA::string_dup(job_parameters.work_directory.c_str());
647   result->local_directory = CORBA::string_dup(job_parameters.local_directory.c_str());
648   result->result_directory = CORBA::string_dup(job_parameters.result_directory.c_str());
649   result->maximum_duration = CORBA::string_dup(job_parameters.maximum_duration.c_str());
650
651   result->resource_required = resourceParameters_CPPtoCORBA(job_parameters.resource_required);
652
653   result->queue = CORBA::string_dup(job_parameters.queue.c_str());
654   result->partition = CORBA::string_dup(job_parameters.partition.c_str());
655   result->exclusive = job_parameters.exclusive;
656   result->mem_per_cpu = job_parameters.mem_per_cpu;
657   result->wckey = CORBA::string_dup(job_parameters.wckey.c_str());
658   result->extra_params = CORBA::string_dup(job_parameters.extra_params.c_str());
659
660   const std::map<std::string, std::string>& specific_parameters
661                                        = job_parameters.specific_parameters;
662   if (!specific_parameters.empty())
663   {
664     result->specific_parameters.length(specific_parameters.size());
665     CORBA::ULong i = 0;
666     for (const auto& it_specific : specific_parameters)
667     {
668       Engines::Parameter_var new_param = new Engines::Parameter;
669       new_param->name  = CORBA::string_dup(it_specific.first.c_str());
670       new_param->value = CORBA::string_dup(it_specific.second.c_str());
671       result->specific_parameters[i] = new_param;
672       i++;
673     }
674   }
675
676   result->launcher_file = CORBA::string_dup(job_parameters.launcher_file.c_str());
677   result->launcher_args = CORBA::string_dup(job_parameters.launcher_args.c_str());
678   return result;
679 }