]> git.proxmox.com Git - ceph.git/blame - ceph/src/mgr/PyModuleRegistry.h
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / mgr / PyModuleRegistry.h
CommitLineData
3efd9988
FG
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) 2017 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#pragma once
16
17// Python.h comes first because otherwise it clobbers ceph's assert
18#include "Python.h"
19
20#include <string>
21#include <map>
22#include <memory>
23
24#include "common/LogClient.h"
25
26#include "ActivePyModules.h"
27#include "StandbyPyModules.h"
28
29class PyModule
30{
31private:
32 const std::string module_name;
33 std::string get_site_packages();
34
35public:
36 SafeThreadState pMyThreadState;
37 PyObject *pClass = nullptr;
38 PyObject *pStandbyClass = nullptr;
39
40 PyModule(const std::string &module_name_)
41 : module_name(module_name_)
42 {
43 }
44
45 ~PyModule();
46
47 int load(PyThreadState *pMainThreadState);
48
49 std::string get_name() const {
50 return module_name;
51 }
52};
53
54/**
55 * This class is responsible for setting up the python runtime environment
56 * and importing the python modules.
57 *
58 * It is *not* responsible for constructing instances of their BaseMgrModule
59 * subclasses.
60 */
61class PyModuleRegistry
62{
63private:
64 mutable Mutex lock{"PyModuleRegistry::lock"};
65
66 LogChannelRef clog;
67
68 std::map<std::string, std::unique_ptr<PyModule>> modules;
69
70 std::unique_ptr<ActivePyModules> active_modules;
71 std::unique_ptr<StandbyPyModules> standby_modules;
72
73 PyThreadState *pMainThreadState;
74
75 // We have our own copy of MgrMap, because we are constructed
76 // before ClusterState exists.
77 MgrMap mgr_map;
78
79public:
80 static std::string config_prefix;
81
82 static void list_modules(std::set<std::string> *modules);
83
84 PyModuleRegistry(LogChannelRef clog_)
85 : clog(clog_)
86 {}
87
88 bool handle_mgr_map(const MgrMap &mgr_map_)
89 {
90 Mutex::Locker l(lock);
91
92 bool modules_changed = mgr_map_.modules != mgr_map.modules;
93 mgr_map = mgr_map_;
94
95 if (standby_modules != nullptr) {
96 standby_modules->handle_mgr_map(mgr_map_);
97 }
98
99 return modules_changed;
100 }
101
102 bool is_initialized() const
103 {
104 return mgr_map.epoch > 0;
105 }
106
107 int init(const MgrMap &map);
108
109 void active_start(
110 PyModuleConfig &config_,
111 DaemonStateIndex &ds, ClusterState &cs, MonClient &mc,
112 LogChannelRef clog_, Objecter &objecter_, Client &client_,
113 Finisher &f);
114 void standby_start(
115 MonClient *monc);
116
117 bool is_standby_running() const
118 {
119 return standby_modules != nullptr;
120 }
121
122 void active_shutdown();
123 void shutdown();
124
125 template<typename Callback, typename...Args>
126 void with_active_modules(Callback&& cb, Args&&...args) const
127 {
128 Mutex::Locker l(lock);
129 assert(active_modules != nullptr);
130
131 std::forward<Callback>(cb)(*active_modules, std::forward<Args>(args)...);
132 }
133
134 // FIXME: breaking interface so that I don't have to go rewrite all
135 // the places that call into these (for now)
136 // >>>
137 void notify_all(const std::string &notify_type,
138 const std::string &notify_id)
139 {
140 if (active_modules) {
141 active_modules->notify_all(notify_type, notify_id);
142 }
143 }
144
145 void notify_all(const LogEntry &log_entry)
146 {
147 if (active_modules) {
148 active_modules->notify_all(log_entry);
149 }
150 }
151
152 std::vector<MonCommand> get_commands() const
153 {
154 assert(active_modules);
155 return active_modules->get_commands();
156 }
157 std::vector<ModuleCommand> get_py_commands() const
158 {
159 assert(active_modules);
160 return active_modules->get_py_commands();
161 }
162 void get_health_checks(health_check_map_t *checks)
163 {
164 assert(active_modules);
165 active_modules->get_health_checks(checks);
166 }
167 std::map<std::string, std::string> get_services() const
168 {
169 assert(active_modules);
170 return active_modules->get_services();
171 }
172 // <<< (end of ActivePyModules cheeky call-throughs)
173};