dhcp from interface

This commit is contained in:
cronekorkn 2024-09-23 20:32:12 +02:00
parent 7907d1f84b
commit 7bd5f41bc9
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw
3 changed files with 108 additions and 37 deletions

View file

@ -1,3 +1,8 @@
from ipaddress import ip_interface
hashable = repo.libs.hashable.hashable
defaults = { defaults = {
'apt': { 'apt': {
'packages': { 'packages': {
@ -31,7 +36,43 @@ defaults = {
@metadata_reactor.provides( @metadata_reactor.provides(
'kea',
) )
def subnets(metadata): def subnets(metadata):
pass subnet4 = set()
interfaces = set()
for network_name, network_conf in metadata.get('network').items():
dhcp_server_config = network_conf.get('dhcp_server_config', None)
if dhcp_server_config:
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',
},
],
}))
interfaces.add(network_conf.get('interface', network_name))
return {
'kea': {
'Dhcp4': {
'interfaces-config': {
'interfaces': interfaces,
},
'subnet4': subnet4,
},
},
}

View file

@ -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( @metadata_reactor.provides(
'systemd/units', 'systemd/units',
) )

View file

@ -18,6 +18,7 @@
'interface': 'eno1', 'interface': 'eno1',
'ipv4': '10.0.0.1/24', 'ipv4': '10.0.0.1/24',
'vlans': {'iot', 'internet', 'guest'}, 'vlans': {'iot', 'internet', 'guest'},
'dhcp_server': True,
}, },
'temp': { 'temp': {
'interface': 'enx00e04c220682', 'interface': 'enx00e04c220682',
@ -28,6 +29,7 @@
'type': 'vlan', 'type': 'vlan',
'id': 2, 'id': 2,
'ipv4': '10.0.2.1/24', 'ipv4': '10.0.2.1/24',
'dhcp_server': True,
}, },
'internet': { 'internet': {
'type': 'vlan', 'type': 'vlan',
@ -38,45 +40,46 @@
'type': 'vlan', 'type': 'vlan',
'id': 9, 'id': 9,
'ipv4': '10.0.9.1/24', 'ipv4': '10.0.9.1/24',
'dhcp_server': True,
}, },
}, },
'kea': { 'kea': {
'Dhcp4': { 'Dhcp4': {
'interfaces-config': { #'interfaces-config': {
'interfaces': ['eno1', 'iot', 'guest'], # 'interfaces': {'eno1', 'iot', 'guest'},
}, #},
'subnet4': [ # 'subnet4': [
{ # {
'subnet': '10.0.0.0/24', # 'subnet': '10.0.0.0/24',
'pools': [ # 'pools': [
{ 'pool': '10.0.0.100 - 10.0.0.200' }, # { 'pool': '10.0.0.100 - 10.0.0.200' },
], # ],
'option-data': [ # 'option-data': [
{ 'name': 'routers', 'data': '10.0.0.1' }, # { 'name': 'routers', 'data': '10.0.0.1' },
{ 'name': 'domain-name-servers', 'data': '10.0.10.2' }, # { 'name': 'domain-name-servers', 'data': '10.0.10.2' },
], # ],
}, # },
{ # {
'subnet': '10.0.2.0/24', # 'subnet': '10.0.2.0/24',
'pools': [ # 'pools': [
{ 'pool': '10.0.2.100 - 10.0.2.200' }, # { 'pool': '10.0.2.100 - 10.0.2.200' },
], # ],
'option-data': [ # 'option-data': [
{ 'name': 'routers', 'data': '10.0.2.1' }, # { 'name': 'routers', 'data': '10.0.2.1' },
{ 'name': 'domain-name-servers', 'data': '10.0.10.2' }, # { 'name': 'domain-name-servers', 'data': '10.0.10.2' },
], # ],
}, # },
{ # {
'subnet': '10.0.9.0/24', # 'subnet': '10.0.9.0/24',
'pools': [ # 'pools': [
{ 'pool': '10.0.9.100 - 10.0.9.200' }, # { 'pool': '10.0.9.100 - 10.0.9.200' },
], # ],
'option-data': [ # 'option-data': [
{ 'name': 'routers', 'data': '10.0.9.1' }, # { 'name': 'routers', 'data': '10.0.9.1' },
{ 'name': 'domain-name-servers', 'data': '10.0.10.2' }, # { 'name': 'domain-name-servers', 'data': '10.0.10.2' },
], # ],
}, # },
], # ],
}, },
}, },
'nftables': { 'nftables': {