Custom Search

Thursday, January 29, 2015

How to OpenStack load Drivers Extensions and Plugins using stevedore

1)
Create a virtual environment

#virtualenv venv
#source venv/bin/activate




2)
Install stevedore
#pip install stevedore

3)
Open Python console

#python
>>> from stevedore import driver
>>> p=driver.DriverManager(namespace="stevedore.example.formatter", name='simple')


* Means, load the driver "simple" from then namespace "stevedore.example.formatter"".

* "namespace" is defined in setup.py as "entry_points".
https://github.com/openstack/stevedore/blob/master/stevedore/example/setup.py

* Driver file "simple"
https://github.com/openstack/stevedore/blob/master/stevedore/example/simple.py

* For more info
http://docs.openstack.org/developer/stevedore/tutorial/creating_plugins.html

4)
"entry_points" from neutron project.
https://github.com/openstack/neutron/blob/master/setup.cfg

==== Custom Example=======

1)
Create a new project named "myproject" and create following files


a)
#setup.py


import setuptools
setuptools.setup(setup_requires=['pbr'], pbr=True)


b)

#setup.cfg

[metadata]
name = myproject

[files]
packages = myproject

[entry_points]
myproject.driver =
    simple_driver1 = myproject.my_driver:SimpleDriver1
    simple_driver2 = myproject.my_driver:SimpleDriver2

myproject.extension =
    simple_extension1 = myproject.my_extension:SimpleExtension1
    simple_extension2 = myproject.my_extension:SimpleExtension2


c)
Inside the "myproject" directory create an another directory with same name
and create following "my_driver.py" and "my_extension.py" files in it.

#mkdir myproject
#cd  myproject

d)
#vim my_driver.py


class SimpleDriver1:

    def get_name(self):
        return "This is SimpleDriver1"

class SimpleDriver2:

    def get_name(
self):
        return "This is SimpleDriver2"

      
e)
#vim my_extension.py


class SimpleExtension1:

    def get_name(
self):
        return "This is SimpleExtension1"

class SimpleExtension2:

    def get_name(
self):
        return "This is SimpleExtension2"


2)
Create a virtual environment

#virtualenv venv
#source venv/bin/activate


3)

Install stevedore
#pip install stevedore
     
4)
Goto "myproject" directory and install the project/module using setup.py script.
#cd myproject
#git init    
#python setup.py install    
  
      
5)
Goto python console, import stevedore and load the drivers and extensions from "myproject" module.

a)
Load the driver named "simple_driver1" and invokes the method "get_name"

>>> from stevedore import driver
>>> mydriver = driver.DriverManager(namespace="myproject.driver", name='simple_driver1')
>>> mydriver.driver
>>> mydriver.driver().get_name()
OR
>>> mydriver.extensions[0].plugin().get_name()


b)
Load all the extensions from the namespace "myproject.extension"

>>> from stevedore import extension
>>> extension = extension.ExtensionManager(namespace="myproject.extension")
>>> extension.extensions
>>> extension.extensions[0].plugin().get_name()
>>> extension.extensions[1].plugin().get_name()



No comments:

Post a Comment