OpenStack是一個不少服務的集合,它所包含的服務有nova(compute)、keystone(identity)、glance(p_w_picpath)、horizon(dashboard)、neutron(network)、swift(object storage)等。每一個服務均可以看作是一個WebApp,每一個service從前端看就是一個REST API server,有可能後端有多個backend server,service之間是經過messaging通訊。html
全部對openstack的操做均可以用api來實現,官方連接:前端
http://docs.openstack.org/api/ python
API使用http協議,再加上 json數據格式,可使用curl、http庫、python client庫來操做。
github
一、使用novaclient查看p_w_picpath listweb
#!/usr/bin/env python import logging import novaclient from novaclient.v1_1 import client # enable debug logging logger = logging.getLogger('novaclient.client') logger.setLevel(logging.DEBUG) debug_stream = logging.StreamHandler() logger.addHandler(debug_stream) auth_url = 'http://10.100.20.22:5000/v2.0' user = 'admin' password = 'devstack' project = 'demo' region = 'RegionOne' service = 'compute' nova = client.Client(user, password, project, auth_url, region_name=region, service_type=service) results = nova.p_w_picpaths.list(detailed=True) for p_w_picpath in results: print p_w_picpath.id, p_w_picpath.name, p_w_picpath.status
二、使用urllib2庫查看p_w_picpathjson
#!/usr/bin/env python import urllib2 import json def get_keystone_token(): """authenticate against keystone identity service returns an auth token, and the service url """ user = 'admin' password = 'devstack' project = 'demo' auth_url = 'http://10.100.20.22:5000/v2.0/tokens' auth_request = urllib2.Request(auth_url) auth_request.add_header('Content-Type', 'application/json;charset=utf8') auth_request.add_header('Accept', 'application/json') auth_request.add_header('User-Agent', 'python-mikeyp') auth_data = {"auth": {"tenantName": project, "passwordCredentials": { "username": user, "password": password} } } auth_request.add_data(json.dumps(auth_data)) auth_response = urllib2.urlopen(auth_request) response_data = json.loads(auth_response.read()) token = response_data['access']['token']['id'] service_list = response_data['access']['serviceCatalog'] for s in service_list: if s['type'] == 'compute' and s['name'] == "'Compute Service'": break nova_url = s['endpoints'][0]['publicURL'] return (token, nova_url) token, service_url = get_keystone_token() p_w_picpath_api = service_url + '/p_w_picpaths/detail' p_w_picpaths_request = urllib2.Request(p_w_picpath_api) p_w_picpaths_request.add_header('Content-Type', 'application/json;charset=utf8') p_w_picpaths_request.add_header('Accept', 'application/json') p_w_picpaths_request.add_header('User-Agent', 'python-mikeyp') p_w_picpaths_request.add_header('X-Auth-Token', token) p_w_picpaths_request.add_header('X-Auth-Project-Id', 'demo') p_w_picpath_response = urllib2.urlopen(p_w_picpaths_request) p_w_picpath_data = json.loads(p_w_picpath_response.read()) print json.dumps(p_w_picpath_data, indent=4)
OpenStack Web Stackswift
一、paste http server:http協議 + networking後端
二、webob請求和響應:http請求和響應的封裝api
三、openstack code(nova、glance、neutron、etc)
四、web service gateway interface(wsgi):wsgi是一種協議
WSGI簡要歸納:
一、wsgi application:python調用須要傳入兩個參數,a、wsgi environment,b、一個start_response函數;application會調用start_response,而後再return response
二、wsgi server:server會調用application
三、wsgi middleware:wsgi中間件,能夠過濾請求
一個簡單的wsgi application
[root@devstack tmp]# cat hello_world.py
# 使用paste+wsgi from paste import httpserver def application(environ, start_response): start_response('200 ok', [('Content-type', 'text/html')]) return ['Hello World'] httpserver.serve(application, host='127.0.0.1', port=8888) # 驗證結果 [root@devstack tmp]# python hello_world.py serving on [root@devstack ~]# curl http://localhost:8888 Hello World
帶有webob + paste + wsgi的application
[root@devstack tmp]# cat wsgi_webob.py from webob import Response from webob.dec import wsgify from paste import httpserver from paste.deploy import loadapp ini_path = '/tmp/wsgi_webob.ini' @wsgify def application(request): return Response('Hello World!') def app_factory(global_config, **local_config): return application wsgi_app = loadapp('config:' + ini_path) httpserver.serve(wsgi_app, host='127.0.0.1', port=8888) [root@devstack tmp]# cat wsgi_webob.ini [app:main] paste.app_factory = wsgi_webob:app_factory
引入wsgi middleware中間件
[root@devstack tmp]# cat wsgi_webob_mi.py from webob import Response from webob.dec import wsgify from webob import exc from paste import httpserver from paste.deploy import loadapp ini_path = '/tmp/wsgi_webob_mi.ini' @wsgify def application(request): return Response('Hello World!') @wsgify.middleware def auth_filter(request, app): if request.headers.get('X-Auth-Token') != 'open-sesame': return exc.HTTPForbidden() return app(request) def app_factory(global_config, **local_config): return application def filter_factory(global_config, **local_config): return auth_filter wsgi_app = loadapp('config:' + ini_path) httpserver.serve(wsgi_app, host='127.0.0.1', port=8888) [root@devstack tmp]# cat wsgi_webob_mi.ini [pipeline:main] pipeline = auth main1 [app:main1] paste.app_factory = wsgi_webob_mi:app_factory [filter:auth] paste.filter_factory = wsgi_webob_mi:filter_factory # 驗證結果 [root@devstack tmp]# python wsgi_webob_mi.py serving on [root@devstack ~]# curl -H "X-Auth-Token: open-sesame" http://localhost:8888 Hello World! [root@devstack ~]# curl http://localhost:8888 # 無結果
還有eventlet.wsgi也有必要了解,連接:http://eventlet.net/doc/modules/wsgi.html
參考連接
https://github.com/lhrc-mikeyp/Presentations/blob/master/openstack_api_wsgi/OpenStack_WSGI.pdf