Salome HOME
Add exportInputFiles to SALOME_Launcher
[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::exportInputFiles(CORBA::Long job_id)
163 {
164   try
165   {
166     _l.exportInputFiles(job_id);
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::getJobResults(CORBA::Long job_id, const char * directory)
177 {
178   try
179   {
180     _l.getJobResults(job_id, directory);
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 void
190 SALOME_Launcher::clearJobWorkingDir(CORBA::Long job_id)
191 {
192   try
193   {
194     _l.clearJobWorkingDir(job_id);
195   }
196   catch(const LauncherException &ex)
197   {
198     INFOS(ex.msg.c_str());
199     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
200   }
201 }
202
203 CORBA::Boolean
204 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
205 {
206   CORBA::Boolean rtn = false;
207   try
208   {
209     rtn = _l.getJobDumpState(job_id, directory);
210   }
211   catch(const LauncherException &ex)
212   {
213     INFOS(ex.msg.c_str());
214     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
215   }
216   return rtn;
217 }
218
219 CORBA::Boolean
220 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
221 {
222   CORBA::Boolean rtn = false;
223   try
224   {
225     rtn = _l.getJobWorkFile(job_id, work_file, directory);
226   }
227   catch(const LauncherException &ex)
228   {
229     INFOS(ex.msg.c_str());
230     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
231   }
232   return rtn;
233 }
234
235 void
236 SALOME_Launcher::removeJob(CORBA::Long job_id)
237 {
238   try
239   {
240     _l.removeJob(job_id);
241     std::ostringstream job_id_str;
242     job_id_str << job_id;
243     notifyObservers("REMOVE_JOB", job_id_str.str());
244   }
245   catch(const LauncherException &ex)
246   {
247     INFOS(ex.msg.c_str());
248     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
249   }
250 }
251
252 void
253 SALOME_Launcher::stopJob(CORBA::Long job_id)
254 {
255   try
256   {
257     _l.stopJob(job_id);
258     std::ostringstream job_id_str;
259     job_id_str << job_id;
260     notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
261   }
262   catch(const LauncherException &ex)
263   {
264     INFOS(ex.msg.c_str());
265     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
266   }
267 }
268
269 char *
270 SALOME_Launcher::dumpJob(CORBA::Long job_id)
271 {
272   std::string result;
273   try
274   {
275     result = _l.dumpJob(job_id);
276   }
277   catch(const LauncherException &ex)
278   {
279     INFOS(ex.msg.c_str());
280     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
281   }
282   return CORBA::string_dup(result.c_str());
283 }
284
285 CORBA::Long
286 SALOME_Launcher::restoreJob(const char * dumpedJob)
287 {
288   CORBA::Long jobId;
289   try{
290     jobId = _l.restoreJob(dumpedJob);
291     if(jobId >= 0)
292     {
293       std::ostringstream job_str;
294       job_str << jobId;
295       notifyObservers("NEW_JOB", job_str.str());
296     }
297   }
298   catch(const LauncherException &ex){
299     INFOS(ex.msg.c_str());
300     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
301   }
302
303   return jobId;
304 }
305
306 //=============================================================================
307 /*! CORBA Method:
308  *  Create a job in the launcher with a file
309  *  \param xmlExecuteFile     : .xml to parse that contains job description
310  *  \param clusterName        : machine chosen
311  */
312 //=============================================================================
313 CORBA::Long
314 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
315                                    const char * clusterName)
316 {
317   CORBA::Long jobId;
318   try{
319     jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
320   }
321   catch(const LauncherException &ex){
322     INFOS(ex.msg.c_str());
323     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
324   }
325
326   return jobId;
327 }
328
329 //=============================================================================
330 /*! CORBA Method:
331  *  the test batch configuration
332  *  \param params             : The batch cluster
333  */
334 //=============================================================================
335 CORBA::Boolean
336 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
337 {
338   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
339   CORBA::Boolean rtn = false;
340   try
341   {
342     // Consider only resources that can run batch jobs
343     Engines::ResourceParameters new_params(params);
344     new_params.can_launch_batch_jobs = true;
345
346     // find a resource matching the required parameters
347     Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
348     if (aMachineList->length() == 0)
349       throw SALOME_Exception("No resources have been found with your parameters");
350
351     const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
352         std::string resource_name(p->name);
353     INFOS("Choose resource for test: " <<  resource_name);
354
355     BatchTest t(*p);
356     if (t.test())
357     {
358       rtn = true;
359     }
360   }
361   catch(const LauncherException &ex){
362     INFOS(ex.msg.c_str());
363     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
364   }
365   return rtn;
366 }
367
368 //=============================================================================
369 /*! CORBA method:
370  *  shutdown all the containers, then the ContainerManager servant
371  */
372 //=============================================================================
373 void SALOME_Launcher::Shutdown()
374 {
375   MESSAGE("Shutdown");
376   _NS->Destroy_Name(_LauncherNameInNS);
377   _ContManager->Shutdown();
378   _ResManager->Shutdown();
379   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
380   _poa->deactivate_object(oid);
381   if(!CORBA::is_nil(_orb))
382     _orb->shutdown(0);
383 }
384
385 //=============================================================================
386 /*! CORBA Method:
387  *  Returns the PID of the process
388  */
389 //=============================================================================
390 CORBA::Long SALOME_Launcher::getPID()
391 {
392   return
393 #ifndef WIN32
394     (CORBA::Long)getpid();
395 #else
396     (CORBA::Long)_getpid();
397 #endif
398 }
399
400 //=============================================================================
401 /*! CORBA Method:
402  *  Returns current launcher jobs list
403  */
404 //=============================================================================
405 Engines::JobsList *
406 SALOME_Launcher::getJobsList()
407 {
408   Engines::JobsList_var jobs_list = new Engines::JobsList();
409   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
410   std::map<int, Launcher::Job *>::const_iterator it_job;
411   int list_id = 0;
412   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
413   {
414     int number          = it_job->first;
415     try
416     {
417       // Prepare CORBA job description
418       Engines::JobDescription_var job_descr = new Engines::JobDescription();
419       Engines::JobParameters_var job_parameters = getJobParameters(number);
420       job_descr->job_id = number;
421       job_descr->job_parameters = job_parameters;
422
423       // Add job description to the sequence
424       jobs_list->length(list_id + 1);
425       jobs_list[list_id] = job_descr;
426       list_id++;
427     }
428     catch (...) {}
429   }
430   return jobs_list._retn();
431 }
432
433 //=============================================================================
434 /*! CORBA Method:
435  * Returns the job description
436  */
437 //=============================================================================
438 Engines::JobParameters *
439 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
440 {
441   Engines::JobParameters_var job_parameters;
442   try
443   {
444     JobParameters_cpp cpp_parameters = _l.getJobParameters(job_id);
445     job_parameters = JobParameters_CPP2CORBA(cpp_parameters);
446   }
447   catch(const LauncherException &ex)
448   {
449     INFOS(ex.msg.c_str());
450     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
451   }
452
453   return job_parameters._retn();
454 }
455
456 //=============================================================================
457 /*! CORBA Method:
458  *  Loads jobs saved in jobs_file
459  */
460 //=============================================================================
461 void
462 SALOME_Launcher::loadJobs(const char* jobs_file)
463 {
464   list<int> new_jobs_id_list;
465   try
466   {
467     // Load the jobs in Launcher
468     new_jobs_id_list = _l.loadJobs(jobs_file);
469   }
470   catch (const LauncherException & ex)
471   {
472     INFOS(ex.msg.c_str());
473     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
474   }
475
476   // Notify observers of the new jobs
477   list<int>::const_iterator it_jobs_id;
478   for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
479   {
480     ostringstream job_id_sstr;
481     job_id_sstr << *it_jobs_id;
482     notifyObservers("NEW_JOB", job_id_sstr.str());
483   }
484   notifyObservers("LOAD_JOBS", jobs_file);
485 }
486
487 //=============================================================================
488 /*! CORBA Method:
489  *  Save jobs of Launcher (in any steps) in file jobs_file
490  */
491 //=============================================================================
492 void
493 SALOME_Launcher::saveJobs(const char* jobs_file)
494 {
495   _l.saveJobs(jobs_file);
496   notifyObservers("SAVE_JOBS", jobs_file);
497 }
498
499 //=============================================================================
500 /*! CORBA Method:
501  *  Add a new observer to the launcher
502  */
503 //=============================================================================
504 void
505 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
506 {
507   bool new_observer = true;
508   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
509   while(iter != _observers.end())
510   {
511     if (std::string(_orb->object_to_string(*iter)) ==
512         std::string(_orb->object_to_string(observer)))
513     {
514       new_observer = false;
515       break;
516     }
517     iter++;
518   }
519   if (new_observer)
520     _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
521
522   // We notify the new observer with all jobs that are currently in the Launcher
523   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
524   std::map<int, Launcher::Job *>::const_iterator it_job;
525   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
526   {
527     int number = it_job->first;
528     std::ostringstream job_id;
529     job_id << number;
530     try
531     {
532       observer->notify("NEW_JOB", job_id.str().c_str());
533     }
534     catch (...)
535     {
536        MESSAGE("Notify Observer, exception catch");
537     }
538
539   }
540 }
541
542 //=============================================================================
543 /*! CORBA Method:
544  *  Add a new observer to the launcher
545  */
546 //=============================================================================
547 void
548 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
549 {
550   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
551   while(iter != _observers.end())
552   {
553     if (std::string(_orb->object_to_string(*iter)) ==
554         std::string(_orb->object_to_string(observer)))
555     {
556       // Observer found
557       iter =_observers.erase(iter++);
558     }
559     else
560     {
561       iter++;
562     }
563   }
564 }
565
566 //=============================================================================
567 /*! Internal Method:
568  *  Notify observers on a new event
569  */
570 //=============================================================================
571 void
572 SALOME_Launcher::notifyObservers(const std::string & event_name,
573                                  const std::string & event_data)
574 {
575   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
576   while(iter != _observers.end())
577   {
578     try
579     {
580       (*iter)->notify(CORBA::string_dup(event_name.c_str()),
581                       CORBA::string_dup(event_data.c_str()));
582     }
583     catch (...)
584     {
585        MESSAGE("Notify Observer, exception catch");
586     }
587     iter++;
588   }
589
590 }
591
592 JobParameters_cpp
593 SALOME_Launcher::JobParameters_CORBA2CPP(
594                                    const Engines::JobParameters& job_parameters)
595 {
596   JobParameters_cpp result;
597
598   result.job_name = job_parameters.job_name.in();
599   result.job_type = job_parameters.job_type.in();
600   result.job_file = job_parameters.job_file.in();
601   result.pre_command = job_parameters.pre_command.in();
602   result.env_file = job_parameters.env_file.in();
603
604   result.in_files.clear();
605   for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
606     result.in_files.push_back(job_parameters.in_files[i].in());
607   result.out_files.clear();
608   for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
609     result.out_files.push_back(job_parameters.out_files[i].in());
610
611   result.work_directory = job_parameters.work_directory.in();
612   result.local_directory = job_parameters.local_directory.in();
613   result.result_directory = job_parameters.result_directory.in();
614   result.maximum_duration = job_parameters.maximum_duration.in();
615
616   result.resource_required = resourceParameters_CORBAtoCPP(job_parameters.resource_required);
617
618   result.queue = job_parameters.queue.in();
619   result.partition = job_parameters.partition.in();
620   result.exclusive = job_parameters.exclusive;
621   result.mem_per_cpu = job_parameters.mem_per_cpu;
622   result.wckey = job_parameters.wckey.in();
623   result.extra_params = job_parameters.extra_params.in();
624
625   result.specific_parameters.clear();
626   for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
627     result.specific_parameters[job_parameters.specific_parameters[i].name.in()]
628                              = job_parameters.specific_parameters[i].value.in();
629
630   result.launcher_file = job_parameters.launcher_file.in();
631   result.launcher_args = job_parameters.launcher_args.in();
632   return result;
633 }
634
635 Engines::JobParameters_var
636 SALOME_Launcher::JobParameters_CPP2CORBA(const JobParameters_cpp& job_parameters)
637 {
638   Engines::JobParameters_var result = new Engines::JobParameters;
639   result->job_name = CORBA::string_dup(job_parameters.job_name.c_str());
640   result->job_type = CORBA::string_dup(job_parameters.job_type.c_str());
641   result->job_file = CORBA::string_dup(job_parameters.job_file.c_str());
642   result->pre_command = CORBA::string_dup(job_parameters.pre_command.c_str());
643   result->env_file = CORBA::string_dup(job_parameters.env_file.c_str());
644   result->in_files.length(job_parameters.in_files.size());
645
646   int i = 0;
647   for(const std::string& it : job_parameters.in_files)
648   {
649     result->in_files[i] = CORBA::string_dup(it.c_str());
650     i++;
651   }
652   result->out_files.length(job_parameters.out_files.size());
653   i = 0;
654   for(const std::string& it : job_parameters.out_files)
655   {
656     result->out_files[i] = CORBA::string_dup(it.c_str());
657     i++;
658   }
659
660   result->work_directory = CORBA::string_dup(job_parameters.work_directory.c_str());
661   result->local_directory = CORBA::string_dup(job_parameters.local_directory.c_str());
662   result->result_directory = CORBA::string_dup(job_parameters.result_directory.c_str());
663   result->maximum_duration = CORBA::string_dup(job_parameters.maximum_duration.c_str());
664
665   result->resource_required = resourceParameters_CPPtoCORBA(job_parameters.resource_required);
666
667   result->queue = CORBA::string_dup(job_parameters.queue.c_str());
668   result->partition = CORBA::string_dup(job_parameters.partition.c_str());
669   result->exclusive = job_parameters.exclusive;
670   result->mem_per_cpu = job_parameters.mem_per_cpu;
671   result->wckey = CORBA::string_dup(job_parameters.wckey.c_str());
672   result->extra_params = CORBA::string_dup(job_parameters.extra_params.c_str());
673
674   const std::map<std::string, std::string>& specific_parameters
675                                        = job_parameters.specific_parameters;
676   if (!specific_parameters.empty())
677   {
678     result->specific_parameters.length(specific_parameters.size());
679     CORBA::ULong i = 0;
680     for (const auto& it_specific : specific_parameters)
681     {
682       Engines::Parameter_var new_param = new Engines::Parameter;
683       new_param->name  = CORBA::string_dup(it_specific.first.c_str());
684       new_param->value = CORBA::string_dup(it_specific.second.c_str());
685       result->specific_parameters[i] = new_param;
686       i++;
687     }
688   }
689
690   result->launcher_file = CORBA::string_dup(job_parameters.launcher_file.c_str());
691   result->launcher_args = CORBA::string_dup(job_parameters.launcher_args.c_str());
692   return result;
693 }