[Freeipa-users] call implemented methods via xml-rpc

Jason Gerard DeRose jderose at redhat.com
Tue Apr 20 12:49:24 UTC 2010


On Tue, 2010-04-20 at 13:03 +0200, ALAHYANE Rachid wrote:
> Hi, 
> 
> 
> Now I have another error. When I use the code
> of doc/examples/python-api.py inside my server XML-RPC (not the ipa
> server) that configured like this :
> 
> 
> == Server ==
> --------- httpd conf ------------
> <Files "xmlrpc">
>   ## python conf
> 
>                                       
>   # ....
>   SetHandler python-program
>   PythonHandler xmlrpchandler
>   PythonDebug on
> </Files>
> ------------------------------------
> 
> 
> the handler xmlrpchandler calls the following method when the client
> requests for the remote method getUserInfos().
> 
> 
> --------- account.py ------------
> def getUserInfos(user_name, env=None):
> 
> 
>     from ipalib import api
> 
> 
>     api.bootstrap_with_global_options(context='webservices')
>     api.finalize()
>     api.Backend.xmlclient.connect()
>     return api.Command.user_show(user_name)  
> ------------------------------------
> 
> 
> 
> 
> == Client ==
> Now when I call  this method from my client, I get this exception : 
> 
> 
> ------------------------------------
> <Fault 2: "account.getUserInfos: <type 'exceptions.StandardError'>:
> API.bootstrap() already called">
> ------------------------------------
> 
> 
> I don't know why it does not work, any ideas ?? 
> 

Initializing ipalib is a somewhat expensive operation, so we only
initialize it once when the process starts.  If you're implementing a
new XML-RPC server that calls ipalib, you will need to slightly modify
the code in the python-api.py example, which I'll explain.

ipalib has 2 modes of operation: client and server.  In client mode,
only plugins in ipalib/plugins/ are loaded.  In server mode, plugins in
ipaserver/plugins/ are also loaded.

In a nutshell, client mode will do some sanity checks and the forward
the call to the server.  In server mode, the same sanity checks are
performed, and then the command is executed, which usually means
creating/modifying LDAP entries.

So assuming you want to initialize ipalib in server mode (sounds like
you do), you will need to do something like this (when the process
starts):

  from ipalib import api
  api.bootstrap(context='example', in_server=True)
  api.finalize()

Note the `in_server=True` that I added.  Then in your handler, you will
need to create a context for the request, something like this:

  def getUserInfos(user_name, env=None):
      # Where are you getting Kerberos credentials?
      api.Backend.ldap2.connect(
        ccache=api.Backend.krb.default_ccname()
      )
      return api.Command.user_show(user_name)

So the recipe is 1) initialize ipalib once at startup, and 2) create a
context (LDAP connection) at each request.  To see how we do this is our
RPC server, look at the ipaserver/rpcserver.py file.

Hope that helps.  I'm glad to see someone wanting to use the Python
API.  ;)

> Thanks,
> 
> 
> 2010/4/19 ALAHYANE Rachid <afkkir at gmail.com>
>         Thank you for your answer, it works ! 
>         
>         2010/4/19 Jason Gerard DeRose <jderose at redhat.com>
>         
>         
>                 On Mon, 2010-04-19 at 16:22 +0200, ALAHYANE Rachid
>                 wrote:
>                 > Hi,
>                 >
>                 >
>                 > Using F12 with the alpha version of ipa, I want to
>                 know if there is
>                 > some ways to call implemented methods like
>                  user_show() with my own
>                 > script python. My goal is to call these methods with
>                 a client xml-rpc
>                 > that I will to developpe later.
>                 >
>                 >
>                 > On my client, I tried this but it does not work :(
>                 
>                 
>                 It needs more documentation, but see
>                 doc/examples/python-api.py
>                 
>                 Let me know if that doesn't work or if you get stuck.
>                  You will need to
>                 do a kinit first.
>                 
>                 
>                 >
>                 ----------------------------------------------------------------------------
>                 > >>> from ipalib import api
>                 > >>> api.bootstrap_with_global_options()
>                 > (<Values at 0xb74f556c: {'debug': None, 'conf':
>                 None, 'env': None,
>                 > 'verbose': None}>, [])
>                 > >>> api.load_plugins()
>                 > >>> api.finalize()
>                 > >>> api.Method.user_show.__doc__
>                 > '\n    Display user.\n    '
>                 > >>> api.Method.user_show(u'raca')
>                 > Traceback (most recent call last):
>                 >   File "<stdin>", line 1, in <module>
>                 >   File
>                 "/usr/lib/python2.6/site-packages/ipalib/frontend.py",
>                 line
>                 > 398, in __call__
>                 >     ret = self.run(*args, **options)
>                 >   File
>                 "/usr/lib/python2.6/site-packages/ipalib/frontend.py",
>                 line
>                 > 667, in run
>                 >     return self.forward(*args, **options)
>                 >   File
>                 "/usr/lib/python2.6/site-packages/ipalib/frontend.py",
>                 line
>                 > 688, in forward
>                 >     return self.Backend.xmlclient.forward(self.name,
>                 *args, **kw)
>                 >   File
>                 "/usr/lib/python2.6/site-packages/ipalib/rpc.py", line
>                 403, in
>                 > forward
>                 >     command = getattr(self.conn, name)
>                 >   File
>                 "/usr/lib/python2.6/site-packages/ipalib/backend.py",
>                 line 96,
>                 > in __get_conn
>                 >     self.id, threading.currentThread().getName())
>                 > AttributeError: no context.xmlclient in thread
>                 'MainThread'
>                 >
>                 >
>                 >
>                 ----------------------------------------------------------------------------
>                 >
>                 >
>                 > Have you any idea ? or some pertinent docs
>                 >
>                 >
>                 > Sorry for my bad English :)
>                 >
>                 >
>                 > --
>                 > Meilleures salutations / Best Regards
>                 >
>                 > Rachid ALAHYANE
>                 >
>                 >
>                 
>                 > _______________________________________________
>                 > Freeipa-users mailing list
>                 > Freeipa-users at redhat.com
>                 >
>                 https://www.redhat.com/mailman/listinfo/freeipa-users
>                 
>         
>         
>         
>         
>         -- 
>         Meilleures salutations / Best Regards
>         
>         Rachid ALAHYANE
>         
>         
> 
> 
> 
> -- 
> Meilleures salutations / Best Regards
> 
> Rachid ALAHYANE
> 
> 




More information about the Freeipa-users mailing list