Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 = (StandbyPyModule*)PyCapsule_GetPointer(
51         this_module_capsule, nullptr);
52     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_config_get(BaseMgrStandbyModule *self, PyObject *args)
65 {
66   char *what = nullptr;
67   if (!PyArg_ParseTuple(args, "s:ceph_config_get", &what)) {
68     derr << "Invalid args!" << dendl;
69     return nullptr;
70   }
71
72   std::string value;
73   bool found = self->this_module->get_config(what, &value);
74   if (found) {
75     dout(10) << "ceph_config_get " << what << " found: " << value.c_str() << dendl;
76     return PyString_FromString(value.c_str());
77   } else {
78     dout(4) << "ceph_config_get " << what << " not found " << dendl;
79     Py_RETURN_NONE;
80   }
81 }
82
83 static PyObject*
84 ceph_get_active_uri(BaseMgrStandbyModule *self, PyObject *args)
85 {
86   return PyString_FromString(self->this_module->get_active_uri().c_str());
87 }
88
89 static PyObject*
90 ceph_log(BaseMgrStandbyModule *self, PyObject *args)
91 {
92   int level = 0;
93   char *record = nullptr;
94   if (!PyArg_ParseTuple(args, "is:log", &level, &record)) {
95     return nullptr;
96   }
97
98   assert(self->this_module);
99
100   self->this_module->log(level, record);
101
102   Py_RETURN_NONE;
103 }
104
105 PyMethodDef BaseMgrStandbyModule_methods[] = {
106
107   {"_ceph_get_mgr_id", (PyCFunction)ceph_get_mgr_id, METH_NOARGS,
108    "Get the name of the Mgr daemon where we are running"},
109
110   {"_ceph_get_config", (PyCFunction)ceph_config_get, METH_VARARGS,
111    "Get a configuration value"},
112
113   {"_ceph_get_active_uri", (PyCFunction)ceph_get_active_uri, METH_NOARGS,
114    "Get the URI of the active instance of this module, if any"},
115
116   {"_ceph_log", (PyCFunction)ceph_log, METH_VARARGS,
117    "Emit a log message"},
118
119   {NULL, NULL, 0, NULL}
120 };
121
122 PyTypeObject BaseMgrStandbyModuleType = {
123   PyVarObject_HEAD_INIT(NULL, 0)
124   "ceph_module.BaseMgrStandbyModule", /* tp_name */
125   sizeof(BaseMgrStandbyModule),     /* tp_basicsize */
126   0,                         /* tp_itemsize */
127   0,                         /* tp_dealloc */
128   0,                         /* tp_print */
129   0,                         /* tp_getattr */
130   0,                         /* tp_setattr */
131   0,                         /* tp_compare */
132   0,                         /* tp_repr */
133   0,                         /* tp_as_number */
134   0,                         /* tp_as_sequence */
135   0,                         /* tp_as_mapping */
136   0,                         /* tp_hash */
137   0,                         /* tp_call */
138   0,                         /* tp_str */
139   0,                         /* tp_getattro */
140   0,                         /* tp_setattro */
141   0,                         /* tp_as_buffer */
142   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,        /* tp_flags */
143   "ceph-mgr Standby Python Plugin", /* tp_doc */
144   0,                         /* tp_traverse */
145   0,                         /* tp_clear */
146   0,                         /* tp_richcompare */
147   0,                         /* tp_weaklistoffset */
148   0,                         /* tp_iter */
149   0,                         /* tp_iternext */
150   BaseMgrStandbyModule_methods,     /* tp_methods */
151   0,                         /* tp_members */
152   0,                         /* tp_getset */
153   0,                         /* tp_base */
154   0,                         /* tp_dict */
155   0,                         /* tp_descr_get */
156   0,                         /* tp_descr_set */
157   0,                         /* tp_dictoffset */
158   (initproc)BaseMgrStandbyModule_init,                         /* tp_init */
159   0,                         /* tp_alloc */
160   BaseMgrStandbyModule_new,     /* tp_new */
161 };