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

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