ceph rbd 패키지 api

4734 단어
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.html4.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//', 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
"+e.message}) @application.route('/ceph/v1.0/rbd/images/',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
	}

}

  
다음으로 전송:https://www.cnblogs.com/waken-captain/p/8322249.html

좋은 웹페이지 즐겨찾기