diff --git a/conf/container.conf b/conf/container.conf index e235da1..2acd587 100644 --- a/conf/container.conf +++ b/conf/container.conf @@ -44,6 +44,7 @@ lxc.cgroup.cpu.cfs_quota_us = %CONTAINER_CPU% lxc.mount.entry = %FS_PREFIX%/global/users/%USERNAME%/data %ROOTFS%/root/nfs none bind,rw,create=dir 0 0 lxc.mount.entry = %FS_PREFIX%/global/users/%USERNAME%/hosts/%CLUSTERID%.hosts %ROOTFS%/etc/hosts none bind,ro,create=file 0 0 lxc.mount.entry = %FS_PREFIX%/global/users/%USERNAME%/ssh %ROOTFS%/root/.ssh none bind,ro,create=dir 0 0 +lxc.mount.entry = %FS_PREFIX%/local/temp/%LXCNAME%/ %ROOTFS%/tmp none bind,rw,create=dir 0 0 # setting hostname lxc.hook.pre-start = HNAME=%HOSTNAME% %LXCSCRIPT%/lxc-prestart diff --git a/src/imagemgr.py b/src/imagemgr.py index b367279..9bc8312 100755 --- a/src/imagemgr.py +++ b/src/imagemgr.py @@ -25,9 +25,9 @@ import env from lvmtool import * class ImageMgr(): - def sys_call(self,command): - output = subprocess.getoutput(command).strip() - return None if output == '' else output + #def sys_call(self,command): + # output = subprocess.getoutput(command).strip() + # return None if output == '' else output def sys_return(self,command): return_value = subprocess.call(command,shell=True) @@ -66,9 +66,13 @@ class ImageMgr(): logger.info("this save operation is not force") if os.path.exists(imgpath+image): return [False,"target image is exists"] - self.sys_call("mkdir -p %s" % imgpath+image) - self.sys_call("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)) - self.sys_call("rm -f %s" % (imgpath+"."+image+"_docklet_share")) + 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) + except Exception as e: + logger.error(e) + self.updateinfo(imgpath,image,description) logger.info("image:%s from LXC:%s create success" % (image,lxc)) return [True, "create image success"] @@ -83,7 +87,11 @@ class ImageMgr(): imgpath = self.imgpath + "private/" + user + "/" else: imgpath = self.imgpath + "public/" + imageowner + "/" - self.sys_call("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))) + 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) + except Exception as e: + logger.error(e) + #self.sys_call("rsync -a --delete --exclude=nfs/ %s/ %s/" % (imgpath+image,self.dealpath(fspath))) #self.updatetime(imgpath,image) return @@ -100,8 +108,13 @@ class ImageMgr(): if Ret.returncode == 0: logger.info("%s not clean" % layer) sys_run("umount -l %s" % layer) - sys_run("rm -rf %s %s" % (rootfs, layer)) - sys_run("mkdir -p %s %s" % (rootfs, layer)) + + try: + sys_run("rm -rf %s %s" % (rootfs, layer)) + sys_run("mkdir -p %s %s" % (rootfs, layer)) + except Exception as e: + logger.error(e) + #prepare volume if check_volume(vgname,lxc): @@ -110,19 +123,18 @@ class ImageMgr(): if not new_volume(vgname,lxc,size): logger.error("volume %s create failed" % lxc) return False - sys_run("mkfs.ext4 /dev/%s/%s" % (vgname,lxc)) - sys_run("mount /dev/%s/%s %s" %(vgname,lxc,layer)) - #self.sys_call("mountpoint %s &>/dev/null && umount -l %s" % (rootfs,rootfs)) - #self.sys_call("mountpoint %s &>/dev/null && umount -l %s" % (layer,layer)) - #self.sys_call("rm -rf %s %s && mkdir -p %s %s" % (rootfs,layer,rootfs,layer)) - #rv = self.sys_return(self.srcpath+"lvmtool.sh check volume %s %s" % (vgname,lxc)) - #if rv == 1: - # self.sys_call(self.srcpath+"lvmtool.sh newvolume %s %s %s %s" % (vgname,lxc,size,layer)) - #else: - # self.sys_call(self.srcpath+"lvmtool.sh mount volume %s %s %s" % (vgname,lxc,layer)) - #self.sys_call("mkdir -p %s/overlay %s/work" % (layer,layer)) - #self.sys_call("mount -t overlay overlay -olowerdir=%s/local/basefs,upperdir=%s/overlay,workdir=%s/work %s" % (self.NFS_PREFIX,layer,layer,rootfs)) - self.sys_call("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh none %s/" % (layer,self.NFS_PREFIX,rootfs)) + + try: + sys_run("mkfs.ext4 /dev/%s/%s" % (vgname,lxc),True) + sys_run("mount /dev/%s/%s %s" %(vgname,lxc,layer),True) + #self.sys_call("mkdir -p %s/overlay %s/work" % (layer,layer)) + #self.sys_call("mount -t overlay overlay -olowerdir=%s/local/basefs,upperdir=%s/overlay,workdir=%s/work %s" % (self.NFS_PREFIX,layer,layer,rootfs)) + sys_run("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh none %s/" % (layer,self.NFS_PREFIX,rootfs),True) + sys_run("mkdir -p %s/local/temp/%s" % (self.NFS_PREFIX,lxc)) + + except Exception as e: + logger.error(e) + logger.info("FS has been prepared for user:%s lxc:%s" % (user,lxc)) #self.prepareImage(user,image,layer+"/overlay") self.prepareImage(user,image,layer) @@ -143,7 +155,12 @@ class ImageMgr(): sys_run("umount -l %s" % layer) if check_volume(vgname, lxc): delete_volume(vgname, lxc) - sys_run("rm -rf %s %s" % (layer,lxcpath)) + try: + sys_run("rm -rf %s %s" % (layer,lxcpath)) + sys_run("rm -rf %s/local/temp/%s" % (self.NFS_PREFIX,lxc)) + except Exception as e: + logger.error(e) + return True def checkFS(self, lxc, vgname="docklet-group"): @@ -157,15 +174,18 @@ class ImageMgr(): sys_run("mount /dev/%s/%s %s" % (vgname,lxc,layer)) Ret = sys_run("mountpoint %s" % rootfs) if Ret.returncode != 0: - self.sys_call("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh none %s/" % (layer,self.NFS_PREFIX,rootfs)) + sys_run("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh none %s/" % (layer,self.NFS_PREFIX,rootfs)) return True def removeImage(self,user,image): imgpath = self.imgpath + "private/" + user + "/" - self.sys_call("rm -rf %s/" % imgpath+image) - self.sys_call("rm -f %s" % imgpath+"."+image+".info") - self.sys_call("rm -f %s" % (imgpath+"."+image+".description")) + try: + sys_run("rm -rf %s/" % imgpath+image, True) + sys_run("rm -f %s" % imgpath+"."+image+".info", True) + sys_run("rm -f %s" % (imgpath+"."+image+".description"), True) + except Exception as e: + logger.error(e) def shareImage(self,user,image): imgpath = self.imgpath + "private/" + user + "/" @@ -177,10 +197,13 @@ class ImageMgr(): image_info_file = open(imgpath+"."+image+".info", 'w') image_info_file.writelines([createtime, isshare]) image_info_file.close() - self.sys_call("mkdir -p %s" % (share_imgpath + image)) - self.sys_call("rsync -a --delete %s/ %s/" % (imgpath+image,share_imgpath+image)) - self.sys_call("cp %s %s" % (imgpath+"."+image+".info",share_imgpath+"."+image+".info")) - self.sys_call("cp %s %s" % (imgpath+"."+image+".description",share_imgpath+"."+image+".description")) + 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) + except Exception as e: + logger.error(e) @@ -195,10 +218,12 @@ class ImageMgr(): image_info_file = open(imgpath+"."+image+".info", 'w') image_info_file.writelines([createtime, isshare]) image_info_file.close() - self.sys_call("rm -rf %s/" % public_imgpath+image) - self.sys_call("rm -f %s" % public_imgpath+"."+image+".info") - self.sys_call("rm -f %s" % public_imgpath+"."+image+".description") - + try: + sys_run("rm -rf %s/" % public_imgpath+image, 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: + logger.error(e) def get_image_info(self, user, image, imagetype): if imagetype == "private": @@ -230,9 +255,9 @@ class ImageMgr(): images["private"] = [] images["public"] = {} imgpath = self.imgpath + "private/" + user + "/" - private_images = self.sys_call("ls %s" % imgpath) - if private_images is not None and private_images[:3] != "ls:": - private_images = private_images.split("\n") + try: + Ret = sys_run("ls %s" % imgpath, True) + private_images = str(Ret.stdout,"utf-8").split() for image in private_images: fimage={} fimage["name"] = image @@ -241,17 +266,18 @@ class ImageMgr(): fimage["time"] = time fimage["description"] = description images["private"].append(fimage) - else: - pass + except Exception as e: + logger.error(e) + imgpath = self.imgpath + "public" + "/" - public_users = self.sys_call("ls %s" % imgpath) - if public_users is not None and public_users[:3] != "ls:": - public_users = public_users.split("\n") + try: + Ret = sys_run("ls %s" % imgpath, True) + public_users = str(Ret.stdout,"utf-8").split() for public_user in public_users: imgpath = self.imgpath + "public/" + public_user + "/" - public_images = self.sys_call("ls %s" % imgpath) - if public_images is not None and public_images[:3] != "ls:": - public_images = public_images.split("\n") + try: + Ret = sys_run("ls %s" % imgpath, True) + public_images = str(Ret.stdout,"utf-8").split() images["public"][public_user] = [] for image in public_images: fimage = {} @@ -260,8 +286,11 @@ class ImageMgr(): fimage["time"] = time fimage["description"] = description images["public"][public_user].append(fimage) - else: - pass + except Exception as e: + logger.error(e) + except Exception as e: + logger.error(e) + return images def isshared(self,user,image): diff --git a/src/lvmtool.py b/src/lvmtool.py index d385470..c030a2d 100755 --- a/src/lvmtool.py +++ b/src/lvmtool.py @@ -37,9 +37,12 @@ def new_group(group_name, size = "5000", file_path = "/opt/docklet/local/docklet os.remove(file_path) if not os.path.isdir(file_path[:file_path.rindex("/")]): os.makedirs(file_path[:file_path.rindex("/")]) - sys_run("dd if=/dev/zero of=%s bs=1M seek=%s count=0" % (file_path,size)) - sys_run("losetup /dev/loop0 " + file_path) - sys_run("vgcreate %s /dev/loop0" % group_name) + try: + sys_run("dd if=/dev/zero of=%s bs=1M seek=%s count=0" % (file_path,size)) + sys_run("losetup /dev/loop0 " + file_path) + sys_run("vgcreate %s /dev/loop0" % group_name) + except Exception as e: + logger.error(e) logger.info("initialize lvm group:%s with size %sM success" % (group_name,size)) return True @@ -55,7 +58,10 @@ def new_group(group_name, size = "5000", file_path = "/opt/docklet/local/docklet Ret = sys_run("vgremove -f " + group_name) if Ret.returncode != 0: logger.error("delete VG %s failed:%s" % (group_name,Ret.stdout.decode('utf-8'))) - sys_run("vgcreate %s %s" % (group_name,disk)) + try: + sys_run("vgcreate %s %s" % (group_name,disk)) + except Exception as e: + logger.error(e) logger.info("initialize lvm group:%s with size %sM success" % (group_name,size)) return True