学习 OpenStack (1):杂记

源码查看小结

使用方式

1.Web界面,Dashboard(horizon)
2.命令行,cliff
3.API,RESTful API

服务部署:(python: wsgi -> application)

Apache + mod_wsgi (官方)
Nginx + uWSGI

API框架

1.Paste + PasteDeploy + Routes + WebOb
2.Pecan (+Paste)

项目结构(以gnocchi为例---裁剪后)

|-- ./doc
|   `-- ./doc/source
|-- ./gnocchi
|   |-- ./gnocchi/__init__.py
|   |-- ./gnocchi/archive_policy.py
|   |-- ./gnocchi/calendar.py
|   |-- ./gnocchi/carbonara.py
|   |-- ./gnocchi/chef.py
|   |-- ./gnocchi/cli
|   |-- ./gnocchi/common
|   |-- ./gnocchi/exceptions.py
|   |-- ./gnocchi/gendoc.py
|   |-- ./gnocchi/gnocchi-config-generator.conf
|   |-- ./gnocchi/incoming
|   |-- ./gnocchi/indexer
|   |-- ./gnocchi/json.py
|   |-- ./gnocchi/opts.py
|   |-- ./gnocchi/resource_type.py
|   |-- ./gnocchi/rest
|   |-- ./gnocchi/service.py
|   |-- ./gnocchi/setuptools.py
|   |-- ./gnocchi/statsd.py
|   |-- ./gnocchi/storage
|   |-- ./gnocchi/tests
|   `-- ./gnocchi/utils.py
|-- ./setup.cfg
|-- ./setup.py
`-- ./tox.ini

rest目录结构

|-- ./rest
|   |-- ./rest/__init__.py
|   |-- ./rest/aggregates
|   |-- ./rest/api-paste.ini
|   |-- ./rest/api.py
|   |-- ./rest/app.py
|   |-- ./rest/auth_helper.py
|   |-- ./rest/influxdb.py
|   |-- ./rest/policy.json
|   |-- ./rest/prometheus
|   `-- ./rest/wsgi.py

代码查看

[静态文件template&static openstack 暂不涉及]
1.python web 框架(pecan)入口查看 wsgi.py
2.paste 全局配置 api-paste.ini

api-paste.ini

api-pase.ini 应用程序入口 composite:xxx
应用程序 app:xxx
过滤器 filter:xxx
管道 pipeline:xxx
api-paste.ini
...

[composite:gnocchi+keystone]## 主程序入口,
use = egg:Paste#urlmap  ## 表示使用 Paste.urlmap 来实现 composite
/ = gnocchiversions_pipeline    ## url = app
/v1 = gnocchiv1+keystone
/healthcheck = healthcheck

[app:gnocchiv1]## 应用
paste.app_factory = gnocchi.rest.app:app_factory    ## 定义application时需要运行的python code
root = gnocchi.rest.api.V1Controller    ## 参数

[filter:keystone_authtoken]## [filter:<filter1>]
use = egg:keystonemiddleware#auth_token
oslo_config_project = gnocchi

[pipeline:gnocchiv1+keystone]## 指定对应 app 的前置处理
## pipeline = <filter1> <filter2> <app> 用filter1、filter2 过滤app
pipeline = http_proxy_to_wsgi keystone_authtoken gnocchiv1

...

wsgi.py

"""This file is loaded by gnocchi-api when executing uwsgi"""
from gnocchi.cli import api
from gnocchi.rest import app
application = app.load_app(api.prepare_service())

项目入口

gnocchi.cli.api.api.prepare_service() 加载服务配置
gnocchi.rest.app.load_app() 生成wsgi application

中间件

def app_factory(global_config, **local_conf):
    global APPCONFIGS
    appconfig = APPCONFIGS.get(global_config.get('configkey'))
    return _setup_app(root=local_conf.get('root'), **appconfig)

def _setup_app(root, conf, not_implemented_middleware):     
    app = pecan.make_app(
        root,
        hooks=(GnocchiHook(conf),),
        guess_content_type_from_ext=False,
        custom_renderers={"json": JsonRenderer}
)

视图

root = gnocchi.rest.api.V1Controller
代码省略

视图 ArchivePoliciesController 简介:

class ArchivePoliciesController(rest.RestController):

    @pecan_expose()
    def _lookup(self, archive_policy, *remainder):
        return ArchivePolicyController(archive_policy), remainder

    @pecan.expose(‘json’)
    def post(self):
        ...

    @pecan.expose()
    def get_all(self):
        ...


class MetricController(rest.RestController):

    _custom_actions = {
            'measures': ['POST', 'GET']
    }

    @pecan.expose()
    def post_measures(self):
        ...

    @pecan.expose()
    def get_measures(self):
        ...

pecan 路由规则

https://pecan.readthedocs.io/en/latest/rest.html#rest

代码调试

python内置调试工具 pdb,在需要断点调试的位置,引入 import pdb; pdb.set_trace()即可;
openstack 基于python语言开发,同样可以通过 pdb 进行调试。

pdb

import pdb
pdb.set_trace()

rpdb(适用场景如下)

  • 1.进程关闭了stdin/stdout
  • 2.Fork多进程
  • 3.服务启动(Apache-httpd)
# 代码内需断点处
import rpdb
rpdb.set_trace()

# bash窗口2 rpdb 默认监听端口 4444
nc localhost 4444

参考

上一篇:OpenStack创建的实例连不上外网,在实例内修改网络配置即可


下一篇:十三、镜像管理