summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2012-03-02 16:18:24 +0000
committerGerrit Code Review <review@openstack.org>2012-03-02 16:18:24 +0000
commit0681315df60385592004605eea6495c961f6f63c (patch)
treec1b9e8e27220c8f29b6d337caf4b97fa63ae42a4 /nova/utils.py
parent45146b337bba2cf1422b276a6988ee00e4c2e3c0 (diff)
parentc9aa0f57b6200313ea1f6c3839d65828024e2d37 (diff)
downloadnova-0681315df60385592004605eea6495c961f6f63c.tar.gz
nova-0681315df60385592004605eea6495c961f6f63c.tar.xz
nova-0681315df60385592004605eea6495c961f6f63c.zip
Merge "Refactor spawn to use UndoManager."
Diffstat (limited to 'nova/utils.py')
-rw-r--r--nova/utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py
index a224b3878..0ea6ee93b 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -1582,3 +1582,29 @@ def strcmp_const_time(s1, s2):
for (a, b) in zip(s1, s2):
result |= ord(a) ^ ord(b)
return result == 0
+
+
+class UndoManager(object):
+ """Provides a mechanism to facilitate rolling back a series of actions
+ when an exception is raised.
+ """
+ def __init__(self):
+ self.undo_stack = []
+
+ def undo_with(self, undo_func):
+ self.undo_stack.append(undo_func)
+
+ def _rollback(self):
+ for undo_func in reversed(self.undo_stack):
+ undo_func()
+
+ def rollback_and_reraise(self, msg=None):
+ """Rollback a series of actions then re-raise the exception.
+
+ NOTE(sirp): This should only be called within an exception handler.
+ """
+ with save_and_reraise_exception():
+ if msg:
+ LOG.exception(msg)
+
+ self._rollback()