From d9cc0c2c9f273a7b11330f87c26d239a26cec87b Mon Sep 17 00:00:00 2001 From: zhongyehong Date: Sun, 8 Apr 2018 15:05:04 +0800 Subject: [PATCH] user can copy an image to other masters --- src/httprest.py | 10 +++++++++ src/imagemgr.py | 27 +++++++++++++++++++++++ web/templates/config.html | 39 ++++++++++++++++++++++++++++++++++ web/web.py | 9 ++++++++ web/webViews/cluster.py | 22 ++++++++++++++++++- web/webViews/dockletrequest.py | 7 ++++-- 6 files changed, 111 insertions(+), 3 deletions(-) diff --git a/src/httprest.py b/src/httprest.py index dfaaaa0..299857b 100755 --- a/src/httprest.py +++ b/src/httprest.py @@ -398,6 +398,16 @@ def delete_image(user, beans, form): G_imagemgr.removeImage(user,image) return json.dumps({'success':'true', 'action':'delete'}) +@app.route("/image/copy/", methods=['POST']) +@login_required +def copy_image(user, beans, form): + global G_imagemgr + image = form.get('image', None) + target = form.get('target',None) + res = G_imagemgr.copyImage(user,image,target) + return json.dumps(res) + + @app.route("/addproxy/", methods=['POST']) @login_required def addproxy(user, beans, form): diff --git a/src/imagemgr.py b/src/imagemgr.py index 9f44f21..61337fd 100755 --- a/src/imagemgr.py +++ b/src/imagemgr.py @@ -257,6 +257,33 @@ class ImageMgr(): except Exception as e: logger.error(e) + def copyImage(self,user,image,target): + path = "/opt/docklet/global/images/private/"+user+"/" + image_info_file = open(path+"."+image+".info", 'r') + [createtime, isshare] = image_info_file.readlines() + recordshare = isshare + isshare = "unshared" + image_info_file.close() + image_info_file = open(path+"."+image+".info", 'w') + image_info_file.writelines([createtime, isshare]) + image_info_file.close() + try: + sys_run('ssh root@%s "mkdir -p %s"' % (target,path)) + sys_run('scp %s%s.tz root@%s:%s' % (path,image,target,path)) + sys_run('scp %s.%s.description root@%s:%s' % (path,image,target,path)) + sys_run('scp %s.%s.info root@%s:%s' % (path,image,target,path)) + except Exception as e: + logger.error(e) + image_info_file = open(path+"."+image+".info", 'w') + image_info_file.writelines([createtime, recordshare]) + image_info_file.close() + return {'success':'false', 'message':str(e)} + image_info_file = open(path+"."+image+".info", 'w') + image_info_file.writelines([createtime, recordshare]) + image_info_file.close() + logger.info("copy image %s of %s to %s success" % (image,user,target)) + return {'success':'true', 'action':'copy image'} + def update_basefs(self,image): imgpath = self.imgpath + "private/root/" basefs = self.NFS_PREFIX+"/local/packagefs/" diff --git a/web/templates/config.html b/web/templates/config.html index 279d345..6c1382b 100755 --- a/web/templates/config.html +++ b/web/templates/config.html @@ -358,12 +358,51 @@ + + {% else %} shared + {% endif %} diff --git a/web/web.py b/web/web.py index 857587b..a3cf229 100755 --- a/web/web.py +++ b/web/web.py @@ -294,6 +294,15 @@ def deleteImage(image,masterip): deleteImageView.masterip = masterip return deleteImageView.as_view() +@app.route("/image//copy//", methods=['POST']) +@login_required +def copyImage(image,masterip): + copyImageView.image = image + copyImageView.masterip = masterip + copyImageView.target = request.form['target'] + return copyImageView.as_view() + + @app.route("/image//updatebase//", methods=['GET']) @login_required def updatebaseImage(image,masterip): diff --git a/web/webViews/cluster.py b/web/webViews/cluster.py index d16605f..214cc6b 100755 --- a/web/webViews/cluster.py +++ b/web/webViews/cluster.py @@ -298,6 +298,25 @@ class unshareImageView(normalView): else: self.error() +class copyImageView(normalView): + error_path = "error.html" + + @classmethod + def post(self): + masterip = self.masterip + data = { + "image": self.image, + "target": self.target + } + result = dockletRequest.post("/image/copy/", data, masterip) + if result: + if result.get('success') == 'true': + return redirect("/config/") + else: + return self.render(self.error_path,message=result.get('message')) + else: + self.error() + class deleteImageView(normalView): template_path = "dashboard.html" @@ -350,6 +369,7 @@ class deleteproxyView(normalView): class configView(normalView): @classmethod def get(self): + masterips = dockletRequest.post_to_all() allimages = dockletRequest.post_to_all('/image/list/') for master in allimages: allimages[master] = allimages[master].get('images') @@ -398,7 +418,7 @@ class configView(normalView): 'memory': defaultmemory, 'disk': defaultdisk } - return self.render("config.html", allimages = allimages, allclusters = allclusters_info, mysession=dict(session), quota = quota, usage = usage, defaultsetting = defaultsetting) + return self.render("config.html", allimages = allimages, allclusters = allclusters_info, mysession=dict(session), quota = quota, usage = usage, defaultsetting = defaultsetting, masterips = masterips) @classmethod def post(self): diff --git a/web/webViews/dockletrequest.py b/web/webViews/dockletrequest.py index 165c11b..f984b1a 100644 --- a/web/webViews/dockletrequest.py +++ b/web/webViews/dockletrequest.py @@ -97,8 +97,11 @@ class dockletRequest(): except Exception as e: logger.debug(e) continue - result[masterip] = res - logger.debug("get result from " + getip(masterip)) + if 'success' in res and res['success'] == 'true': + result[masterip] = res + logger.info("get result from %s success" % getip(masterip)) + else: + logger.error("get result from %s failed" % getip(masterip)) return result