1. Installing the PYTHON,UWSGI,NGINX Environment
PIP installation omitted
Yumgroupinstall "Developmenttools" Yuminstallzlib-develbzip2-develpcre-developenssl-develncurses-develsqlite-develreadline-develtk-develyuminstallpython-dev Elpipinstalluwsgi
2. Understand the Restful API
Http://www.ruanyifeng.com/blog/2014/05/restful_api.html
3. Understanding the Flask Framework
Http://www.pythondoc.com/flask-restful/first.html
4. Call the Python plugin library
http://docs.ceph.org.cn/rbd/librbdpy/
5. Write Interface Program
#!/usr/bin/env python#-*-coding:utf-8-*-from flask Import flask, jsonify, Make_responseimport radosimport rbdfrom Flas K.ext.httpauth Import httpbasicauthimport time ' Ceph interface package, mainly implemented CRD function Curl Xxx.xx.xx.xx2/ceph/v1.0/rbd-u admin: D41d8cd98f00b204e9fdsafdsafdasf333fcurl-x POST xxx.xx.xx.xx/ceph/v1.0/rbd/images/v0/10234556-u Admin: D8cd98f00b204e9fdsafdsafdasf333fcurl-x DELETE xxx.xx.xx.xx/ceph/v1.0/rbd/images/cai-ceph-a3-u Admin: d8cd98f00b204e9fdsafdsafdasf333f Author: Cai Sisheng "auth = Httpbasicauth () application = Flask (__name__) Application.debug = Truedef Connect_ceph (): cluster = Rados. Rados (conffile= '/etc/ceph/ceph.conf ') cluster.connect () Ioctx = Cluster.open_ioctx (' RBD ') return ioctx@auth.get_p Assworddef Get_password (username): if username = = ' Admin ': Return ' d8cd98f00b204e9fdsafdsafdasf333f ' return N One@auth.error_handlerdef 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_requireddef get_rbd_list (): Ioctx = Connect_ceph () Rbd_inst = RBD. RBD () images = Rbd_inst.list (ioctx) return jsonify ({"Code": $, "info": Images}) @application. Route ('/ceph/v1.0/rbd/ Images/<image_name>/<int:size> ', methods=[' POST ']) @auth. Login_requireddef Create_volume (Image_name, Size): Ioctx = Connect_ceph () Rbd_inst = RBD. RBD () try:rbd_inst.create (Ioctx, image_name, size) return jsonify ({"Code": $, "info": "Create volume S Uccess "}) except Exception as E:return jsonify ({" Code ": +," info ":" Create Volume failure\r\n "+e.message}) @ap Plication.route ('/ceph/v1.0/rbd/images/<image_name> ', methods=[' DELETE ') @auth. Login_requireddef Delete_ Rbd_image (image_name): if image_name = = "or Len (image_name) = = 0:return Jsonify ({" Code ": +," info ":" Parame ter 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": $, "info": "Delete Success"}) except Exception as E:if e.message = = ' Error removing image ': Delete_rbd_image (image_name) return jsonify ({"Code": +, "info": E.M Essage}) Else:return jsonify ({"Code": 403, "info": "Requests image_name not being exist"}) if __name__ = = ' __main__ ': Application.Run (host= ' 0.0.0.0 ', port=80)
6.UWSGI Configuration
Vim Uwsgi.ini
[uwsgi]master=truewsgi-file=manage.pycallable=applicationsocket=/opt/soft/python-ceph-api/ ceph-api.sockchmod-socket=664processes=10threads=4buffer-size=32768touch-reload=/opt/soft/ Python-ceph-apimodule=rbd_ap
7.nginx Configuration
Vim/etc/nging/conf.d/ceph_api.conf
upstreamscloud_django{#serverunix:///path/to/your/mysite/mysite.sock; #forafilesocketserverunix:///opt/soft/ Python-ceph-api/ceph-api.sock, #forawebportsocket (we ' Llusethisfirst)}server{# theportyoursitewillbeservedonlisten20000, #thedomainnameitwillservefor #server_namewww.scloud.cn;# Substituteyourmachine ' sIPaddressorFQDNcharsetutf-8; #maxuploadsizeclient_max_body_size75M; #adjusttotaste # djangomedialocation/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}}
Ceph RBD Encapsulation API