From 52d1e8fb826a2902b9c2d90fd2759b5d92174a00 Mon Sep 17 00:00:00 2001 From: zhongyehong Date: Sat, 6 Jan 2018 14:05:12 +0800 Subject: [PATCH] fix a bug of update base image prepare base image in prepare.sh add a packagefs layer --- bin/docklet-worker | 8 +++++--- prepare.sh | 23 +++++++++++---------- src/imagemgr.py | 40 +++---------------------------------- src/network.py | 5 ++++- web/templates/settings.html | 8 ++++---- 5 files changed, 28 insertions(+), 56 deletions(-) diff --git a/bin/docklet-worker b/bin/docklet-worker index fdf5491..5c2b887 100755 --- a/bin/docklet-worker +++ b/bin/docklet-worker @@ -85,9 +85,11 @@ pre_start () { iptables -t nat -A POSTROUTING -s $CLUSTER_NET -j MASQUERADE if [ ! -d $FS_PREFIX/local/basefs ]; then - log_daemon_msg "create basefs ..." - [ ! -f $tempdir/basefs.tar.bz ] && log_daemon_msg "$tempdir/basefs.tar.bz not exist, run prepare.sh first" && exit 1 - tar xvf $tempdir/basefs.tar.bz -C $FS_PREFIX/local > /dev/null + log_daemon_msg "basefs does not exist, run prepare.sh first" && exit 1 + fi + + if [ ! -d $FS_PREFIX/local/packagefs ]; then + mkdir -p $FS_PREFIX/local/packagefs fi } diff --git a/prepare.sh b/prepare.sh index df81a91..bcddb85 100755 --- a/prepare.sh +++ b/prepare.sh @@ -51,9 +51,20 @@ echo "" [[ -f web/templates/home.html ]] || { echo "Generating HomePage from home.template" && cp web/templates/home.template web/templates/home.html; } mkdir -p /opt/docklet/global -mkdir -p /opt/docklet/local +mkdir -p /opt/docklet/local/ + echo "directory /opt/docklet have been created" +if [ ! -d /opt/docklet/local/basefs ]; then + mkdir -p /opt/docklet/local/basefs + echo "Generating basefs" + wget -P /opt/docklet/local http://iwork.pku.edu.cn:1616/basefs-0.11.tar.bz2 && tar xvf /opt/docklet/local/basefs-0.11.tar.bz2 -C /opt/docklet/local/ > /dev/null + [ $? != "0" ] && echo "Generate basefs failed, please download it from http://unias.github.io/docklet/download to FS_PREFIX/local and then extract it using root. (defalut FS_PRERIX is /opt/docklet)" +fi + +echo "Some packagefs can be downloaded from http://unias.github.io/docklet.download" +echo "you can download the packagefs and extract it to FS_PREFIX/local using root. (default FS_PREFIX is /opt/docklet" + echo "" echo "All preparation installations are done." echo "****************************************" @@ -61,14 +72,6 @@ echo "* Please Read Lines Below Before Start *" echo "****************************************" echo "" -echo "Before staring : you need a basefs image. " -echo "basefs images are provided at: " -echo " http://unias.github.io/docklet/download" -echo "please download it to FS_PREFIX/local and then extract it using root. (defalut FS_PRERIX is /opt/docklet)" -echo "you will get a directory structure like" -echo " /opt/docklet/local/basefs/etc " -echo " /opt/docklet/local/basefs/bin " -echo " /opt/docklet/local/basefs/..." echo "you may want to custom home page of docklet. Please modify web/templates/home.html" echo "Next, make sure exim4 can deliver mail out. To enable, run:" @@ -76,6 +79,4 @@ echo "dpkg-reconfigure exim4-config" echo "select internet site" echo "" - - echo "Then start docklet as described in README.md" diff --git a/src/imagemgr.py b/src/imagemgr.py index 291aa5d..9f44f21 100755 --- a/src/imagemgr.py +++ b/src/imagemgr.py @@ -154,7 +154,7 @@ class ImageMgr(): #self.prepareImage(user,image,layer+"/overlay") self.prepareImage(user,image,layer) logger.info("image has been prepared") - sys_run("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh -o udba=reval none %s/" % (layer,self.NFS_PREFIX,rootfs),True) + sys_run("mount -t aufs -o br=%s=rw:%s/local/packagefs=ro+wh:%s/local/basefs=ro+wh -o udba=reval none %s/" % (layer,self.NFS_PREFIX,self.NFS_PREFIX,rootfs),True) sys_run("mkdir -m 777 -p %s/local/temp/%s" % (self.NFS_PREFIX,lxc)) except Exception as e: @@ -204,7 +204,7 @@ class ImageMgr(): sys_run("mount /dev/%s/%s %s" % (vgname,lxc,layer)) Ret = sys_run("mountpoint %s" % rootfs) if Ret.returncode != 0: - sys_run("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh -o udba=reval none %s/" % (layer,self.NFS_PREFIX,rootfs)) + sys_run("mount -t aufs -o br=%s=rw:%s/local/packagefs=ro+wh:%s/local/basefs=ro+wh -o udba=reval none %s/" % (layer,self.NFS_PREFIX,self.NFS_PREFIX,rootfs)) return True @@ -256,44 +256,10 @@ class ImageMgr(): sys_run("rm -f %s" % public_imgpath+"."+image+".description", True) except Exception as e: logger.error(e) - """ - def update_basefs(self,image): - imgpath = self.imgpath + "private/root/" - layer = self.NFS_PREFIX + "/local/volume/update_base" - mountpoint = self.NFS_PREFIX + "/local/basefs_mp" - tmpdir = self.NFS_PREFIX + "/local/basefs_tmp" - olddir = self.NFS_PREFIX + "/local/basefs_old" - try: - logger.info("create directory %s, %s, %s" % (layer,mountpoint,tmpdir)) - sys_run("mkdir -p %s" % layer) - sys_run("mkdir -p %s" % mountpoint) - sys_run("mkdir -p %s" % tmpdir) - logger.info("load image from %s" % imgpath+image) - 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+image,self.dealpath(layer)),True) - logger.info("mount old base image and new image by aufs") - sys_run("mount -t aufs -o br=%s=rw:%s/local/basefs=ro+wh -o udba=reval none %s/" % (layer,self.NFS_PREFIX,mountpoint),True) - logger.info("save new image to %s" % tmpdir) - sys_run("rsync -a --delete %s/ %s/" % (self.dealpath(mountpoint),self.dealpath(tmpdir)),True) - logger.info("umount %s" % mountpoint) - sys_run("umount %s" % mountpoint) - logger.info("remove directory %s, %s" % (layer,mountpoint)) - sys_run("rm -rf %s/" % mountpoint) - sys_run("rm -rf %s/" % layer) - logger.info("move old base image to an tmp directory") - sys_run("mv %s %s" % (self.NFS_PREFIX + "/local/basefs",olddir)) - logger.info("move new base image from %s to %s" % (tmpdir, self.NFS_PREFIX+"/local/basefs")) - sys_run("mv %s %s" % (tmpdir, self.NFS_PREFIX+"/local/basefs")) - logger.info("remove old base image") - sys_run("rm -rf %s/" % olddir) - logger.info("update base image success") - except Exception as e: - logger.error(e) - return True - """ def update_basefs(self,image): imgpath = self.imgpath + "private/root/" - basefs = self.NFS_PREFIX+"/local/basefs/" + basefs = self.NFS_PREFIX+"/local/packagefs/" tmppath = self.NFS_PREFIX + "/local/tmpimg/" tmpimage = str(random.randint(0,10000000)) try: diff --git a/src/network.py b/src/network.py index 50654fb..db233b0 100755 --- a/src/network.py +++ b/src/network.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -import json, sys, netifaces +import json, sys, netifaces, threading from nettools import netcontrol,ovscontrol from log import logger @@ -386,9 +386,11 @@ class NetworkMgr(object): [status, userdata] = self.etcd.getkey("network/users/"+username) usercopy = json.loads(userdata) user = UserPool(copy = usercopy) + logger.debug("load user into dict") self.users[username] = user def dump_user(self, username): + logger.debug("dump user into etcd") self.etcd.setkey("network/users/"+username, json.dumps({'info':self.users[username].info, 'gateway':self.users[username].gateway, 'pool':self.users[username].pool})) def load_usrgw(self,username): @@ -586,6 +588,7 @@ class NetworkMgr(object): result = self.users[username].acquire_cidr(num) self.dump_user(username) del self.users[username] + return result # ip_or_ips : one IP address or a list of IPs diff --git a/web/templates/settings.html b/web/templates/settings.html index c585f84..c71a486 100644 --- a/web/templates/settings.html +++ b/web/templates/settings.html @@ -241,15 +241,15 @@