From a680f9a0d32529d203037f8b3c30ddcc0b79b804 Mon Sep 17 00:00:00 2001 From: zhuyj17 Date: Sun, 30 Apr 2017 15:51:29 +0800 Subject: [PATCH] Let the system can change to non-distributed gateway smoothly. --- conf/nginx_docklet.conf | 7 ++----- src/container.py | 4 ++-- src/vclustermgr.py | 37 +++++++++++++++---------------------- tools/nginx_config.sh | 7 +++++++ 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/conf/nginx_docklet.conf b/conf/nginx_docklet.conf index aaf4cda..f71f95e 100644 --- a/conf/nginx_docklet.conf +++ b/conf/nginx_docklet.conf @@ -9,11 +9,8 @@ server access_log off; } - location ~ ^/(\d+\.\d+\.\d+\.\d+)/(.+)$ { - set $proxy_server $1; - set $other $2; - rewrite ^(.*)$ /$other break; - proxy_pass http://$1:%PROXY_PORT; + location ~ ^/(\d+\.\d+\.\d+\.\d+)/ { + proxy_pass http://%PROXY_SERVER_IP:%PROXY_PORT; proxy_set_header Host $host:$server_port; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; diff --git a/src/container.py b/src/container.py index ae766a9..ae99f61 100755 --- a/src/container.py +++ b/src/container.py @@ -23,7 +23,7 @@ class Container(object): self.imgmgr = imagemgr.ImageMgr() self.historymgr = History_Manager() - def create_container(self, lxc_name, username, setting, clustername, clusterid, containerid, hostname, ip, gateway, vlanid, image): + def create_container(self, lxc_name, proxy_server_ip, username, setting, clustername, clusterid, containerid, hostname, ip, gateway, vlanid, image): logger.info("create container %s of %s for %s" %(lxc_name, clustername, username)) try: setting = json.loads(setting) @@ -120,7 +120,7 @@ BASE_URL=%s HUB_PREFIX=%s HUB_API_URL=%s IP=%s -""" % (username, 10000, cookiename, '/go/'+username+'/'+clustername, '/jupyter', +""" % (username, 10000, cookiename, '/'+ proxy_server_ip +'/go/'+username+'/'+clustername, '/jupyter', authurl, ip.split('/')[0]) config.write(jconfigs) config.close() diff --git a/src/vclustermgr.py b/src/vclustermgr.py index d8c66e9..1fdcef5 100755 --- a/src/vclustermgr.py +++ b/src/vclustermgr.py @@ -191,11 +191,11 @@ class VclusterMgr(object): return [False, "proxy already exists"] target = "http://" + ip + ":" + port + "/" clusterinfo['proxy_ip'] = ip + ":" + port - if not clusterinfo['proxy_server_ip'] == self.addr: + if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(clusterinfo['proxy_server_ip']) - worker.set_route("/_web/" + username + "/" + clustername, target) + worker.set_route("/"+ clusterinfo['proxy_server_ip'] + "/_web/" + username + "/" + clustername, target) else: - proxytool.set_route("/_web/" + username + "/" + clustername, target) + proxytool.set_route("/" + clusterinfo['proxy_server_ip'] + "/_web/" + username + "/" + clustername, target) clusterfile = open(self.fspath + "/global/users/" + username + "/clusters/" + clustername, 'w') clusterfile.write(json.dumps(clusterinfo)) clusterfile.close() @@ -206,11 +206,11 @@ class VclusterMgr(object): if 'proxy_ip' not in clusterinfo: return [True, clusterinfo] clusterinfo.pop('proxy_ip') - if not clusterinfo['proxy_server_ip'] == self.addr: + if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(clusterinfo['proxy_server_ip']) - worker.delete_route("/_web/" + username + "/" + clustername) + worker.delete_route("/" + clusterinfo['proxy_server_ip'] + "/_web/" + username + "/" + clustername) else: - proxytool.delete_route("/_web/" + username + "/" + clustername) + proxytool.delete_route("/" + clusterinfo['proxy_server_ip'] + "/_web/" + username + "/" + clustername) clusterfile = open(self.fspath + "/global/users/" + username + "/clusters/" + clustername, 'w') clusterfile.write(json.dumps(clusterinfo)) clusterfile.close() @@ -365,16 +365,13 @@ class VclusterMgr(object): # so, check is necessary self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True') # set proxy - if (not info['proxy_server_ip'] == self.addr) and (self.distributedgw == 'False'): - info['proxy_server_ip'] = self.addr - info['proxy_url'] = env.getenv("PORTAL_URL") +"/"+ self.addr +"/_web/" + username + "/" + clustername try: target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000" - if not info['proxy_server_ip'] == self.addr: + if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip']) - worker.set_route('/go/'+username+'/'+clustername, target) + worker.set_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername, target) else: - proxytool.set_route('/go/'+username+'/'+clustername, target) + proxytool.set_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername, target) except: return [False, "start cluster failed with setting proxy failed"] for container in info['containers']: @@ -408,17 +405,13 @@ class VclusterMgr(object): # need to check and recover gateway of this user self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True') # recover proxy of cluster - if (not info['proxy_server_ip'] == self.addr) and (self.distributedgw == 'False'): - info['proxy_server_ip'] = self.addr - info['proxy_url'] = env.getenv("PORTAL_URL") +"/"+ self.addr +"/_web/" + username + "/" + clustername - self.write_clusterinfo(info,clustername,username) try: target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000" - if not info['proxy_server_ip'] == self.addr: + if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip']) - worker.set_route('/go/'+username+'/'+clustername, target) + worker.set_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername, target) else: - proxytool.set_route('/go/'+username+'/'+clustername, target) + proxytool.set_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername, target) except: return [False, "start cluster failed with setting proxy failed"] # recover containers of this cluster @@ -436,11 +429,11 @@ class VclusterMgr(object): return [False, "cluster not found"] if info['status'] == 'stopped': return [False, 'cluster is already stopped'] - if not info['proxy_server_ip'] == self.addr: + if self.distributedgw == 'True': worker = self.nodemgr.ip_to_rpc(info['proxy_server_ip']) - worker.delete_route('/go/'+username+'/'+clustername) + worker.delete_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername) else: - proxytool.delete_route('/go/'+username+'/'+clustername) + proxytool.delete_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername) for container in info['containers']: worker = self.nodemgr.ip_to_rpc(container['host']) if worker is None: diff --git a/tools/nginx_config.sh b/tools/nginx_config.sh index bbe26d6..aee2275 100755 --- a/tools/nginx_config.sh +++ b/tools/nginx_config.sh @@ -15,6 +15,13 @@ echo "copy nginx_docklet.conf to nginx config path..." cp ../conf/nginx_docklet.conf ${NGINX_CONF}/ sed -i "s/%MASTER_IP/${MASTER_IP}/g" ${NGINX_CONF}/nginx_docklet.conf sed -i "s/%NGINX_PORT/${NGINX_PORT}/g" ${NGINX_CONF}/nginx_docklet.conf +if [ "${DISTRIBUTED_GATEWAY}" = "True" ] +then + sed -i "s/%PROXY_SERVER_IP/\$1/g" ${NGINX_CONF}/nginx_docklet.conf +else + sed -i "s/%PROXY_SERVER_IP/${MASTER_IP}/g" ${NGINX_CONF}/nginx_docklet.conf +fi + sed -i "s/%PROXY_PORT/${PROXY_PORT}/g" ${NGINX_CONF}/nginx_docklet.conf sed -i "s/%WEB_PORT/${WEB_PORT}/g" ${NGINX_CONF}/nginx_docklet.conf