From c78bf1c0c2b1a07372ae68a4b2811ea2abab408a Mon Sep 17 00:00:00 2001 From: Firmlyzhu Date: Mon, 29 May 2017 16:45:35 +0800 Subject: [PATCH] Add public IP --- conf/docklet.conf.template | 2 +- src/container.py | 2 +- src/env.py | 2 +- src/vclustermgr.py | 54 +++++++++++++++++++++--------------- web/templates/dashboard.html | 2 +- web/webViews/dashboard.py | 2 +- 6 files changed, 37 insertions(+), 27 deletions(-) diff --git a/conf/docklet.conf.template b/conf/docklet.conf.template index 64757d2..74873f8 100644 --- a/conf/docklet.conf.template +++ b/conf/docklet.conf.template @@ -148,7 +148,7 @@ # PUBLIC_IP : publick ip of this machine. If DISTRIBUTED_GATEWAY is True, # users' gateways can be setup on this machine. Users can visit this machine # by the public ip. default: IP of NETWORK_DEVICE. -# PUBLIC_IP="" +# PUBLIC_IP=0.0.0.0 # NGINX_CONF: the config path of nginx, default: /etc/nginx # NGINX_CONF="/etc/nginx" diff --git a/src/container.py b/src/container.py index c73a7c7..d297da0 100755 --- a/src/container.py +++ b/src/container.py @@ -249,7 +249,7 @@ IP=%s config = open(jconfigpath, 'r') context = config.read() config.close() - context = context.replace(old_ip, new_ip) + context = context.replace(old_ip+"/go", new_ip+"/go") config = open(jconfigpath, 'w') config.write(context) config.close() diff --git a/src/env.py b/src/env.py index f2e8293..2189744 100755 --- a/src/env.py +++ b/src/env.py @@ -62,7 +62,7 @@ def getenv(key): if 2 in addr: return os.environ.get("PUBLIC_IP",addr[2][0]['addr']) else: - return os.environ.get("PUBLIC_IP"."0.0.0.0") + return os.environ.get("PUBLIC_IP","0.0.0.0") elif key == "NGINX_CONF": return os.environ.get("NGINX_CONF","/etc/nginx") elif key =="USER_IP": diff --git a/src/vclustermgr.py b/src/vclustermgr.py index 7b26c93..0b387dd 100755 --- a/src/vclustermgr.py +++ b/src/vclustermgr.py @@ -8,6 +8,7 @@ from log import logger import env import proxytool import requests +import traceback userpoint = "http://" + env.getenv('USER_IP') + ":" + str(env.getenv('USER_PORT')) def post_to_user(url = '/', data={}): @@ -420,16 +421,24 @@ class VclusterMgr(object): worker.update_baseurl(container['containername'],oldip,newip) worker.stop_container(container['containername']) + def check_public_ip(self, clustername, username): + [status, info] = self.get_clusterinfo(clustername, username) + [status, proxy_public_ip] = self.etcd.getkey("machines/publicIP/"+info['proxy_server_ip']) + if not info['proxy_public_ip'] == proxy_public_ip: + logger.info("%s %s proxy_public_ip has been changed, base_url need to be modified."%(username,clustername)) + oldpublicIP= info['proxy_public_ip'] + self.update_proxy_ipAndurl(clustername,username,info['proxy_server_ip']) + self.update_cluster_baseurl(clustername,username,oldpublicIP,proxy_public_ip) + return False + else: + return True + def start_cluster(self, clustername, username, uid): [status, info] = self.get_clusterinfo(clustername, username) if not status: return [False, "cluster not found"] if info['status'] == 'running': return [False, "cluster is already running"] - # check gateway for user - # after reboot, user gateway goes down and lose its configuration - # so, check is necessary - self.networkmgr.check_usergw(username, uid, self.nodemgr,self.distributedgw=='True') # set proxy if not "proxy_server_ip" in info.keys(): info['proxy_server_ip'] = self.addr @@ -437,6 +446,9 @@ class VclusterMgr(object): target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000" if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip']) + # check public ip + if not self.check_public_ip(clustername,username): + [status, info] = self.get_clusterinfo(clustername, username) worker.set_route("/" + info['proxy_public_ip'] + '/go/'+username+'/'+clustername, target) else: if not info['proxy_server_ip'] == self.addr: @@ -445,17 +457,17 @@ class VclusterMgr(object): self.update_proxy_ipAndurl(clustername,username,self.addr) [status, info] = self.get_clusterinfo(clustername, username) self.update_cluster_baseurl(clustername,username,oldpublicIP,info['proxy_public_ip']) + # check public ip + if not self.check_public_ip(clustername,username): + [status, info] = self.get_clusterinfo(clustername, username) proxytool.set_route("/" + info['proxy_public_ip'] + '/go/'+username+'/'+clustername, target) except: + logger.info(traceback.format_exc()) return [False, "start cluster failed with setting proxy failed"] - # check public ip - [status, proxy_public_ip] = self.etcd.getkey("machines/publicIP/"+info['proxy_server_ip']) - if not info['proxy_public_ip'] == proxy_public_ip: - logger.info("%s %s proxy_public_ip has been changed, base_url need to be modified."%(username,clustername)) - oldpublicIP= info['proxy_public_ip'] - self.update_proxy_ipAndurl(clustername,username,info['proxy_server_ip']) - [status, info] = self.get_clusterinfo(clustername, username) - self.update_cluster_baseurl(clustername,username,oldpublicIP,info['proxy_public_ip']) + # check gateway for user + # after reboot, user gateway goes down and lose its configuration + # so, check is necessary + self.networkmgr.check_usergw(username, uid, self.nodemgr,self.distributedgw=='True') # start containers for container in info['containers']: # set up gre from user's gateway host to container's host. @@ -498,13 +510,14 @@ class VclusterMgr(object): self.update_cluster_baseurl(clustername,username,info['proxy_server_ip'],info['proxy_public_ip']) if info['status'] == 'stopped': return [True, "cluster no need to start"] - # need to check and recover gateway of this user - self.networkmgr.check_usergw(username, uid, self.nodemgr,self.distributedgw=='True') # recover proxy of cluster try: target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000" if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip']) + # check public ip + if not self.check_public_ip(clustername,username): + [status, info] = self.get_clusterinfo(clustername, username) worker.set_route("/" + info['proxy_public_ip'] + '/go/'+username+'/'+clustername, target) else: if not info['proxy_server_ip'] == self.addr: @@ -513,17 +526,14 @@ class VclusterMgr(object): self.update_proxy_ipANdurl(clustername,username,self.addr) [status, info] = self.get_clusterinfo(clustername, username) self.update_cluster_baseurl(clustername,username,oldpublicIP,info['proxy_public_ip']) + # check public ip + if not self.check_public_ip(clustername,username): + [status, info] = self.get_clusterinfo(clustername, username) proxytool.set_route("/" + info['proxy_public_ip'] + '/go/'+username+'/'+clustername, target) except: return [False, "start cluster failed with setting proxy failed"] - # check public ip - [status, proxy_public_ip] = self.etcd.getkey("machines/publicIP/"+info['proxy_server_ip']) - if not info['proxy_public_ip'] == proxy_public_ip: - logger.info("%s %s proxy_public_ip has been changed, base_url need to be modified."%(username,clustername)) - oldpublicIP= info['proxy_public_ip'] - self.update_proxy_ipAndurl(clustername,username,info['proxy_server_ip']) - [status, info] = self.get_clusterinfo(clustername, username) - self.update_cluster_baseurl(clustername,username,oldpublicIP,info['proxy_public_ip']) + # need to check and recover gateway of this user + self.networkmgr.check_usergw(username, uid, self.nodemgr,self.distributedgw=='True') # recover containers of this cluster for container in info['containers']: # set up gre from user's gateway host to container's host. diff --git a/web/templates/dashboard.html b/web/templates/dashboard.html index 0569769..6da526f 100755 --- a/web/templates/dashboard.html +++ b/web/templates/dashboard.html @@ -55,7 +55,7 @@ - + {% else %}
Stopped
diff --git a/web/webViews/dashboard.py b/web/webViews/dashboard.py index 582b716..d0a83ab 100644 --- a/web/webViews/dashboard.py +++ b/web/webViews/dashboard.py @@ -24,7 +24,7 @@ class dashboardView(normalView): message = message.get("message") single_cluster['status'] = message['status'] single_cluster['id'] = message['clusterid'] - single_cluster['proxy_server_ip'] = message['proxy_server_ip'] + single_cluster['proxy_public_ip'] = message['proxy_public_ip'] full_clusters.append(single_cluster) else: self.error()