From 6dca28918be80a3ef0c2959a99067337f59f5972 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 14 Nov 2023 14:14:44 +0100 Subject: wip --- 6/bgp.pl | 13 ++----------- 6/bgp.py | 23 +++++++++++++++++++++++ 6/group_vars/hash/bgp.yaml | 11 +++++++++++ 6/group_vars/knot/bgp.yaml | 11 +++++++++++ 6/group_vars/kv24ix/bgp.yaml | 8 ++++++++ 6/group_vars/lhn2ix/bgp.yaml | 8 ++++++++ requirements.txt | 2 ++ 7 files changed, 65 insertions(+), 11 deletions(-) create mode 100755 6/bgp.py create mode 100644 6/group_vars/hash/bgp.yaml create mode 100644 6/group_vars/knot/bgp.yaml create mode 100644 6/group_vars/kv24ix/bgp.yaml create mode 100644 6/group_vars/lhn2ix/bgp.yaml create mode 100644 requirements.txt diff --git a/6/bgp.pl b/6/bgp.pl index d9b448b..6d16500 100644 --- a/6/bgp.pl +++ b/6/bgp.pl @@ -84,15 +84,6 @@ to_dict(neighbor(_, Remote), Dict) :- host_config(Remote, RC), } }. -routers_entry(router(R), Dict) :- - D = to_dict(router(R)), - pairs_keys_values(Data, [R], [D]), - dict_create(Dict, _, Data). - -bird_config(Dict) :- +bird_config(RouterDicts) :- routers(Routers), - maplist(to_dict(), Routers, RouterDicts), -% maplist(routers_entry(), Routers, RouterDicts), - Dict = _{routers:RouterDicts}. -% findall(neighbor(H, R), (host(H), host(R), neighbor(H, R)), Routers), -% yaml_write(current_output, _{neighbors:BgpConnectionDicts}). + maplist(to_dict(), Routers, RouterDicts). diff --git a/6/bgp.py b/6/bgp.py new file mode 100755 index 0000000..5096b67 --- /dev/null +++ b/6/bgp.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +from swiplserver import PrologMQI, PrologThread +import yaml +import os + +def to_ansible(kind, objs): + for o in objs: + host = o['host'] + print(f"k={host}") + os.makedirs(f"group_vars/{host}", exist_ok=True) + with open(f"group_vars/{host}/{kind}.yaml", "w") as f: + f.write(yaml.dump(o)) + +with PrologMQI() as mqi: + with mqi.create_thread() as p: + result = p.query("consult(main)") + print(result) + + result = p.query("bgp:bird_config(BirdDict)") + r = result[0]["BirdDict"] + print(yaml.dump(r)) + to_ansible("bgp", r) diff --git a/6/group_vars/hash/bgp.yaml b/6/group_vars/hash/bgp.yaml new file mode 100644 index 0000000..e479515 --- /dev/null +++ b/6/group_vars/hash/bgp.yaml @@ -0,0 +1,11 @@ +host: hash +neighbors: +- neighbor: + hostname: fdf3:aad9:a885:0b3a::1 + name: knot +- neighbor: + hostname: fdf3:aad9:a885:0b3a::16 + name: kv24ix +- neighbor: + hostname: fdf3:aad9:a885:0b3a::15 + name: lhn2ix diff --git a/6/group_vars/knot/bgp.yaml b/6/group_vars/knot/bgp.yaml new file mode 100644 index 0000000..808e2cf --- /dev/null +++ b/6/group_vars/knot/bgp.yaml @@ -0,0 +1,11 @@ +host: knot +neighbors: +- neighbor: + hostname: fdf3:aad9:a885:0b3a::13 + name: hash +- neighbor: + hostname: fdf3:aad9:a885:0b3a::16 + name: kv24ix +- neighbor: + hostname: fdf3:aad9:a885:0b3a::15 + name: lhn2ix diff --git a/6/group_vars/kv24ix/bgp.yaml b/6/group_vars/kv24ix/bgp.yaml new file mode 100644 index 0000000..4f69f7c --- /dev/null +++ b/6/group_vars/kv24ix/bgp.yaml @@ -0,0 +1,8 @@ +host: kv24ix +neighbors: +- neighbor: + hostname: fdf3:aad9:a885:0b3a::13 + name: hash +- neighbor: + hostname: fdf3:aad9:a885:0b3a::1 + name: knot diff --git a/6/group_vars/lhn2ix/bgp.yaml b/6/group_vars/lhn2ix/bgp.yaml new file mode 100644 index 0000000..73732ae --- /dev/null +++ b/6/group_vars/lhn2ix/bgp.yaml @@ -0,0 +1,8 @@ +host: lhn2ix +neighbors: +- neighbor: + hostname: fdf3:aad9:a885:0b3a::13 + name: hash +- neighbor: + hostname: fdf3:aad9:a885:0b3a::1 + name: knot diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ba12eb2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +swiplserver==1.0.2 +pyyaml==6.0.1 -- cgit v1.2.3