change the image from directory to an archive

This commit is contained in:
zhongyehong 2016-09-23 17:11:37 +08:00
parent 86db4c8aa8
commit e1f9c24b15
2 changed files with 70 additions and 19 deletions

View File

@ -18,7 +18,7 @@ design:
from configparser import ConfigParser
from io import StringIO
import os,sys,subprocess,time,re,datetime,threading
import os,sys,subprocess,time,re,datetime,threading,random
from log import logger
import env
@ -63,6 +63,8 @@ class ImageMgr():
def createImage(self,user,image,lxc,description="Not thing", imagenum=10):
fspath = self.NFS_PREFIX + "/local/volume/" + lxc
imgpath = self.imgpath + "private/" + user + "/"
tmppath = self.NFS_PREFIX + "/local/tmpimg/"
tmpimage = str(random.randint(0,10000000)) + ".tgz"
if not os.path.exists(imgpath+image) and os.path.exists(imgpath):
cur_imagenum = 0
@ -71,13 +73,19 @@ class ImageMgr():
cur_imagenum += 1
if cur_imagenum >= int(imagenum):
return [False,"image number limit exceeded"]
sys_run("mkdir -p %s" % tmppath, True)
sys_run("mkdir -p %s" % imgpath,True)
try:
sys_run("mkdir -p %s" % imgpath+image,True)
sys_run("rsync -a --delete --exclude=lost+found/ --exclude=root/nfs/ --exclude=dev/ --exclude=mnt/ --exclude=tmp/ --exclude=media/ --exclude=proc/ --exclude=sys/ %s/ %s/" % (self.dealpath(fspath),imgpath+image),True)
sys_run("rm -f %s" % (imgpath+"."+image+"_docklet_share"),True)
sys_run("tar -zcvf %s -C %s ." % (tmppath+tmpimage,self.dealpath(fspath)), True)
except Exception as e:
logger.error(e)
try:
sys_run("cp %s %s" % (tmppath+tmpimage, imgpath+image+".tgz"), True)
#sys_run("rsync -a --delete --exclude=lost+found/ --exclude=root/nfs/ --exclude=dev/ --exclude=mnt/ --exclude=tmp/ --exclude=media/ --exclude=proc/ --exclude=sys/ %s/ %s/" % (self.dealpath(fspath),imgpath+image),True)
except Exception as e:
logger.error(e)
sys_run("rm -f %s" % tmppath+tmpimage, True)
#sys_run("rm -f %s" % (imgpath+"."+image+"_docklet_share"),True)
self.updateinfo(imgpath,image,description)
logger.info("image:%s from LXC:%s create success" % (image,lxc))
return [True, "create image success"]
@ -86,6 +94,8 @@ class ImageMgr():
imagename = image['name']
imagetype = image['type']
imageowner = image['owner']
tmppath = self.NFS_PREFIX + "/local/tmpimg/"
tmpimage = str(random.randint(0,10000000)) + ".tgz"
if imagename == "base" and imagetype == "base":
return
if imagetype == "private":
@ -93,9 +103,15 @@ class ImageMgr():
else:
imgpath = self.imgpath + "public/" + imageowner + "/"
try:
sys_run("rsync -a --delete --exclude=lost+found/ --exclude=root/nfs/ --exclude=dev/ --exclude=mnt/ --exclude=tmp/ --exclude=media/ --exclude=proc/ --exclude=sys/ %s/ %s/" % (imgpath+imagename,self.dealpath(fspath)),True)
sys_run("cp %s %s" % (imgpath+imagename+".tgz", tmppath+tmpimage))
except Exception as e:
logger.error(e)
try:
sys_run("tar -C %s -zxvf %s" % (self.dealpath(fspath),tmppath+tmpimage), True)
#sys_run("rsync -a --delete --exclude=lost+found/ --exclude=root/nfs/ --exclude=dev/ --exclude=mnt/ --exclude=tmp/ --exclude=media/ --exclude=proc/ --exclude=sys/ %s/ %s/" % (imgpath+imagename,self.dealpath(fspath)),True)
except Exception as e:
logger.error(e)
sys_run("rm -f %s" % tmppath+tmpimage)
#self.sys_call("rsync -a --delete --exclude=nfs/ %s/ %s/" % (imgpath+image,self.dealpath(fspath)))
#self.updatetime(imgpath,image)
@ -194,7 +210,7 @@ class ImageMgr():
def removeImage(self,user,image):
imgpath = self.imgpath + "private/" + user + "/"
try:
sys_run("rm -rf %s/" % imgpath+image, True)
sys_run("rm -rf %s/" % imgpath+image+".tgz", True)
sys_run("rm -f %s" % imgpath+"."+image+".info", True)
sys_run("rm -f %s" % (imgpath+"."+image+".description"), True)
except Exception as e:
@ -210,20 +226,21 @@ class ImageMgr():
image_info_file = open(imgpath+"."+image+".info", 'w')
image_info_file.writelines([createtime, isshare])
image_info_file.close()
sys_run("mkdir -p %s" % share_imgpath, True)
try:
sys_run("mkdir -p %s" % (share_imgpath + image), True)
sys_run("rsync -a --delete %s/ %s/" % (imgpath+image,share_imgpath+image), True)
sys_run("cp %s %s" % (imgpath+"."+image+".info",share_imgpath+"."+image+".info"), True)
sys_run("cp %s %s" % (imgpath+"."+image+".description",share_imgpath+"."+image+".description"), True)
sys_run("cp %s %s" % (imgpath+image+".tgz", share_imgpath+image+".tgz"), True)
#sys_run("rsync -a --delete %s/ %s/" % (imgpath+image,share_imgpath+image), True)
except Exception as e:
logger.error(e)
sys_run("cp %s %s" % (imgpath+"."+image+".info",share_imgpath+"."+image+".info"), True)
sys_run("cp %s %s" % (imgpath+"."+image+".description",share_imgpath+"."+image+".description"), True)
def unshareImage(self,user,image):
public_imgpath = self.imgpath + "public/" + user + "/"
imgpath = self.imgpath + "private/" + user + "/"
if os.path.exists(imgpath + image):
if os.path.isfile(imgpath + image + ".tgz"):
image_info_file = open(imgpath+"."+image+".info", 'r')
[createtime, isshare] = image_info_file.readlines()
isshare = "unshare"
@ -232,7 +249,8 @@ class ImageMgr():
image_info_file.writelines([createtime, isshare])
image_info_file.close()
try:
sys_run("rm -rf %s/" % public_imgpath+image, True)
#sys_run("rm -rf %s/" % public_imgpath+image, True)
sys_run("rm -f %s" % public_imgpath+image+".tgz", True)
sys_run("rm -f %s" % public_imgpath+"."+image+".info", True)
sys_run("rm -f %s" % public_imgpath+"."+image+".description", True)
except Exception as e:
@ -333,10 +351,11 @@ class ImageMgr():
Ret = sys_run("ls %s" % imgpath, True)
private_images = str(Ret.stdout,"utf-8").split()
for image in private_images:
imagename = image[:-4]
fimage={}
fimage["name"] = image
fimage["isshared"] = self.isshared(user,image)
[time, description] = self.get_image_info(user, image, "private")
fimage["name"] = imagename
fimage["isshared"] = self.isshared(user,imagename)
[time, description] = self.get_image_info(user, imagename, "private")
fimage["time"] = time
fimage["description"] = description
images["private"].append(fimage)
@ -354,9 +373,10 @@ class ImageMgr():
public_images = str(Ret.stdout,"utf-8").split()
images["public"][public_user] = []
for image in public_images:
imagename = image[:-4]
fimage = {}
fimage["name"] = image
[time, description] = self.get_image_info(public_user, image, "public")
fimage["name"] = imagename
[time, description] = self.get_image_info(public_user, imagename, "public")
fimage["time"] = time
fimage["description"] = description
images["public"][public_user].append(fimage)

View File

@ -3,6 +3,7 @@
import os, json, sys
sys.path.append("../src/")
from model import db, User
from lvmtool import sys_run
fspath="/opt/docklet"
def update_quotainfo():
@ -149,8 +150,38 @@ def enable_gluster_quota():
nfspath = "/users/%s/data" % user.username
sys_run("gluster volume quota %s limit-usage %s %sGB" % (volume_name,nfspath,nfs_quota))
def update_image():
private_imagepath = fspath + "/global/images/private/"
public_imagepath = fspath + "/global/images/public/"
userdirs = os.listdir(private_imagepath)
for userdir in userdirs:
if os.path.isdir(private_imagepath+userdir+"/"):
currentdir = private_imagepath+userdir+"/"
images = os.listdir(currentdir)
for image in images:
if os.path.isdir(currentdir+image+"/"):
try:
sys_run("tar -zcvf %s -C %s ." % (currentdir+image+".tgz",currentdir+image))
sys_run("rm -rf %s" % currentdir+image)
except Exception as e:
print(e)
userdirs = os.listdir(public_imagepath)
for userdir in userdirs:
if os.path.isdir(userdir):
currentdir = public_imagepath+userdir+"/"
images = os.listdir(currentdir)
for image in images:
if os.path.isdir(currentdir+image+"/"):
try:
sys_run("tar -zcvf %s -C %s ." % (currentdir+image+".tgz",currentdir+image))
sys_run("rm -rf %s" % currentdir+image)
except Exception as e:
print(e)
if __name__ == '__main__':
update_quotainfo()
# update_quotainfo()
if "fix-name-error" in sys.argv:
name_error()
# enable_gluster_quota()
if "update-image" in sys.argv:
update_image()