1 #include "SALOME_ResourcesManager.hxx"
2 #include "SALOME_Container_i.hxx"
3 #include "Utils_ExceptHandlers.hxx"
14 #define MAX_SIZE_FOR_HOSTNAME 256;
19 SALOME_ResourcesManager::SALOME_ResourcesManager(const char *xmlFilePath):_path_resources(xmlFilePath)
23 SALOME_ResourcesManager::SALOME_ResourcesManager()
25 _path_resources=getenv("KERNEL_ROOT_DIR");
26 _path_resources+="/share/salome/resources/CatalogResources.xml";
30 SALOME_ResourcesManager::~SALOME_ResourcesManager()
34 vector<string> SALOME_ResourcesManager::GetResourcesFitting(const MachineParameters& params,const char *moduleName) throw(SALOME_Exception)
36 vector <std::string> ret;
37 //To be sure that we search in a correct list.
39 const char *hostname=(const char *)params.hostname;
42 if(_resourcesList.find(hostname)!=_resourcesList.end())
43 // params.hostame is in the list of resources so return it.
44 ret.push_back(hostname);
46 //user specified an unknown hostame so notify to him.
47 throw SALOME_Exception("unknown host");
50 // Search for available resources sorted by priority
52 SelectOnlyResourcesWithOS(ret,params.OS);
53 KeepOnlyResourcesWithModule(ret,moduleName);
54 //set wanted parameters
55 ResourceDataToSort::_nbOfNodesWanted=params.nb_node;
56 ResourceDataToSort::_nbOfProcPerNodeWanted=params.nb_proc_per_node;
57 ResourceDataToSort::_CPUFreqMHzWanted=params.cpu_clock;
58 ResourceDataToSort::_memInMBWanted=params.mem_mb;
60 list<ResourceDataToSort> li;
61 for(vector<string>::iterator iter=ret.begin();iter!=ret.end();iter++)
62 li.push_back(_resourcesList[(*iter)].DataForSort);
65 for(list<ResourceDataToSort>::iterator iter2=li.begin();iter2!=li.end();iter2++)
66 ret[i++]=(*iter2)._hostName;
70 // // user don't specify parameters so default behaviour, only localhost returned
72 // char *hostName=new char[MAX_SIZE_FOR_HOSTNAME];
73 // gethostname(hostName,MAX_SIZE_FOR_HOSTNAME);
74 // ret.push_back(hostName);
75 // delete [] hostName;
80 int SALOME_ResourcesManager::AddResourceInCatalog(const MachineParameters& paramsOfNewResources, const map<string,string>& modulesOnNewResources,
81 const char *environPathOfPrerequired,
82 const char *alias, const char *userName, AccessModeType mode, AccessProtocolType prot) throw(SALOME_Exception)
84 map<string,string>::const_iterator iter=modulesOnNewResources.find("KERNEL");
85 if(iter!=modulesOnNewResources.end())
87 ParserResourcesType newElt;
88 newElt.DataForSort._hostName=paramsOfNewResources.hostname;
92 newElt.UserName=userName;
93 newElt.ModulesPath=modulesOnNewResources;
94 // for(int i=0;i<modulesOnNewResources.size();i++)
95 // newElt.ModulesPath[modulesOnNewResources[i]]=pathOfModulesOnNewResources[i];
96 newElt.PreReqFilePath=environPathOfPrerequired;
97 newElt.OS=paramsOfNewResources.OS;
98 newElt.DataForSort._memInMB=paramsOfNewResources.mem_mb;
99 newElt.DataForSort._CPUFreqMHz=paramsOfNewResources.cpu_clock;
100 newElt.DataForSort._nbOfNodes=paramsOfNewResources.nb_node;
101 newElt.DataForSort._nbOfProcPerNode=paramsOfNewResources.nb_proc_per_node;
102 _resourcesList[newElt.DataForSort._hostName]=newElt;
103 cout << "AddResourceInCatalog ... " << _resourcesList.size() << endl;
107 throw SALOME_Exception("KERNEL is not present in this resource");
110 void SALOME_ResourcesManager::DeleteResourceInCatalog(const char *hostname)
112 _resourcesList.erase(hostname);
115 void SALOME_ResourcesManager::WriteInXmlFile()
117 QDomDocument doc("ResourcesCatalog");
118 SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
119 handler->PrepareDocToXmlFile(doc);
121 QFile file( _path_resources );
122 if( !file.open( IO_WriteOnly ) )
123 cout << "WRITING ERROR !!!" << endl;
126 QTextStream ts( &file );
127 ts << doc.toString();
130 cout << "WRITING DONE!!!" << endl;
133 const MapOfParserResourcesType& SALOME_ResourcesManager::ParseXmlFile()
135 SALOME_ResourcesCatalog_Handler* handler = new SALOME_ResourcesCatalog_Handler(_resourcesList);
136 QFile xmlFile(_path_resources);
138 QXmlInputSource source(xmlFile);
140 QXmlSimpleReader reader;
141 reader.setContentHandler( handler );
142 reader.setErrorHandler( handler );
143 reader.parse( source );
146 return _resourcesList;
149 bool SALOME_ResourcesManager::_verify_resources(MapOfParserResourcesType resourceslist)
151 // bool _return_value = true;
152 // bool _bool = false ;
153 // vector<string> _machine_list;
154 // _machine_list.resize(0);
156 // // Fill a list of all computers indicated in the resources list
157 // for (unsigned int ind = 0; ind < resourceslist.size(); ind++)
158 // _machine_list.push_back(resourceslist[ind].HostName);
160 // // Parse if a computer name is twice in the list of computers
161 // for (unsigned int ind = 0; ind < _machine_list.size(); ind++)
163 // for (unsigned int ind1 = ind+1 ; ind1 < _machine_list.size(); ind1++)
165 // if(_machine_list[ind].compare(_machine_list[ind1]) == 0)
167 // MESSAGE("The computer " << _machine_list[ind] << " is indicated more than once in the resources list")
168 // _return_value = false;
173 // return _return_value;
177 const MapOfParserResourcesType& SALOME_ResourcesManager::GetList() const
179 return _resourcesList;
182 string SALOME_ResourcesManager::FindBest(const MachineList& listOfMachines)
184 return _dynamicResourcesSelecter.FindBest(listOfMachines);
187 string SALOME_ResourcesManager::BuildTempFileToLaunchRemoteContainer(const string& machine,const char *containerName)
189 _TmpFileName=BuildTemporaryFileName();
190 ofstream tempOutputFile(_TmpFileName.c_str());
191 const ParserResourcesType& resInfo=_resourcesList[machine];
192 tempOutputFile << "/bin/sh <<EOF" << endl;
194 for(map<string,string>::const_iterator iter=resInfo.ModulesPath.begin();iter!=resInfo.ModulesPath.end();iter++)
196 string curModulePath((*iter).second);
197 tempOutputFile << "export " << (*iter).first << "_ROOT_DIR="<< curModulePath << endl;
198 tempOutputFile << "export LD_LIBRARY_PATH=" << curModulePath << "/lib/salome" << ":${LD_LIBRARY_PATH}" << endl;
199 tempOutputFile << "export PYTHONPATH=" << curModulePath << "/bin/salome:" << curModulePath << "/lib/python2.2/site-packages/salome:";
200 tempOutputFile << curModulePath << "/lib/python2.2/site-packages/salome/shared_modules:${PYTHONPATH}" << endl;
202 tempOutputFile << "source " << resInfo.PreReqFilePath << endl;
204 tempOutputFile << (*(resInfo.ModulesPath.find("KERNEL"))).second << "/bin/salome/";
205 if(Engines_Container_i::isPythonContainer(containerName))
206 tempOutputFile << "SALOME_ContainerPy.py ";
208 tempOutputFile << "SALOME_Container ";
209 tempOutputFile << containerName << " -";
210 AddOmninamesParams(tempOutputFile);
211 tempOutputFile << " > /tmp/" << containerName << "_" << machine << ".log 2>&1 &" << endl;
212 tempOutputFile << "EOF" << endl;
213 tempOutputFile.flush();
214 tempOutputFile.close();
217 if(resInfo.Protocol==rsh)
219 else if(resInfo.Protocol==ssh)
222 throw SALOME_Exception("Unknown protocol");
224 _CommandForRemAccess=command;
226 command+=_TmpFileName;
230 string SALOME_ResourcesManager::BuildCommandToLaunchLocalContainer(const char *containerName)
234 if(Engines_Container_i::isPythonContainer(containerName))
235 command="SALOME_ContainerPy.py ";
237 command="SALOME_Container ";
238 command+=containerName;
240 AddOmninamesParams(command);
242 command+=containerName;
244 command += GetHostname();
245 command += ".log 2>&1 &" ;
249 void SALOME_ResourcesManager::RmTmpFile()
253 string command="rm ";
254 command+=_TmpFileName;
255 system(command.c_str());
259 string SALOME_ResourcesManager::BuildCommand(const string& machine,const char *containerName)
261 // rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 &
262 const ParserResourcesType& resInfo=_resourcesList[machine];
263 bool pyCont=Engines_Container_i::isPythonContainer(containerName);
265 if(resInfo.Protocol==rsh)
266 command = "rsh -n " ;
267 else if(resInfo.Protocol==ssh)
268 command = "ssh -f -n ";
270 throw SALOME_Exception("Not implemented yet...");
273 string path = (*(resInfo.ModulesPath.find("KERNEL"))).second;
275 command += "/bin/salome/";
277 command += "SALOME_ContainerPy.py ";
279 command += "SALOME_Container ";
280 command += containerName;
282 AddOmninamesParams(command);
283 command += " > /tmp/";
284 command += containerName;
287 command += ".log 2>&1 &" ;
290 // int status = system( command.c_str() ) ;
291 // if (status == -1) {
292 // MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status -1)") ;
294 // else if (status == 217) {
295 // MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status 217)") ;
299 // while ( CORBA::is_nil( aFactoryServer ) && count ) {
302 // if ( count != 10 )
303 // MESSAGE( count << ". Waiting for FactoryServer on " << machine)
304 // aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
306 // if ( CORBA::is_nil( aFactoryServer ) ) {
307 // MESSAGE("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ;
309 // else if ( strcmp( theComputer.c_str() , GetHostname().c_str() ) ) {
310 // _FactoryServer = aFactoryServer ;
315 // Warning need an updated parsed list : _resourcesList
316 void SALOME_ResourcesManager::SelectOnlyResourcesWithOS(vector<string>& hosts,const char *OS) const throw(SALOME_Exception)
319 for(map<string, ParserResourcesType>::const_iterator iter=_resourcesList.begin();iter!=_resourcesList.end();iter++)
320 if((*iter).second.OS==base)
321 hosts.push_back((*iter).first);
324 //Warning need an updated parsed list : _resourcesList
325 void SALOME_ResourcesManager::KeepOnlyResourcesWithModule(vector<string>& hosts,const char *moduleName) const throw(SALOME_Exception)
327 for(vector<string>::iterator iter=hosts.begin();iter!=hosts.end();iter++)
329 MapOfParserResourcesType::const_iterator it=_resourcesList.find(*iter);
330 const map<string,string>& mapOfModulesOfCurrentHost=(((*it).second).ModulesPath);
331 if(mapOfModulesOfCurrentHost.find(moduleName)==mapOfModulesOfCurrentHost.end())
338 void SALOME_ResourcesManager::AddOmninamesParams(string& command) const
340 string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
341 ifstream omniORBfile( omniORBcfg.c_str() ) ;
342 char ORBInitRef[12] ;
343 char nameservice[132] ;
344 omniORBfile >> ORBInitRef ;
345 command += ORBInitRef ;
347 omniORBfile >> nameservice ;
348 omniORBfile.close() ;
349 char * bsn = strchr( nameservice , '\n' ) ;
353 command += nameservice ;
356 void SALOME_ResourcesManager::AddOmninamesParams(ofstream& fileStream) const
358 string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
359 ifstream omniORBfile( omniORBcfg.c_str() ) ;
360 char ORBInitRef[12] ;
361 char nameservice[132] ;
362 omniORBfile >> ORBInitRef ;
363 fileStream << ORBInitRef;
365 omniORBfile >> nameservice ;
366 omniORBfile.close() ;
367 char * bsn = strchr( nameservice , '\n' ) ;
371 fileStream << nameservice;
374 string SALOME_ResourcesManager::BuildTemporaryFileName() const
376 //build more complex file name to support multiple salome session
377 return "/tmp/command.sh";