Let the system can change to non-distributed gateway smoothly.

This commit is contained in:
zhuyj17 2017-04-30 15:51:29 +08:00
parent 38854367ed
commit a680f9a0d3
4 changed files with 26 additions and 29 deletions

View File

@ -9,11 +9,8 @@ server
access_log off; access_log off;
} }
location ~ ^/(\d+\.\d+\.\d+\.\d+)/(.+)$ { location ~ ^/(\d+\.\d+\.\d+\.\d+)/ {
set $proxy_server $1; proxy_pass http://%PROXY_SERVER_IP:%PROXY_PORT;
set $other $2;
rewrite ^(.*)$ /$other break;
proxy_pass http://$1:%PROXY_PORT;
proxy_set_header Host $host:$server_port; proxy_set_header Host $host:$server_port;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;

View File

@ -23,7 +23,7 @@ class Container(object):
self.imgmgr = imagemgr.ImageMgr() self.imgmgr = imagemgr.ImageMgr()
self.historymgr = History_Manager() 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)) logger.info("create container %s of %s for %s" %(lxc_name, clustername, username))
try: try:
setting = json.loads(setting) setting = json.loads(setting)
@ -120,7 +120,7 @@ BASE_URL=%s
HUB_PREFIX=%s HUB_PREFIX=%s
HUB_API_URL=%s HUB_API_URL=%s
IP=%s IP=%s
""" % (username, 10000, cookiename, '/go/'+username+'/'+clustername, '/jupyter', """ % (username, 10000, cookiename, '/'+ proxy_server_ip +'/go/'+username+'/'+clustername, '/jupyter',
authurl, ip.split('/')[0]) authurl, ip.split('/')[0])
config.write(jconfigs) config.write(jconfigs)
config.close() config.close()

View File

@ -191,11 +191,11 @@ class VclusterMgr(object):
return [False, "proxy already exists"] return [False, "proxy already exists"]
target = "http://" + ip + ":" + port + "/" target = "http://" + ip + ":" + port + "/"
clusterinfo['proxy_ip'] = 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 = 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: 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 = open(self.fspath + "/global/users/" + username + "/clusters/" + clustername, 'w')
clusterfile.write(json.dumps(clusterinfo)) clusterfile.write(json.dumps(clusterinfo))
clusterfile.close() clusterfile.close()
@ -206,11 +206,11 @@ class VclusterMgr(object):
if 'proxy_ip' not in clusterinfo: if 'proxy_ip' not in clusterinfo:
return [True, clusterinfo] return [True, clusterinfo]
clusterinfo.pop('proxy_ip') 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 = 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: 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 = open(self.fspath + "/global/users/" + username + "/clusters/" + clustername, 'w')
clusterfile.write(json.dumps(clusterinfo)) clusterfile.write(json.dumps(clusterinfo))
clusterfile.close() clusterfile.close()
@ -365,16 +365,13 @@ class VclusterMgr(object):
# so, check is necessary # so, check is necessary
self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True') self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True')
# set proxy # 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: try:
target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000" 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 = 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: else:
proxytool.set_route('/go/'+username+'/'+clustername, target) proxytool.set_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername, target)
except: except:
return [False, "start cluster failed with setting proxy failed"] return [False, "start cluster failed with setting proxy failed"]
for container in info['containers']: for container in info['containers']:
@ -408,17 +405,13 @@ class VclusterMgr(object):
# need to check and recover gateway of this user # need to check and recover gateway of this user
self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True') self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True')
# recover proxy of cluster # 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: try:
target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000" 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 = 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: else:
proxytool.set_route('/go/'+username+'/'+clustername, target) proxytool.set_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername, target)
except: except:
return [False, "start cluster failed with setting proxy failed"] return [False, "start cluster failed with setting proxy failed"]
# recover containers of this cluster # recover containers of this cluster
@ -436,11 +429,11 @@ class VclusterMgr(object):
return [False, "cluster not found"] return [False, "cluster not found"]
if info['status'] == 'stopped': if info['status'] == 'stopped':
return [False, 'cluster is already 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 = 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: else:
proxytool.delete_route('/go/'+username+'/'+clustername) proxytool.delete_route("/" + info['proxy_server_ip'] + '/go/'+username+'/'+clustername)
for container in info['containers']: for container in info['containers']:
worker = self.nodemgr.ip_to_rpc(container['host']) worker = self.nodemgr.ip_to_rpc(container['host'])
if worker is None: if worker is None:

View File

@ -15,6 +15,13 @@ echo "copy nginx_docklet.conf to nginx config path..."
cp ../conf/nginx_docklet.conf ${NGINX_CONF}/ cp ../conf/nginx_docklet.conf ${NGINX_CONF}/
sed -i "s/%MASTER_IP/${MASTER_IP}/g" ${NGINX_CONF}/nginx_docklet.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 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/%PROXY_PORT/${PROXY_PORT}/g" ${NGINX_CONF}/nginx_docklet.conf
sed -i "s/%WEB_PORT/${WEB_PORT}/g" ${NGINX_CONF}/nginx_docklet.conf sed -i "s/%WEB_PORT/${WEB_PORT}/g" ${NGINX_CONF}/nginx_docklet.conf