Set proxy route by rpc.

This commit is contained in:
zhuyj17 2017-03-19 16:02:53 +08:00
parent 611b440131
commit afaaf5e86f
4 changed files with 38 additions and 24 deletions

View File

@ -111,6 +111,9 @@ class VclusterMgr(object):
clusterpath = self.fspath+"/global/users/"+username+"/clusters/"+clustername
hostpath = self.fspath+"/global/users/"+username+"/hosts/"+str(clusterid)+".hosts"
hosts = "127.0.0.1\tlocalhost\n"
proxy_url = ""
proxy_server_ip = ""
proxy_server_port = ""
containers = []
for i in range(0, clustersize):
onework = workers[random.randint(0, len(workers)-1)]
@ -118,6 +121,10 @@ class VclusterMgr(object):
[success,message] = self.networkmgr.setup_usrgw(username, self.nodemgr, onework)
if not success:
return [False, message]
if i == 0:
proxy_url = "http://" + self.nodemgr.rpc_to_ip(onework) + ":" + str(env.getenv("PROXY_PORT")) + "/_web/" + username + "/" + clustername
proxy_server_ip = self.nodemgr.rpc_to_ip(onework)
proxy_server_port = env.getenv("PROXY_PORT")
lxc_name = username + "-" + str(clusterid) + "-" + str(i)
hostname = "host-"+str(i)
logger.info ("create container with : name-%s, username-%s, clustername-%s, clusterid-%s, hostname-%s, ip-%s, gateway-%s, image-%s" % (lxc_name, username, clustername, str(clusterid), hostname, ips[i], gateway, image_json))
@ -132,8 +139,8 @@ class VclusterMgr(object):
hostfile.write(hosts)
hostfile.close()
clusterfile = open(clusterpath, 'w')
proxy_url = env.getenv("PORTAL_URL") + "/_web/" + username + "/" + clustername
info = {'clusterid':clusterid, 'status':'stopped', 'size':clustersize, 'containers':containers, 'nextcid': clustersize, 'create_time':datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'start_time':"------" , 'proxy_url':proxy_url}
info = {'clusterid':clusterid, 'status':'stopped', 'size':clustersize, 'containers':containers, 'nextcid': clustersize, 'create_time':datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'start_time':"------" , 'proxy_url':proxy_url, 'proxy_server_ip':proxy_server_ip}
info['proxy_server_port'] = proxy_server_port
clusterfile.write(json.dumps(info))
clusterfile.close()
return [True, info]
@ -186,10 +193,11 @@ class VclusterMgr(object):
return [False, "proxy already exists"]
target = "http://" + ip + ":" + port
clusterinfo['proxy_ip'] = ip + ":" + port
worker = self.nodemgr.ip_to_rpc(clusterinfo['proxy_server_ip'])
clusterfile = open(self.fspath + "/global/users/" + username + "/clusters/" + clustername, 'w')
clusterfile.write(json.dumps(clusterinfo))
clusterfile.close()
proxytool.set_route("/_web/" + username + "/" + clustername, target)
worker.set_route("/_web/" + username + "/" + clustername, target)
return [True, clusterinfo]
def deleteproxy(self, username, clustername):
@ -197,10 +205,11 @@ class VclusterMgr(object):
if 'proxy_ip' not in clusterinfo:
return [True, clusterinfo]
clusterinfo.pop('proxy_ip')
worker = self.nodemgr.ip_to_rpc(clusterinfo['proxy_server_ip'])
clusterfile = open(self.fspath + "/global/users/" + username + "/clusters/" + clustername, 'w')
clusterfile.write(json.dumps(clusterinfo))
clusterfile.close()
proxytool.delete_route("/_web/" + username + "/" + clustername)
worker.delete_route("/_web/" + username + "/" + clustername)
return [True, clusterinfo]
def flush_cluster(self,username,clustername,containername):
@ -348,7 +357,8 @@ class VclusterMgr(object):
# set proxy
try:
target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000"
proxytool.set_route('/go/'+username+'/'+clustername, target)
worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip'])
worker.set_route('/go/'+username+'/'+clustername, target)
except:
return [False, "start cluster failed with setting proxy failed"]
for container in info['containers']:
@ -382,7 +392,8 @@ class VclusterMgr(object):
# recover proxy of cluster
try:
target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000"
proxytool.set_route('/go/'+username+'/'+clustername, target)
worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip'])
worker.set_route('/go/'+username+'/'+clustername, target)
except:
return [False, "start cluster failed with setting proxy failed"]
# recover containers of this cluster

View File

@ -15,7 +15,7 @@ from socketserver import ThreadingMixIn
import threading
import etcdlib, network, container
from nettools import netcontrol
import monitor
import monitor, proxytool
from lvmtool import new_group, recover_group
##################################################################
@ -128,6 +128,8 @@ class Worker(object):
self.rpcserver.register_function(netcontrol.setup_gw)
self.rpcserver.register_function(netcontrol.del_gw)
self.rpcserver.register_function(netcontrol.check_gw)
self.rpcserver.register_function(proxytool.set_route)
self.rpcserver.register_function(proxytool.delete_route)
# register functions or instances to server for rpc
#self.rpcserver.register_function(function_name)

View File

@ -53,7 +53,7 @@
<button type="button" class="btn btn-xs btn-default"> Delete </button>
</td>
<td>
<a href="/go/{{ mysession['username'] }}/{{ cluster['name'] }}" target="_blank"><button type="button" class="btn btn-xs btn-success">&nbsp;&nbsp;&nbsp;Go&nbsp;&nbsp;&nbsp;</button></a>
<a href="{{ cluster['proxy_server_url'] }}/go/{{ mysession['username'] }}/{{ cluster['name'] }}" target="_blank"><button type="button" class="btn btn-xs btn-success">&nbsp;&nbsp;&nbsp;Go&nbsp;&nbsp;&nbsp;</button></a>
</td>
{% else %}
<td><a href="/vclusters/"><div class="text-warning"><i class="fa fa-stop "></i> Stopped</div></a></td>

View File

@ -24,6 +24,7 @@ class dashboardView(normalView):
message = message.get("message")
single_cluster['status'] = message['status']
single_cluster['id'] = message['clusterid']
single_cluster['proxy_server_url'] = "http://" + message['proxy_server_ip'] + ":" + str(message['proxy_server_port'])
full_clusters.append(single_cluster)
else:
self.error()