From: John Spray Date: Sun, 5 Mar 2017 15:30:05 +0000 (-0500) Subject: pybind/rados: enable construction with CephContext X-Git-Tag: v12.0.3~79^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=abd9f26793da35a667813827e936b037bd882ab6;p=ceph.git pybind/rados: enable construction with CephContext The CephContext pointer is passed around inside a Python capsule. It would have initially been created by some other (non-cython) bit of C++ code, such as ceph-mgr. This enables mgr modules to instantiate librados with all the same config/auth/id as the mgr daemon that they are running within. Useful if you want to spin up e.g. a librbd instance. Signed-off-by: John Spray --- diff --git a/src/pybind/rados/rados.pyx b/src/pybind/rados/rados.pyx index 02c1f82d7980..54a818507f06 100644 --- a/src/pybind/rados/rados.pyx +++ b/src/pybind/rados/rados.pyx @@ -14,6 +14,7 @@ method. # Copyright 2016 Mehdi Abaakouk from cpython cimport PyObject, ref +from cpython.pycapsule cimport * from libc cimport errno from libc.stdint cimport * from libc.stdlib cimport malloc, realloc, free @@ -124,6 +125,7 @@ cdef extern from "rados/librados.h" nogil: void rados_version(int *major, int *minor, int *extra) int rados_create2(rados_t *pcluster, const char *const clustername, const char * const name, uint64_t flags) + int rados_create_with_context(rados_t *cluster, rados_config_t cct) int rados_connect(rados_t cluster) void rados_shutdown(rados_t cluster) int rados_conf_read_file(rados_t cluster, const char *path) @@ -576,7 +578,8 @@ cdef class Rados(object): @requires(('rados_id', opt(str_type)), ('name', opt(str_type)), ('clustername', opt(str_type)), ('conffile', opt(str_type))) def __setup(self, rados_id=None, name=None, clustername=None, - conf_defaults=None, conffile=None, conf=None, flags=0): + conf_defaults=None, conffile=None, conf=None, flags=0, + context=None): self.monitor_callback = None self.parsed_args = [] self.conf_defaults = conf_defaults @@ -600,8 +603,14 @@ cdef class Rados(object): int _flags = flags int ret - with nogil: - ret = rados_create2(&self.cluster, _clustername, _name, _flags) + if context: + # Unpack void* (aka rados_config_t) from capsule + rados_config = PyCapsule_GetPointer(context, NULL) + with nogil: + ret = rados_create_with_context(&self.cluster, rados_config) + else: + with nogil: + ret = rados_create2(&self.cluster, _clustername, _name, _flags) if ret != 0: raise Error("rados_initialize failed with error code: %d" % ret)