action: Create and use a different Logger for each action.
[matthijs/projects/backupninja.git] / src / lib / backupninja / action.py
1 # -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
2 # vim: set filetype=python sw=4 sts=4 expandtab autoindent:
3 #
4 #    Backupninja python reimplementation, based on original backupninja program
5 #    by riseup.net.
6 #    Copyright (C) 2010  Matthijs Kooijman <matthijs@stdin.nl>
7 #
8 #    This program is free software; you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation; either version 2 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License along
19 #    with this program; if not, write to the Free Software Foundation, Inc.,
20 #    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 """ Running backup actions """
23
24 import os
25 import logging
26 import logging as log
27
28 from backupninja import config
29 from backupninja import handlers
30
31 def run_all_actions(opts, global_config):
32     """
33     Run all defined actions.
34
35     opts are the parsed commandline options, global_config is the parsed
36     global configuration.
37     """
38     log.info('Running all actions')
39     try:
40         action_configs = config.list_actions(opts)
41     except OSError, e:
42         log.critical('Unable to list actions: %s', e)
43         return
44
45     action_configs.sort()
46
47     for action_config in action_configs:
48         run_action(action_config, opts, global_config)
49
50 def run_action(action_config, opts, global_config):
51     """
52     Run a single action. action_config is the full path to its
53     configuration file. opts are the parsed commandline options,
54     global_config is the parsed global configuration.
55     """
56     config_name = os.path.basename(action_config)
57     # Split the action filename
58     parts = os.path.basename(config_name).split('.')
59     if (len(parts) != 2):
60         log.error('Invalid action filename: "%s". Should be in the form name.type, where type is a valid handler.' % action_config)
61         return
62     (action_name, action_ty) = parts
63
64     action_log = logging.getLogger(config_name)
65     action_log.info('Running')
66
67     try:
68         # Create a handler for this action
69         action = handlers.create_action(action_ty, logger=action_log)
70         # Let the handler load its configuration file
71         action.load_config(action_config)
72         # Run it
73         action.run(test=opts.test)
74         action.finish(test=opts.test)
75         # Ask the action if there where any failures
76         success = not action.failed
77     except Exception, e:
78         action_log.error('Unexpected exception: %s', e)
79         import traceback
80         log.debug(traceback.format_exc())
81         success = False
82
83     if success:
84         action_log.info('Succeeded')
85     else:
86         action_log.info('Failed')