]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/BaseMgrStandbyModule.cc
84c9b79f2f7bab51d3045235021873e259ffeea3
[ceph.git] / ceph / src / mgr / BaseMgrStandbyModule.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 John Spray <john.spray@redhat.com>
7 *
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.
12 */
13
14
15 #include "BaseMgrStandbyModule.h"
16
17 #include "StandbyPyModules.h"
18
19
20 #define dout_context g_ceph_context
21 #define dout_subsys ceph_subsys_mgr
22
23 typedef struct {
24 PyObject_HEAD
25 StandbyPyModule *this_module;
26 } BaseMgrStandbyModule;
27
28 static PyObject *
29 BaseMgrStandbyModule_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
30 {
31 BaseMgrStandbyModule *self;
32
33 self = (BaseMgrStandbyModule *)type->tp_alloc(type, 0);
34
35 return (PyObject *)self;
36 }
37
38 static int
39 BaseMgrStandbyModule_init(BaseMgrStandbyModule *self, PyObject *args, PyObject *kwds)
40 {
41 PyObject *this_module_capsule = nullptr;
42 static const char *kwlist[] = {"this_module", NULL};
43
44 if (! PyArg_ParseTupleAndKeywords(args, kwds, "O",
45 const_cast<char**>(kwlist),
46 &this_module_capsule)) {
47 return -1;
48 }
49
50 self->this_module = static_cast<StandbyPyModule*>(PyCapsule_GetPointer(
51 this_module_capsule, nullptr));
52 ceph_assert(self->this_module);
53
54 return 0;
55 }
56
57 static PyObject*
58 ceph_get_mgr_id(BaseMgrStandbyModule *self, PyObject *args)
59 {
60 return PyUnicode_FromString(g_conf()->name.get_id().c_str());
61 }
62
63 static PyObject*
64 ceph_get_module_option(BaseMgrStandbyModule *self, PyObject *args)
65 {
66 char *what = nullptr;
67 char *prefix = nullptr;
68 if (!PyArg_ParseTuple(args, "s|s:ceph_get_module_option", &what, &prefix)) {
69 derr << "Invalid args!" << dendl;
70 return nullptr;
71 }
72 PyThreadState *tstate = PyEval_SaveThread();
73 std::string final_key;
74 std::string value;
75 bool found = false;
76 if (prefix) {
77 final_key = std::string(prefix) + "/" + what;
78 found = self->this_module->get_config(final_key, &value);
79 }
80 if (!found) {
81 final_key = what;
82 found = self->this_module->get_config(final_key, &value);
83 }
84 PyEval_RestoreThread(tstate);
85 if (found) {
86 dout(10) << __func__ << " " << final_key << " found: " << value
87 << dendl;
88 return self->this_module->py_module->get_typed_option_value(what, value);
89 } else {
90 if (prefix) {
91 dout(4) << __func__ << " [" << prefix << "/]" << what << " not found "
92 << dendl;
93 } else {
94 dout(4) << __func__ << " " << what << " not found " << dendl;
95 }
96 Py_RETURN_NONE;
97 }
98 }
99
100 static PyObject*
101 ceph_option_get(BaseMgrStandbyModule *self, PyObject *args)
102 {
103 char *what = nullptr;
104 if (!PyArg_ParseTuple(args, "s:ceph_option_get", &what)) {
105 derr << "Invalid args!" << dendl;
106 return nullptr;
107 }
108
109 std::string value;
110 int r = g_conf().get_val(string(what), &value);
111 if (r >= 0) {
112 dout(10) << "ceph_option_get " << what << " found: " << value << dendl;
113 return PyUnicode_FromString(value.c_str());
114 } else {
115 dout(4) << "ceph_option_get " << what << " not found " << dendl;
116 Py_RETURN_NONE;
117 }
118 }
119
120 static PyObject*
121 ceph_store_get(BaseMgrStandbyModule *self, PyObject *args)
122 {
123 char *what = nullptr;
124 if (!PyArg_ParseTuple(args, "s:ceph_store_get", &what)) {
125 derr << "Invalid args!" << dendl;
126 return nullptr;
127 }
128
129 // Drop GIL for blocking mon command execution
130 PyThreadState *tstate = PyEval_SaveThread();
131
132 std::string value;
133 bool found = self->this_module->get_store(what, &value);
134
135 PyEval_RestoreThread(tstate);
136
137 if (found) {
138 dout(10) << "ceph_store_get " << what << " found: " << value.c_str() << dendl;
139 return PyUnicode_FromString(value.c_str());
140 } else {
141 dout(4) << "ceph_store_get " << what << " not found " << dendl;
142 Py_RETURN_NONE;
143 }
144 }
145
146 static PyObject*
147 ceph_get_active_uri(BaseMgrStandbyModule *self, PyObject *args)
148 {
149 return PyUnicode_FromString(self->this_module->get_active_uri().c_str());
150 }
151
152 static PyObject*
153 ceph_log(BaseMgrStandbyModule *self, PyObject *args)
154 {
155 char *record = nullptr;
156 if (!PyArg_ParseTuple(args, "s:log", &record)) {
157 return nullptr;
158 }
159
160 ceph_assert(self->this_module);
161
162 self->this_module->log(record);
163
164 Py_RETURN_NONE;
165 }
166
167 PyMethodDef BaseMgrStandbyModule_methods[] = {
168
169 {"_ceph_get_mgr_id", (PyCFunction)ceph_get_mgr_id, METH_NOARGS,
170 "Get the name of the Mgr daemon where we are running"},
171
172 {"_ceph_get_module_option", (PyCFunction)ceph_get_module_option, METH_VARARGS,
173 "Get a module configuration option value"},
174
175 {"_ceph_get_option", (PyCFunction)ceph_option_get, METH_VARARGS,
176 "Get a native configuration option value"},
177
178 {"_ceph_get_store", (PyCFunction)ceph_store_get, METH_VARARGS,
179 "Get a KV store value"},
180
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"},
183
184 {"_ceph_log", (PyCFunction)ceph_log, METH_VARARGS,
185 "Emit a log message"},
186
187 {NULL, NULL, 0, NULL}
188 };
189
190 PyTypeObject BaseMgrStandbyModuleType = {
191 PyVarObject_HEAD_INIT(NULL, 0)
192 "ceph_module.BaseMgrStandbyModule", /* tp_name */
193 sizeof(BaseMgrStandbyModule), /* tp_basicsize */
194 0, /* tp_itemsize */
195 0, /* tp_dealloc */
196 0, /* tp_print */
197 0, /* tp_getattr */
198 0, /* tp_setattr */
199 0, /* tp_compare */
200 0, /* tp_repr */
201 0, /* tp_as_number */
202 0, /* tp_as_sequence */
203 0, /* tp_as_mapping */
204 0, /* tp_hash */
205 0, /* tp_call */
206 0, /* tp_str */
207 0, /* tp_getattro */
208 0, /* tp_setattro */
209 0, /* tp_as_buffer */
210 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
211 "ceph-mgr Standby Python Plugin", /* tp_doc */
212 0, /* tp_traverse */
213 0, /* tp_clear */
214 0, /* tp_richcompare */
215 0, /* tp_weaklistoffset */
216 0, /* tp_iter */
217 0, /* tp_iternext */
218 BaseMgrStandbyModule_methods, /* tp_methods */
219 0, /* tp_members */
220 0, /* tp_getset */
221 0, /* tp_base */
222 0, /* tp_dict */
223 0, /* tp_descr_get */
224 0, /* tp_descr_set */
225 0, /* tp_dictoffset */
226 (initproc)BaseMgrStandbyModule_init, /* tp_init */
227 0, /* tp_alloc */
228 BaseMgrStandbyModule_new, /* tp_new */
229 };