summaryrefslogtreecommitdiffstats
path: root/nova/utils.py
diff options
context:
space:
mode:
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()