1.安装python,uwsgi,nginx环境
pip安装省略
yumgroupinstall"Developmenttools"
yuminstallzlib-develbzip2-develpcre-developenssl-develncurses-develsqlite-develreadline-develtk-devel
yuminstallpython-devel
pipinstalluwsgi
2.明白 Restful API
http://www.ruanyifeng.com/blog/2014/05/restful_api.html
3.了解flask框架
http://www.pythondoc.com/flask-restful/first.html
4.调用python插件库
http://docs.ceph.org.cn/rbd/librbdpy/
5.写接口程序
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, make_response
import rados
import rbd
from flask.ext.httpauth import HTTPBasicAuth
import time
'''
ceph 接口封装,主要实现 CRD 功能
curl xxx.xx.xx.xx2/ceph/v1.0/rbd -u admin:d41d8cd98f00b204e9fdsafdsafdasf333f
curl -X POST xxx.xx.xx.xx/ceph/v1.0/rbd/images/V0/10234556 -u admin:d8cd98f00b204e9fdsafdsafdasf333f
curl -X DELETE xxx.xx.xx.xx/ceph/v1.0/rbd/images/cai-ceph-a3 -u admin:d8cd98f00b204e9fdsafdsafdasf333f
作者: 蔡锡生
'''
auth = HTTPBasicAuth()
application = Flask(__name__)
application.debug = True
def connect_ceph():
cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')
cluster.connect()
ioctx = cluster.open_ioctx('rbd')
return ioctx
@auth.get_password
def get_password(username):
if username == 'admin':
return 'd8cd98f00b204e9fdsafdsafdasf333f'
return None
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)
@application.route('/')
def index():
return "Hello, world"
@application.route('/ceph/v1.0/rbd', methods=['GET'])
@auth.login_required
def get_rbd_list():
ioctx = connect_ceph()
rbd_inst = rbd.RBD()
images = rbd_inst.list(ioctx)
return jsonify({"code": 200, "info": images})
@application.route('/ceph/v1.0/rbd/images/<image_name>/<int:size>', methods=['POST'])
@auth.login_required
def create_volume(image_name, size):
ioctx = connect_ceph()
rbd_inst = rbd.RBD()
try:
rbd_inst.create(ioctx, image_name, size)
return jsonify({"code": 200, "info": "create volume success"})
except Exception as e:
return jsonify({"code": 400, "info": "create volume failure\r\n"+e.message})
@application.route('/ceph/v1.0/rbd/images/<image_name>',methods=['DELETE'])
@auth.login_required
def delete_rbd_image(image_name):
if image_name == '' or len(image_name) == 0:
return jsonify({"code": 400, "info": "parameter can't be null"})
ioctx = connect_ceph()
print dir(rbd)
rbd_inst = rbd.RBD()
if image_name in rbd_inst.list(ioctx):
try:
time.sleep(3)
rbd_inst.remove(ioctx, image_name)
return jsonify({"code": 200, "info": "delete success"})
except Exception as e:
if e.message == 'error removing image':
delete_rbd_image(image_name)
return jsonify({"code": 400, "info": e.message})
else:
return jsonify({"code": 403, "info": "requests image_name not be exist"})
if __name__ == '__main__':
application.run(host='0.0.0.0', port=80)
6.uwsgi 配置
vim uwsgi.ini
[uwsgi]
master=true
wsgi-file=manage.py
callable=application
socket=/opt/soft/python-ceph-api/ceph-api.sock
chmod-socket=664
processes=10
threads=4
buffer-size=32768
touch-reload=/opt/soft/python-ceph-api
module=rbd_ap
7.nginx 配置
vim /etc/nging/conf.d/ceph_api.conf
upstreamscloud_django{
#serverunix:///path/to/your/mysite/mysite.sock;#forafilesocket
serverunix:///opt/soft/python-ceph-api/ceph-api.sock;#forawebportsocket(we'llusethisfirst)
}
server{
#theportyoursitewillbeservedon
listen20000;
#thedomainnameitwillservefor
#server_namewww.scloud.cn;#substituteyourmachine'sIPaddressorFQDN
charsetutf-8;
#maxuploadsize
client_max_body_size75M;#adjusttotaste
#Djangomedia
location/media{
#alias/opt/soddft/scloud/media;#yourDjangoproject'smediafiles-amendasrequired
}
location/static{
#alias/opt/soft/scloud/static;#yourDjangoproject'sstaticfiles-amendasrequired
}
#Finally,sendallnon-mediarequeststotheDjangoserver.
location/{
uwsgi_passscloud_django;
include/etc/nginx/uwsgi_params;#theuwsgi_paramsfileyouinstalled
}
}