[sandbox PATCH 06/11] Image: Add delete function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Refactoring delete function from virt-sandbox-image to DockerSource. Delete function
can delete templates by name.
---
 virt-sandbox-image/sources/DockerSource.py | 53 +++++++++++++++++++++++++++
 virt-sandbox-image/sources/Source.py       |  3 ++
 virt-sandbox-image/virt-sandbox-image.py   | 59 ++++--------------------------
 3 files changed, 64 insertions(+), 51 deletions(-)

diff --git a/virt-sandbox-image/sources/DockerSource.py b/virt-sandbox-image/sources/DockerSource.py
index f33f94b..180d3f2 100644
--- a/virt-sandbox-image/sources/DockerSource.py
+++ b/virt-sandbox-image/sources/DockerSource.py
@@ -280,6 +280,59 @@ class DockerSource(Source):
                '/mnt']
         subprocess.call(cmd)
 
+    def delete_template(self,**args):
+        imageusage = {}
+        imageparent = {}
+        imagenames = {}
+        name = args['name']
+        destdir = args['imagepath']
+        destdir = destdir if destdir is not None else default_template_dir
+        imagedirs = os.listdir(destdir)
+        for imagetagid in imagedirs:
+            indexfile = destdir + "/" + imagetagid + "/index.json"
+            if os.path.exists(indexfile):
+                with open(indexfile,"r") as f:
+                    index = json.load(f)
+                imagenames[index["name"]] = imagetagid
+            jsonfile = destdir + "/" + imagetagid + "/template.json"
+            if os.path.exists(jsonfile):
+                with open(jsonfile,"r") as f:
+                    template = json.load(f)
+
+                parent = template.get("parent",None)
+                if parent:
+                    if parent not in imageusage:
+                        imageusage[parent] = []
+                    imageusage[parent].append(imagetagid)
+                    imageparent[imagetagid] = parent
+
+
+        if not name in imagenames:
+            raise ValueError(["Image %s does not exist locally" %name])
+
+        imagetagid = imagenames[name]
+        while imagetagid != None:
+            debug("Remove %s\n" % imagetagid)
+            parent = imageparent.get(imagetagid,None)
+
+            indexfile = destdir + "/" + imagetagid + "/index.json"
+            if os.path.exists(indexfile):
+               os.remove(indexfile)
+            jsonfile = destdir + "/" + imagetagid + "/template.json"
+            if os.path.exists(jsonfile):
+                os.remove(jsonfile)
+            datafile = destdir + "/" + imagetagid + "/template.tar.gz"
+            if os.path.exists(datafile):
+                os.remove(datafile)
+            imagedir = destdir + "/" + imagetagid
+            shutil.rmtree(imagedir)
+
+            if parent:
+                if len(imageusage[parent]) != 1:
+                    debug("Parent %s is shared\n" % parent)
+                    parent = None
+            imagetagid = parent
+
 def debug(msg):
     sys.stderr.write(msg)
 
diff --git a/virt-sandbox-image/sources/Source.py b/virt-sandbox-image/sources/Source.py
index 89db14a..36c4343 100644
--- a/virt-sandbox-image/sources/Source.py
+++ b/virt-sandbox-image/sources/Source.py
@@ -15,4 +15,7 @@ class Source():
     def create_template(self,**args):
       pass
 
+    @abstractmethod
+    def delete_template(self,**args):
+      pass
 
diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
index 58055b3..c320105 100644
--- a/virt-sandbox-image/virt-sandbox-image.py
+++ b/virt-sandbox-image/virt-sandbox-image.py
@@ -83,55 +83,6 @@ def debug(msg):
 def info(msg):
     sys.stdout.write(msg)
 
-def delete_template(name, destdir):
-    imageusage = {}
-    imageparent = {}
-    imagenames = {}
-    imagedirs = os.listdir(destdir)
-    for imagetagid in imagedirs:
-        indexfile = destdir + "/" + imagetagid + "/index.json"
-        if os.path.exists(indexfile):
-            with open(indexfile, "r") as f:
-                index = json.load(f)
-            imagenames[index["name"]] = imagetagid
-        jsonfile = destdir + "/" + imagetagid + "/template.json"
-        if os.path.exists(jsonfile):
-            with open(jsonfile, "r") as f:
-                template = json.load(f)
-
-            parent = template.get("parent", None)
-            if parent:
-                if parent not in imageusage:
-                    imageusage[parent] = []
-                imageusage[parent].append(imagetagid)
-                imageparent[imagetagid] = parent
-
-    if not name in imagenames:
-        raise ValueError(["Image %s does not exist locally" % name])
-
-    imagetagid = imagenames[name]
-    while imagetagid != None:
-        debug("Remove %s\n" %  imagetagid)
-        parent = imageparent.get(imagetagid, None)
-
-        indexfile = destdir + "/" + imagetagid + "/index.json"
-        if os.path.exists(indexfile):
-            os.remove(indexfile)
-        jsonfile = destdir + "/" + imagetagid + "/template.json"
-        if os.path.exists(jsonfile):
-            os.remove(jsonfile)
-        datafile = destdir + "/" + imagetagid + "/template.tar.gz"
-        if os.path.exists(datafile):
-            os.remove(datafile)
-        imagedir = destdir + "/" + imagetagid
-        os.rmdir(imagedir)
-
-        if parent:
-            if len(imageusage[parent]) != 1:
-                debug("Parent %s is shared\n" % parent)
-                parent = None
-        imagetagid = parent
-
 def download(args):
     try:
         dynamic_source_loader(args.source).download_template(name=args.name,
@@ -145,8 +96,11 @@ def download(args):
         print "Download Error %s" % str(e)
 
 def delete(args):
-    info("Deleting %s from %s\n" % (args.name, default_template_dir))
-    delete_template(args.name, default_template_dir)
+    try:
+        dynamic_source_loader(args.source).delete_template(name=args.name,
+                                                           imagepath=args.imagepath)
+    except Exception,e:
+        print "Delete Error %s", str(e)
 
 def create(args):
     try:
@@ -193,6 +147,9 @@ def gen_delete_args(subparser):
     parser = subparser.add_parser("delete",
                                    help=_("Delete template data"))
     requires_name(parser)
+    requires_source(parser)
+    parser.add_argument("imagepath",
+                        help=_("Path for image"))
     parser.set_defaults(func=delete)
 
 def gen_create_args(subparser):
-- 
2.1.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]