From 9f3b977350e44418adfffada39e0e49373a52b6a Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Wed, 27 Sep 2023 13:54:41 +0200 Subject: [PATCH] [EDF28648] : On container launch time from ContainerManager it s possible to execute python code for initialization --- idl/SALOME_Component.idl | 2 ++ idl/SALOME_ContainerManager.idl | 2 ++ src/Container/Container_i.cxx | 31 +++++++++++++++++++++-- src/Container/SALOME_ContainerManager.cxx | 7 +++++ src/Container/SALOME_ContainerManager.hxx | 3 +++ src/Container/SALOME_Container_i.hxx | 2 ++ src/Launcher_SWIG/Launcher.i | 26 +++++++++++++++++++ 7 files changed, 71 insertions(+), 2 deletions(-) diff --git a/idl/SALOME_Component.idl b/idl/SALOME_Component.idl index 11a21e1cc..2e3b662f4 100644 --- a/idl/SALOME_Component.idl +++ b/idl/SALOME_Component.idl @@ -84,6 +84,8 @@ module Engines FieldsDict get_os_environment(); + void execute_python_code( in string code ) raises(SALOME::SALOME_Exception); + /*! \brief Loads a new component class (dynamic library). \param componentName like COMPONENT, (Python or C++ implementation) diff --git a/idl/SALOME_ContainerManager.idl b/idl/SALOME_ContainerManager.idl index 9251df6ab..474d51642 100644 --- a/idl/SALOME_ContainerManager.idl +++ b/idl/SALOME_ContainerManager.idl @@ -100,6 +100,8 @@ interface ContainerManager void SetOverrideEnvForContainers(in KeyValDict env); KeyValDict GetOverrideEnvForContainers(); + + void SetCodeOnContainerStartUp(in string code); } ; }; diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 8e740c5a8..dd8ea1843 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -32,6 +32,7 @@ #include #include #include +#include #ifndef WIN32 #include #include @@ -63,6 +64,7 @@ int SIGUSR1 = 1000; #include "SALOME_Embedded_NamingService.hxx" #include "Basics_Utils.hxx" #include "PythonCppUtils.hxx" +#include "Utils_CorbaException.hxx" #ifdef _XOPEN_SOURCE #undef _XOPEN_SOURCE @@ -1090,6 +1092,16 @@ Engines::FieldsDict *Abstract_Engines_Container_i::get_os_environment() return ret.release(); } +void Abstract_Engines_Container_i::execute_python_code(const char *code) +{ + AutoGIL gstate; + if( PyRun_SimpleString( code ) != 0 ) + { + std::string error = parseException(); + THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR); + } +} + //============================================================================= //! Create a new component instance with environment variables specified /*! @@ -2330,9 +2342,24 @@ Engines_Container_SSL_i *KERNEL::getContainerSA() CORBA::PolicyList policies; policies.length(0); // - char *argv[4] = {"Container","FactoryServer","toto",nullptr}; + constexpr int ARGC = 4; + constexpr const char *ARGV[ARGC] = {"Container","FactoryServer","toto",nullptr}; + std::unique_ptr argv( new char *[ARGC+1] ); + std::vector< std::unique_ptr > argvv(ARGC); + argv[ARGC] = nullptr; + for(int i = 0 ; i < ARGC ; ++i) + { + if(ARGV[i]) + { + argvv[i].reset( new char[strlen(ARGV[i])+1] ); + strcpy(argvv[i].get(),ARGV[i]); + argv[i] = argvv[i].get(); + } + else + argv[i] = nullptr; + } SALOME_Fake_NamingService ns; - _container_singleton_ssl = new Engines_Container_SSL_i(orb,poa,"FactoryServer",2,argv,&ns,false); + _container_singleton_ssl = new Engines_Container_SSL_i(orb,poa,(char *)"FactoryServer",2,argv.get(),&ns,false); PortableServer::ObjectId * cont_id = _container_singleton_ssl->getCORBAId(); // CORBA::Object_var zeRef = poa->id_to_reference(*cont_id); diff --git a/src/Container/SALOME_ContainerManager.cxx b/src/Container/SALOME_ContainerManager.cxx index adff087e5..150897038 100644 --- a/src/Container/SALOME_ContainerManager.cxx +++ b/src/Container/SALOME_ContainerManager.cxx @@ -324,6 +324,11 @@ Engines::KeyValDict *SALOME_ContainerManager::GetOverrideEnvForContainers() return ret.release(); } +void SALOME_ContainerManager::SetCodeOnContainerStartUp(const char *code) +{ + _code_to_exe_on_startup = code; +} + //============================================================================= //! Give a suitable Container given constraints /*! CORBA Method: @@ -498,6 +503,8 @@ Engines::Container_ptr SALOME_ContainerManager::GiveContainer(const Engines::Con } } cont->override_environment_python( envCorba ); + if( !_code_to_exe_on_startup.empty() ) + cont->execute_python_code( _code_to_exe_on_startup.c_str() ); return cont._retn(); } else diff --git a/src/Container/SALOME_ContainerManager.hxx b/src/Container/SALOME_ContainerManager.hxx index 67a333482..b579d5c3d 100644 --- a/src/Container/SALOME_ContainerManager.hxx +++ b/src/Container/SALOME_ContainerManager.hxx @@ -56,6 +56,8 @@ public: Engines::KeyValDict *GetOverrideEnvForContainers() override; + void SetCodeOnContainerStartUp(const char *code) override; + // C++ Methods void Shutdown(); @@ -212,5 +214,6 @@ private: std::vector< std::pair > _override_env; int _time_out_in_second; int _delta_time_ns_lookup_in_ms; + std::string _code_to_exe_on_startup; }; #endif diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index f68b9652c..ca2fac0a3 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -77,6 +77,8 @@ public: void override_environment( const Engines::FieldsDict& env ) override; Engines::FieldsDict *get_os_environment() override; + + void execute_python_code(const char *code) override; virtual Engines::EngineComponent_ptr create_component_instance_env(const char *componentName, diff --git a/src/Launcher_SWIG/Launcher.i b/src/Launcher_SWIG/Launcher.i index f01e3126e..2681d2dbe 100644 --- a/src/Launcher_SWIG/Launcher.i +++ b/src/Launcher_SWIG/Launcher.i @@ -365,6 +365,32 @@ def BuildCatalogFromScratch(protocol): contRes = CreateContainerResource(hostname=k,applipath=os.environ["APPLI"],protocol=protocol,nbOfNodes=v) rmcpp.AddResourceInCatalog(contRes) +def GetRequestForGiveContainer(hostname, contName): + import Engines + import os + rp=Engines.ResourceParameters(name=hostname, + hostname=hostname, + can_launch_batch_jobs=False, + can_run_containers=True, + OS="Linux", + componentList=[], + nb_proc=1, + mem_mb=1000, + cpu_clock=1000, + nb_node=1, + nb_proc_per_node=1, + policy="first", + resList=[]) + + cp=Engines.ContainerParameters(container_name=contName, + mode="start", + workingdir=os.path.expanduser("~"), + nb_proc=1, + isMPI=False, + parallelLib="", + resource_params=rp) + return cp + ResourceDefinition_cpp.repr = ResourceDefinition_cpp_repr ResourceDefinition_cpp.__repr__ = ResourceDefinition_cpp_repr ResourcesManager_cpp.GetList = ResourcesManager_cpp_GetList -- 2.39.2