From 01bcfd863828ba96c818a89894f09962e74edf13 Mon Sep 17 00:00:00 2001 From: cronekorkn Date: Mon, 23 Sep 2024 20:32:12 +0200 Subject: [PATCH] dhcp from interface --- bundles/kea-dhcpd/metadata.py | 75 +++++++++++++++++++++++++--- bundles/macbook/files/macbook-update | 2 +- bundles/network/metadata.py | 27 ++++++++++ bundles/telegraf/items.py | 2 +- nodes/home.homeassistant.py | 1 + nodes/home.homematic.py | 1 + nodes/home.hue.py | 8 ++- nodes/home.router.py | 42 ++-------------- 8 files changed, 108 insertions(+), 50 deletions(-) diff --git a/bundles/kea-dhcpd/metadata.py b/bundles/kea-dhcpd/metadata.py index 860c04e..2e27323 100644 --- a/bundles/kea-dhcpd/metadata.py +++ b/bundles/kea-dhcpd/metadata.py @@ -1,3 +1,8 @@ +from ipaddress import ip_interface, ip_network + +hashable = repo.libs.hashable.hashable + + defaults = { 'apt': { 'packages': { @@ -7,15 +12,15 @@ defaults = { 'kea': { 'Dhcp4': { 'interfaces-config': { - 'interfaces': [], + 'interfaces': set(), }, 'lease-database': { 'type': 'memfile', 'lfc-interval': 3600 }, - 'subnet4': [], - 'loggers': [ - { + 'subnet4': set(), + 'loggers': set([ + hashable({ 'name': 'kea-dhcp4', 'output_options': [ { @@ -23,15 +28,69 @@ defaults = { } ], 'severity': 'INFO', - }, - ], + }), + ]), }, }, } @metadata_reactor.provides( - + 'kea/Dhcp4/interfaces-config/interfaces', + 'kea/Dhcp4/subnet4', ) def subnets(metadata): - pass + subnet4 = set() + interfaces = set() + reservations = set( + hashable({ + 'hw-address': network_conf['mac'], + 'ip-address': str(ip_interface(network_conf['ipv4']).ip), + }) + for other_node in repo.nodes + for network_conf in other_node.metadata.get('network', {}).values() + if 'mac' in network_conf + ) + + for network_name, network_conf in metadata.get('network').items(): + dhcp_server_config = network_conf.get('dhcp_server_config', None) + + if dhcp_server_config: + _network = ip_network(dhcp_server_config['subnet']) + + subnet4.add(hashable({ + 'subnet': dhcp_server_config['subnet'], + 'pools': [ + { + 'pool': f'{dhcp_server_config['pool_from']} - {dhcp_server_config['pool_to']}', + }, + ], + 'option-data': [ + { + 'name': 'routers', + 'data': dhcp_server_config['router'], + }, + { + 'name': 'domain-name-servers', + 'data': '10.0.10.2', + }, + ], + 'reservations': set( + reservation + for reservation in reservations + if ip_interface(reservation['ip-address']).ip in _network + ), + })) + + interfaces.add(network_conf.get('interface', network_name)) + + return { + 'kea': { + 'Dhcp4': { + 'interfaces-config': { + 'interfaces': interfaces, + }, + 'subnet4': subnet4, + }, + }, + } diff --git a/bundles/macbook/files/macbook-update b/bundles/macbook/files/macbook-update index 5673b17..395fa4f 100644 --- a/bundles/macbook/files/macbook-update +++ b/bundles/macbook/files/macbook-update @@ -18,7 +18,7 @@ git -C ~/.zsh/oh-my-zsh pull brew upgrade brew upgrade --cask --greedy -pyenv install --keep-existing +pyenv install --skip-existing sudo softwareupdate -ia --verbose diff --git a/bundles/network/metadata.py b/bundles/network/metadata.py index d9b51e6..8e8adbd 100644 --- a/bundles/network/metadata.py +++ b/bundles/network/metadata.py @@ -5,6 +5,33 @@ defaults = { } +@metadata_reactor.provides( + 'network', +) +def dhcp(metadata): + networks = {} + + for network_name, network_conf in metadata.get('network').items(): + _interface = ip_interface(network_conf['ipv4']) + _ip = _interface.ip + _network = _interface.network + _hosts = list(_network.hosts()) + + if network_conf.get('dhcp_server', False): + networks[network_name] = { + 'dhcp_server_config': { + 'subnet': str(_network), + 'pool_from': str(_hosts[len(_hosts)//2]), + 'pool_to': str(_hosts[-3]), + 'router': str(_ip), + 'domain-name-servers': str(_ip), + } + } + return { + 'network': networks, + } + + @metadata_reactor.provides( 'systemd/units', ) diff --git a/bundles/telegraf/items.py b/bundles/telegraf/items.py index 6974535..e674846 100644 --- a/bundles/telegraf/items.py +++ b/bundles/telegraf/items.py @@ -9,7 +9,7 @@ files = { node.metadata.get('telegraf/config'), cls=MetadataJSONEncoder, )), - sort_keys=True + sort_keys=True, ), 'triggers': [ 'svc_systemd:telegraf:restart', diff --git a/nodes/home.homeassistant.py b/nodes/home.homeassistant.py index 01516f1..ca03dc2 100644 --- a/nodes/home.homeassistant.py +++ b/nodes/home.homeassistant.py @@ -29,6 +29,7 @@ 'internal': { 'interface': 'eth0', 'ipv4': '10.0.0.16/24', + 'mac': 'd8:3a:dd:16:fc:9d', 'gateway4': '10.0.0.1', }, }, diff --git a/nodes/home.homematic.py b/nodes/home.homematic.py index 5b9357e..b716d3f 100644 --- a/nodes/home.homematic.py +++ b/nodes/home.homematic.py @@ -14,6 +14,7 @@ 'network': { 'internal': { 'ipv4': '10.0.2.8/24', + 'mac': 'b8:27:eb:15:30:86', }, }, 'dns': { diff --git a/nodes/home.hue.py b/nodes/home.hue.py index c1df787..0470bf3 100644 --- a/nodes/home.hue.py +++ b/nodes/home.hue.py @@ -5,7 +5,13 @@ 'home', ], 'metadata': { - 'id': '', + 'id': '87879bc1-130f-4fca-a8d2-e1d93a794df4', + 'network': { + 'internal': { + 'ipv4': '10.0.2.100/24', + 'mac': '00:17:88:67:e7:f2', + }, + }, 'dns': { 'hue.ckn.li': { 'A': {'10.0.2.100'}, diff --git a/nodes/home.router.py b/nodes/home.router.py index 8dbc32d..57fffba 100644 --- a/nodes/home.router.py +++ b/nodes/home.router.py @@ -18,6 +18,7 @@ 'interface': 'eno1', 'ipv4': '10.0.0.1/24', 'vlans': {'iot', 'internet', 'guest'}, + 'dhcp_server': True, }, 'temp': { 'interface': 'enx00e04c220682', @@ -28,6 +29,7 @@ 'type': 'vlan', 'id': 2, 'ipv4': '10.0.2.1/24', + 'dhcp_server': True, }, 'internet': { 'type': 'vlan', @@ -38,45 +40,7 @@ 'type': 'vlan', 'id': 9, 'ipv4': '10.0.9.1/24', - }, - }, - 'kea': { - 'Dhcp4': { - 'interfaces-config': { - 'interfaces': ['eno1', 'iot', 'guest'], - }, - 'subnet4': [ - { - 'subnet': '10.0.0.0/24', - 'pools': [ - { 'pool': '10.0.0.100 - 10.0.0.200' }, - ], - 'option-data': [ - { 'name': 'routers', 'data': '10.0.0.1' }, - { 'name': 'domain-name-servers', 'data': '10.0.10.2' }, - ], - }, - { - 'subnet': '10.0.2.0/24', - 'pools': [ - { 'pool': '10.0.2.100 - 10.0.2.200' }, - ], - 'option-data': [ - { 'name': 'routers', 'data': '10.0.2.1' }, - { 'name': 'domain-name-servers', 'data': '10.0.10.2' }, - ], - }, - { - 'subnet': '10.0.9.0/24', - 'pools': [ - { 'pool': '10.0.9.100 - 10.0.9.200' }, - ], - 'option-data': [ - { 'name': 'routers', 'data': '10.0.9.1' }, - { 'name': 'domain-name-servers', 'data': '10.0.10.2' }, - ], - }, - ], + 'dhcp_server': True, }, }, 'nftables': {