Re: [PATCH sandbox v5 08/20] Image: Add delete function

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

 



On Tue, 2015-09-08 at 17:29 +0100, Daniel P. Berrange wrote:
> From: Eren Yagdiran <erenyagdiran@xxxxxxxxx>
> 
> Refactoring delete function from virt-sandbox-image to DockerSource. Delete function
> can delete templates by name.
> 
> Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
> ---
>  libvirt-sandbox/image/cli.py                  | 58 ++++-----------------------
>  libvirt-sandbox/image/sources/DockerSource.py | 50 +++++++++++++++++++++++
>  libvirt-sandbox/image/sources/Source.py       | 10 +++++
>  3 files changed, 67 insertions(+), 51 deletions(-)
> 
> diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
> index f3c0ab7..5490c4b 100755
> --- a/libvirt-sandbox/image/cli.py
> +++ b/libvirt-sandbox/image/cli.py
> @@ -69,55 +69,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(templatename=args.template,
> @@ -131,8 +82,11 @@ def download(args):
>          print "Download Error %s" % str(e)
>  
>  def delete(args):
> -    info("Deleting %s from %s\n" % (args.template, default_template_dir))
> -    delete_template(args.template, default_template_dir)
> +    try:
> +        dynamic_source_loader(args.source).delete_template(templatename=args.template,
> +                                                           templatedir=args.template_dir)
> +    except Exception,e:
> +        print "Delete Error %s", str(e)
>  
>  def create(args):
>      try:
> @@ -183,6 +137,8 @@ def gen_delete_args(subparser):
>      parser = subparser.add_parser("delete",
>                                     help=_("Delete template data"))
>      requires_template(parser)
> +    requires_source(parser)
> +    requires_template_dir(parser)
>      parser.set_defaults(func=delete)
>  
>  def gen_create_args(subparser):
> diff --git a/libvirt-sandbox/image/sources/DockerSource.py b/libvirt-sandbox/image/sources/DockerSource.py
> index c1c8a7d..ab18b52 100644
> --- a/libvirt-sandbox/image/sources/DockerSource.py
> +++ b/libvirt-sandbox/image/sources/DockerSource.py
> @@ -295,5 +295,55 @@ class DockerSource(Source):
>          cmd = cmd + params
>          subprocess.call(cmd)
>  
> +    def delete_template(self, templatename, templatedir):
> +        imageusage = {}
> +        imageparent = {}
> +        imagenames = {}
> +        imagedirs = os.listdir(templatedir)
> +        for imagetagid in imagedirs:
> +            indexfile = templatedir + "/" + imagetagid + "/index.json"
> +            if os.path.exists(indexfile):
> +                with open(indexfile,"r") as f:
> +                    index = json.load(f)
> +                imagenames[index["name"]] = imagetagid
> +            jsonfile = templatedir + "/" + 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 templatename in imagenames:
> +            raise ValueError(["Image %s does not exist locally" %templatename])
> +
> +        imagetagid = imagenames[templatename]
> +        while imagetagid != None:
> +            debug("Remove %s\n" % imagetagid)
> +            parent = imageparent.get(imagetagid,None)
> +
> +            indexfile = templatedir + "/" + imagetagid + "/index.json"
> +            if os.path.exists(indexfile):
> +               os.remove(indexfile)
> +            jsonfile = templatedir + "/" + imagetagid + "/template.json"
> +            if os.path.exists(jsonfile):
> +                os.remove(jsonfile)
> +            datafile = templatedir + "/" + imagetagid + "/template.tar.gz"
> +            if os.path.exists(datafile):
> +                os.remove(datafile)
> +            imagedir = templatedir + "/" + 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/libvirt-sandbox/image/sources/Source.py b/libvirt-sandbox/image/sources/Source.py
> index 436eef6..4aea5c9 100644
> --- a/libvirt-sandbox/image/sources/Source.py
> +++ b/libvirt-sandbox/image/sources/Source.py
> @@ -61,3 +61,13 @@ class Source():
>          inside a sandbox using the requested libvirt connection URI.
>          """
>          pass
> +
> +    @abstractmethod
> +    def delete_template(self, templatename, templatedir):
> +        """
> +        :param templatename: name of the template image to delete
> +        :param templatedir: local directory path from which to delete template
> +
> +        Delete all local files associated with the template
> +        """
> +        pass

ACK

--
Cedric


--
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]