0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 20:58:44 +00:00

Python CLI Package: added basic reconfiguration stuff

This commit is contained in:
Maria Matejka 2023-05-21 11:58:08 +02:00
parent 1fa3226b0b
commit 0f0f8166d4
4 changed files with 54 additions and 0 deletions

40
python/BIRD/Actions.py Normal file
View File

@ -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

View File

@ -27,6 +27,8 @@ class Basic:
class Code: class Code:
OK = 0 OK = 0
Welcome = 1 Welcome = 1
ReadingConfiguration = 2
Reconfigured = 3
Status = 13 Status = 13
Version = 1000 Version = 1000
ProtocolInfo = 1002 ProtocolInfo = 1002

View File

@ -6,6 +6,7 @@ from BIRD.Basic import BIRDException
from BIRD.Socket import Socket from BIRD.Socket import Socket
from BIRD.Status import Status, Version from BIRD.Status import Status, Version
from BIRD.Protocol import ProtocolList from BIRD.Protocol import ProtocolList
from BIRD.Actions import Actions
from BIRD.Config import Timestamp, ProtocolConfig, DeviceProtocolConfig from BIRD.Config import Timestamp, ProtocolConfig, DeviceProtocolConfig
@ -115,6 +116,7 @@ class BIRD:
self.version = Version(bird=self) self.version = Version(bird=self)
self.status = Status(bird=self) self.status = Status(bird=self)
self.protocols = ProtocolList(bird=self) self.protocols = ProtocolList(bird=self)
self.actions = Actions(bird=self)
self.within = False self.within = False

View File

@ -17,4 +17,14 @@ async def main():
for name, channel in protocol.channels.items(): for name, channel in protocol.channels.items():
print(f" {name}: {channel.route_change_stats}") 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()) asyncio.run(main())