Blame SOURCES/0005-Convert-running-healthchecks-into-a-class-and-add-pr.patch

43cbd4
From af5c169151f0697d34954ac5c9dd0686dc73ef3f Mon Sep 17 00:00:00 2001
43cbd4
From: Rob Crittenden <rcritten@redhat.com>
43cbd4
Date: Mon, 18 Nov 2019 15:13:44 -0500
43cbd4
Subject: [PATCH 5/5] Convert running healthchecks into a class and add
43cbd4
 pre-check
43cbd4
43cbd4
Add a pre-check so that dependencies can be checked per product.
43cbd4
In the case of IPA verify that the server is installed otherwise
43cbd4
bail out.
43cbd4
---
43cbd4
 src/ipahealthcheck/core/core.py | 168 +++++++++++++++++---------------
43cbd4
 src/ipahealthcheck/core/main.py |  17 +++-
43cbd4
 2 files changed, 104 insertions(+), 81 deletions(-)
43cbd4
43cbd4
diff --git a/src/ipahealthcheck/core/core.py b/src/ipahealthcheck/core/core.py
43cbd4
index 182eac3..aef6197 100644
43cbd4
--- a/src/ipahealthcheck/core/core.py
43cbd4
+++ b/src/ipahealthcheck/core/core.py
43cbd4
@@ -188,85 +188,97 @@ def limit_results(results, source, check):
43cbd4
     return new_results
43cbd4
 
43cbd4
 
43cbd4
-def run_healthcheck(entry_points, configfile):
43cbd4
-    framework = object()
43cbd4
-    plugins = []
43cbd4
-    output = constants.DEFAULT_OUTPUT
43cbd4
-
43cbd4
-    logger.setLevel(logging.INFO)
43cbd4
-
43cbd4
-    options = parse_options(output_registry)
43cbd4
-
43cbd4
-    if options.debug:
43cbd4
-        logger.setLevel(logging.DEBUG)
43cbd4
-
43cbd4
-    config = read_config(configfile)
43cbd4
-    if config is None:
43cbd4
-        return 1
43cbd4
-
43cbd4
-    for name, registry in find_registries(entry_points).items():
43cbd4
-        try:
43cbd4
-            registry.initialize(framework)
43cbd4
-        except Exception as e:
43cbd4
-            print("Unable to initialize %s: %s" % (name, e))
43cbd4
-            return 1
43cbd4
-        for plugin in find_plugins(name, registry):
43cbd4
-            plugins.append(plugin)
43cbd4
-
43cbd4
-    for out in output_registry.plugins:
43cbd4
-        if out.__name__.lower() == options.output:
43cbd4
-            output = out(options)
43cbd4
-
43cbd4
-    if options.list_sources:
43cbd4
-        return list_sources(plugins)
43cbd4
-
43cbd4
-    if options.infile:
43cbd4
-        try:
43cbd4
-            with open(options.infile, 'r') as f:
43cbd4
-                raw_data = f.read()
43cbd4
-
43cbd4
-            json_data = json.loads(raw_data)
43cbd4
-            results = json_to_results(json_data)
43cbd4
-            available = ()
43cbd4
-        except Exception as e:
43cbd4
-            print("Unable to import '%s': %s" % (options.infile, e))
43cbd4
+class RunChecks:
43cbd4
+    def __init__(self, entry_points, configfile):
43cbd4
+        self.entry_points = entry_points
43cbd4
+        self.configfile = configfile
43cbd4
+
43cbd4
+    def pre_check(self):
43cbd4
+        pass
43cbd4
+
43cbd4
+    def run_healthcheck(self):
43cbd4
+        framework = object()
43cbd4
+        plugins = []
43cbd4
+        output = constants.DEFAULT_OUTPUT
43cbd4
+    
43cbd4
+        logger.setLevel(logging.INFO)
43cbd4
+    
43cbd4
+        options = parse_options(output_registry)
43cbd4
+    
43cbd4
+        if options.debug:
43cbd4
+            logger.setLevel(logging.DEBUG)
43cbd4
+    
43cbd4
+        config = read_config(self.configfile)
43cbd4
+        if config is None:
43cbd4
             return 1
43cbd4
-        if options.source:
43cbd4
-            results = limit_results(results, options.source, options.check)
43cbd4
-    else:
43cbd4
-        results, available = run_service_plugins(plugins, config,
43cbd4
-                                                 options.source,
43cbd4
-                                                 options.check)
43cbd4
-        results.extend(run_plugins(plugins, config, available,
43cbd4
-                                   options.source, options.check))
43cbd4
-
43cbd4
-    if options.source and len(results.results) == 0:
43cbd4
-        for plugin in plugins:
43cbd4
-            if not source_or_check_matches(plugin, options.source,
43cbd4
-                                           options.check):
43cbd4
-                continue
43cbd4
 
43cbd4
-            if not set(plugin.requires).issubset(available):
43cbd4
-                print("Source '%s' is missing one or more requirements '%s'" %
43cbd4
-                      (options.source, ', '.join(plugin.requires)))
43cbd4
+        rval = self.pre_check()
43cbd4
+        if rval is not None:
43cbd4
+            return rval
43cbd4
+    
43cbd4
+        for name, registry in find_registries(self.entry_points).items():
43cbd4
+            try:
43cbd4
+                registry.initialize(framework)
43cbd4
+            except Exception as e:
43cbd4
+                print("Unable to initialize %s: %s" % (name, e))
43cbd4
                 return 1
43cbd4
-
43cbd4
-        if options.check:
43cbd4
-            print("Check '%s' not found in Source '%s'" %
43cbd4
-                  (options.check, options.source))
43cbd4
+            for plugin in find_plugins(name, registry):
43cbd4
+                plugins.append(plugin)
43cbd4
+    
43cbd4
+        for out in output_registry.plugins:
43cbd4
+            if out.__name__.lower() == options.output:
43cbd4
+                output = out(options)
43cbd4
+    
43cbd4
+        if options.list_sources:
43cbd4
+            return list_sources(plugins)
43cbd4
+    
43cbd4
+        if options.infile:
43cbd4
+            try:
43cbd4
+                with open(options.infile, 'r') as f:
43cbd4
+                    raw_data = f.read()
43cbd4
+    
43cbd4
+                json_data = json.loads(raw_data)
43cbd4
+                results = json_to_results(json_data)
43cbd4
+                available = ()
43cbd4
+            except Exception as e:
43cbd4
+                print("Unable to import '%s': %s" % (options.infile, e))
43cbd4
+                return 1
43cbd4
+            if options.source:
43cbd4
+                results = limit_results(results, options.source, options.check)
43cbd4
         else:
43cbd4
-            print("Source '%s' not found" % options.source)
43cbd4
-        return 1
43cbd4
-
43cbd4
-    try:
43cbd4
-        output.render(results)
43cbd4
-    except Exception as e:
43cbd4
-        logger.error('Output raised %s: %s', e.__class__.__name__, e)
43cbd4
-
43cbd4
-    return_value = 0
43cbd4
-    for result in results.results:
43cbd4
-        if result.result != constants.SUCCESS:
43cbd4
-            return_value = 1
43cbd4
-            break
43cbd4
-
43cbd4
-    return return_value
43cbd4
+            results, available = run_service_plugins(plugins, config,
43cbd4
+                                                     options.source,
43cbd4
+                                                     options.check)
43cbd4
+            results.extend(run_plugins(plugins, config, available,
43cbd4
+                                       options.source, options.check))
43cbd4
+    
43cbd4
+        if options.source and len(results.results) == 0:
43cbd4
+            for plugin in plugins:
43cbd4
+                if not source_or_check_matches(plugin, options.source,
43cbd4
+                                               options.check):
43cbd4
+                    continue
43cbd4
+    
43cbd4
+                if not set(plugin.requires).issubset(available):
43cbd4
+                    print("Source '%s' is missing one or more requirements '%s'" %
43cbd4
+                          (options.source, ', '.join(plugin.requires)))
43cbd4
+                    return 1
43cbd4
+    
43cbd4
+            if options.check:
43cbd4
+                print("Check '%s' not found in Source '%s'" %
43cbd4
+                      (options.check, options.source))
43cbd4
+            else:
43cbd4
+                print("Source '%s' not found" % options.source)
43cbd4
+            return 1
43cbd4
+    
43cbd4
+        try:
43cbd4
+            output.render(results)
43cbd4
+        except Exception as e:
43cbd4
+            logger.error('Output raised %s: %s', e.__class__.__name__, e)
43cbd4
+    
43cbd4
+        return_value = 0
43cbd4
+        for result in results.results:
43cbd4
+            if result.result != constants.SUCCESS:
43cbd4
+                return_value = 1
43cbd4
+                break
43cbd4
+    
43cbd4
+        return return_value
43cbd4
diff --git a/src/ipahealthcheck/core/main.py b/src/ipahealthcheck/core/main.py
43cbd4
index d9e85d7..63a1de6 100644
43cbd4
--- a/src/ipahealthcheck/core/main.py
43cbd4
+++ b/src/ipahealthcheck/core/main.py
43cbd4
@@ -6,12 +6,23 @@ from os import environ
43cbd4
 import sys
43cbd4
 
43cbd4
 from ipahealthcheck.core import constants
43cbd4
-from ipahealthcheck.core.core import run_healthcheck
43cbd4
+from ipahealthcheck.core.core import RunChecks
43cbd4
+
43cbd4
+from ipaserver.install.installutils import is_ipa_configured
43cbd4
+
43cbd4
+
43cbd4
+class IPAChecks(RunChecks):
43cbd4
+    def pre_check(self):
43cbd4
+        if not is_ipa_configured():
43cbd4
+            print("IPA is not configured")
43cbd4
+            return 1
43cbd4
 
43cbd4
 
43cbd4
 def main():
43cbd4
     environ["KRB5_CLIENT_KTNAME"] = "/etc/krb5.keytab"
43cbd4
     environ["KRB5CCNAME"] = "MEMORY:"
43cbd4
 
43cbd4
-    sys.exit(run_healthcheck(['ipahealthcheck.registry'],
43cbd4
-                             constants.CONFIG_FILE))
43cbd4
+    ipachecks = IPAChecks(['ipahealthcheck.registry',
43cbd4
+                           'pkihealthcheck.registry'],
43cbd4
+                          constants.CONFIG_FILE)
43cbd4
+    sys.exit(ipachecks.run_healthcheck())
43cbd4
-- 
43cbd4
2.20.1
43cbd4