summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2020-12-27 19:01:44 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2020-12-27 19:01:44 +0100
commit1dcf198c10c554ec43fd762d61f151fb8b04df80 (patch)
tree2169871f2a2d97ff6908e7ed2748f60a50ab259e
parentdd3669fc54269d4ab5e729006503f612fcbb0c80 (diff)
downloadrules-sandbox-1dcf198c10c554ec43fd762d61f151fb8b04df80.tar.gz
rules-sandbox-1dcf198c10c554ec43fd762d61f151fb8b04df80.tar.bz2
rules-sandbox-1dcf198c10c554ec43fd762d61f151fb8b04df80.tar.xz
rules-sandbox-1dcf198c10c554ec43fd762d61f151fb8b04df80.zip
properly classified into public and private containers
-rw-r--r--main.py95
1 files 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)