summaryrefslogtreecommitdiffstats
path: root/src/mgr/ActivePyModules.h
blob: 8a14a4671bfff856f555f222a4cac6e0f32c23c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 John Spray <john.spray@inktank.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 */

#pragma once

#include "ActivePyModule.h"

#include "common/Finisher.h"
#include "common/Mutex.h"

#include "PyFormatter.h"

#include "osdc/Objecter.h"
#include "client/Client.h"
#include "common/LogClient.h"
#include "mon/MgrMap.h"
#include "mon/MonCommand.h"
#include "mon/mon_types.h"

#include "DaemonState.h"
#include "ClusterState.h"
#include "OSDPerfMetricTypes.h"

class health_check_map_t;
class DaemonServer;
class MgrSession;
class ModuleCommand;
class PyModuleRegistry;

class ActivePyModules
{
  // module class instances not yet created
  std::set<std::string, std::less<>> pending_modules;
  // module class instances already created
  std::map<std::string, std::shared_ptr<ActivePyModule>> modules;
  PyModuleConfig &module_config;
  std::map<std::string, std::string> store_cache;
  DaemonStateIndex &daemon_state;
  ClusterState &cluster_state;
  MonClient &monc;
  LogChannelRef clog, audit_clog;
  Objecter &objecter;
  Client   &client;
  Finisher &finisher;
public:
  Finisher cmd_finisher;
private:
  DaemonServer &server;
  PyModuleRegistry &py_module_registry;

  map<std::string,ProgressEvent> progress_events;

  mutable Mutex lock{"ActivePyModules::lock"};

public:
  ActivePyModules(PyModuleConfig &module_config,
            std::map<std::string, std::string> store_data,
            DaemonStateIndex &ds, ClusterState &cs, MonClient &mc,
            LogChannelRef clog_, LogChannelRef audit_clog_, Objecter &objecter_, Client &client_,
            Finisher &f, DaemonServer &server, PyModuleRegistry &pmr);

  ~ActivePyModules();

  // FIXME: wrap for send_command?
  MonClient &get_monc() {return monc;}
  Objecter  &get_objecter() {return objecter;}
  Client    &get_client() {return client;}
  PyObject *get_python(const std::string &what);
  PyObject *get_server_python(const std::string &hostname);
  PyObject *list_servers_python();
  PyObject *get_metadata_python(
    const std::string &svc_type, const std::string &svc_id);
  PyObject *get_daemon_status_python(
    const std::string &svc_type, const std::string &svc_id);
  PyObject *get_counter_python(
    const std::string &svc_type,
    const std::string &svc_id,
    const std::string &path);
  PyObject *get_latest_counter_python(
    const std::string &svc_type,
    const std::string &svc_id,
    const std::string &path);
  PyObject *get_perf_schema_python(
     const std::string &svc_type,
     const std::string &svc_id);
  PyObject *get_context();
  PyObject *get_osdmap();
  PyObject *with_perf_counters(
      std::function<void(
        PerfCounterInstance& counter_instance,
        PerfCounterType& counter_type,
        PyFormatter& f)> fct,
      const std::string &svc_name,
      const std::string &svc_id,
      const std::string &path) const;

  OSDPerfMetricQueryID add_osd_perf_query(
      const OSDPerfMetricQuery &query,
      const std::optional<OSDPerfMetricLimit> &limit);
  void remove_osd_perf_query(OSDPerfMetricQueryID query_id);
  PyObject *get_osd_perf_counters(OSDPerfMetricQueryID query_id);

  bool get_store(const std::string &module_name,
      const std::string &key, std::string *val) const;
  PyObject *get_store_prefix(const std::string &module_name,
			      const std::string &prefix) const;
  void set_store(const std::string &module_name,
      const std::string &key, const boost::optional<std::string> &val);

  bool get_config(const std::string &module_name,
      const std::string &key, std::string *val) const;
  void set_config(const std::string &module_name,
      const std::string &key, const boost::optional<std::string> &val);

  PyObject *get_typed_config(const std::string &module_name,
			     const std::string &key,
			     const std::string &prefix = "") const;

  void set_health_checks(const std::string& module_name,
			 health_check_map_t&& checks);
  void get_health_checks(health_check_map_t *checks);

  void update_progress_event(const std::string& evid,
			     const std::string& desc,
			     float progress);
  void complete_progress_event(const std::string& evid);
  void clear_all_progress_events();
  void get_progress_events(std::map<std::string,ProgressEvent>* events);

  void config_notify();

  void set_uri(const std::string& module_name, const std::string &uri);

  int handle_command(
    const ModuleCommand& module_command,
    const MgrSession& session,
    const cmdmap_t &cmdmap,
    const bufferlist &inbuf,
    std::stringstream *ds,
    std::stringstream *ss);

  std::map<std::string, std::string> get_services() const;

  // Public so that MonCommandCompletion can use it
  // FIXME: for send_command completion notifications,
  // send it to only the module that sent the command, not everyone
  void notify_all(const std::string &notify_type,
                  const std::string &notify_id);
  void notify_all(const LogEntry &log_entry);

  bool is_pending(std::string_view name) const {
    return pending_modules.count(name) > 0;
  }
  bool module_exists(const std::string &name) const
  {
    return modules.count(name) > 0;
  }

  bool method_exists(
      const std::string &module_name,
      const std::string &method_name) const
  {
    return modules.at(module_name)->method_exists(method_name);
  }

  PyObject *dispatch_remote(
      const std::string &other_module,
      const std::string &method,
      PyObject *args,
      PyObject *kwargs,
      std::string *err);

  int init();
  void shutdown();

  void start_one(PyModuleRef py_module);

  void dump_server(const std::string &hostname,
                   const DaemonStateCollection &dmc,
                   Formatter *f);

  void cluster_log(const std::string &channel, clog_type prio,
    const std::string &message);
};