Blob Blame History Raw
From af5c169151f0697d34954ac5c9dd0686dc73ef3f Mon Sep 17 00:00:00 2001
From: Rob Crittenden <rcritten@redhat.com>
Date: Mon, 18 Nov 2019 15:13:44 -0500
Subject: [PATCH 5/5] Convert running healthchecks into a class and add
 pre-check

Add a pre-check so that dependencies can be checked per product.
In the case of IPA verify that the server is installed otherwise
bail out.
---
 src/ipahealthcheck/core/core.py | 168 +++++++++++++++++---------------
 src/ipahealthcheck/core/main.py |  17 +++-
 2 files changed, 104 insertions(+), 81 deletions(-)

diff --git a/src/ipahealthcheck/core/core.py b/src/ipahealthcheck/core/core.py
index 182eac3..aef6197 100644
--- a/src/ipahealthcheck/core/core.py
+++ b/src/ipahealthcheck/core/core.py
@@ -188,85 +188,97 @@ def limit_results(results, source, check):
     return new_results
 
 
-def run_healthcheck(entry_points, configfile):
-    framework = object()
-    plugins = []
-    output = constants.DEFAULT_OUTPUT
-
-    logger.setLevel(logging.INFO)
-
-    options = parse_options(output_registry)
-
-    if options.debug:
-        logger.setLevel(logging.DEBUG)
-
-    config = read_config(configfile)
-    if config is None:
-        return 1
-
-    for name, registry in find_registries(entry_points).items():
-        try:
-            registry.initialize(framework)
-        except Exception as e:
-            print("Unable to initialize %s: %s" % (name, e))
-            return 1
-        for plugin in find_plugins(name, registry):
-            plugins.append(plugin)
-
-    for out in output_registry.plugins:
-        if out.__name__.lower() == options.output:
-            output = out(options)
-
-    if options.list_sources:
-        return list_sources(plugins)
-
-    if options.infile:
-        try:
-            with open(options.infile, 'r') as f:
-                raw_data = f.read()
-
-            json_data = json.loads(raw_data)
-            results = json_to_results(json_data)
-            available = ()
-        except Exception as e:
-            print("Unable to import '%s': %s" % (options.infile, e))
+class RunChecks:
+    def __init__(self, entry_points, configfile):
+        self.entry_points = entry_points
+        self.configfile = configfile
+
+    def pre_check(self):
+        pass
+
+    def run_healthcheck(self):
+        framework = object()
+        plugins = []
+        output = constants.DEFAULT_OUTPUT
+    
+        logger.setLevel(logging.INFO)
+    
+        options = parse_options(output_registry)
+    
+        if options.debug:
+            logger.setLevel(logging.DEBUG)
+    
+        config = read_config(self.configfile)
+        if config is None:
             return 1
-        if options.source:
-            results = limit_results(results, options.source, options.check)
-    else:
-        results, available = run_service_plugins(plugins, config,
-                                                 options.source,
-                                                 options.check)
-        results.extend(run_plugins(plugins, config, available,
-                                   options.source, options.check))
-
-    if options.source and len(results.results) == 0:
-        for plugin in plugins:
-            if not source_or_check_matches(plugin, options.source,
-                                           options.check):
-                continue
 
-            if not set(plugin.requires).issubset(available):
-                print("Source '%s' is missing one or more requirements '%s'" %
-                      (options.source, ', '.join(plugin.requires)))
+        rval = self.pre_check()
+        if rval is not None:
+            return rval
+    
+        for name, registry in find_registries(self.entry_points).items():
+            try:
+                registry.initialize(framework)
+            except Exception as e:
+                print("Unable to initialize %s: %s" % (name, e))
                 return 1
-
-        if options.check:
-            print("Check '%s' not found in Source '%s'" %
-                  (options.check, options.source))
+            for plugin in find_plugins(name, registry):
+                plugins.append(plugin)
+    
+        for out in output_registry.plugins:
+            if out.__name__.lower() == options.output:
+                output = out(options)
+    
+        if options.list_sources:
+            return list_sources(plugins)
+    
+        if options.infile:
+            try:
+                with open(options.infile, 'r') as f:
+                    raw_data = f.read()
+    
+                json_data = json.loads(raw_data)
+                results = json_to_results(json_data)
+                available = ()
+            except Exception as e:
+                print("Unable to import '%s': %s" % (options.infile, e))
+                return 1
+            if options.source:
+                results = limit_results(results, options.source, options.check)
         else:
-            print("Source '%s' not found" % options.source)
-        return 1
-
-    try:
-        output.render(results)
-    except Exception as e:
-        logger.error('Output raised %s: %s', e.__class__.__name__, e)
-
-    return_value = 0
-    for result in results.results:
-        if result.result != constants.SUCCESS:
-            return_value = 1
-            break
-
-    return return_value
+            results, available = run_service_plugins(plugins, config,
+                                                     options.source,
+                                                     options.check)
+            results.extend(run_plugins(plugins, config, available,
+                                       options.source, options.check))
+    
+        if options.source and len(results.results) == 0:
+            for plugin in plugins:
+                if not source_or_check_matches(plugin, options.source,
+                                               options.check):
+                    continue
+    
+                if not set(plugin.requires).issubset(available):
+                    print("Source '%s' is missing one or more requirements '%s'" %
+                          (options.source, ', '.join(plugin.requires)))
+                    return 1
+    
+            if options.check:
+                print("Check '%s' not found in Source '%s'" %
+                      (options.check, options.source))
+            else:
+                print("Source '%s' not found" % options.source)
+            return 1
+    
+        try:
+            output.render(results)
+        except Exception as e:
+            logger.error('Output raised %s: %s', e.__class__.__name__, e)
+    
+        return_value = 0
+        for result in results.results:
+            if result.result != constants.SUCCESS:
+                return_value = 1
+                break
+    
+        return return_value
diff --git a/src/ipahealthcheck/core/main.py b/src/ipahealthcheck/core/main.py
index d9e85d7..63a1de6 100644
--- a/src/ipahealthcheck/core/main.py
+++ b/src/ipahealthcheck/core/main.py
@@ -6,12 +6,23 @@ from os import environ
 import sys
 
 from ipahealthcheck.core import constants
-from ipahealthcheck.core.core import run_healthcheck
+from ipahealthcheck.core.core import RunChecks
+
+from ipaserver.install.installutils import is_ipa_configured
+
+
+class IPAChecks(RunChecks):
+    def pre_check(self):
+        if not is_ipa_configured():
+            print("IPA is not configured")
+            return 1
 
 
 def main():
     environ["KRB5_CLIENT_KTNAME"] = "/etc/krb5.keytab"
     environ["KRB5CCNAME"] = "MEMORY:"
 
-    sys.exit(run_healthcheck(['ipahealthcheck.registry'],
-                             constants.CONFIG_FILE))
+    ipachecks = IPAChecks(['ipahealthcheck.registry',
+                           'pkihealthcheck.registry'],
+                          constants.CONFIG_FILE)
+    sys.exit(ipachecks.run_healthcheck())
-- 
2.20.1