2012-11-16 15:45:59 +00:00
|
|
|
Hooks
|
|
|
|
=====
|
|
|
|
|
|
|
|
Hooks provide a mechanism to extend Nova with custom code through a plugin
|
|
|
|
mechanism.
|
|
|
|
|
|
|
|
Named hooks are added to nova code via a decorator that will lazily load
|
|
|
|
plugin code matching the name. The loading works via setuptools
|
|
|
|
`entry points`_.
|
|
|
|
|
2013-07-05 22:26:06 -04:00
|
|
|
.. _`entry points`: http://pythonhosted.org/setuptools/pkg_resources.html#entry-points
|
2012-11-16 15:45:59 +00:00
|
|
|
|
|
|
|
What are hooks good for?
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
Hooks are good for anchoring your custom code to Nova internal APIs.
|
|
|
|
|
|
|
|
What are hooks NOT good for?
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
Hooks should not be used when API stability is a key factor. Internal APIs may
|
|
|
|
change. Consider using a notification driver if this is important to you.
|
|
|
|
|
|
|
|
Declaring hooks in the Nova codebase
|
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
The following example declares a *resize_hook* around the *resize_instance* method::
|
|
|
|
|
|
|
|
from nova import hooks
|
|
|
|
|
|
|
|
@hooks.add_hook("resize_hook")
|
|
|
|
def resize_instance(self, context, instance, a=1, b=2):
|
|
|
|
...
|
|
|
|
|
|
|
|
Hook objects can now be attached via entry points to the *resize_hook*.
|
|
|
|
|
|
|
|
Adding hook object code
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
1. Setup a Python package with a setup.py file.
|
|
|
|
2. Add the following to the setup.py setup call::
|
|
|
|
|
2014-04-02 10:25:26 -07:00
|
|
|
entry_points = {
|
2012-11-16 15:45:59 +00:00
|
|
|
'nova.hooks': [
|
2014-09-26 22:00:27 -04:00
|
|
|
'resize_hook=your_package.hooks:YourHookClass',
|
2012-11-16 15:45:59 +00:00
|
|
|
]
|
2014-04-02 10:25:26 -07:00
|
|
|
},
|
2012-11-16 15:45:59 +00:00
|
|
|
|
|
|
|
3. *YourHookClass* should be an object with *pre* and/or *post* methods::
|
|
|
|
|
|
|
|
class YourHookClass(object):
|
|
|
|
|
|
|
|
def pre(self, *args, **kwargs):
|
|
|
|
....
|
|
|
|
|
|
|
|
def post(self, rv, *args, **kwargs):
|
|
|
|
....
|