Integrating custom code with Nova using hooks

Would you like to run some custom Python code when Nova creates and
destroys virtual instances on your compute hosts? This is possible
using Nova’s support for hooks, but the existing documentation is
somewhat short on examples, so I’ve spent some time trying to get
things working.

The demo_nova_hooks repository contains a working example of the
techniques discussed in this article.

What’s a hook?

A Nova “hook” is a mechanism that allows you to attach a class of your
own design to a particular function or method call in Nova. Your
class should define a pre method (that will be called before the
method is called) and post function (that will be called after the
method is called):

The pre method will be called with the positional and keyword
arguments being passed to the hooked function. The post method
receives the return value of the called method in addition to the
positional and keyword arguments.

You connect your code to available hooks using Setuptools entry
points. For example, assuming that the above code lived in
module named your_package.hooks, you might have the following in the
corresponding setup.py file:

When called, self is a nova.compute.api.API object, context is a
nova.context.RequestContext object, instance_type is a dictionary
containing information about the selected flavor, and image_href is
an image UUID.

delete_instance

The delete_instance hook is attached to the _delete_instance
method in the nova.compute.manager.ComputeManager class, which is
called whenever Nova needs to delete an instance. The hook will
receive the following arguments:

def _delete_instance(self, context, instance, bdms, quotas):

Where:

self is a nova.compute.manager.ComputeManager object,

context is a nova.context.RequestContext,

instance is a nova.objects.instance.Instance object

bdms is a nova.objects.block_device.BlockDeviceMappingList
object, and

quotas is a nova.objects.quotas.Quotas object

instance_network_info

The instance_network_info hook is attached to the
update_instance_cache_with_nw_info function in
nova.network.base_api.py. The hook will receive the following
arguments:

I am not running Nova Network in my environment, so I have not
examined this hook in any additional detail.

A working example

The demo_nova_hooks repository implements simple logging-only
implementations of create_instance and delete_instance hooks. You
can install this code, restart Nova services, boot an instances, and
verify that the code has executed by looking at the logs generated in
/var/log/nova.