]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Python CLI Package: added basic reconfiguration stuff
authorMaria Matejka <mq@ucw.cz>
Sun, 21 May 2023 09:58:08 +0000 (11:58 +0200)
committerMaria Matejka <mq@ucw.cz>
Tue, 23 May 2023 11:45:04 +0000 (13:45 +0200)
python/BIRD/Actions.py [new file with mode: 0644]
python/BIRD/Basic.py
python/BIRD/__init__.py
python/test.py

diff --git a/python/BIRD/Actions.py b/python/BIRD/Actions.py
new file mode 100644 (file)
index 0000000..32ddf74
--- /dev/null
@@ -0,0 +1,40 @@
+from BIRD.Basic import Basic, Code
+
+class Actions(Basic):
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+
+    class ConfigureState:
+        def __init__(self, bird):
+            self.bird = bird
+            self.data = {}
+            self.configure_dispatch = {
+                    Code.ReadingConfiguration: self.reading,
+                    Code.Reconfigured: self.done,
+                    }
+
+        def reading(self, data):
+            if "reading_from" in self.data:
+                raise ActionException(f"Duplicit configuration file name in response: {data}")
+
+            if not data.startswith(pfx := "Reading configuration from "):
+                raise ActionException(f"Malformed configuration file name notice in response: {data}")
+
+            self.data["reading_from"] = data[len(pfx):]
+
+        def done(self, data):
+            if "done" in self.data:
+                raise ActionException(f"Reconfiguration finished twice")
+
+            self.data["done"] = True
+
+    async def configure(self):
+        await self.bird.cli.open()
+        data = await self.bird.cli.socket.command("configure")
+        state = self.ConfigureState(self.bird)
+
+        for line in data:
+            state.configure_dispatch[line["code"]](line["data"])
+
+        return state.data
+
index 01ff0d6f4b9e5f5ae1ac92708ee83d8f76ce8d3e..537a6f40eb00f843541986473b391cf03db26217 100644 (file)
@@ -27,6 +27,8 @@ class Basic:
 class Code:
     OK = 0
     Welcome = 1
+    ReadingConfiguration = 2
+    Reconfigured = 3
     Status = 13
     Version = 1000
     ProtocolInfo = 1002
index 5511be8949b87762a46986dd23cab687e06ffb4b..16b13595fa560ef435b40595776bed2a52f2b75b 100644 (file)
@@ -6,6 +6,7 @@ from BIRD.Basic import BIRDException
 from BIRD.Socket import Socket
 from BIRD.Status import Status, Version
 from BIRD.Protocol import ProtocolList
+from BIRD.Actions import Actions
 
 from BIRD.Config import Timestamp, ProtocolConfig, DeviceProtocolConfig
 
@@ -115,6 +116,7 @@ class BIRD:
         self.version = Version(bird=self)
         self.status = Status(bird=self)
         self.protocols = ProtocolList(bird=self)
+        self.actions = Actions(bird=self)
 
         self.within = False
 
index 67252d5f1449f8c564f916618aad0d312a161290..c303b3585acb14a51b475434cc453e8950c4e674 100644 (file)
@@ -17,4 +17,14 @@ async def main():
             for name, channel in protocol.channels.items():
                 print(f"  {name}: {channel.route_change_stats}")
 
+        print(await b.actions.configure())
+
+        await b.protocols.update()
+        print(b.protocols)
+
+        for name, protocol in b.protocols.data.items():
+            print(f"{name}: {protocol.channels}")
+            for name, channel in protocol.channels.items():
+                print(f"  {name}: {channel.route_change_stats}")
+
 asyncio.run(main())