improve libguestfs exception handling

* nova/virt/disk/vfs/guestfs.py (teardown): Handle specific
exceptions when tearing down so that (future) nova bugs
aren't masked.  Also handle AttributeError so that the
relatively new handle() and close() don't cause erroneous
debug logs on older versions of libguestfs.
(has_file): remove the last use of generic exception
catching and use the more specific RuntimeError.

Change-Id: Ia7345675d75c1c6727e9254791d44475c0ed9295
This commit is contained in:
Pádraig Brady 2013-01-03 14:25:06 +00:00
parent 54ee787d7a
commit 2ee83a493a
2 changed files with 31 additions and 18 deletions

View File

@ -96,13 +96,13 @@ class GuestFS(object):
def stat(self, path):
if not path in self.files:
raise Exception("No such file: " + path)
raise RuntimeError("No such file: " + path)
return self.files[path]["mode"]
def chown(self, uid, gid, path):
if not path in self.files:
raise Exception("No such file: " + path)
raise RuntimeError("No such file: " + path)
if uid != -1:
self.files[path]["uid"] = uid
@ -111,7 +111,7 @@ class GuestFS(object):
def chmod(self, mode, path):
if not path in self.files:
raise Exception("No such file: " + path)
raise RuntimeError("No such file: " + path)
self.files[path]["mode"] = mode
@ -123,7 +123,7 @@ class GuestFS(object):
def aug_get(self, cfgpath):
if not self.auginit:
raise Exception("Augeus not initialized")
raise RuntimeError("Augeus not initialized")
if cfgpath == "/files/etc/passwd/root/uid":
return 0
@ -137,4 +137,4 @@ class GuestFS(object):
return 500
elif cfgpath == "/files/etc/group/admins/gid":
return 600
raise Exception("Unknown path %s", cfgpath)
raise RuntimeError("Unknown path %s", cfgpath)

View File

@ -101,6 +101,7 @@ class VFSGuestFS(vfs.VFS):
self.handle.aug_init("/", 0)
except RuntimeError, e:
# dereference object and implicitly close()
self.handle = None
raise exception.NovaException(
_("Error mounting %(imgfile)s with libguestfs (%(e)s)") %
@ -111,19 +112,31 @@ class VFSGuestFS(vfs.VFS):
def teardown(self):
LOG.debug(_("Tearing down appliance"))
try:
self.handle.aug_close()
except Exception, e:
LOG.debug(_("Failed to close augeas %s"), e)
try:
self.handle.shutdown()
except Exception, e:
LOG.debug(_("Failed to shutdown appliance %s"), e)
try:
self.handle.close()
except Exception, e:
LOG.debug(_("Failed to close guest handle %s"), e)
self.handle = None
try:
self.handle.aug_close()
except RuntimeError, e:
LOG.warn(_("Failed to close augeas %s"), e)
try:
self.handle.shutdown()
except AttributeError:
# Older libguestfs versions haven't an explicit shutdown
pass
except RuntimeError, e:
LOG.warn(_("Failed to shutdown appliance %s"), e)
try:
self.handle.close()
except AttributeError:
# Older libguestfs versions haven't an explicit close
pass
except RuntimeError, e:
LOG.warn(_("Failed to close guest handle %s"), e)
finally:
# dereference object and implicitly close()
self.handle = None
@staticmethod
def _canonicalize_path(path):
@ -157,7 +170,7 @@ class VFSGuestFS(vfs.VFS):
try:
self.handle.stat(path)
return True
except Exception, e:
except RuntimeError:
return False
def set_permissions(self, path, mode):