[libvirt] [sandbox PATCH v4 06/21] Image: Add check_writable and runtime resolver

Daniel P. Berrange berrange at redhat.com
Fri Sep 4 10:07:18 UTC 2015


On Fri, Aug 28, 2015 at 01:47:34PM +0000, Eren Yagdiran wrote:
> These helper functions are for selecting right directories according
> to running user privileges
> ---
>  virt-sandbox-image/virt-sandbox-image.py | 27 ++++++++++++++++++++++++++-
>  1 file changed, 26 insertions(+), 1 deletion(-)
> 
> diff --git a/virt-sandbox-image/virt-sandbox-image.py b/virt-sandbox-image/virt-sandbox-image.py
> index 9e98bf2..5917dd6 100755
> --- a/virt-sandbox-image/virt-sandbox-image.py
> +++ b/virt-sandbox-image/virt-sandbox-image.py
> @@ -32,6 +32,8 @@ import sys
>  import urllib2
>  import subprocess
>  
> +template_dir = None
> +storage_dir = None
>  default_privileged_template_dir = "/var/lib/libvirt/templates"
>  default_home_dir = os.environ['HOME']
>  default_unprivileged_template_dir = default_home_dir + "/.local/share/libvirt/templates"
> @@ -40,6 +42,29 @@ default_unprivileged_storage_dir = default_unprivileged_template_dir + "/storage
>  debug = False
>  verbose = False
>  
> +def check_dir_writable(path):
> +    if not os.access(path,os.W_OK):
> +        return False
> +    return True
> +
> +def runtime_dir_resolver():
> +    global default_privileged_template_dir
> +    global default_privileged_storage_dir
> +    global default_unprivileged_template_dir
> +    global default_unprivileged_storage_dir
> +    global template_dir
> +    global storage_dir
> +    if(check_dir_writable(default_privileged_template_dir)):
> +        template_dir = default_privileged_template_dir
> +        storage_dir = default_privileged_storage_dir
> +        return
> +    template_dir = default_unprivileged_template_dir
> +    storage_dir = default_unprivileged_storage_dir
> +    if not os.path.exists(template_dir):
> +        os.makedirs(template_dir)
> +    if not os.path.exists(storage_dir):
> +        os.makedirs(storage_dir)

This code is called in early startup before  we've even decided
what command to run. We should not be calling os.makedirs() at
this point. eg if the user asks  virt-sandbox-image --help
they would not expect us to create any files/dirs.

We should only create the directories when we actually need to
storage some data in them, eg for the 'download' command, but
not for the 'delete' command for example.

> +
>  sys.dont_write_byte_code = True
>  
>  import importlib
> @@ -380,8 +405,8 @@ def gen_create_args(subparser):
>      parser.set_defaults(func=create)
>  
>  if __name__ == '__main__':
> +    runtime_dir_resolver()
>      parser = argparse.ArgumentParser(description='Sandbox Container Image Tool')
> -
>      subparser = parser.add_subparsers(help=_("commands"))
>      gen_download_args(subparser)
>      gen_delete_args(subparser)

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list