1 #include "SALOME_ResourcesManager.hxx"
2 #include "SALOME_Container_i.hxx"
3 #include "Utils_ExceptHandlers.hxx"
16 #include <sys/types.h>
19 #define MAX_SIZE_FOR_HOSTNAME 256;
24 SALOME_ResourcesManager::SALOME_ResourcesManager(const char *xmlFilePath):_path_resources(xmlFilePath)
28 SALOME_ResourcesManager::SALOME_ResourcesManager()
30 _path_resources=getenv("KERNEL_ROOT_DIR");
31 _path_resources+="/share/salome/resources/CatalogResources.xml";
35 SALOME_ResourcesManager::~SALOME_ResourcesManager()
39 vector<string> SALOME_ResourcesManager::GetFittingResources(const Engines::MachineParameters& params,const char *moduleName) throw(SALOME_Exception)
41 vector <std::string> ret;
42 //To be sure that we search in a correct list.
44 const char *hostname=(const char *)params.hostname;
47 if(_resourcesList.find(hostname)!=_resourcesList.end())
48 // params.hostame is in the list of resources so return it.
49 ret.push_back(hostname);
51 //user specified an unknown hostame so notify to him.
52 throw SALOME_Exception("unknown host");
55 // Search for available resources sorted by priority
57 SelectOnlyResourcesWithOS(ret,params.OS);
58 KeepOnlyResourcesWithModule(ret,moduleName);
59 //set wanted parameters
60 ResourceDataToSort::_nbOfNodesWanted=params.nb_node;
61 ResourceDataToSort::_nbOfProcPerNodeWanted=params.nb_proc_per_node;
62 ResourceDataToSort::_CPUFreqMHzWanted=params.cpu_clock;
63 ResourceDataToSort::_memInMBWanted=params.mem_mb;
65 list<ResourceDataToSort> li;
66 for(vector<string>::iterator iter=ret.begin();iter!=ret.end();iter++)
67 li.push_back(_resourcesList[(*iter)].DataForSort);
70 for(list<ResourceDataToSort>::iterator iter2=li.begin();iter2!=li.end();iter2++)
71 ret[i++]=(*iter2)._hostName;
76 int SALOME_ResourcesManager::AddResourceInCatalog(const Engines::MachineParameters& paramsOfNewResources, const map<string,string>& modulesOnNewResources,
77 const char *environPathOfPrerequired,
78 const char *alias, const char *userName, AccessModeType mode, AccessProtocolType prot) throw(SALOME_Exception)
80 map<string,string>::const_iterator iter=modulesOnNewResources.find("KERNEL");
81 if(iter!=modulesOnNewResources.end())
83 ParserResourcesType newElt;
84 newElt.DataForSort._hostName=paramsOfNewResources.hostname;
88 newElt.UserName=userName;
89 newElt.ModulesPath=modulesOnNewResources;
90 newElt.PreReqFilePath=environPathOfPrerequired;
91 newElt.OS=paramsOfNewResources.OS;
92 newElt.DataForSort._memInMB=paramsOfNewResources.mem_mb;
93 newElt.DataForSort._CPUFreqMHz=paramsOfNewResources.cpu_clock;
94 newElt.DataForSort._nbOfNodes=paramsOfNewResources.nb_node;
95 newElt.DataForSort._nbOfProcPerNode=paramsOfNewResources.nb_proc_per_node;
96 _resourcesList[newElt.DataForSort._hostName]=newElt;
100 throw SALOME_Exception("KERNEL is not present in this resource");
103 void SALOME_ResourcesManager::DeleteResourceInCatalog(const char *hostname)
105 _resourcesList.erase(hostname);
108 void SALOME_ResourcesManager::WriteInXmlFile()
110 QDomDocument doc("ResourcesCatalog");
111 SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
112 handler->PrepareDocToXmlFile(doc);
114 QFile file( _path_resources );
115 if( !file.open( IO_WriteOnly ) )
116 cout << "WRITING ERROR !!!" << endl;
118 QTextStream ts( &file );
119 ts << doc.toString();
122 cout << "WRITING DONE!!!" << endl;
125 const MapOfParserResourcesType& SALOME_ResourcesManager::ParseXmlFile()
127 SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
128 QFile xmlFile(_path_resources);
130 QXmlInputSource source(xmlFile);
132 QXmlSimpleReader reader;
133 reader.setContentHandler( handler );
134 reader.setErrorHandler( handler );
135 reader.parse( source );
138 return _resourcesList;
141 bool SALOME_ResourcesManager::_verify_resources(MapOfParserResourcesType resourceslist)
143 // bool _return_value = true;
144 // bool _bool = false ;
145 // vector<string> _machine_list;
146 // _machine_list.resize(0);
148 // // Fill a list of all computers indicated in the resources list
149 // for (unsigned int ind = 0; ind < resourceslist.size(); ind++)
150 // _machine_list.push_back(resourceslist[ind].HostName);
152 // // Parse if a computer name is twice in the list of computers
153 // for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
155 // for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
157 // if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
159 // MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the resources list")
160 // _return_value = false;
165 // return _return_value;
169 const MapOfParserResourcesType& SALOME_ResourcesManager::GetList() const
171 return _resourcesList;
174 string SALOME_ResourcesManager::FindBest(const Engines::MachineList& listOfMachines)
176 return _dynamicResourcesSelecter.FindBest(listOfMachines);
179 string SALOME_ResourcesManager::BuildTempFileToLaunchRemoteContainer(const string& machine,const char *containerName)
181 _TmpFileName=BuildTemporaryFileName();
182 ofstream tempOutputFile;
183 tempOutputFile.open(_TmpFileName.c_str(),ofstream::out );
184 const ParserResourcesType& resInfo=_resourcesList[machine];
185 tempOutputFile << "#! /bin/sh" << endl;
187 for(map<string,string>::const_iterator iter=resInfo.ModulesPath.begin();iter!=resInfo.ModulesPath.end();iter++)
189 string curModulePath((*iter).second);
190 tempOutputFile << (*iter).first << "_ROOT_DIR="<< curModulePath << endl;
191 tempOutputFile << "export " << (*iter).first << "_ROOT_DIR" << endl;
192 tempOutputFile << "LD_LIBRARY_PATH=" << curModulePath << "/lib/salome" << ":${LD_LIBRARY_PATH}" << endl;
193 tempOutputFile << "PYTHONPATH=" << curModulePath << "/bin/salome:" << curModulePath << "/lib/salome:" << curModulePath << "/lib/python2.2/site-packages/salome:";
194 tempOutputFile << curModulePath << "/lib/python2.2/site-packages/salome/shared_modules:${PYTHONPATH}" << endl;
196 tempOutputFile << "export LD_LIBRARY_PATH" << endl;
197 tempOutputFile << "export PYTHONPATH" << endl;
198 tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
200 tempOutputFile << (*(resInfo.ModulesPath.find("KERNEL"))).second << "/bin/salome/";
201 if(Engines_Container_i::isPythonContainer(containerName))
202 tempOutputFile << "SALOME_ContainerPy.py ";
204 tempOutputFile << "SALOME_Container ";
205 tempOutputFile << containerName << " -";
206 AddOmninamesParams(tempOutputFile);
207 tempOutputFile << " &" << endl;
208 tempOutputFile.flush();
209 tempOutputFile.close();
210 chmod(_TmpFileName.c_str(),0x1ED);
213 if(resInfo.Protocol==rsh)
216 string commandRcp="rcp ";
217 commandRcp+=_TmpFileName;
221 commandRcp+=_TmpFileName;
222 system(commandRcp.c_str());
224 else if(resInfo.Protocol==ssh)
227 throw SALOME_Exception("Unknown protocol");
229 _CommandForRemAccess=command;
231 command+=_TmpFileName;
234 command += containerName;
238 cout << "Command is ... " << command << endl;
242 string SALOME_ResourcesManager::BuildCommandToLaunchLocalContainer(const char *containerName)
246 if(Engines_Container_i::isPythonContainer(containerName))
247 command="SALOME_ContainerPy.py ";
249 command="SALOME_Container ";
250 command+=containerName;
252 AddOmninamesParams(command);
254 command+=containerName;
256 command += GetHostname();
257 command += ".log 2>&1 &" ;
258 cout << "Command is ... " << command << endl << flush;
262 void SALOME_ResourcesManager::RmTmpFile()
266 string command="rm ";
267 command+=_TmpFileName;
268 char *temp=strdup(command.c_str());
269 int lgthTemp=strlen(temp);
270 temp[lgthTemp-3]='*';
271 temp[lgthTemp-2]='\0';
277 string SALOME_ResourcesManager::BuildCommand(const string& machine,const char *containerName)
279 // rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 &
280 const ParserResourcesType& resInfo=_resourcesList[machine];
281 bool pyCont=Engines_Container_i::isPythonContainer(containerName);
283 if(resInfo.Protocol==rsh)
284 command = "rsh -n " ;
285 else if(resInfo.Protocol==ssh)
286 command = "ssh -f -n ";
288 throw SALOME_Exception("Not implemented yet...");
291 string path = (*(resInfo.ModulesPath.find("KERNEL"))).second;
293 command += "/bin/salome/";
295 command += "SALOME_ContainerPy.py ";
297 command += "SALOME_Container ";
298 command += containerName;
300 AddOmninamesParams(command);
301 command += " > /tmp/";
302 command += containerName;
305 command += ".log 2>&1 &" ;
310 // Warning need an updated parsed list : _resourcesList
311 void SALOME_ResourcesManager::SelectOnlyResourcesWithOS(vector<string>& hosts,const char *OS) const throw(SALOME_Exception)
314 for(map<string, ParserResourcesType>::const_iterator iter=_resourcesList.begin();iter!=_resourcesList.end();iter++)
315 if((*iter).second.OS==base)
316 hosts.push_back((*iter).first);
319 //Warning need an updated parsed list : _resourcesList
320 void SALOME_ResourcesManager::KeepOnlyResourcesWithModule(vector<string>& hosts,const char *moduleName) const throw(SALOME_Exception)
322 for(vector<string>::iterator iter=hosts.begin();iter!=hosts.end();)
324 MapOfParserResourcesType::const_iterator it=_resourcesList.find(*iter);
325 const map<string,string>& mapOfModulesOfCurrentHost=(((*it).second).ModulesPath);
326 if(mapOfModulesOfCurrentHost.find(moduleName)==mapOfModulesOfCurrentHost.end())
333 void SALOME_ResourcesManager::AddOmninamesParams(string& command) const
335 string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
336 ifstream omniORBfile( omniORBcfg.c_str() ) ;
337 char ORBInitRef[12] ;
338 char nameservice[132] ;
339 omniORBfile >> ORBInitRef ;
340 command += ORBInitRef ;
342 omniORBfile >> nameservice ;
343 omniORBfile.close() ;
344 char * bsn = strchr( nameservice , '\n' ) ;
348 command += nameservice ;
351 void SALOME_ResourcesManager::AddOmninamesParams(ofstream& fileStream) const
353 string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
354 ifstream omniORBfile( omniORBcfg.c_str() ) ;
355 char ORBInitRef[12] ;
356 char nameservice[132] ;
357 omniORBfile >> ORBInitRef ;
358 fileStream << ORBInitRef;
360 omniORBfile >> nameservice ;
361 omniORBfile.close() ;
362 char * bsn = strchr( nameservice , '\n' ) ;
366 fileStream << nameservice;
369 string SALOME_ResourcesManager::BuildTemporaryFileName() const
371 //build more complex file name to support multiple salome session
372 char *temp=new char[19];
373 strcpy(temp,"/tmp/command");
374 strcat(temp,"XXXXXX");
376 string command(temp);