]> SALOME platform Git repositories - modules/kernel.git/blob - src/ResourcesManager/SALOME_ResourcesManager.cxx
Salome HOME
7765c78299657926f01549bc1043840612292cc0
[modules/kernel.git] / src / ResourcesManager / SALOME_ResourcesManager.cxx
1 #include "SALOME_ResourcesManager.hxx"
2 #include "SALOME_Container_i.hxx"
3 #include "Utils_ExceptHandlers.hxx"
4 #include "OpUtil.hxx"
5
6 #include <qdom.h>
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fstream>
11 #include <iostream>
12 #include <string.h>
13 #include <map>
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 #define MAX_SIZE_FOR_HOSTNAME 256;
19
20 using namespace std;
21
22 //just for test
23 SALOME_ResourcesManager::SALOME_ResourcesManager(const char *xmlFilePath):_path_resources(xmlFilePath)
24 {
25 }
26
27 SALOME_ResourcesManager::SALOME_ResourcesManager()
28 {
29   _path_resources=getenv("KERNEL_ROOT_DIR");
30   _path_resources+="/share/salome/resources/CatalogResources.xml";
31   ParseXmlFile();
32 }
33
34 SALOME_ResourcesManager::~SALOME_ResourcesManager()
35 {
36 }
37
38 vector<string> SALOME_ResourcesManager::GetFittingResources(const Engines::MachineParameters& params,const char *moduleName) throw(SALOME_Exception)
39 {
40   vector <std::string> ret;
41   //To be sure that we search in a correct list.
42   ParseXmlFile();
43   const char *hostname=(const char *)params.hostname;
44   if(hostname[0]!='\0')
45     {
46       if(_resourcesList.find(hostname)!=_resourcesList.end())
47         // params.hostame is in the list of resources so return it.
48         ret.push_back(hostname);
49       else
50         //user specified an unknown hostame so notify to him.
51         throw SALOME_Exception("unknown host");
52     }
53   else
54     // Search for available resources sorted by priority
55     {
56       SelectOnlyResourcesWithOS(ret,params.OS);
57       KeepOnlyResourcesWithModule(ret,moduleName);
58       //set wanted parameters
59       ResourceDataToSort::_nbOfNodesWanted=params.nb_node;
60       ResourceDataToSort::_nbOfProcPerNodeWanted=params.nb_proc_per_node;
61       ResourceDataToSort::_CPUFreqMHzWanted=params.cpu_clock;
62       ResourceDataToSort::_memInMBWanted=params.mem_mb;
63       //end of set
64       list<ResourceDataToSort> li;
65       for(vector<string>::iterator iter=ret.begin();iter!=ret.end();iter++)
66         li.push_back(_resourcesList[(*iter)].DataForSort);
67       li.sort();
68       unsigned int i=0;
69       for(list<ResourceDataToSort>::iterator iter2=li.begin();iter2!=li.end();iter2++)
70         ret[i++]=(*iter2)._hostName;
71     }
72   return ret;
73 }
74
75 int SALOME_ResourcesManager::AddResourceInCatalog(const Engines::MachineParameters& paramsOfNewResources, const map<string,string>& modulesOnNewResources,
76                                                   const char *environPathOfPrerequired,
77                                                   const char *alias, const char *userName, AccessModeType mode, AccessProtocolType prot) throw(SALOME_Exception)
78 {
79   map<string,string>::const_iterator iter=modulesOnNewResources.find("KERNEL");
80   if(iter!=modulesOnNewResources.end())
81     {
82       ParserResourcesType newElt;
83       newElt.DataForSort._hostName=paramsOfNewResources.hostname;
84       newElt.Alias=alias;
85       newElt.Protocol=prot;
86       newElt.Mode=mode;
87       newElt.UserName=userName;
88       newElt.ModulesPath=modulesOnNewResources;
89       newElt.PreReqFilePath=environPathOfPrerequired;
90       newElt.OS=paramsOfNewResources.OS;
91       newElt.DataForSort._memInMB=paramsOfNewResources.mem_mb;
92       newElt.DataForSort._CPUFreqMHz=paramsOfNewResources.cpu_clock;
93       newElt.DataForSort._nbOfNodes=paramsOfNewResources.nb_node;
94       newElt.DataForSort._nbOfProcPerNode=paramsOfNewResources.nb_proc_per_node;
95       _resourcesList[newElt.DataForSort._hostName]=newElt;
96       return 0;
97     }
98   else
99     throw SALOME_Exception("KERNEL is not present in this resource");
100 }
101
102 void SALOME_ResourcesManager::DeleteResourceInCatalog(const char *hostname)
103 {
104   _resourcesList.erase(hostname);
105 }
106
107 void SALOME_ResourcesManager::WriteInXmlFile()
108 {
109   QDomDocument doc("ResourcesCatalog");
110   SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
111   handler->PrepareDocToXmlFile(doc);
112   delete handler;
113   QFile file( _path_resources );
114   if( !file.open( IO_WriteOnly ) )
115     cout << "WRITING ERROR !!!" << endl;
116
117   QTextStream ts( &file );
118   ts << doc.toString();
119
120   file.close();
121   cout << "WRITING DONE!!!" << endl;
122 }
123
124 const MapOfParserResourcesType& SALOME_ResourcesManager::ParseXmlFile() 
125 {
126   SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
127   QFile xmlFile(_path_resources);
128
129   QXmlInputSource source(xmlFile);
130
131   QXmlSimpleReader reader;
132   reader.setContentHandler( handler );
133   reader.setErrorHandler( handler );
134   reader.parse( source );
135   xmlFile.close();
136   delete handler;
137   return _resourcesList;
138 }
139
140 bool SALOME_ResourcesManager::_verify_resources(MapOfParserResourcesType resourceslist)
141 {
142 //     bool _return_value = true;
143 //     bool _bool = false ;
144 //     vector<string> _machine_list;
145 //     _machine_list.resize(0);
146
147 //   // Fill a list of all computers indicated in the resources list
148 //   for (unsigned int ind = 0; ind < resourceslist.size(); ind++)
149 //           _machine_list.push_back(resourceslist[ind].HostName);   
150
151 //   // Parse if a computer name is twice in the list of computers
152 //   for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
153 //     {
154 //      for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
155 //        {
156 //          if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
157 //            {
158 //              MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the resources list")
159 //              _return_value = false;
160 //           }
161 //        }
162 //     }
163
164 //    return _return_value;
165   return true;
166 }
167
168 const MapOfParserResourcesType& SALOME_ResourcesManager::GetList() const
169 {
170   return _resourcesList;
171 }
172
173 string SALOME_ResourcesManager::FindBest(const Engines::MachineList& listOfMachines)
174 {
175   return _dynamicResourcesSelecter.FindBest(listOfMachines);
176 }
177
178 string SALOME_ResourcesManager::BuildTempFileToLaunchRemoteContainer(const string& machine,const char *containerName)
179 {
180   _TmpFileName=BuildTemporaryFileName();
181   ofstream tempOutputFile;
182   tempOutputFile.open(_TmpFileName.c_str(),ofstream::out );
183   const ParserResourcesType& resInfo=_resourcesList[machine];
184   tempOutputFile << "#! /bin/sh" << endl;
185   //set env vars
186   for(map<string,string>::const_iterator iter=resInfo.ModulesPath.begin();iter!=resInfo.ModulesPath.end();iter++)
187     {
188       string curModulePath((*iter).second);
189       tempOutputFile << (*iter).first << "_ROOT_DIR="<< curModulePath << endl;
190       tempOutputFile << "export " << (*iter).first << "_ROOT_DIR" << endl;
191       tempOutputFile << "LD_LIBRARY_PATH=" << curModulePath << "/lib/salome" << ":${LD_LIBRARY_PATH}" << endl;
192       tempOutputFile << "PYTHONPATH=" << curModulePath << "/bin/salome:" << curModulePath << "/lib/python2.2/site-packages/salome:";
193       tempOutputFile << curModulePath << "/lib/python2.2/site-packages/salome/shared_modules:${PYTHONPATH}" << endl;
194     }
195   tempOutputFile << "export LD_LIBRARY_PATH" << endl;
196   tempOutputFile << "export PYTHONPATH" << endl;
197   tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
198   // ! env vars
199   tempOutputFile << (*(resInfo.ModulesPath.find("KERNEL"))).second << "/bin/salome/";
200   if(Engines_Container_i::isPythonContainer(containerName))
201     tempOutputFile << "SALOME_ContainerPy.py ";
202   else
203     tempOutputFile << "SALOME_Container ";
204   tempOutputFile << containerName << " -";
205   AddOmninamesParams(tempOutputFile);
206   tempOutputFile << " > /tmp/" << "/" << containerName << "_" << machine << ".log 2>&1 &" << endl;//" &" << endl;
207   //tempOutputFile << "EOF" << endl;
208   //tempOutputFile << "&" << endl;
209   tempOutputFile.flush();
210   tempOutputFile.close();
211   chmod(_TmpFileName.c_str(),0x1ED);
212   //Build command
213   string command;
214   if(resInfo.Protocol==rsh)
215     {
216       command = "rsh ";
217       string commandRcp="rcp ";
218       commandRcp+=_TmpFileName;
219       commandRcp+=" ";
220       commandRcp+=machine;
221       commandRcp+=":";
222       commandRcp+=_TmpFileName;
223       system(commandRcp.c_str());
224     }
225   else if(resInfo.Protocol==ssh)
226     command = "ssh ";
227   else
228     throw SALOME_Exception("Unknown protocol");
229   command+=machine;
230   _CommandForRemAccess=command;
231   command+=" ";
232   command+=_TmpFileName;
233   command+=" & ";
234   cout << "Command is ... " << command << endl;
235   return command;
236 }
237
238 string SALOME_ResourcesManager::BuildCommandToLaunchLocalContainer(const char *containerName)
239 {
240   _TmpFileName="";
241   string command;
242   if(Engines_Container_i::isPythonContainer(containerName))
243     command="SALOME_ContainerPy.py ";
244   else
245     command="SALOME_Container ";
246   command+=containerName;
247   command+=" -";
248   AddOmninamesParams(command);
249   command+=" > /tmp/";
250   command+=containerName;
251   command += "_";
252   command += GetHostname();
253   command += ".log 2>&1 &" ;
254   cout << "Command is ... " << command << endl << flush;
255   return command;
256 }
257
258 void SALOME_ResourcesManager::RmTmpFile()
259 {
260   if(_TmpFileName!="")
261     {
262       string command="rm ";
263       command+=_TmpFileName;
264       system(command.c_str());
265     }
266 }
267
268 string SALOME_ResourcesManager::BuildCommand(const string& machine,const char *containerName)
269 {
270 // rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 &
271   const ParserResourcesType& resInfo=_resourcesList[machine];
272   bool pyCont=Engines_Container_i::isPythonContainer(containerName);
273   string command;
274   if(resInfo.Protocol==rsh)
275     command = "rsh -n " ;
276   else if(resInfo.Protocol==ssh)
277     command = "ssh -f -n ";
278   else
279     throw SALOME_Exception("Not implemented yet...");
280       command += machine;
281   command += " ";
282   string path = (*(resInfo.ModulesPath.find("KERNEL"))).second;
283   command +=path;
284   command += "/bin/salome/";
285   if ( pyCont )
286     command += "SALOME_ContainerPy.py ";
287   else
288     command += "SALOME_Container ";
289   command += containerName;
290   command += " -";
291   AddOmninamesParams(command);
292   command += " > /tmp/";
293   command += containerName;
294   command += "_";
295   command += machine;
296   command += ".log 2>&1 &" ;
297   SCRUTE( command );
298   return command;
299 }
300
301 // Warning need an updated parsed list : _resourcesList
302 void SALOME_ResourcesManager::SelectOnlyResourcesWithOS(vector<string>& hosts,const char *OS) const throw(SALOME_Exception)
303 {
304   string base(OS);
305   for(map<string, ParserResourcesType>::const_iterator iter=_resourcesList.begin();iter!=_resourcesList.end();iter++)
306     if((*iter).second.OS==base)
307       hosts.push_back((*iter).first);
308 }
309
310 //Warning need an updated parsed list : _resourcesList
311 void SALOME_ResourcesManager::KeepOnlyResourcesWithModule(vector<string>& hosts,const char *moduleName) const throw(SALOME_Exception)
312 {
313    for(vector<string>::iterator iter=hosts.begin();iter!=hosts.end();iter++)
314      {
315        MapOfParserResourcesType::const_iterator it=_resourcesList.find(*iter);
316        const map<string,string>& mapOfModulesOfCurrentHost=(((*it).second).ModulesPath);
317        if(mapOfModulesOfCurrentHost.find(moduleName)==mapOfModulesOfCurrentHost.end())
318          {
319            hosts.erase(iter);
320          }
321      }
322 }
323
324 void SALOME_ResourcesManager::AddOmninamesParams(string& command) const
325 {
326   string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
327   ifstream omniORBfile( omniORBcfg.c_str() ) ;
328   char ORBInitRef[12] ;
329   char nameservice[132] ;
330   omniORBfile >> ORBInitRef ;
331   command += ORBInitRef ;
332   command += " " ;
333   omniORBfile >> nameservice ;
334   omniORBfile.close() ;
335   char * bsn = strchr( nameservice , '\n' ) ;
336   if ( bsn ) {
337     bsn[ 0 ] = '\0' ;
338   }
339   command += nameservice ;
340 }
341
342 void SALOME_ResourcesManager::AddOmninamesParams(ofstream& fileStream) const
343 {
344   string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
345   ifstream omniORBfile( omniORBcfg.c_str() ) ;
346   char ORBInitRef[12] ;
347   char nameservice[132] ;
348   omniORBfile >> ORBInitRef ;
349   fileStream << ORBInitRef;
350   fileStream << " ";
351   omniORBfile >> nameservice ;
352   omniORBfile.close() ;
353   char * bsn = strchr( nameservice , '\n' ) ;
354   if ( bsn ) {
355     bsn[ 0 ] = '\0' ;
356   }
357   fileStream << nameservice;
358 }
359
360 string SALOME_ResourcesManager::BuildTemporaryFileName() const
361 {
362   //build more complex file name to support multiple salome session
363   string command( "/tmp/" );
364   char *temp=new char[14];
365   strcpy(temp,"command");
366   strcat(temp,"XXXXXX");
367   mkstemp(temp);
368   command += temp;
369   delete [] temp;
370   command += ".sh";
371   return command;
372 }
373
374