#Sora #openstack Base Library stevedore Trial Summary

Source: Internet
Author: User
Tags id3 virtualenv












What is stevedore?



Stevedore is built on the functionality of Setuptools's entry point, which is used by Python programs to dynamically load code that is used by multiple components in OpenStack: Plugin for example Ceilometer,neutron. Of course, you can use it directly



Some of Python's black magic implements the plugin's loading, but it's too primitive. Stevedore based on entry point provides a higher level of encapsulation.



Official documentation for Stevedore here: http://docs.openstack.org/developer/stevedore/



Learning and Getting Started setuptools:http://www.360doc.com/content/14/0306/11/13084517_358166737.shtml



Partial translations of official documents: http://www.360doc.com/content/14/0429/19/9482_373285413.shtml



From  Kong Lingxian (source address not the person's blog) setup.py detailed: http://blog.sina.com.cn/s/blog_4951301d0101etvj.html









I plan to introduce stevedore and Oslo.config in the Sora project to simplify some of the development components, first testing the stevedore and writing a simple scheduler plugin



Environment Preparation:



Install stevedore libraries, and organize related catalogs


Pip Install stevedoremkdir SORACD Soramkdir Scheduler #scheduler在sora目录中


Build such a directory tree:









Steps:



Create an abstract class scheduler, the new plugin to inherit scheduler and override the related methods scheduler


#sora/scheduler/base.pyimport Abcclass Scheduler (object): __metaclass__ = abc. Abcmeta @abc. Abstractmethod def Scheduler (self,data): Pass





Inherit base class Create plug-in simple and memory


#sora/scheduler/memory.pyimport baseclass Memoryscheduler (base.scheduler): Def scheduler (self,data): id = Data[max (d ata[' memory ')] return ID
#sora/scheduler/simple.pyimport baseimport randomclass simplescheduler (base.scheduler): Def scheduler (SELF,DATA): ID = Data[random.choice (data[' memory ')] return ID


The simple plugin randomly selects a node ID, while memory selects the node ID with the most






Writing setup.py


#sora/setup.pyfrom setuptools import setup, find_packagessetup (     Name= ' Sora-scheduler ',     version= ' 1.0 ',     description= ' Sora.scheduler ',     author= ' Hochikong ',     author_email= ' Hochikong ',     platforms=[' any '],    scripts=[],   # provides =[' Sora.scheduler ',    #           ],     packages=find_packages (),    include_package_data=true,     entry_points={         ' Sora.scheduler ': [              ' Memorybase = scheduler.memory: Memoryscheduler ',             ' randombase =  scheduler.simple:simplescheduler ', &NBSp;       ],    },    zip_safe= False,)





To install a package you have written:


[Email protected]:~/sora# python setup.py installrunning installrunning bdist_ eggrunning egg_infocreating sora_scheduler.egg-infowriting sora_scheduler.egg-info/ pkg-infowriting top-level names to sora_scheduler.egg-info/top_level.txtwriting  dependency_links to sora_scheduler.egg-info/dependency_links.txtwriting entry points  to sora_scheduler.egg-info/entry_points.txtwriting manifest file  ' sora_scheduler.egg-info/ SOURCES.txt ' reading manifest file  ' sora_scheduler.egg-info/sources.txt ' writing manifest  file  ' Sora_scheduler.egg-info/sources.txt ' installing library code to build/ bdist.linux-x86_64/eggrunning install_librunning build_pycreating buildcreating build/ lib.linux-x86_64-2.7creating build/lib.linux-x86_64-2.7/schedulercopying scheduler/__init__.py  -> build/lib.linux-x86_64-2.7/schedulercopying scheduler/base.py -> build/lib.linux-x86_64-2.7/schedulercopying  scheduler/memory.py -> build/lib.linux-x86_64-2.7/schedulercopying scheduler/simple.py  -> build/lib.linux-x86_64-2.7/schedulercreating build/bdist.linux-x86_64creating build/ Bdist.linux-x86_64/eggcreating build/bdist.linux-x86_64/egg/schedulercopying build/lib.linux-x86_ 64-2.7/scheduler/__init__.py -> build/bdist.linux-x86_64/egg/schedulercopying build/ lib.linux-x86_64-2.7/scheduler/base.py -> build/bdist.linux-x86_64/egg/schedulercopying  Build/lib.linux-x86_64-2.7/scheduler/memory.py -> build/bdist.linux-x86_64/egg/schedulercopying  build/lib.linux-x86_64-2.7/scheduler/simple.py -> build/bdist.linux-x86_64/egg/ schedulerbyte-compiling build/bdist.linux-x86_64/egg/scheduler/__init__.py to __init__. Pycbyte-compiling build/bdist.linux-x86_64/egg/schedulEr/base.py to base.pycbyte-compiling build/bdist.linux-x86_64/egg/scheduler/memory.py to  memory.pycbyte-compiling build/bdist.linux-x86_64/egg/scheduler/simple.py to  Simple.pyccreating build/bdist.linux-x86_64/egg/egg-infocopying sora_scheduler.egg-info/pkg-info  -> build/bdist.linux-x86_64/egg/egg-infocopying sora_scheduler.egg-info/sources.txt - > build/bdist.linux-x86_64/egg/EGG-INFOcopying sora_scheduler.egg-info/dependency_links.txt  -> build/bdist.linux-x86_64/egg/egg-infocopying sora_scheduler.egg-info/entry_points.txt - > build/bdist.linux-x86_64/egg/EGG-INFOcopying sora_scheduler.egg-info/not-zip-safe ->  build/bdist.linux-x86_64/egg/EGG-INFOcopying sora_scheduler.egg-info/top_level.txt ->  build/bdist.linux-x86_64/egg/egg-infocreating distcreating  ' Dist/sora_scheduler-1.0-py2.7.egg '   and adding  ' Build/bdist.Linux-x86_64/egg '  to itremoving  ' Build/bdist.linux-x86_64/egg '   (and everything  Under it) processing sora_scheduler-1.0-py2.7.eggcreating /usr/local/lib/python2.7/dist-packages /sora_scheduler-1.0-py2.7.eggextracting sora_scheduler-1.0-py2.7.egg to /usr/local/lib/ Python2.7/dist-packagesadding sora-scheduler 1.0 to easy-install.pth fileinstalled  /usr/local/lib/python2.7/dist-packages/sora_scheduler-1.0-py2.7.eggprocessing dependencies for  sora-scheduler==1.0finished processing dependencies for sora-scheduler==1.0





Try loading the plugin manually:


[Email protected]:~/sora# pythonpython 2.7.6 (default, Mar, 22:59:56) [GCC 4.8.2] on Linux2type ' help ', "copyright "," credits "or" license "for more information.>>> from scheduler.memory import memoryscheduler>>> dt = {: ' Id1 ', 324: ' Id2 ', 434: ' ID3 ', ' memory ':[13,324,434]}>>> driver = Memoryscheduler () >>> Driver.scheduler (DT) ' ID3 ' >>>


But manual loading doesn't make much sense.






Using Stevedore's DriverManager:


>>> from stevedore import driver>>> dt = {: ' Id1 ', 324: ' Id2 ', 434: ' ID3 ', ' Memory ':[13,324,434]}> >> mgr = driver. DriverManager (... namespace= ' sora.scheduler ',... name= ' randombase ',... invoke_on_load=true, #设置为true, i.e. Automatically instantiate the plug-in class after loading, or, if it is a function, call ...) >>> mgr.driver.scheduler (DT) ' ID3 ' >>> mgr.driver.scheduler (dt) ' Id2 ' >>> Mgr.driver.scheduler (DT) ' ID3 ' >>> mgr.driver.scheduler (dt) ' Id2 ' >>> mgr.driver.scheduler (dt) ' ID1 ' >>>


Here I import the randombase, how to call plugin in the method is very obvious






Incidentally, check the installation status of my package in Python:


[email protected]:~# cd /usr/local/lib/python2.7/dist-packages/[email protected]:/usr/ local/lib/python2.7/dist-packages# lsamqp                                              opensslamqp-1.4.6.dist-info                            pbranyjson                                           pbr-1.3.0.dist-infoanyjson-0.3.3.egg-info                          pikabackports                                        pika-0.9.14.egg-infobackports.ssl_match_ hostname-3.4.0.2.egg-info  psutilbilliard                                          psutil-2.2.1.egg-infobilliard-3.3.0.19.egg-info                      _psutil_linux.so_billiard.so                                     _psutil_posix.sobottle-0.12.8.egg-info                           pymongobottle.py                                        pymongo-2.8.egg-infobottle.pyc                                       pyOpenSSL-0.15.1.dist-infobson                                              python_etcd-0.3.3.egg-infocelery                                           pytzcelery-3.1.17.dist-info                          pytz-2015.2.dist-infodocker                                           six-1.9.0.dist-infodocker_ py-1.0.0.egg-info                        six.pyeasy-install.pth                                 six.pycetcd                                             sora_scheduler-1.0-py2.7.eggeventlet                                          sqlalchemy-0.9.9-py2.7-linux-x86_64.eggeventlet-0.17.1.dist-info                       stevedorefuntests                                          stevedore-1.6.0.dist-infoglances                                          testsGlances-2.3.egg-info                             virtualenv-12.0.7.dist-infogreenlet-0.4.5.egg-info                          virtualenv.pygreenlet.so                                      virtualenv.pycgridfs                                           virtualenv_supportkombu                                             websocketkombu-3.0.24.dist-info                          websocket_client-0.25.0.egg-info





You can see that there is a Sora_scheduler-1.0-py2.7.egg directory inside, check it out:






I'm not sure why Python does not need to specify Sora_scheduler-1.0-py2.7.egg when importing the Scheduler module, which may be related to the egg, and when the import scheduler is executed, the imported Sora_ Under the Scheduler-1.0-py2.7.egg directory



Scheduler package, the same as the SQLAlchemy, there is a Sqlalchemy-0.9.9-py2.7-linux-x86_64.egg, import only to execute import sqlalchemy






Questions:



Some people may ask, if I want to add a new plugin to do, I think, the best way is to modify the setup.py, update the package can be installed once






Reference:



Http://docs.openstack.org/developer/stevedore/



Http://www.360doc.com/content/14/0306/11/13084517_358166737.shtml



Http://www.360doc.com/content/14/0429/19/9482_373285413.shtml






#Sora #openstack Base Library stevedore Trial Summary


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.