summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/hello/module.py
blob: 94d532ee70b7c34facdb9c37f1847e116372ecb2 (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
"""
A hello world module

See doc/mgr/hello.rst for more info.
"""

from mgr_module import MgrModule, HandleCommandResult
from threading import Event


class Hello(MgrModule):
    # these are CLI commands we implement
    COMMANDS = [
        {
            "cmd": "hello "
                   "name=person_name,type=CephString,req=false",
            "desc": "Prints hello world to mgr.x.log",
            "perm": "r"
        },
    ]

    # these are module options we understand.  These can be set with
    # 'ceph config set global mgr/hello/<name> <value>'.  e.g.,
    # 'ceph config set global mgr/hello/place Earth'
    MODULE_OPTIONS = [
        {
            'name': 'place',
            'default': 'world',
        },
        {
            'name': 'emphatic',
            'type': 'bool',
            'default': True,
        },
    ]

    def __init__(self, *args, **kwargs):
        super(Hello, self).__init__(*args, **kwargs)

        # set up some members to enable the serve() method and shutdown
        self.run = True
        self.event = Event()

    def handle_command(self, inbuf, cmd):
        self.log.info("hello_world_info")
        self.log.debug("hello_world_debug")
        self.log.error("hello_world_error")

        status_code = 0
        output_buffer = "Output buffer is for data results"
        output_string = "Output string is for informative text"
        if 'person_name' in cmd:
            message = "Hello, " + cmd['person_name']
        else:
            message = "Hello " + self.get_module_option('place')
        if self.get_module_option('emphatic'):
            message += '!'

        return HandleCommandResult(retval=status_code, stdout=output_buffer,
                                   stderr=message + "\n" + output_string)

    def serve(self):
        """
        This method is called by the mgr when the module starts and can be
        used for any background activity.
        """
        self.log.info("Starting")
        while self.run:
            sleep_interval = 5
            self.log.debug('Sleeping for %d seconds', sleep_interval)
            ret = self.event.wait(sleep_interval)
            self.event.clear()

    def shutdown(self):
        """
        This method is called by the mgr when the module needs to shut
        down (i.e., when the serve() function needs to exit.
        """
        self.log.info('Stopping')
        self.run = False
        self.event.set()