设为首页 收藏本站
查看: 854|回复: 0

[经验分享] OpenStack API and WSGI

[复制链接]

尚未签到

发表于 2018-6-1 11:02:09 | 显示全部楼层 |阅读模式
  OpenStack是一个很多服务的集合,它所包含的服务有nova(compute)、keystone(identity)、glance(image)、horizon(dashboard)、neutron(network)、swift(object storage)等。每个服务都可以看做是一个WebApp,每个service从前端看就是一个REST API server,有可能后端有多个backend server,service之间是通过messaging通信。
  

  所有对openstack的操作都可以用api来实现,官方链接:
  http://docs.openstack.org/api/
  http://api.openstack.org/
  

  API使用http协议,再加上 json数据格式,可以使用curl、http库、python client库来操作。

  1、使用novaclient查看image list
#!/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.images.list(detailed=True)
for image in results:
    print image.id, image.name, image.status  

  2、使用urllib2库查看image
#!/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()
image_api = service_url + '/images/detail'
images_request = urllib2.Request(image_api)
images_request.add_header('Content-Type', 'application/json;charset=utf8')
images_request.add_header('Accept', 'application/json')
images_request.add_header('User-Agent', 'python-mikeyp')
images_request.add_header('X-Auth-Token', token)
images_request.add_header('X-Auth-Project-Id', 'demo')
image_response = urllib2.urlopen(images_request)
image_data = json.loads(image_response.read())
print json.dumps(image_data, indent=4)   

  OpenStack Web Stack
  1、paste http server:http协议 + networking
  2、webob请求和响应:http请求和响应的封装
  3、openstack code(nova、glance、neutron、etc)
  4、web service gateway interface(wsgi):wsgi是一种协议
  

  WSGI简要概括:
  1、wsgi application:python调用需要传入两个参数,a、wsgi environment,b、一个start_response函数;application会调用start_response,然后再return response
  2、wsgi server:server会调用application
  3、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
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-498077-1-1.html 上篇帖子: fuel7.0 openstack webui 默认密码查看 下篇帖子: openstack之dashboard与cinder(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表