Openstack Swift中间件编写

关于openstack swift的资料能够看这里这里还有这里html

准备环境

从零开始接触的同窗能够先从swift的all in one部署开始学习,在本机搭建好swift环境就能够进行简单的测试了。因为swift是用Python语言写的,若是要开发swift的中间件的还须要在本地安装Pythone的IDE,我比较喜欢JETBRAIN(他们比较出名的是JAVA的IDE——IDEA)公司的IDE——Pycharm。准备环境以下:node

  • Ububutn 12.04 LTS 64bit
  • Python2.7(虽然如今已经有Python3了,但swift是用2.x的Python写的,Python3不向后兼容Python2)
  • Pycharm3

中间件介绍

swift经过提供基于HTTP协议的API给外界调用来完成对象存储的功能,咱们从swift的各个部署说明里面能够看到,proxy server和storage node的配置文件里面都有一个[pipeline:main],这个是swift各个服务的请求链,由多个中间件组成的一个中间件集合。pipeline有点像J2EE里面filter,每一个http请求须要通过各个服务的pipeline。python

proxy-server.conf
1
2
3
4
5
6
... [pipeline:main] # Yes, proxy-logging appears twice. This is so that # middleware-originated requests get logged too. pipeline = catch_errors healthcheck proxy-logging bulk ratelimit crossdomain slo cache tempurl tempauth staticweb account-quotas container-quotas proxy-logging proxy-server ... 
account-server.conf
1
2
3
4
... [pipeline:main] pipeline = recon account-server ... 

中间件编写

了解了swift的基本功能流程后,咱们就能够来写本身的中间件了。git

没有写过中间件的同窗能够经过学习其余中间件开始,在swift的源码中配置了不少中间件,有一些功能很是简单。好比name_check中间件,这个中间件的做用是拿来分析请求的url,判断url中是否有特殊字符,长度是否超出规定长度等。这个中间件没有配置在swift的标准配置中,有须要的能够自行加上本机的swift环境作测试。github

咱们先来看一下name_check中间件的配置信息:web

proxy-server.conf
1
2
3
4
5
6
7
8
[pipeline:main] pipeline = catch_errors healthcheck name_check cache ratelimit tempauth sos  proxy-logging proxy-server  [filter:name_check] use = egg:swift#name_check forbidden_chars = '"`<> maximum_length = 255 

在上面的例子中,name_check中间件加在healthcheck这个中间件后面,filter:name_check下面的配置信息是name_check的一些配置参数。shell

  • forbidden_chars: 指url中不能包含的特殊字符
  • maximum_length: 指url的最大长度

咱们再来看name_check的单元测试:ubuntu

test_name_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class FakeApp(object):   def __call__(self, env, start_response):  return Response(body="OK")(env, start_response)   class TestNameCheckMiddleware(unittest.TestCase):   def setUp(self):  self.conf = {'maximum_length': MAX_LENGTH, 'forbidden_chars':  FORBIDDEN_CHARS, 'forbidden_regexp': FORBIDDEN_REGEXP}  self.test_check = name_check.filter_factory(self.conf)(FakeApp())   def test_valid_length_and_character(self):  path = '/V1.0/' + 'c' * (MAX_LENGTH - 6)  resp = Request.blank(path, environ={'REQUEST_METHOD': 'PUT'}  ).get_response(self.test_check)  self.assertEquals(resp.body, 'OK')   ...... # other test cases  if __name__ == '__main__':  unittest.main() 

看源码先从单元测试看起,能够以最快的速度了解源代码的功能。在这个测试案例中,测试先mock了一个虚拟的app,这个app不会真实的调用swift,而是会将http response返回预设好的值。
再看其中的一个测试案例,这里给定了一个最大长度url,而后经过调用name_check中间件,指望请求能够正常经过。swift

最后咱们再来看name_check中间件的源码几个方法: * init: 中间件的初始化方法 * call: 中间件被调用时触发的方法 * filter_factory: 这个是类之外的方法,在swift服务启动时会建立中间件实例,并加入到pipeline中。app

学习完这个简单的中间件后,相信你们均可以依葫芦画瓢开始写本身的中间件了。

修改配置文件

编写完中间件以后,还须要将中间件配置到swift中,这样才算真正完成中间件的建立。

首先先中止swift的服务

shell
1
swift@ubuntu:~$ swift-init main stop 

接着修改conf文件

假设你增长的中间件是proxy server的中间件,就修改proxy-server.conf,自行决定要放到pipeline中的哪一个位置,具体要看你的中间件是执行什么功能。

proxy-server.conf
1
2
3
4
5
6
7
8
[pipeline:main] pipeline = catch_errors healthcheck your_middleware cache ratelimit tempauth sos  proxy-logging proxy-server  [filter:your_middleware] use = egg:swift#your_middleware your_middleware_config1 = value1 your_middleware_config1 = value2 

要修改swift的根目录下的setup.cfg文件

setup.cfg
1
2
3
paste.filter_factory =  #这里加入一行本身的中间件,能够看下name_check中间件是怎么写的  name_check = swift.common.middleware.name_check:filter_factory 

执行命令从新安装swift

shell
1
2
swift@ubuntu:~$ cd swift目录 swift@ubuntu:~$ sudo python setyp.py develop 

最后重启swift服务

shell
1
swift@ubuntu:~$ swift-init main start 
相关文章
相关标签/搜索