[Openstack] Working example of Nova "hooks"?

Kimball, Conrad conrad.kimball at boeing.com
Tue Aug 19 18:40:08 UTC 2014

I'm looking for a working example of using "hooks" to extend Nova.

I have found this reference: http://docs.openstack.org/developer/nova/devref/hooks.html
and this one: http://docs.openstack.org/developer/nova/api/nova.hooks.html.

I have found several occurrences of the "@hooks.add_hook()" decorator in the Nova code, for example in nova/compute/api.py
this code where I should be able to add pre- and/or post-hooks for instance creation:

    def create(self, context, instance_type,
               image_href, kernel_id=None, ramdisk_id=None,
               min_count=None, max_count=None,
               display_name=None, display_description=None,
               key_name=None, key_data=None, security_group=None,
               availability_zone=None, user_data=None, metadata=None,
               injected_files=None, admin_password=None,
               block_device_mapping=None, access_ip_v4=None,
               access_ip_v6=None, requested_networks=None, config_drive=None,
               auto_disk_config=None, scheduler_hints=None, legacy_bdm=True):

But no luck so far...

I tried writing a simple "create_instance" hook that would just make a log entry that it had been called:

First, the hook code itself, source file named openstack_hook_example1.py:
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)

class Example1HookClass(object):
    def pre(self, *args, **kwargs):
        LOG.warn(_("Example1HookClass pre called"))

    def post(self, rv, *args, **kwargs):
        LOG.warn(_("Example1HookClass post called"))

And the setup.py to go with it:
from setuptools import setup

    description='Demonstration of OpenStack hooks, example #1',
        'nova.hooks': [
          'create_instance': openstack_hook_example1.hooks.Example1HookClass,

But when I try to install it ("python setup.py install") it fails with this error:
   File "setup.py", line 16
     'create_instance': openstack_hook_example1.hooks.Example1HookClass,

So it looks as if the entry_point code fragment in the first-mentioned web site above has an error.

And finally, I have not found any documentation about how to configure OpenStack to use my
Hook after I manage to get it installed.

Thanks in advance,

More information about the Openstack mailing list