]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/hello/module.py
5 See doc/mgr/hello.rst for more info.
8 from mgr_module
import CLIReadCommand
, HandleCommandResult
, MgrModule
, Option
9 from threading
import Event
10 from typing
import cast
, Any
, Optional
, TYPE_CHECKING
14 class Hello(MgrModule
):
15 # These are module options we understand. These can be set with
17 # ceph config set global mgr/hello/<name> <value>
21 # ceph config set global mgr/hello/place Earth
26 desc
='a place in the world',
27 runtime
=True), # can be updated at runtime (no mgr restart)
28 Option(name
='emphatic',
30 desc
='whether to say it loudly',
35 enum_allowed
=['a', 'b', 'c'],
40 # These are "native" Ceph options that this module cares about.
45 def __init__(self
, *args
: Any
, **kwargs
: Any
):
46 super().__init
__(*args
, **kwargs
)
48 # set up some members to enable the serve() method and shutdown()
52 # ensure config options members are initialized; see config_notify()
55 # for mypy which does not run the code
57 self
.mgr_tick_period
= 0
59 def config_notify(self
) -> None:
61 This method is called whenever one of our config options is changed.
63 # This is some boilerplate that stores MODULE_OPTIONS in a class
64 # member, so that, for instance, the 'emphatic' option is always
65 # available as 'self.emphatic'.
66 for opt
in self
.MODULE_OPTIONS
:
69 self
.get_module_option(opt
['name']))
70 self
.log
.debug(' mgr option %s = %s',
71 opt
['name'], getattr(self
, opt
['name']))
72 # Do the same for the native options.
73 for opt
in self
.NATIVE_OPTIONS
:
76 self
.get_ceph_option(opt
))
77 self
.log
.debug(' native option %s = %s', opt
, getattr(self
, opt
))
79 # there are CLI commands we implement
80 @CLIReadCommand('hello')
81 def hello(self
, person_name
: Optional
[str] = None) -> HandleCommandResult
:
85 if person_name
is None:
86 who
= cast(str, self
.get_module_option('place'))
89 fin
= '!' if self
.get_module_option('emphatic') else ''
90 return HandleCommandResult(stdout
=f
'Hello, {who}{fin}')
92 @CLIReadCommand('count')
93 def count(self
, num
: int) -> HandleCommandResult
:
101 err
= 'That\'s too small a number'
104 err
= 'That\'s too big a number'
107 out
= 'Hello, I am the count!\n'
108 out
+= ', '.join([str(x
) for x
in range(1, num
+ 1)]) + '!'
109 return HandleCommandResult(retval
=ret
,
113 def serve(self
) -> None:
115 This method is called by the mgr when the module starts and can be
116 used for any background activity.
118 self
.log
.info("Starting")
120 # Do some useful background work here.
122 # Use mgr_tick_period (default: 2) here just to illustrate
123 # consuming native ceph options. Any real background work
124 # would presumably have some more appropriate frequency.
125 sleep_interval
= self
.mgr_tick_period
126 self
.log
.debug('Sleeping for %d seconds', sleep_interval
)
127 self
.event
.wait(sleep_interval
)
130 def shutdown(self
) -> None:
132 This method is called by the mgr when the module needs to shut
133 down (i.e., when the serve() function needs to exit).
135 self
.log
.info('Stopping')