summaryrefslogtreecommitdiffstats
path: root/src/mgr/ActivePyModule.h
blob: 1cbf6d18ac2f4e1d9feec69102e7031ba61fb820 (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
// -*- 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) 2016 John Spray <john.spray@redhat.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

// Python.h comes first because otherwise it clobbers ceph's assert
#include "Python.h"

#include "common/cmdparse.h"
#include "common/LogEntry.h"
#include "common/Thread.h"
#include "mon/health_check.h"
#include "mgr/Gil.h"

#include "PyModuleRunner.h"

#include <vector>
#include <string>


class ActivePyModule;
class ActivePyModules;
class MgrSession;
class ModuleCommand;

class ActivePyModule : public PyModuleRunner
{
private:
  health_check_map_t health_checks;

  // Optional, URI exposed by plugins that implement serve()
  std::string uri;

  std::string m_command_perms;
  const MgrSession* m_session = nullptr;

public:
  ActivePyModule(const PyModuleRef &py_module_,
      LogChannelRef clog_)
    : PyModuleRunner(py_module_, clog_)
  {}

  int load(ActivePyModules *py_modules);
  void notify(const std::string &notify_type, const std::string &notify_id);
  void notify_clog(const LogEntry &le);

  bool method_exists(const std::string &method) const;

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

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


  bool set_health_checks(health_check_map_t&& c) {
    // when health checks change a report is immediately sent to the monitors.
    // currently modules have static health check details, but this equality
    // test could be made smarter if too much noise shows up in the future.
    bool changed = health_checks != c;
    health_checks = std::move(c);
    return changed;
  }
  void get_health_checks(health_check_map_t *checks);
  void config_notify();

  void set_uri(const std::string &str)
  {
    uri = str;
  }

  std::string get_uri() const
  {
    return uri;
  }

  bool is_authorized(const std::map<std::string, std::string>& arguments) const;

};

std::string handle_pyerror();