summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorJoshua Harlow <harlowja@gmail.com>2013-07-20 12:38:25 -0700
committerJoshua Harlow <harlowja@gmail.com>2013-07-20 12:38:25 -0700
commit4e9a13142f1ee81c905a2cc9401a88f115ec778e (patch)
tree0eb59a4ff81e9ed93435f9e1137649cd03462c66 /cloudinit
parent1edfb2a7a36a2bdddfe0ca48ba5d23721bf17a35 (diff)
downloadvyos-cloud-init-4e9a13142f1ee81c905a2cc9401a88f115ec778e.tar.gz
vyos-cloud-init-4e9a13142f1ee81c905a2cc9401a88f115ec778e.zip
Init and finalize refactor.
Instead of previously initializing and not finalizing the handles that completed successfully when a handler initializing or running failed we should attempt to always give said handlers a chance to finalize (even when another handler fails).
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/stages.py82
1 files changed, 53 insertions, 29 deletions
diff --git a/cloudinit/stages.py b/cloudinit/stages.py
index df49cabb..6893afd9 100644
--- a/cloudinit/stages.py
+++ b/cloudinit/stages.py
@@ -383,36 +383,60 @@ class Init(object):
# Form our cloud interface
data = self.cloudify()
- # Init the handlers first
- called = []
- for (_ctype, mod) in c_handlers.iteritems():
- if mod in called:
- continue
- handlers.call_begin(mod, data, frequency)
- called.append(mod)
-
- # Walk the user data
- part_data = {
- 'handlers': c_handlers,
- # Any new handlers that are encountered get writen here
- 'handlerdir': idir,
- 'data': data,
- # The default frequency if handlers don't have one
- 'frequency': frequency,
- # This will be used when new handlers are found
- # to help write there contents to files with numbered
- # names...
- 'handlercount': 0,
- }
- handlers.walk(user_data_msg, handlers.walker_callback, data=part_data)
+ # This list contains the modules initialized (so that we only finalize
+ # ones that were actually initialized)
+ inited_handlers = []
+
+ def init_handlers():
+ # Init the handlers first
+ called = []
+ for (_ctype, mod) in c_handlers.iteritems():
+ if mod in called:
+ # Avoid initing the same module twice (if said module
+ # is registered to more than one content-type).
+ continue
+ handlers.call_begin(mod, data, frequency)
+ inited_handlers.append(mod)
+ called.append(mod)
+
+ def walk_handlers():
+ # Walk the user data
+ part_data = {
+ 'handlers': c_handlers,
+ # Any new handlers that are encountered get writen here
+ 'handlerdir': idir,
+ 'data': data,
+ # The default frequency if handlers don't have one
+ 'frequency': frequency,
+ # This will be used when new handlers are found
+ # to help write there contents to files with numbered
+ # names...
+ 'handlercount': 0,
+ }
+ handlers.walk(user_data_msg, handlers.walker_callback,
+ data=part_data)
+
+ def finalize_handlers():
+ # Give callbacks opportunity to finalize
+ called = []
+ for (_ctype, mod) in c_handlers.iteritems():
+ if mod in called:
+ # Avoid finalizing the same module twice (if said module
+ # is registered to more than one content-type).
+ continue
+ if mod not in inited_handlers:
+ continue
+ called.append(mod)
+ try:
+ handlers.call_end(mod, data, frequency)
+ except:
+ util.logexc(LOG, "Failed to finalize handler: %s", mod)
- # Give callbacks opportunity to finalize
- called = []
- for (_ctype, mod) in c_handlers.iteritems():
- if mod in called:
- continue
- handlers.call_end(mod, data, frequency)
- called.append(mod)
+ try:
+ init_handlers()
+ walk_handlers()
+ finally:
+ finalize_handlers()
# Perform post-consumption adjustments so that
# modules that run during the init stage reflect