From 1dcf198c10c554ec43fd762d61f151fb8b04df80 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 27 Dec 2020 19:01:44 +0100 Subject: properly classified into public and private containers --- main.py | 95 +++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index 11c4e32..60164f2 100644 --- a/main.py +++ b/main.py @@ -54,6 +54,12 @@ with ruleset("my-rules"): VimScore.declare_rules() VsOperations.declare_rules() + @when_all(+s.exception) + def second(c): + print("Processing failed!") + print(c.s["exception"]) + c.s.exception = None + @when_all(pri(1000), (m.type == 'dba-container')) def dba_container(c): print(f"dba-container: {c.m}") @@ -70,12 +76,6 @@ with ruleset("my-rules"): c.m.ports = [80] c.assert_fact(c.m) - @when_all(+s.exception) - def second(c): - print("Processing failed!") - print(c.s["exception"]) - c.s.exception = None - # The none() part doesn't work as is, but it is worked around with the try/except block. @when_all( (c.container << m.type == "dba-container"), @@ -99,36 +99,73 @@ with ruleset("my-rules"): # print(f"c.cluster: {c.cluster}") # pass -with ruleset("set-2"): - @when_all((m.type == "dba-container")) - def container(c): - print("container()..") - vimscoreCi = VimScore.make("ci", "development") vimscoreProduction = VimScore.make("production", "master") vsOps = VsOperations.make() -x = assert_fact("my-rules", vimscoreCi); print(f"x: {x}") -#x = assert_fact("my-rules", vimscoreProduction); print(f"x: {x}") +#x = assert_fact("my-rules", vimscoreCi); print(f"x: {x}") +x = assert_fact("my-rules", vimscoreProduction); print(f"x: {x}") #x = assert_fact("my-rules", vsOps); print(f"x: {x}") -print("Facts:") -for f in get_facts("my-rules"): - print(f"fact: {f}") +if True: + print("Facts:") + for f in get_facts("my-rules"): + print(f"fact: {f}") + + print("dba-clusters:") + for f in [f for f in get_facts("my-rules") if f["type"] == "dba-cluster"]: + cluster_name = f["name"] + + del f["name"] + print(f" cluster:") + print(f" name: {cluster_name}") + print(f" json: {f}") + + print(" dba-containers:") + for f in [f for f in get_facts("my-rules") if f.get("cluster") == cluster_name and f["type"] == "dba-container"]: + del f["cluster"] + del f["type"] + print(f" container: {f}") -print("dba-clusters:") -for f in [f for f in get_facts("my-rules") if f["type"] == "dba-cluster"]: - cluster_name = f["name"] +with ruleset("phase-2"): - del f["name"] - print(f" cluster:") - print(f" name: {cluster_name}") - print(f" json: {f}") + @when_all(+s.exception) + def second(c): + print("Processing failed!") + print(c.s["exception"]) + c.s.exception = None + +# @when_all(pri(100), m.type == "dba-cluster") +# def container(c): +# print(f"default: cluster: {c.m}") + + @when_all((m.type == "dba-container") & -m.ports_classified) + def mark_public_containers(c): +# print(f"marking container.. {c.m}") + item = c.m + c.retract_fact(item) + +# print(f"marking container.. {c.m.name}, ports={c.m.ports}") + item["public_ports"] = len(item.ports or []) > 0 + item["ports_classified"] = True +# print(f"marking container.. {item}") + c.assert_fact(item) + + @when_all(pri(50), + c.cluster << (m.type == "dba-cluster"), +# c.container << ((m.type == "dba-container") & (m.cluster == c.cluster.name) & m.ports.anyItem(item > 0)) + c.container << ((m.type == "dba-container") & +m.ports_classified & (m.public_ports > 0)) + ) + def container(c): + print(f"public container") + print(f" cluster: {c.cluster}") + print(f" container: {c.container}") + + @when_all(((m.type == "dba-container") & (+m.ports_classified) & (m.public_ports == 0))) + def container(c): + print(f"private container: {c.m}") - print(" dba-containers:") - for f in [f for f in get_facts("my-rules") if f.get("cluster") == cluster_name and f["type"] == "dba-container"]: - del f["cluster"] - del f["type"] - print(f" container: {f}") +print("PHASE 2") -#post("set-2", {"foo": "bar"}) +for f in [f for f in get_facts("my-rules") if f["type"] in ("dba-cluster", "dba-container")]: + assert_fact("phase-2", f) -- cgit v1.2.3