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) 2017 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.
20 #include <boost/optional.hpp>
21 #include "common/ceph_mutex.h"
24 #include "mon/MgrMap.h"
29 std::string
handle_pyerror();
31 std::string
peek_pyerror();
34 * A Ceph CLI command description provided from a Python module
38 std::string cmdstring
;
39 std::string helpstring
;
43 // Call the ActivePyModule of this name to handle the command
44 std::string module_name
;
49 mutable ceph::mutex lock
= ceph::make_mutex("PyModule::lock");
51 const std::string module_name
;
52 std::string
get_site_packages();
53 int load_subclass_of(const char* class_name
, PyObject
** py_class
);
55 // Did the MgrMap identify this module as one that should run?
58 // Did the MgrMap flag this module as always on?
59 bool always_on
= false;
61 // Did we successfully import this python module and look up symbols?
62 // (i.e. is it possible to instantiate a MgrModule subclass instance?)
65 // Did the module identify itself as being able to run?
66 // (i.e. should we expect instantiating and calling serve() to work?)
69 // Did the module encounter an unexpected error while running?
70 // (e.g. throwing an exception from serve())
73 // Populated if loaded, can_run or failed indicates a problem
74 std::string error_string
;
76 // Helper for loading MODULE_OPTIONS and COMMANDS members
78 const std::string
&attr_name
,
79 std::function
<int(PyObject
*)> fn
);
82 std::vector
<ModuleCommand
> commands
;
84 int register_options(PyObject
*cls
);
86 std::map
<std::string
, MgrMap::ModuleOption
> options
;
89 static std::string config_prefix
;
91 SafeThreadState pMyThreadState
;
92 PyObject
*pClass
= nullptr;
93 PyObject
*pStandbyClass
= nullptr;
95 explicit PyModule(const std::string
&module_name_
)
96 : module_name(module_name_
)
102 bool is_option(const std::string
&option_name
);
103 const std::map
<std::string
,MgrMap::ModuleOption
>& get_options() const {
107 PyObject
*get_typed_option_value(
108 const std::string
& option
,
109 const std::string
& value
);
111 int load(PyThreadState
*pMainThreadState
);
112 #if PY_MAJOR_VERSION >= 3
113 static PyObject
* init_ceph_logger();
114 static PyObject
* init_ceph_module();
116 static void init_ceph_logger();
117 static void init_ceph_module();
120 void set_enabled(const bool enabled_
)
125 void set_always_on(const bool always_on_
) {
126 always_on
= always_on_
;
130 * Extend `out` with the contents of `this->commands`
132 void get_commands(std::vector
<ModuleCommand
> *out
) const
134 std::lock_guard
l(lock
);
135 ceph_assert(out
!= nullptr);
136 out
->insert(out
->end(), commands
.begin(), commands
.end());
141 * Mark the module as failed, recording the reason in the error
144 void fail(const std::string
&reason
)
146 std::lock_guard
l(lock
);
148 error_string
= reason
;
151 bool is_enabled() const {
152 std::lock_guard
l(lock
);
153 return enabled
|| always_on
;
156 bool is_failed() const { std::lock_guard
l(lock
) ; return failed
; }
157 bool is_loaded() const { std::lock_guard
l(lock
) ; return loaded
; }
158 bool is_always_on() const { std::lock_guard
l(lock
) ; return always_on
; }
160 const std::string
&get_name() const {
161 std::lock_guard
l(lock
) ; return module_name
;
163 const std::string
&get_error_string() const {
164 std::lock_guard
l(lock
) ; return error_string
;
166 bool get_can_run() const {
167 std::lock_guard
l(lock
) ; return can_run
;
171 typedef std::shared_ptr
<PyModule
> PyModuleRef
;
173 class PyModuleConfig
{
175 mutable ceph::mutex lock
= ceph::make_mutex("PyModuleConfig::lock");
176 std::map
<std::string
, std::string
> config
;
180 PyModuleConfig(PyModuleConfig
&mconfig
);
186 const std::string
&module_name
,
187 const std::string
&key
, const boost::optional
<std::string
>& val
);