1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "BaseMgrStandbyModule.h"
17 #include "StandbyPyModules.h"
20 #define dout_context g_ceph_context
21 #define dout_subsys ceph_subsys_mgr
25 StandbyPyModule
*this_module
;
26 } BaseMgrStandbyModule
;
29 BaseMgrStandbyModule_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
31 BaseMgrStandbyModule
*self
;
33 self
= (BaseMgrStandbyModule
*)type
->tp_alloc(type
, 0);
35 return (PyObject
*)self
;
39 BaseMgrStandbyModule_init(BaseMgrStandbyModule
*self
, PyObject
*args
, PyObject
*kwds
)
41 PyObject
*this_module_capsule
= nullptr;
42 static const char *kwlist
[] = {"this_module", NULL
};
44 if (! PyArg_ParseTupleAndKeywords(args
, kwds
, "O",
45 const_cast<char**>(kwlist
),
46 &this_module_capsule
)) {
50 self
->this_module
= static_cast<StandbyPyModule
*>(PyCapsule_GetPointer(
51 this_module_capsule
, nullptr));
52 ceph_assert(self
->this_module
);
58 ceph_get_mgr_id(BaseMgrStandbyModule
*self
, PyObject
*args
)
60 return PyString_FromString(g_conf()->name
.get_id().c_str());
64 ceph_get_module_option(BaseMgrStandbyModule
*self
, PyObject
*args
)
67 char *prefix
= nullptr;
68 if (!PyArg_ParseTuple(args
, "s|s:ceph_get_module_option", &what
, &prefix
)) {
69 derr
<< "Invalid args!" << dendl
;
72 std::string final_key
;
76 final_key
= std::string(prefix
) + "/" + what
;
77 found
= self
->this_module
->get_config(final_key
, &value
);
81 found
= self
->this_module
->get_config(final_key
, &value
);
84 dout(10) << __func__
<< " " << final_key
<< " found: " << value
86 return self
->this_module
->py_module
->get_typed_option_value(what
, value
);
89 dout(4) << __func__
<< " [" << prefix
<< "/]" << what
<< " not found "
92 dout(4) << __func__
<< " " << what
<< " not found " << dendl
;
99 ceph_option_get(BaseMgrStandbyModule
*self
, PyObject
*args
)
101 char *what
= nullptr;
102 if (!PyArg_ParseTuple(args
, "s:ceph_option_get", &what
)) {
103 derr
<< "Invalid args!" << dendl
;
108 int r
= g_conf().get_val(string(what
), &value
);
110 dout(10) << "ceph_option_get " << what
<< " found: " << value
<< dendl
;
111 return PyString_FromString(value
.c_str());
113 dout(4) << "ceph_option_get " << what
<< " not found " << dendl
;
119 ceph_store_get(BaseMgrStandbyModule
*self
, PyObject
*args
)
121 char *what
= nullptr;
122 if (!PyArg_ParseTuple(args
, "s:ceph_store_get", &what
)) {
123 derr
<< "Invalid args!" << dendl
;
127 // Drop GIL for blocking mon command execution
128 PyThreadState
*tstate
= PyEval_SaveThread();
131 bool found
= self
->this_module
->get_store(what
, &value
);
133 PyEval_RestoreThread(tstate
);
136 dout(10) << "ceph_store_get " << what
<< " found: " << value
.c_str() << dendl
;
137 return PyString_FromString(value
.c_str());
139 dout(4) << "ceph_store_get " << what
<< " not found " << dendl
;
145 ceph_get_active_uri(BaseMgrStandbyModule
*self
, PyObject
*args
)
147 return PyString_FromString(self
->this_module
->get_active_uri().c_str());
151 ceph_log(BaseMgrStandbyModule
*self
, PyObject
*args
)
154 char *record
= nullptr;
155 if (!PyArg_ParseTuple(args
, "is:log", &level
, &record
)) {
159 ceph_assert(self
->this_module
);
161 self
->this_module
->log(level
, record
);
166 PyMethodDef BaseMgrStandbyModule_methods
[] = {
168 {"_ceph_get_mgr_id", (PyCFunction
)ceph_get_mgr_id
, METH_NOARGS
,
169 "Get the name of the Mgr daemon where we are running"},
171 {"_ceph_get_module_option", (PyCFunction
)ceph_get_module_option
, METH_VARARGS
,
172 "Get a module configuration option value"},
174 {"_ceph_get_option", (PyCFunction
)ceph_option_get
, METH_VARARGS
,
175 "Get a native configuration option value"},
177 {"_ceph_get_store", (PyCFunction
)ceph_store_get
, METH_VARARGS
,
178 "Get a KV store value"},
180 {"_ceph_get_active_uri", (PyCFunction
)ceph_get_active_uri
, METH_NOARGS
,
181 "Get the URI of the active instance of this module, if any"},
183 {"_ceph_log", (PyCFunction
)ceph_log
, METH_VARARGS
,
184 "Emit a log message"},
186 {NULL
, NULL
, 0, NULL
}
189 PyTypeObject BaseMgrStandbyModuleType
= {
190 PyVarObject_HEAD_INIT(NULL
, 0)
191 "ceph_module.BaseMgrStandbyModule", /* tp_name */
192 sizeof(BaseMgrStandbyModule
), /* tp_basicsize */
200 0, /* tp_as_number */
201 0, /* tp_as_sequence */
202 0, /* tp_as_mapping */
208 0, /* tp_as_buffer */
209 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
210 "ceph-mgr Standby Python Plugin", /* tp_doc */
213 0, /* tp_richcompare */
214 0, /* tp_weaklistoffset */
217 BaseMgrStandbyModule_methods
, /* tp_methods */
222 0, /* tp_descr_get */
223 0, /* tp_descr_set */
224 0, /* tp_dictoffset */
225 (initproc
)BaseMgrStandbyModule_init
, /* tp_init */
227 BaseMgrStandbyModule_new
, /* tp_new */