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.
17 // Python.h comes first because otherwise it clobbers ceph's assert
24 #include "common/LogClient.h"
26 #include "ActivePyModules.h"
27 #include "StandbyPyModules.h"
32 const std::string module_name;
33 std::string get_site_packages();
36 SafeThreadState pMyThreadState;
37 PyObject *pClass = nullptr;
38 PyObject *pStandbyClass = nullptr;
40 PyModule(const std::string &module_name_)
41 : module_name(module_name_)
47 int load(PyThreadState *pMainThreadState);
49 std::string get_name() const {
55 * This class is responsible for setting up the python runtime environment
56 * and importing the python modules.
58 * It is *not* responsible for constructing instances of their BaseMgrModule
61 class PyModuleRegistry
64 mutable Mutex lock{"PyModuleRegistry::lock"};
68 std::map<std::string, std::unique_ptr<PyModule>> modules;
70 std::unique_ptr<ActivePyModules> active_modules;
71 std::unique_ptr<StandbyPyModules> standby_modules;
73 PyThreadState *pMainThreadState;
75 // We have our own copy of MgrMap, because we are constructed
76 // before ClusterState exists.
80 static std::string config_prefix;
82 static void list_modules(std::set<std::string> *modules);
84 PyModuleRegistry(LogChannelRef clog_)
88 bool handle_mgr_map(const MgrMap &mgr_map_)
90 Mutex::Locker l(lock);
92 bool modules_changed = mgr_map_.modules != mgr_map.modules;
95 if (standby_modules != nullptr) {
96 standby_modules->handle_mgr_map(mgr_map_);
99 return modules_changed;
102 bool is_initialized() const
104 return mgr_map.epoch > 0;
107 int init(const MgrMap &map);
110 PyModuleConfig &config_,
111 DaemonStateIndex &ds, ClusterState &cs, MonClient &mc,
112 LogChannelRef clog_, Objecter &objecter_, Client &client_,
117 bool is_standby_running() const
119 return standby_modules != nullptr;
122 void active_shutdown();
125 template<typename Callback, typename...Args>
126 void with_active_modules(Callback&& cb, Args&&...args) const
128 Mutex::Locker l(lock);
129 assert(active_modules != nullptr);
131 std::forward<Callback>(cb)(*active_modules, std::forward<Args>(args)...);
134 // FIXME: breaking interface so that I don't have to go rewrite all
135 // the places that call into these (for now)
137 void notify_all(const std::string ¬ify_type,
138 const std::string ¬ify_id)
140 if (active_modules) {
141 active_modules->notify_all(notify_type, notify_id);
145 void notify_all(const LogEntry &log_entry)
147 if (active_modules) {
148 active_modules->notify_all(log_entry);
152 std::vector<MonCommand> get_commands() const
154 assert(active_modules);
155 return active_modules->get_commands();
157 std::vector<ModuleCommand> get_py_commands() const
159 assert(active_modules);
160 return active_modules->get_py_commands();
162 void get_health_checks(health_check_map_t *checks)
164 assert(active_modules);
165 active_modules->get_health_checks(checks);
167 std::map<std::string, std::string> get_services() const
169 assert(active_modules);
170 return active_modules->get_services();
172 // <<< (end of ActivePyModules cheeky call-throughs)