Salome HOME
Move XML persistence functions from SALOME_Launcher class to a dedicated class Launch...
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 // Copyright (C) 2007-2013  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.
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 "Utils_CorbaException.hxx"
28
29
30 #include "Launcher_Job_Command.hxx"
31 #include "Launcher_Job_YACSFile.hxx"
32 #include "Launcher_Job_PythonSALOME.hxx"
33 #include "Launcher_Job_Writer.hxx"
34
35 #ifdef WIN32
36 # include <process.h>
37 #else
38 # include <unistd.h>
39 #endif
40 #include <sys/types.h>
41 #include <vector>
42 #include <list>
43
44 #include <stdio.h>
45 #include <sstream>
46
47 using namespace std;
48
49 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
50
51 //=============================================================================
52 /*! 
53  *  Constructor
54  *  \param orb
55  */
56 //=============================================================================
57 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
58 {
59   MESSAGE("SALOME_Launcher constructor");
60   _NS = new SALOME_NamingService(orb);
61   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
62   _l.SetResourcesManager(_ResManager->GetImpl());
63   _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
64   _ResManager->_remove_ref();
65   _ContManager->_remove_ref();
66
67   _orb = CORBA::ORB::_duplicate(orb) ;
68   _poa = PortableServer::POA::_duplicate(poa) ;
69   PortableServer::ObjectId_var id = _poa->activate_object(this);
70   CORBA::Object_var obj = _poa->id_to_reference(id);
71   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
72
73   _NS->Register(refContMan,_LauncherNameInNS);
74   MESSAGE("SALOME_Launcher constructor end");
75 }
76
77 //=============================================================================
78 /*! 
79  * destructor
80  */
81 //=============================================================================
82 SALOME_Launcher::~SALOME_Launcher()
83 {
84   MESSAGE("SALOME_Launcher destructor");
85   delete _NS;
86   MESSAGE("SALOME_Launcher destructor end");
87 }
88
89
90 CORBA::Long 
91 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
92 {
93   std::string job_type = job_parameters.job_type.in();
94
95   if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome")
96   {
97     std::string message("SALOME_Launcher::createJob: bad job type: ");
98     message += job_type;
99     THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR);
100   }
101
102   Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
103
104   if (job_type == "command")
105     new_job = new Launcher::Job_Command();
106   else if (job_type == "yacs_file")
107     new_job = new Launcher::Job_YACSFile();
108   else if (job_type == "python_salome")
109     new_job = new Launcher::Job_PythonSALOME();
110
111   // Name
112   new_job->setJobName(job_parameters.job_name.in());
113
114   // Directories
115   std::string work_directory = job_parameters.work_directory.in();
116   std::string local_directory = job_parameters.local_directory.in();
117   std::string result_directory = job_parameters.result_directory.in();
118   new_job->setWorkDirectory(work_directory);
119   new_job->setLocalDirectory(local_directory);
120   new_job->setResultDirectory(result_directory);
121
122   // Parameters for COORM
123   std::string launcher_file = job_parameters.launcher_file.in();
124   std::string launcher_args = job_parameters.launcher_args.in();
125   new_job->setLauncherFile(launcher_file);
126   new_job->setLauncherArgs(launcher_args);
127
128   // Job File
129   std::string job_file = job_parameters.job_file.in();
130   try
131   {
132     new_job->setJobFile(job_file);
133   }
134   catch(const LauncherException &ex)
135   {
136     INFOS(ex.msg.c_str());
137     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
138   }
139
140   // Files
141   std::string env_file = job_parameters.env_file.in();
142   new_job->setEnvFile(env_file);
143   for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
144     new_job->add_in_file(job_parameters.in_files[i].in());
145   for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
146     new_job->add_out_file(job_parameters.out_files[i].in());
147
148   // Expected During Time
149   try
150   {
151     std::string maximum_duration = job_parameters.maximum_duration.in();
152     new_job->setMaximumDuration(maximum_duration);
153   }
154   catch(const LauncherException &ex){
155     INFOS(ex.msg.c_str());
156     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
157   }
158
159   // Queue
160   std::string queue = job_parameters.queue.in();
161   new_job->setQueue(queue);
162
163   // Exclusive
164   new_job->setExclusive(job_parameters.exclusive);
165
166   // Memory required per CPU
167   new_job->setMemPerCpu(job_parameters.mem_per_cpu);
168
169   // Resources requirements
170   try
171   {
172     resourceParams p;
173     p.name = job_parameters.resource_required.name;
174     p.hostname = job_parameters.resource_required.hostname;
175     p.OS = job_parameters.resource_required.OS;
176     p.nb_proc = job_parameters.resource_required.nb_proc;
177     p.nb_node = job_parameters.resource_required.nb_node;
178     p.nb_proc_per_node = job_parameters.resource_required.nb_proc_per_node;
179     p.cpu_clock = job_parameters.resource_required.cpu_clock;
180     p.mem_mb = job_parameters.resource_required.mem_mb;
181     new_job->setResourceRequiredParams(p);
182   }
183   catch(const LauncherException &ex){
184     INFOS(ex.msg.c_str());
185     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
186   }
187
188   // Adding specific parameters to the job
189   for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
190     new_job->addSpecificParameter(job_parameters.specific_parameters[i].name.in(),
191                                   job_parameters.specific_parameters[i].value.in());
192   try
193   {
194     new_job->checkSpecificParameters();
195   }
196   catch(const LauncherException &ex)
197   {
198     INFOS(ex.msg.c_str());
199     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
200   }
201
202   try
203   {
204     _l.createJob(new_job);
205     std::ostringstream job_id;
206     job_id << new_job->getNumber();
207     notifyObservers("NEW_JOB", job_id.str());
208   }
209   catch(const LauncherException &ex)
210   {
211     INFOS(ex.msg.c_str());
212     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
213   }
214   return new_job->getNumber();
215 }
216
217 void 
218 SALOME_Launcher::launchJob(CORBA::Long job_id)
219 {
220   try
221   {
222     _l.launchJob(job_id);
223   }
224   catch(const LauncherException &ex)
225   {
226     INFOS(ex.msg.c_str());
227     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
228   }
229 }
230
231 char *
232 SALOME_Launcher::getJobState(CORBA::Long job_id)
233 {
234   std::string result;
235   try
236   {
237     result = _l.getJobState(job_id);
238   }
239   catch(const LauncherException &ex)
240   {
241     INFOS(ex.msg.c_str());
242     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
243   }
244   return CORBA::string_dup(result.c_str());
245 }
246
247 // Get names or ids of hosts assigned to the job
248 char *
249 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
250 {
251   std::string result;
252   try
253   {
254     result = _l.getAssignedHostnames(job_id);
255   }
256   catch(const LauncherException &ex)
257   {
258     INFOS(ex.msg.c_str());
259     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
260   }
261   return CORBA::string_dup(result.c_str());
262 }
263
264 void
265 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
266 {
267   try
268   {
269     _l.getJobResults(job_id, directory);
270   }
271   catch(const LauncherException &ex)
272   {
273     INFOS(ex.msg.c_str());
274     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
275   }
276 }
277
278 CORBA::Boolean
279 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
280 {
281   CORBA::Boolean rtn = false;
282   try
283   {
284     rtn = _l.getJobDumpState(job_id, directory);
285   }
286   catch(const LauncherException &ex)
287   {
288     INFOS(ex.msg.c_str());
289     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
290   }
291   return rtn;
292 }
293
294 void 
295 SALOME_Launcher::removeJob(CORBA::Long job_id)
296 {
297   try
298   {
299     _l.removeJob(job_id);
300     std::ostringstream job_id_str;
301     job_id_str << job_id;
302     notifyObservers("REMOVE_JOB", job_id_str.str());
303   }
304   catch(const LauncherException &ex)
305   {
306     INFOS(ex.msg.c_str());
307     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
308   }
309 }
310
311 void 
312 SALOME_Launcher::stopJob(CORBA::Long job_id)
313 {
314   try
315   {
316     _l.stopJob(job_id);
317     std::ostringstream job_id_str;
318     job_id_str << job_id;
319     notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
320   }
321   catch(const LauncherException &ex)
322   {
323     INFOS(ex.msg.c_str());
324     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
325   }
326 }
327
328 //=============================================================================
329 /*! CORBA Method:
330  *  Create a job in the launcher with a file
331  *  \param xmlExecuteFile     : .xml to parse that contains job description
332  *  \param clusterName        : machine choosed
333  */
334 //=============================================================================
335 CORBA::Long 
336 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
337                                    const char * clusterName)
338 {
339   CORBA::Long jobId;
340   try{
341     jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
342   }
343   catch(const LauncherException &ex){
344     INFOS(ex.msg.c_str());
345     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
346   }
347
348   return jobId;
349 }
350
351 //=============================================================================
352 /*! CORBA Method:
353  *  the test batch configuration 
354  *  \param params             : The batch cluster
355  */
356 //=============================================================================
357 CORBA::Boolean 
358 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
359 {
360   MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
361   CORBA::Boolean rtn = false;
362   try
363   {
364     // Consider only resources that can run batch jobs
365     Engines::ResourceParameters new_params(params);
366     new_params.can_launch_batch_jobs = true;
367
368     // find a resource matching the required parameters
369     Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
370     if (aMachineList->length() == 0)
371       throw SALOME_Exception("No resources have been found with your parameters");
372
373     const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
374         std::string resource_name(p->name);
375     INFOS("Choose resource for test: " <<  resource_name);
376     
377     BatchTest t(*p);
378     if (t.test()) 
379     {
380       rtn = true;
381     }
382   }
383   catch(const LauncherException &ex){
384     INFOS(ex.msg.c_str());
385     THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
386   }
387   return rtn;
388 }
389
390 //=============================================================================
391 /*! CORBA method:
392  *  shutdown all the containers, then the ContainerManager servant
393  */
394 //=============================================================================
395 void SALOME_Launcher::Shutdown()
396 {
397   MESSAGE("Shutdown");
398   _NS->Destroy_Name(_LauncherNameInNS);
399   _ContManager->Shutdown();
400   _ResManager->Shutdown();
401   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
402   _poa->deactivate_object(oid);
403   if(!CORBA::is_nil(_orb))
404     _orb->shutdown(0);
405 }
406
407 //=============================================================================
408 /*! CORBA Method:
409  *  Returns the PID of the process
410  */
411 //=============================================================================
412 CORBA::Long SALOME_Launcher::getPID()
413 {
414   return 
415 #ifndef WIN32
416     (CORBA::Long)getpid();
417 #else
418     (CORBA::Long)_getpid();
419 #endif
420 }
421
422 //=============================================================================
423 /*! CORBA Method:
424  *  Returns current launcher jobs list
425  */
426 //=============================================================================
427 Engines::JobsList *
428 SALOME_Launcher::getJobsList()
429 {
430   Engines::JobsList_var jobs_list = new Engines::JobsList();
431   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
432   std::map<int, Launcher::Job *>::const_iterator it_job;
433   int list_id = 0;
434   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
435   {
436     int number          = it_job->first;
437     try
438     {
439       // Prepare CORBA job description
440       Engines::JobDescription_var job_descr = new Engines::JobDescription();
441       Engines::JobParameters_var job_parameters = getJobParameters(number);
442       job_descr->job_id = number;
443       job_descr->job_parameters = job_parameters;
444
445       // Add job description to the sequence
446       jobs_list->length(list_id + 1);
447       jobs_list[list_id] = job_descr;
448       list_id++;
449     }
450     catch (...) {}
451   }
452   return jobs_list._retn();
453 }
454
455 //=============================================================================
456 /*! CORBA Method:
457  * Returns the job description
458  */
459 //=============================================================================
460 Engines::JobParameters *
461 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
462 {
463   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
464   std::map<int, Launcher::Job *>::const_iterator it_job = cpp_jobs.find(job_id);
465   if (it_job == cpp_jobs.end())
466   {
467     INFOS("Cannot find the job, is it created ? job number: " << job_id);
468     THROW_SALOME_CORBA_EXCEPTION("Job does not exist", SALOME::INTERNAL_ERROR);
469   }
470
471   Launcher::Job * job = it_job->second;
472   Engines::JobParameters_var job_parameters = new Engines::JobParameters;
473   job_parameters->job_name         = CORBA::string_dup(job->getJobName().c_str());
474   job_parameters->job_type         = CORBA::string_dup(job->getJobType().c_str());
475   job_parameters->job_file         = CORBA::string_dup(job->getJobFile().c_str());
476   job_parameters->env_file         = CORBA::string_dup(job->getEnvFile().c_str());
477   job_parameters->work_directory   = CORBA::string_dup(job->getWorkDirectory().c_str());
478   job_parameters->local_directory  = CORBA::string_dup(job->getLocalDirectory().c_str());
479   job_parameters->result_directory = CORBA::string_dup(job->getResultDirectory().c_str());
480
481   // Parameters for COORM
482   job_parameters->launcher_file = CORBA::string_dup(job->getLauncherFile().c_str());
483   job_parameters->launcher_args = CORBA::string_dup(job->getLauncherArgs().c_str());
484
485   int i = 0;
486   int j = 0;
487   std::list<std::string> in_files  = job->get_in_files();
488   std::list<std::string> out_files = job->get_out_files();
489   job_parameters->in_files.length(in_files.size());
490   for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
491   {
492     job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
493     i++;
494   }
495   job_parameters->out_files.length(out_files.size());
496   for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
497   {
498     job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
499     j++;
500   }
501
502   job_parameters->maximum_duration = CORBA::string_dup(job->getMaximumDuration().c_str());
503   job_parameters->queue            = CORBA::string_dup(job->getQueue().c_str());
504   job_parameters->exclusive        = job->getExclusive();
505   job_parameters->mem_per_cpu      = job->getMemPerCpu();
506
507   resourceParams resource_params = job->getResourceRequiredParams();
508   job_parameters->resource_required.name             = CORBA::string_dup(resource_params.name.c_str());
509   job_parameters->resource_required.hostname         = CORBA::string_dup(resource_params.hostname.c_str());
510   job_parameters->resource_required.OS               = CORBA::string_dup(resource_params.OS.c_str());
511   job_parameters->resource_required.nb_proc          = resource_params.nb_proc;
512   job_parameters->resource_required.nb_node          = resource_params.nb_node;
513   job_parameters->resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
514   job_parameters->resource_required.cpu_clock        = resource_params.cpu_clock;
515   job_parameters->resource_required.mem_mb           = resource_params.mem_mb;
516
517   std::map<std::string, std::string> specific_parameters = job->getSpecificParameters();
518   if (!specific_parameters.empty())
519   {
520     job_parameters->specific_parameters.length(specific_parameters.size());
521     std::map<std::string, std::string>::const_iterator it_specific;
522     CORBA::ULong i = 0;
523     for (it_specific = specific_parameters.begin() ; it_specific != specific_parameters.end(); it_specific++)
524     {
525       Engines::Parameter_var new_param = new Engines::Parameter;
526       new_param->name  = CORBA::string_dup((it_specific->first).c_str());
527       new_param->value = CORBA::string_dup((it_specific->second).c_str());
528       job_parameters->specific_parameters[i] = new_param;
529       i++;
530     }
531   }
532
533   return job_parameters._retn();
534 }
535
536 //=============================================================================
537 /*! CORBA Method:
538  *  Loads jobs saved in jobs_file
539  */
540 //=============================================================================
541 void
542 SALOME_Launcher::loadJobs(const char* jobs_file)
543 {
544   // Load the jobs in Launcher
545   list<int> new_jobs_id_list = _l.loadJobs(jobs_file);
546
547   // Notify observers of the new jobs
548   list<int>::const_iterator it_jobs_id;
549   for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
550   {
551     ostringstream job_id_sstr;
552     job_id_sstr << *it_jobs_id;
553     notifyObservers("NEW_JOB", job_id_sstr.str());
554   }
555   notifyObservers("LOAD_JOBS", jobs_file);
556 }
557
558 //=============================================================================
559 /*! CORBA Method:
560  *  Save jobs of Launcher (in any steps) in file jobs_file
561  */
562 //=============================================================================
563 void
564 SALOME_Launcher::saveJobs(const char* jobs_file)
565 {
566   _l.saveJobs(jobs_file);
567   notifyObservers("SAVE_JOBS", jobs_file);
568 }
569
570 //=============================================================================
571 /*! CORBA Method:
572  *  Add a new observer to the launcher
573  */
574 //=============================================================================
575 void
576 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
577 {
578   bool new_observer = true;
579   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
580   while(iter != _observers.end())
581   {
582     if (std::string(_orb->object_to_string(*iter)) ==
583         std::string(_orb->object_to_string(observer)))
584     {
585       new_observer = false;
586       break;
587     }
588     iter++;
589   }
590   if (new_observer)
591     _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
592
593   // We notify the new observer with all jobs that are currently in the Launcher
594   std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
595   std::map<int, Launcher::Job *>::const_iterator it_job;
596   for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
597   {
598     int number = it_job->first;
599     std::ostringstream job_id;
600     job_id << number;
601     try
602     {
603       observer->notify("NEW_JOB", job_id.str().c_str());
604     }
605     catch (...) 
606     {
607        MESSAGE("Notify Observer, exception catch");
608     }
609
610   }
611 }
612
613 //=============================================================================
614 /*! CORBA Method:
615  *  Add a new observer to the launcher
616  */
617 //=============================================================================
618 void
619 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
620 {
621   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
622   while(iter != _observers.end())
623   {
624     if (std::string(_orb->object_to_string(*iter)) ==
625         std::string(_orb->object_to_string(observer)))
626     {
627       // Observer found
628       iter =_observers.erase(iter++);
629     }
630     else
631     {
632       iter++;
633     }
634   }
635 }
636
637 //=============================================================================
638 /*! Internal Method:
639  *  Notify observers on a new event
640  */
641 //=============================================================================
642 void
643 SALOME_Launcher::notifyObservers(const std::string & event_name,
644                                  const std::string & event_data)
645 {
646   std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
647   while(iter != _observers.end())
648   {
649     try
650     {
651       (*iter)->notify(CORBA::string_dup(event_name.c_str()),
652                       CORBA::string_dup(event_data.c_str()));
653     }
654     catch (...) 
655     {
656        MESSAGE("Notify Observer, exception catch");
657     }
658     iter++;
659   }
660
661 }