]> git.proxmox.com Git - ceph.git/blame - ceph/src/pybind/mgr/hello/module.py
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / hello / module.py
CommitLineData
11fdf7f2
TL
1
2"""
3A hello world module
4
5See doc/mgr/hello.rst for more info.
6"""
7
8from mgr_module import MgrModule, HandleCommandResult
9from threading import Event
9f95a23c 10import errno
11fdf7f2
TL
11
12class Hello(MgrModule):
13 # these are CLI commands we implement
14 COMMANDS = [
15 {
16 "cmd": "hello "
17 "name=person_name,type=CephString,req=false",
9f95a23c
TL
18 "desc": "Say hello",
19 "perm": "r"
20 },
21 {
22 "cmd": "count "
23 "name=num,type=CephInt",
24 "desc": "Do some counting",
11fdf7f2
TL
25 "perm": "r"
26 },
27 ]
28
9f95a23c
TL
29 # These are module options we understand. These can be set with
30 #
31 # ceph config set global mgr/hello/<name> <value>
32 #
33 # e.g.,
34 #
35 # ceph config set global mgr/hello/place Earth
36 #
11fdf7f2
TL
37 MODULE_OPTIONS = [
38 {
39 'name': 'place',
40 'default': 'world',
9f95a23c
TL
41 'desc': 'a place in the world',
42 'runtime': True, # can be updated at runtime (no mgr restart)
11fdf7f2
TL
43 },
44 {
45 'name': 'emphatic',
46 'type': 'bool',
9f95a23c 47 'desc': 'whether to say it loudly',
11fdf7f2 48 'default': True,
9f95a23c 49 'runtime': True,
11fdf7f2 50 },
9f95a23c
TL
51 {
52 'name': 'foo',
53 'type': 'enum',
54 'enum_allowed': [ 'a', 'b', 'c' ],
55 'default': 'a',
56 'runtime': True,
57 },
58 ]
59
60 # These are "native" Ceph options that this module cares about.
61 NATIVE_OPTIONS = [
62 'mgr_tick_period',
11fdf7f2
TL
63 ]
64
65 def __init__(self, *args, **kwargs):
66 super(Hello, self).__init__(*args, **kwargs)
67
9f95a23c 68 # set up some members to enable the serve() method and shutdown()
11fdf7f2
TL
69 self.run = True
70 self.event = Event()
71
9f95a23c
TL
72 # ensure config options members are initialized; see config_notify()
73 self.config_notify()
11fdf7f2 74
11fdf7f2 75
9f95a23c
TL
76 def config_notify(self):
77 """
78 This method is called whenever one of our config options is changed.
79 """
80 # This is some boilerplate that stores MODULE_OPTIONS in a class
81 # member, so that, for instance, the 'emphatic' option is always
82 # available as 'self.emphatic'.
83 for opt in self.MODULE_OPTIONS:
84 setattr(self,
85 opt['name'],
86 self.get_module_option(opt['name']))
87 self.log.debug(' mgr option %s = %s',
88 opt['name'], getattr(self, opt['name']))
89 # Do the same for the native options.
90 for opt in self.NATIVE_OPTIONS:
91 setattr(self,
92 opt,
93 self.get_ceph_option(opt))
94 self.log.debug(' native option %s = %s', opt, getattr(self, opt))
95
96 def handle_command(self, inbuf, cmd):
97 ret = 0
98 out = ''
99 err = ''
100 if cmd['prefix'] == 'hello':
101 if 'person_name' in cmd:
102 out = "Hello, " + cmd['person_name']
103 else:
104 out = "Hello " + self.get_module_option('place')
105 if self.get_module_option('emphatic'):
106 out += '!'
107 elif cmd['prefix'] == 'count':
108 num = cmd.get('num', 0)
109 if num < 1:
110 err = 'That\'s too small a number'
111 ret = -errno.EINVAL
112 elif num > 10:
113 err = 'That\'s too big a number'
114 ret = -errno.EINVAL
115 else:
116 out = 'Hello, I am the count!\n'
117 out += ', '.join([str(x) for x in range(1, num + 1)]) + '!'
118 return HandleCommandResult(
119 retval=ret, # exit code
120 stdout=out, # stdout
121 stderr=err)
11fdf7f2
TL
122
123 def serve(self):
124 """
125 This method is called by the mgr when the module starts and can be
126 used for any background activity.
127 """
128 self.log.info("Starting")
129 while self.run:
9f95a23c
TL
130 # Do some useful background work here.
131
132 # Use mgr_tick_period (default: 2) here just to illustrate
133 # consuming native ceph options. Any real background work
134 # would presumably have some more appropriate frequency.
135 sleep_interval = self.mgr_tick_period
11fdf7f2
TL
136 self.log.debug('Sleeping for %d seconds', sleep_interval)
137 ret = self.event.wait(sleep_interval)
138 self.event.clear()
139
140 def shutdown(self):
141 """
142 This method is called by the mgr when the module needs to shut
9f95a23c 143 down (i.e., when the serve() function needs to exit).
11fdf7f2
TL
144 """
145 self.log.info('Stopping')
146 self.run = False
147 self.event.set()