From 1cf434cb395b6b06297281427a47aeebe224b436 Mon Sep 17 00:00:00 2001 From: Jakub Filak Date: Tue, 12 Nov 2013 16:39:08 +0100 Subject: [ABRT PATCH 71/76] run the autoreporting event from abrt-action-notify If AutoreportingEnabled option from abrt.conf holds "yes", then abrt-action-notify runs AutoreportingEvent from abrt.conf. Related to #750 Signed-off-by: Jakub Filak --- doc/abrt-action-notify.txt | 30 +++++++++++++- src/plugins/abrt-action-notify | 92 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/doc/abrt-action-notify.txt b/doc/abrt-action-notify.txt index c5bd7b0..56a456c 100644 --- a/doc/abrt-action-notify.txt +++ b/doc/abrt-action-notify.txt @@ -7,7 +7,7 @@ abrt-action-notify - Announces a new occurrence of problem via all accessible ch SYNOPSIS -------- -'abrt-action-notify' [-h] [-d PROBLEM_DIR] +'abrt-action-notify' [-h] -d PROBLEM_DIR [-v] [-a] [-e AUTOREPORTING_EVENT] DESCRIPTION ----------- @@ -27,12 +27,40 @@ EVENT=notify package!= OPTIONS ------- +-v, --verbose:: + Be verbose + -d, --problem-dir PROBLEM_DIR:: Problem directory [Default: current directory] -h, --help:: Show help message +-a, --autoreporting:: + Force to run autoreporting event + +-e, --autoreporting-event AUTOREPORTING_EVENT:: + Overwrite autoreporting event name + +ENVIRONMENT +---------- +ABRT_VERBOSE:: + ABRT verbosity level + +FILES +----- +/etc/abrt/abrt.conf + +AutoreportingEnabled:: + If enabled, abrt-action-notify runs AutoreportingEvent + +AutoreportingEvent:: + Name of event to be run if autoreporting is enabled + +SEE ALSO +-------- +abrt.conf(5) + AUTHORS ------- * ABRT team diff --git a/src/plugins/abrt-action-notify b/src/plugins/abrt-action-notify index 722d7f6..aa12105 100644 --- a/src/plugins/abrt-action-notify +++ b/src/plugins/abrt-action-notify @@ -20,9 +20,15 @@ from argparse import ArgumentParser import dbus import dbus.lowlevel +import problem + import report from reportclient import (RETURN_OK, - RETURN_FAILURE) + RETURN_FAILURE, + RETURN_CANCEL_BY_USER, + RETURN_STOP_EVENT_RUN, + log1, + set_verbosity) CD_DUMPDIR = "Directory" FILENAME_PACKAGE = "package" @@ -30,6 +36,39 @@ FILENAME_UID = "uid" FILENAME_UUID = "uuid" FILENAME_DUPHASH = "duphash" + +def run_autoreport(problem_data, event_name): + """Runs autoreporting event + + Requires CD_DUMPDIR key in problem_data. + + Keyword arguments: + problem_data -- problem data of notified problems + + Returns None as it raises an exception on error + + Raises: + KeyError -- if any of required elements is missing + RuntimeError -- if event run fails + """ + + dir_name = problem_data.get(CD_DUMPDIR) + if dir_name is None: + raise KeyError(CD_DUMPDIR) + + log1("Running autoreporting event: '{0}'".format(event_name)) + + res = report.run_event_state() + ret = res.run_event_on_dir_name(dir_name[0], event_name) + + if res.children_count == 0 and ret == 0: + raise RuntimeError("No processing is specified for event '{0}'" + .format(event_name)) + + if not ret in [RETURN_OK, RETURN_CANCEL_BY_USER, RETURN_STOP_EVENT_RUN]: + raise RuntimeError("Event '{0}' exited with {1}" + .format(event_name, ret)) + def emit_crash_dbus_signal(problem_data): """Emits a Crash signal on D-Bus Problem bus @@ -132,15 +171,43 @@ def build_notification_problem_data(problem_dir): if __name__ == "__main__": CMDARGS = ArgumentParser( description=("Announce a new or duplicated problem via" - " all accessible channels")) + " all accessible channels"), + epilog=("Reads the default configuration from 'abrt.conf' file")) CMDARGS.add_argument("-d", "--problem-dir", type=str, required=True, help="An absolute path to a new or duplicated problem directory") + CMDARGS.add_argument("-v", "--verbose", + action="count", dest="verbose", default=0, + help="Be verbose") + CMDARGS.add_argument("-a", "--autoreporting", + action="store_true", dest="autoreporting", default=False, + help="Force to run autoreporting event") + CMDARGS.add_argument("-e", "--autoreporting-event", + type=str, dest="autoreporting_event", + help="Overwrite autoreporting event name") OPTIONS = CMDARGS.parse_args() DIR_PATH = OPTIONS.problem_dir + verbose = 0 + ABRT_VERBOSE = os.getenv("ABRT_VERBOSE") + if ABRT_VERBOSE: + try: + verbose = int(ABRT_VERBOSE) + except: + pass + + verbose += OPTIONS.verbose + set_verbosity(verbose) + os.environ["ABRT_VERBOSE"] = str(verbose) + + try: + conf = problem.load_conf_file("abrt.conf") + except OSError as ex: + sys.stderr.write("{0}".format(str(ex))) + sys.exit(RETURN_FAILURE) + try: PD = build_notification_problem_data(DIR_PATH) except ValueError as ex: @@ -160,5 +227,26 @@ if __name__ == "__main__": .format(ex.message)) sys.exit(RETURN_FAILURE) + if OPTIONS.autoreporting or conf.get("AutoreportingEnabled", "no") == "yes": + event_name = OPTIONS.autoreporting_event + if not event_name: + if "AutoreportingEvent" in conf: + event_name = conf["AutoreportingEvent"] + else: + sys.stderr.write("Autoreporting event is not configured") + sys.stderr.exit(RETURN_FAILURE) + + try: + run_autoreport(PD, event_name) + except RuntimeError as ex: + sys.stderr.write("Cannot notify '{0}': {1}\n". + format(DIR_PATH, ex.message)) + sys.exit(RETURN_FAILURE) + except KeyError as ex: + # this is a bug in build_notification_problem_data() + sys.stderr.write("BUG: problem data misses required element '{0}'" + .format(ex.message)) + sys.exit(RETURN_FAILURE) + sys.exit(RETURN_OK) -- 1.8.3.1