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 PyUnicode_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 PyThreadState
*tstate
= PyEval_SaveThread();
73 std::string final_key
;
77 final_key
= std::string(prefix
) + "/" + what
;
78 found
= self
->this_module
->get_config(final_key
, &value
);
82 found
= self
->this_module
->get_config(final_key
, &value
);
84 PyEval_RestoreThread(tstate
);
86 dout(10) << __func__
<< " " << final_key
<< " found: " << value
88 return self
->this_module
->py_module
->get_typed_option_value(what
, value
);
91 dout(4) << __func__
<< " [" << prefix
<< "/]" << what
<< " not found "
94 dout(4) << __func__
<< " " << what
<< " not found " << dendl
;
101 ceph_option_get(BaseMgrStandbyModule
*self
, PyObject
*args
)
103 char *what
= nullptr;
104 if (!PyArg_ParseTuple(args
, "s:ceph_option_get", &what
)) {
105 derr
<< "Invalid args!" << dendl
;
110 int r
= g_conf().get_val(string(what
), &value
);
112 dout(10) << "ceph_option_get " << what
<< " found: " << value
<< dendl
;
113 return PyUnicode_FromString(value
.c_str());
115 dout(4) << "ceph_option_get " << what
<< " not found " << dendl
;
121 ceph_store_get(BaseMgrStandbyModule
*self
, PyObject
*args
)
123 char *what
= nullptr;
124 if (!PyArg_ParseTuple(args
, "s:ceph_store_get", &what
)) {
125 derr
<< "Invalid args!" << dendl
;
129 // Drop GIL for blocking mon command execution
130 PyThreadState
*tstate
= PyEval_SaveThread();
133 bool found
= self
->this_module
->get_store(what
, &value
);
135 PyEval_RestoreThread(tstate
);
138 dout(10) << "ceph_store_get " << what
<< " found: " << value
.c_str() << dendl
;
139 return PyUnicode_FromString(value
.c_str());
141 dout(4) << "ceph_store_get " << what
<< " not found " << dendl
;
147 ceph_get_active_uri(BaseMgrStandbyModule
*self
, PyObject
*args
)
149 return PyUnicode_FromString(self
->this_module
->get_active_uri().c_str());
153 ceph_log(BaseMgrStandbyModule
*self
, PyObject
*args
)
155 char *record
= nullptr;
156 if (!PyArg_ParseTuple(args
, "s:log", &record
)) {
160 ceph_assert(self
->this_module
);
162 self
->this_module
->log(record
);
167 PyMethodDef BaseMgrStandbyModule_methods
[] = {
169 {"_ceph_get_mgr_id", (PyCFunction
)ceph_get_mgr_id
, METH_NOARGS
,
170 "Get the name of the Mgr daemon where we are running"},
172 {"_ceph_get_module_option", (PyCFunction
)ceph_get_module_option
, METH_VARARGS
,
173 "Get a module configuration option value"},
175 {"_ceph_get_option", (PyCFunction
)ceph_option_get
, METH_VARARGS
,
176 "Get a native configuration option value"},
178 {"_ceph_get_store", (PyCFunction
)ceph_store_get
, METH_VARARGS
,
179 "Get a KV store value"},
181 {"_ceph_get_active_uri", (PyCFunction
)ceph_get_active_uri
, METH_NOARGS
,
182 "Get the URI of the active instance of this module, if any"},
184 {"_ceph_log", (PyCFunction
)ceph_log
, METH_VARARGS
,
185 "Emit a log message"},
187 {NULL
, NULL
, 0, NULL
}
190 PyTypeObject BaseMgrStandbyModuleType
= {
191 PyVarObject_HEAD_INIT(NULL
, 0)
192 "ceph_module.BaseMgrStandbyModule", /* tp_name */
193 sizeof(BaseMgrStandbyModule
), /* tp_basicsize */
201 0, /* tp_as_number */
202 0, /* tp_as_sequence */
203 0, /* tp_as_mapping */
209 0, /* tp_as_buffer */
210 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
211 "ceph-mgr Standby Python Plugin", /* tp_doc */
214 0, /* tp_richcompare */
215 0, /* tp_weaklistoffset */
218 BaseMgrStandbyModule_methods
, /* tp_methods */
223 0, /* tp_descr_get */
224 0, /* tp_descr_set */
225 0, /* tp_dictoffset */
226 (initproc
)BaseMgrStandbyModule_init
, /* tp_init */
228 BaseMgrStandbyModule_new
, /* tp_new */