add migrate container in vclustermgr

This commit is contained in:
Firmlyzhu 2019-04-20 18:18:00 +08:00
parent 0deaa377c6
commit 7736f219db
1 changed files with 53 additions and 0 deletions

View File

@ -728,6 +728,59 @@ class VclusterMgr(object):
full_clusters.append(single_cluster)''' full_clusters.append(single_cluster)'''
return [True, clusters] return [True, clusters]
def migrate_container(self, clustername, username, containername, new_host, proxy_public_ip, user_info):
[status, info] = self.get_clusterinfo(clustername, username)
if not status:
return [False, "cluster not found"]
if info['status'] != 'stopped':
return [False, 'cluster is not stopped']
con_db = Container.query.get(containername)
if con_db is None:
return [False, 'Container not found']
if host == new_host:
return [False, 'Container has been on the new host']
oldworker = self.nodemgr.ip_to_rpc(con_db.host)
if oldworker is None:
return [False, "Old host worker can't be found or has been stopped."]
oldworker.stop_container(containername)
imagename = "migrate-" + containername + "-" + datetime.datetime.now().strftime("%Y-%m-%d")
status,msg = oldworker.create_image(username,imagename,containername,"",10000)
if not status:
return [False, msg]
#con_db.lastsave = datetime.datetime.now()
#con_db.image = imagename
'''self.networkmgr.load_usrgw(username)
proxy_server_ip = self.networkmgr.usrgws[username]
[status, proxy_public_ip] = self.etcd.getkey("machines/publicIP/"+proxy_server_ip)
if not status:
logger.error("Fail to get proxy_public_ip %s."%(proxy_server_ip))
return [False, "Fail to get proxy server public IP."]'''
uid = json.loads(user_info)["data"]["id"]
setting = {
'cpu': con_db.setting_cpu,
'memory': con_db.setting_mem,
'disk': con_db.setting_disk
}
_, clusterid, cid = containername.split('-')
hostname = "host-"+str(cid)
gateway = self.networkmgr.get_usergw(username)
image = {'name':imagename,'type':'private','owner':username }
worker = self.nodemgr.ip_to_rpc(new_host)
if worker is None:
self.imagemgr.removeImage(username,imagename)
return [False, "New host worker can't be found or has been stopped."]
status,msg = worker.create_container(containername, proxy_public_ip, username, uid, json.dumps(setting),
clustername, str(clusterid), str(cid), hostname, con_db.ip, gateway, json.dumps(image))
if not status:
return [False, msg]
oldworker.delete_container(containername)
self.imagemgr.removeImage(username,imagename)
return [True,""]
def is_cluster(self, clustername, username): def is_cluster(self, clustername, username):
[status, clusters] = self.list_clusters(username) [status, clusters] = self.list_clusters(username)
if clustername in clusters: if clustername in clusters: