1 #include "SALOME_ResourcesManager.hxx"
2 #include "SALOME_Container_i.hxx"
3 #include "Utils_ExceptHandlers.hxx"
15 #include <sys/types.h>
18 #define MAX_SIZE_FOR_HOSTNAME 256;
23 SALOME_ResourcesManager::SALOME_ResourcesManager(const char *xmlFilePath):_path_resources(xmlFilePath)
27 SALOME_ResourcesManager::SALOME_ResourcesManager()
29 _path_resources=getenv("KERNEL_ROOT_DIR");
30 _path_resources+="/share/salome/resources/CatalogResources.xml";
34 SALOME_ResourcesManager::~SALOME_ResourcesManager()
38 vector<string> SALOME_ResourcesManager::GetFittingResources(const Engines::MachineParameters& params,const char *moduleName) throw(SALOME_Exception)
40 vector <std::string> ret;
41 //To be sure that we search in a correct list.
43 const char *hostname=(const char *)params.hostname;
46 if(_resourcesList.find(hostname)!=_resourcesList.end())
47 // params.hostame is in the list of resources so return it.
48 ret.push_back(hostname);
50 //user specified an unknown hostame so notify to him.
51 throw SALOME_Exception("unknown host");
54 // Search for available resources sorted by priority
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;
64 list<ResourceDataToSort> li;
65 for(vector<string>::iterator iter=ret.begin();iter!=ret.end();iter++)
66 li.push_back(_resourcesList[(*iter)].DataForSort);
69 for(list<ResourceDataToSort>::iterator iter2=li.begin();iter2!=li.end();iter2++)
70 ret[i++]=(*iter2)._hostName;
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)
79 map<string,string>::const_iterator iter=modulesOnNewResources.find("KERNEL");
80 if(iter!=modulesOnNewResources.end())
82 ParserResourcesType newElt;
83 newElt.DataForSort._hostName=paramsOfNewResources.hostname;
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;
99 throw SALOME_Exception("KERNEL is not present in this resource");
102 void SALOME_ResourcesManager::DeleteResourceInCatalog(const char *hostname)
104 _resourcesList.erase(hostname);
107 void SALOME_ResourcesManager::WriteInXmlFile()
109 QDomDocument doc("ResourcesCatalog");
110 SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
111 handler->PrepareDocToXmlFile(doc);
113 QFile file( _path_resources );
114 if( !file.open( IO_WriteOnly ) )
115 cout << "WRITING ERROR !!!" << endl;
117 QTextStream ts( &file );
118 ts << doc.toString();
121 cout << "WRITING DONE!!!" << endl;
124 const MapOfParserResourcesType& SALOME_ResourcesManager::ParseXmlFile()
126 SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
127 QFile xmlFile(_path_resources);
129 QXmlInputSource source(xmlFile);
131 QXmlSimpleReader reader;
132 reader.setContentHandler( handler );
133 reader.setErrorHandler( handler );
134 reader.parse( source );
137 return _resourcesList;
140 bool SALOME_ResourcesManager::_verify_resources(MapOfParserResourcesType resourceslist)
142 // bool _return_value = true;
143 // bool _bool = false ;
144 // vector<string> _machine_list;
145 // _machine_list.resize(0);
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);
151 // // Parse if a computer name is twice in the list of computers
152 // for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
154 // for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
156 // if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
158 // MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the resources list")
159 // _return_value = false;
164 // return _return_value;
168 const MapOfParserResourcesType& SALOME_ResourcesManager::GetList() const
170 return _resourcesList;
173 string SALOME_ResourcesManager::FindBest(const Engines::MachineList& listOfMachines)
175 return _dynamicResourcesSelecter.FindBest(listOfMachines);
178 string SALOME_ResourcesManager::BuildTempFileToLaunchRemoteContainer(const string& machine,const char *containerName)
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;
186 for(map<string,string>::const_iterator iter=resInfo.ModulesPath.begin();iter!=resInfo.ModulesPath.end();iter++)
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;
195 tempOutputFile << "export LD_LIBRARY_PATH" << endl;
196 tempOutputFile << "export PYTHONPATH" << endl;
197 tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
199 tempOutputFile << (*(resInfo.ModulesPath.find("KERNEL"))).second << "/bin/salome/";
200 if(Engines_Container_i::isPythonContainer(containerName))
201 tempOutputFile << "SALOME_ContainerPy.py ";
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);
214 if(resInfo.Protocol==rsh)
217 string commandRcp="rcp ";
218 commandRcp+=_TmpFileName;
222 commandRcp+=_TmpFileName;
223 system(commandRcp.c_str());
225 else if(resInfo.Protocol==ssh)
228 throw SALOME_Exception("Unknown protocol");
230 _CommandForRemAccess=command;
232 command+=_TmpFileName;
234 cout << "Command is ... " << command << endl;
238 string SALOME_ResourcesManager::BuildCommandToLaunchLocalContainer(const char *containerName)
242 if(Engines_Container_i::isPythonContainer(containerName))
243 command="SALOME_ContainerPy.py ";
245 command="SALOME_Container ";
246 command+=containerName;
248 AddOmninamesParams(command);
250 command+=containerName;
252 command += GetHostname();
253 command += ".log 2>&1 &" ;
254 cout << "Command is ... " << command << endl << flush;
258 void SALOME_ResourcesManager::RmTmpFile()
262 string command="rm ";
263 command+=_TmpFileName;
264 system(command.c_str());
268 string SALOME_ResourcesManager::BuildCommand(const string& machine,const char *containerName)
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);
274 if(resInfo.Protocol==rsh)
275 command = "rsh -n " ;
276 else if(resInfo.Protocol==ssh)
277 command = "ssh -f -n ";
279 throw SALOME_Exception("Not implemented yet...");
282 string path = (*(resInfo.ModulesPath.find("KERNEL"))).second;
284 command += "/bin/salome/";
286 command += "SALOME_ContainerPy.py ";
288 command += "SALOME_Container ";
289 command += containerName;
291 AddOmninamesParams(command);
292 command += " > /tmp/";
293 command += containerName;
296 command += ".log 2>&1 &" ;
301 // Warning need an updated parsed list : _resourcesList
302 void SALOME_ResourcesManager::SelectOnlyResourcesWithOS(vector<string>& hosts,const char *OS) const throw(SALOME_Exception)
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);
310 //Warning need an updated parsed list : _resourcesList
311 void SALOME_ResourcesManager::KeepOnlyResourcesWithModule(vector<string>& hosts,const char *moduleName) const throw(SALOME_Exception)
313 for(vector<string>::iterator iter=hosts.begin();iter!=hosts.end();iter++)
315 MapOfParserResourcesType::const_iterator it=_resourcesList.find(*iter);
316 const map<string,string>& mapOfModulesOfCurrentHost=(((*it).second).ModulesPath);
317 if(mapOfModulesOfCurrentHost.find(moduleName)==mapOfModulesOfCurrentHost.end())
324 void SALOME_ResourcesManager::AddOmninamesParams(string& command) const
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 ;
333 omniORBfile >> nameservice ;
334 omniORBfile.close() ;
335 char * bsn = strchr( nameservice , '\n' ) ;
339 command += nameservice ;
342 void SALOME_ResourcesManager::AddOmninamesParams(ofstream& fileStream) const
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;
351 omniORBfile >> nameservice ;
352 omniORBfile.close() ;
353 char * bsn = strchr( nameservice , '\n' ) ;
357 fileStream << nameservice;
360 string SALOME_ResourcesManager::BuildTemporaryFileName() const
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");