Blame SOURCES/abrt-2.1.7-run-the-autoreporting-event-from-abrt-action-notify.patch

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