Repository: RedHatInsights/insights-core

Issue:
cluster rules execute during non-cluster analysis
If cluster rules are loaded during an evaluation like `insights-run -p my_rules`, they are treated as regular rules instead of cluster rules.


Discussion:

Resolving patch:
diff --git a/insights/__init__.py b/insights/__init__.py
--- a/insights/__init__.py
+++ b/insights/__init__.py
@@ -85,12 +85,13 @@ def process_dir(broker, root, graph, context, inventory=None):
     if isinstance(ctx, ClusterArchiveContext):
         from .core.cluster import process_cluster
         archives = [f for f in ctx.all_files if f.endswith(COMPRESSION_TYPES)]
-        return process_cluster(archives, broker=broker, inventory=inventory)
+        return process_cluster(graph, archives, broker=broker, inventory=inventory)
 
     broker[ctx.__class__] = ctx
     if isinstance(ctx, SerializedArchiveContext):
         h = Hydration(ctx.root)
         broker = h.hydrate(broker=broker)
+    graph = dict((k, v) for k, v in graph.items() if k in dr.COMPONENTS[dr.GROUPS.single])
     broker = dr.run(graph, broker=broker)
     return broker
 
@@ -117,6 +118,7 @@ def _run(broker, graph=None, root=None, context=None, inventory=None):
     if not root:
         context = context or HostContext
         broker[context] = context()
+        graph = dict((k, v) for k, v in graph.items() if k in dr.COMPONENTS[dr.GROUPS.single])
         return dr.run(graph, broker=broker)
 
     if os.path.isdir(root):
diff --git a/insights/core/cluster.py b/insights/core/cluster.py
--- a/insights/core/cluster.py
+++ b/insights/core/cluster.py
@@ -44,13 +44,13 @@ def attach_machine_id(result, mid):
     return result
 
 
-def process_archives(archives):
+def process_archives(graph, archives):
     for archive in archives:
         with extract(archive) as ex:
             ctx = create_context(ex.tmp_dir)
             broker = dr.Broker()
             broker[ctx.__class__] = ctx
-            yield dr.run(broker=broker)
+            yield dr.run(graph, broker=broker)
 
 
 def extract_facts(brokers):
@@ -66,18 +66,22 @@ def extract_facts(brokers):
     return results
 
 
-def process_facts(facts, meta, broker):
+def process_facts(facts, meta, broker, cluster_graph):
     broker[ClusterMeta] = meta
     for k, v in facts.items():
         broker[k] = pd.DataFrame(v)
-    return dr.run(dr.COMPONENTS[dr.GROUPS.cluster], broker=broker)
+    return dr.run(cluster_graph, broker=broker)
 
 
-def process_cluster(archives, broker, inventory=None):
+def process_cluster(graph, archives, broker, inventory=None):
+    host_graph = dict((k, v) for k, v in graph.items() if k in dr.COMPONENTS[dr.GROUPS.single])
+    host_graph[machine_id] = dr.DELEGATES[machine_id].dependencies
+    cluster_graph = dict((k, v) for k, v in graph.items() if k not in host_graph)
+
     inventory = parse_inventory(inventory) if inventory else {}
 
-    brokers = process_archives(archives)
+    brokers = process_archives(host_graph, archives)
     facts = extract_facts(brokers)
     meta = ClusterMeta(len(archives), inventory)
 
-    return process_facts(facts, meta, broker)
+    return process_facts(facts, meta, broker, cluster_graph)
diff --git a/insights/core/dr.py b/insights/core/dr.py
--- a/insights/core/dr.py
+++ b/insights/core/dr.py
@@ -947,7 +947,9 @@ def run(components=None, broker=None):
     for component in run_order(components):
         start = time.time()
         try:
-            if component not in broker and component in DELEGATES and is_enabled(component):
+            if (component not in broker and component in components and
+               component in DELEGATES and
+               is_enabled(component)):
                 log.info("Trying %s" % get_name(component))
                 result = DELEGATES[component].process(broker)
                 broker[component] = result


Test patch: