本文整理汇总了Python中proxy.Proxy.add_server方法的典型用法代码示例。如果您正苦于以下问题:Python Proxy.add_server方法的具体用法?Python Proxy.add_server怎么用?Python Proxy.add_server使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类proxy.Proxy
的用法示例。
在下文中一共展示了Proxy.add_server方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: api_proxy
# 需要导入模块: from proxy import Proxy [as 别名]
# 或者: from proxy.Proxy import add_server [as 别名]
def api_proxy():
user = request.args['user'] if 'user' in request.args else None
pkey = os.path.join(cfg.get("ipyno", "sshdir"), cfg.get('nova-admin', "vm_key")+'.pem')
pcfg = dict(cfg.items('proxy'))
try_ipydb = get_ipydb(request, False)
if try_ipydb['status'] != 200:
return return_json(None, try_ipydb['error'], try_ipydb['status'])
ipydb = try_ipydb['data']
proxy = Proxy(pcfg, pkey)
response = return_json(None, 'Method Not Allowed (%s): %s'%(request.method, request.url), 405)
# return list of server objs from proxy (nova-admin auth)
if request.method == 'GET':
try_nova = get_nova(request, 'nova-admin')
if try_nova['status'] != 200:
response = return_json(None, try_nova['error'], try_nova['status'])
elif not user:
response = return_json(None, "Bad Request: missing user", 400)
else:
data = proxy.get_server(user)
response = return_json(data['data']) if data['status'] == 200 else return_json(None, data['error'], data['status'])
# return server obj from proxy for user - if does not exist create it (add user to proxy and db)
# NOTE: this is the only API call that uses OAuth and is accessable by a normal user and not an admin
elif request.method == 'POST':
auth = check_auth(request.headers)
if auth['error']:
return return_json(None, auth['error'], 401)
res = ipydb.get('user', auth['username'])
# user already has a proxy
if res:
data = proxy.get_server(auth['username'])
return return_json(data['data']) if data['status'] == 200 else return_json(None, data['error'], data['status'])
# set proxy for user - get free port, add to db, add to nginx config
vm = ipydb.reserve()
port = ipydb.next_port(int(pcfg["pstart"]), int(pcfg["pend"]))
if vm and port:
vm = ipydb.update(vm['id'], user=auth['username'], port=port)
data = proxy.add_server(auth['username'], vm['ip'], port)
response = return_json(data['data']) if data['status'] == 200 else return_json(None, data['error'], data['status'])
else:
response = return_json(None, 'Service Unavailable: no free ipython servers available', 503)
# delete vm from proxy based on user, remove proxy/user info from db (nova auth)
elif request.method == 'DELETE':
try_nova = get_nova(request, 'nova-admin')
if try_nova['status'] != 200:
response = return_json(None, try_nova['error'], try_nova['status'])
elif not user:
response = return_json(None, "Bad Request: missing user", 400)
else:
vm = ipydb.get('user', user)
# user in DB and vaild auth - now we delete
if vm:
ipydb.drop_user(vm['id'])
res = proxy.remove_server(user)
# TODO: stop ipython on vm
response = return_json("user '%s' removed"%user) if res['status'] == 200 else return_json(None, res['error'], res['status'])
else:
response = return_json(None, "Bad Request: invalid user %s"%user, 400)
ipydb.exit()
return response