]> git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/BaseMgrStandbyModule.cc
update sources to ceph Nautilus 14.2.1
[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 PyString_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 std::string final_key;
73 std::string value;
74 bool found = false;
75 if (prefix) {
76 final_key = std::string(prefix) + "/" + what;
77 found = self->this_module->get_config(final_key, &value);
78 }
79 if (!found) {
80 final_key = what;
81 found = self->this_module->get_config(final_key, &value);
82 }
83 if (found) {
84 dout(10) << __func__ << " " << final_key << " found: " << value
85 << dendl;
86 return self->this_module->py_module->get_typed_option_value(what, value);
87 } else {
88 if (prefix) {
89 dout(4) << __func__ << " [" << prefix << "/]" << what << " not found "
90 << dendl;
91 } else {
92 dout(4) << __func__ << " " << what << " not found " << dendl;
93 }
94 Py_RETURN_NONE;
95 }
96 }
97
98 static PyObject*
99 ceph_option_get(BaseMgrStandbyModule *self, PyObject *args)
100 {
101 char *what = nullptr;
102 if (!PyArg_ParseTuple(args, "s:ceph_option_get", &what)) {
103 derr << "Invalid args!" << dendl;
104 return nullptr;
105 }
106
107 std::string value;
108 int r = g_conf().get_val(string(what), &value);
109 if (r >= 0) {
110 dout(10) << "ceph_option_get " << what << " found: " << value << dendl;
111 return PyString_FromString(value.c_str());
112 } else {
113 dout(4) << "ceph_option_get " << what << " not found " << dendl;
114 Py_RETURN_NONE;
115 }
116 }
117
118 static PyObject*
119 ceph_store_get(BaseMgrStandbyModule *self, PyObject *args)
120 {
121 char *what = nullptr;
122 if (!PyArg_ParseTuple(args, "s:ceph_store_get", &what)) {
123 derr << "Invalid args!" << dendl;
124 return nullptr;
125 }
126
127 // Drop GIL for blocking mon command execution
128 PyThreadState *tstate = PyEval_SaveThread();
129
130 std::string value;
131 bool found = self->this_module->get_store(what, &value);
132
133 PyEval_RestoreThread(tstate);
134
135 if (found) {
136 dout(10) << "ceph_store_get " << what << " found: " << value.c_str() << dendl;
137 return PyString_FromString(value.c_str());
138 } else {
139 dout(4) << "ceph_store_get " << what << " not found " << dendl;
140 Py_RETURN_NONE;
141 }
142 }
143
144 static PyObject*
145 ceph_get_active_uri(BaseMgrStandbyModule *self, PyObject *args)
146 {
147 return PyString_FromString(self->this_module->get_active_uri().c_str());
148 }
149
150 static PyObject*
151 ceph_log(BaseMgrStandbyModule *self, PyObject *args)
152 {
153 int level = 0;
154 char *record = nullptr;
155 if (!PyArg_ParseTuple(args, "is:log", &level, &record)) {
156 return nullptr;
157 }
158
159 ceph_assert(self->this_module);
160
161 self->this_module->log(level, record);
162
163 Py_RETURN_NONE;
164 }
165
166 PyMethodDef BaseMgrStandbyModule_methods[] = {
167
168 {"_ceph_get_mgr_id", (PyCFunction)ceph_get_mgr_id, METH_NOARGS,
169 "Get the name of the Mgr daemon where we are running"},
170
171 {"_ceph_get_module_option", (PyCFunction)ceph_get_module_option, METH_VARARGS,
172 "Get a module configuration option value"},
173
174 {"_ceph_get_option", (PyCFunction)ceph_option_get, METH_VARARGS,
175 "Get a native configuration option value"},
176
177 {"_ceph_get_store", (PyCFunction)ceph_store_get, METH_VARARGS,
178 "Get a KV store value"},
179
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"},
182
183 {"_ceph_log", (PyCFunction)ceph_log, METH_VARARGS,
184 "Emit a log message"},
185
186 {NULL, NULL, 0, NULL}
187 };
188
189 PyTypeObject BaseMgrStandbyModuleType = {
190 PyVarObject_HEAD_INIT(NULL, 0)
191 "ceph_module.BaseMgrStandbyModule", /* tp_name */
192 sizeof(BaseMgrStandbyModule), /* tp_basicsize */
193 0, /* tp_itemsize */
194 0, /* tp_dealloc */
195 0, /* tp_print */
196 0, /* tp_getattr */
197 0, /* tp_setattr */
198 0, /* tp_compare */
199 0, /* tp_repr */
200 0, /* tp_as_number */
201 0, /* tp_as_sequence */
202 0, /* tp_as_mapping */
203 0, /* tp_hash */
204 0, /* tp_call */
205 0, /* tp_str */
206 0, /* tp_getattro */
207 0, /* tp_setattro */
208 0, /* tp_as_buffer */
209 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
210 "ceph-mgr Standby Python Plugin", /* tp_doc */
211 0, /* tp_traverse */
212 0, /* tp_clear */
213 0, /* tp_richcompare */
214 0, /* tp_weaklistoffset */
215 0, /* tp_iter */
216 0, /* tp_iternext */
217 BaseMgrStandbyModule_methods, /* tp_methods */
218 0, /* tp_members */
219 0, /* tp_getset */
220 0, /* tp_base */
221 0, /* tp_dict */
222 0, /* tp_descr_get */
223 0, /* tp_descr_set */
224 0, /* tp_dictoffset */
225 (initproc)BaseMgrStandbyModule_init, /* tp_init */
226 0, /* tp_alloc */
227 BaseMgrStandbyModule_new, /* tp_new */
228 };