action: Create and use a different Logger for each action.
[matthijs/projects/backupninja.git] / src / lib / backupninja / action.py
index 95c43fd9d940760f0ea990e6dacb6bdb07a6444e..fe1947f4fd2b3c8da7599c1f73e3b161ca0f6d1c 100644 (file)
@@ -21,6 +21,8 @@
 
 """ Running backup actions """
 
+import os
+import logging
 import logging as log
 
 from backupninja import config
@@ -33,34 +35,52 @@ def run_all_actions(opts, global_config):
     opts are the parsed commandline options, global_config is the parsed
     global configuration.
     """
-    actions = config.list_actions(opts)
+    log.info('Running all actions')
+    try:
+        action_configs = config.list_actions(opts)
+    except OSError, e:
+        log.critical('Unable to list actions: %s', e)
+        return
+
+    action_configs.sort()
 
-    for action in actions:
-        run_action(action, opts, global_config)
+    for action_config in action_configs:
+        run_action(action_config, opts, global_config)
 
-def run_action(action, opts, global_config):
+def run_action(action_config, opts, global_config):
     """
-    Run a single action. opts are the parsed commandline options,
+    Run a single action. action_config is the full path to its
+    configuration file. opts are the parsed commandline options,
     global_config is the parsed global configuration.
     """
+    config_name = os.path.basename(action_config)
     # Split the action filename
-    parts = action.split('.', 2)
-    if (len(parts) < 2):
-        log.error('Invalid action filename: "%s". Should be in the form name.type, where type is a valid handler.')
+    parts = os.path.basename(config_name).split('.')
+    if (len(parts) != 2):
+        log.error('Invalid action filename: "%s". Should be in the form name.type, where type is a valid handler.' % action_config)
         return
     (action_name, action_ty) = parts
 
-    # Get the config for this action
-    action_config = config.get_action_config(opts, action)
-    # Create a handler for this action
-    handler = handlers.create_handler(action_ty, action_config)
+    action_log = logging.getLogger(config_name)
+    action_log.info('Running')
+
+    try:
+        # Create a handler for this action
+        action = handlers.create_action(action_ty, logger=action_log)
+        # Let the handler load its configuration file
+        action.load_config(action_config)
+        # Run it
+        action.run(test=opts.test)
+        action.finish(test=opts.test)
+        # Ask the action if there where any failures
+        success = not action.failed
+    except Exception, e:
+        action_log.error('Unexpected exception: %s', e)
+        import traceback
+        log.debug(traceback.format_exc())
+        success = False
 
-    # Silently skip invalid handlers, create_handler will have
-    # logged an error
-    if handler:
-        try:
-            # Run the handler
-            handler.run()
-            handler.finish()
-        except Exception, e:
-            log.error('Running action "%s" failed: %s', action, e)
+    if success:
+        action_log.info('Succeeded')
+    else:
+        action_log.info('Failed')