dhcp from interface

This commit is contained in:
cronekorkn 2024-09-23 20:32:12 +02:00
parent c0944f9fa2
commit 01bcfd8638
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw
8 changed files with 108 additions and 50 deletions

View file

@ -1,3 +1,8 @@
from ipaddress import ip_interface, ip_network
hashable = repo.libs.hashable.hashable
defaults = { defaults = {
'apt': { 'apt': {
'packages': { 'packages': {
@ -7,15 +12,15 @@ defaults = {
'kea': { 'kea': {
'Dhcp4': { 'Dhcp4': {
'interfaces-config': { 'interfaces-config': {
'interfaces': [], 'interfaces': set(),
}, },
'lease-database': { 'lease-database': {
'type': 'memfile', 'type': 'memfile',
'lfc-interval': 3600 'lfc-interval': 3600
}, },
'subnet4': [], 'subnet4': set(),
'loggers': [ 'loggers': set([
{ hashable({
'name': 'kea-dhcp4', 'name': 'kea-dhcp4',
'output_options': [ 'output_options': [
{ {
@ -23,15 +28,69 @@ defaults = {
} }
], ],
'severity': 'INFO', 'severity': 'INFO',
}, }),
], ]),
}, },
}, },
} }
@metadata_reactor.provides( @metadata_reactor.provides(
'kea/Dhcp4/interfaces-config/interfaces',
'kea/Dhcp4/subnet4',
) )
def subnets(metadata): 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,
},
},
}

View file

@ -18,7 +18,7 @@ git -C ~/.zsh/oh-my-zsh pull
brew upgrade brew upgrade
brew upgrade --cask --greedy brew upgrade --cask --greedy
pyenv install --keep-existing pyenv install --skip-existing
sudo softwareupdate -ia --verbose sudo softwareupdate -ia --verbose

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

@ -9,7 +9,7 @@ files = {
node.metadata.get('telegraf/config'), node.metadata.get('telegraf/config'),
cls=MetadataJSONEncoder, cls=MetadataJSONEncoder,
)), )),
sort_keys=True sort_keys=True,
), ),
'triggers': [ 'triggers': [
'svc_systemd:telegraf:restart', 'svc_systemd:telegraf:restart',

View file

@ -29,6 +29,7 @@
'internal': { 'internal': {
'interface': 'eth0', 'interface': 'eth0',
'ipv4': '10.0.0.16/24', 'ipv4': '10.0.0.16/24',
'mac': 'd8:3a:dd:16:fc:9d',
'gateway4': '10.0.0.1', 'gateway4': '10.0.0.1',
}, },
}, },

View file

@ -14,6 +14,7 @@
'network': { 'network': {
'internal': { 'internal': {
'ipv4': '10.0.2.8/24', 'ipv4': '10.0.2.8/24',
'mac': 'b8:27:eb:15:30:86',
}, },
}, },
'dns': { 'dns': {

View file

@ -5,7 +5,13 @@
'home', 'home',
], ],
'metadata': { 'metadata': {
'id': '', 'id': '87879bc1-130f-4fca-a8d2-e1d93a794df4',
'network': {
'internal': {
'ipv4': '10.0.2.100/24',
'mac': '00:17:88:67:e7:f2',
},
},
'dns': { 'dns': {
'hue.ckn.li': { 'hue.ckn.li': {
'A': {'10.0.2.100'}, 'A': {'10.0.2.100'},

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,7 @@
'type': 'vlan', 'type': 'vlan',
'id': 9, 'id': 9,
'ipv4': '10.0.9.1/24', 'ipv4': '10.0.9.1/24',
}, 'dhcp_server': True,
},
'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' },
],
},
],
}, },
}, },
'nftables': { 'nftables': {