[sandbox 10/11] virt-sandbox-image: fix exception catching

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

 



Catch the exception in the main to avoid problems when auto-calling
another function that fails. For example if run calls create and that
one fails, run would have ignored the error.
---
 libvirt-sandbox/image/cli.py | 156 ++++++++++++++++++++-----------------------
 1 file changed, 73 insertions(+), 83 deletions(-)

diff --git a/libvirt-sandbox/image/cli.py b/libvirt-sandbox/image/cli.py
index f4472d9..eb6bbb4 100755
--- a/libvirt-sandbox/image/cli.py
+++ b/libvirt-sandbox/image/cli.py
@@ -66,98 +66,85 @@ def info(msg):
     sys.stdout.write(msg)
 
 def download(args):
-    try:
-        tmpl = template.Template.from_uri(args.template)
-        source = tmpl.get_source_impl()
-        source.download_template(template=tmpl,
-                                 templatedir=args.template_dir)
-    except Exception,e:
-        print "Download Error %s" % str(e)
+    tmpl = template.Template.from_uri(args.template)
+    source = tmpl.get_source_impl()
+    source.download_template(template=tmpl,
+                             templatedir=args.template_dir)
 
 def delete(args):
-    try:
-        tmpl = template.Template.from_uri(args.template)
-        source = tmpl.get_source_impl()
-        source.delete_template(template=tmpl,
-                               templatedir=args.template_dir)
-    except Exception,e:
-        print "Delete Error %s" % str(e)
+    tmpl = template.Template.from_uri(args.template)
+    source = tmpl.get_source_impl()
+    source.delete_template(template=tmpl,
+                           templatedir=args.template_dir)
 
 def create(args):
-    try:
-        tmpl = template.Template.from_uri(args.template)
-        source = tmpl.get_source_impl()
+    tmpl = template.Template.from_uri(args.template)
+    source = tmpl.get_source_impl()
 
-        if not source.was_downloaded(tmpl, args.template_dir):
-            download(args)
+    if not source.was_downloaded(tmpl, args.template_dir):
+        download(args)
 
-        fmt = default_format
-        if "format" in vars(args):
-            fmt = args.format
+    fmt = default_format
+    if "format" in vars(args):
+        fmt = args.format
 
-        source.create_template(template=tmpl,
-                               templatedir=args.template_dir,
-                               connect=args.connect,
-                               format=fmt)
-    except Exception,e:
-        print "Create Error %s" % str(e)
+    source.create_template(template=tmpl,
+                           templatedir=args.template_dir,
+                           connect=args.connect,
+                           format=fmt)
 
 def run(args):
-    try:
-        if args.connect is not None:
-            check_connect(args.connect)
-        tmpl = template.Template.from_uri(args.template)
-        source = tmpl.get_source_impl()
-
-        # Create the template image if needed
-        if not source.has_template(tmpl, args.template_dir):
-            create(args)
-
-        name = args.name
-        if name is None:
-            randomid = ''.join(random.choice(string.lowercase) for i in range(10))
-            name = tmpl.path[1:] + ":" + randomid
-
-        diskfile = source.get_disk(template=tmpl,
-                                   templatedir=args.template_dir,
-                                   imagedir=args.image_dir,
-                                   sandboxname=name)
-
-        format = "qcow2"
-        commandToRun = source.get_command(tmpl, args.template_dir, args.args)
-        if len(commandToRun) == 0:
-            commandToRun = ["/bin/sh"]
-        cmd = ['virt-sandbox', '--name', name]
-        if args.connect is not None:
-            cmd.append("-c")
-            cmd.append(args.connect)
-        params = ['-m','host-image:/=%s,format=%s' %(diskfile,format)]
-
-        networkArgs = args.network
-        if networkArgs is not None:
-            params.append('-N')
-            params.append(networkArgs)
-
-        allEnvs = source.get_env(tmpl, args.template_dir)
-        envArgs = args.env
-        if envArgs is not None:
-            allEnvs = allEnvs + envArgs
-        for env in allEnvs:
-            envsplit = env.split("=")
-            envlen = len(envsplit)
-            if envlen == 2:
-                params.append("--env")
-                params.append(env)
-            else:
-                pass
-
-        cmd = cmd + params + ['--'] + commandToRun
-        subprocess.call(cmd)
-        os.unlink(diskfile)
-        source.post_run(tmpl, args.template_dir, name)
-
-    except Exception,e:
-        print "Run Error %s" % str(e)
+    if args.connect is not None:
+        check_connect(args.connect)
+    tmpl = template.Template.from_uri(args.template)
+    source = tmpl.get_source_impl()
+
+    # Create the template image if needed
+    if not source.has_template(tmpl, args.template_dir):
+        create(args)
+
+    name = args.name
+    if name is None:
+        randomid = ''.join(random.choice(string.lowercase) for i in range(10))
+        name = tmpl.path[1:] + ":" + randomid
+
+    diskfile = source.get_disk(template=tmpl,
+                               templatedir=args.template_dir,
+                               imagedir=args.image_dir,
+                               sandboxname=name)
+
+    format = "qcow2"
+    commandToRun = source.get_command(tmpl, args.template_dir, args.args)
+    if len(commandToRun) == 0:
+        commandToRun = ["/bin/sh"]
+    cmd = ['virt-sandbox', '--name', name]
+    if args.connect is not None:
+        cmd.append("-c")
+        cmd.append(args.connect)
+    params = ['-m','host-image:/=%s,format=%s' %(diskfile,format)]
+
+    networkArgs = args.network
+    if networkArgs is not None:
+        params.append('-N')
+        params.append(networkArgs)
+
+    allEnvs = source.get_env(tmpl, args.template_dir)
+    envArgs = args.env
+    if envArgs is not None:
+        allEnvs = allEnvs + envArgs
+    for env in allEnvs:
+        envsplit = env.split("=")
+        envlen = len(envsplit)
+        if envlen == 2:
+            params.append("--env")
+            params.append(env)
+        else:
+            pass
+
+    cmd = cmd + params + ['--'] + commandToRun
+    subprocess.call(cmd)
+    os.unlink(diskfile)
+    source.post_run(tmpl, args.template_dir, name)
 
 def requires_template(parser):
     parser.add_argument("template",
@@ -276,3 +263,6 @@ def main():
         sys.stderr.write("%s: %s\n" % (sys.argv[0], e))
         sys.stderr.flush()
         sys.exit(1)
+    except Exception, e:
+        print e.message
+        sys.exit(1)
-- 
2.1.4

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