restart individual network interface on change

+ use prefix 24 instead of 31
This commit is contained in:
2021-12-14 09:29:37 +01:00
parent e2acd8030d
commit 1deb91dbbb

View File

@ -203,18 +203,22 @@ def create_if(name, cfg, mac, dhcp_server_cfg):
result['BOOTPROTO']='dhcp' result['BOOTPROTO']='dhcp'
# default: all <= 192.0.0.0 # default: all <= 192.0.0.0
# others have to be added explicitly # others have to be added explicitly
dhcp_server_cfg.append((('0.0.0.0','128.0.0.0'), [])) dhcp_server_cfg.append(('0.0.0.0/128.0.0.0', []))
dhcp_server_cfg.append((('128.0.0.0','192.0.0.0'), [])) dhcp_server_cfg.append(('128.0.0.0/192.0.0.0', []))
for nw in cfg.split(',')[1:]: for nw in cfg.split(',')[1:]:
nw = IPv4Interface(cfg).network nw = IPv4Interface(cfg).network
dhcp_server_cfg.append((nw.with_netmask.split('/'), [])) dhcp_server_cfg.append((nw.with_netmask.split('/'), []))
else: else:
cfgip = IPv4Interface(cfg) cfgip = IPv4Interface(cfg)
network = cfgip.network network = cfgip.network
if network.prefixlen == 32: if network.prefixlen == 32: # or no prefix specified
otherip = IPv4Interface('%s/31' % cfgip.ip) otherip = IPv4Interface('%s/24' % cfgip.ip)
network = otherip.network network = otherip.network
cfgip = network.network_address + (1 - int(otherip) % 2) if str(cfgip.ip).endswith('.1'):
cfgip = network.network_address + 2
else:
cfgip = network.network_address + 1
print(name, network.network_address, cfgip, otherip.ip)
dhcp_server_cfg.append((network.with_netmask, [(str(otherip.ip), str(otherip.ip))])) dhcp_server_cfg.append((network.with_netmask, [(str(otherip.ip), str(otherip.ip))]))
result['IPADDR'] = str(cfgip) result['IPADDR'] = str(cfgip)
else: # subnet with multiple adresses -> static adresses only. dhcp range not yet implemented else: # subnet with multiple adresses -> static adresses only. dhcp range not yet implemented
@ -344,13 +348,13 @@ def handle_config():
show.dirty = True show.dirty = True
if cfgfile is None: if cfgfile is None:
return False return False
to_start = {}
ifname = '' ifname = ''
parser = ConfigParser() parser = ConfigParser()
dh = [] dhcp_server_cfg = []
try: try:
parser.read(cfgfile) parser.read(cfgfile)
network = dict(parser['NETWORK']) network = dict(parser['NETWORK'])
dhcp_server_cfg = []
for ifname in IFNAMES: for ifname in IFNAMES:
content = create_if(ifname, network.pop(ifname, 'off'), netaddr_dict[ifname], dhcp_server_cfg) content = create_if(ifname, network.pop(ifname, 'off'), netaddr_dict[ifname], dhcp_server_cfg)
content = '\n'.join('%s=%s' % kv for kv in content.items()) content = '\n'.join('%s=%s' % kv for kv in content.items())
@ -358,19 +362,25 @@ def handle_config():
if todo and more_info == 'NONE': if todo and more_info == 'NONE':
print('change', ifname) print('change', ifname)
show.dirty = True show.dirty = True
if dh: to_start[ifname] = 'if_restart'
if dhcp_server_cfg:
content = [DHCP_HEADER] content = [DHCP_HEADER]
for subnet, rangelist in dhcp_server_cfg: for subnet, rangelist in dhcp_server_cfg:
dh.append('subnet %s netmask %s {\n' % subnet) adr, mask = subnet.split('/')
dh.append(' option netmask %s;\n' % subnet[1]) content.append('subnet %s netmask %s {\n' % (adr, mask))
#content.append(' option netmask %s;\n' % mask)
for rng in rangelist: for rng in rangelist:
dh.append(' range %s %s;\n' % rng) content.append(' range %s %s;\n' % rng)
dh.append('}\n') content.append('}\n')
content = ''.join(content) content = ''.join(content)
todo = write_when_new('/etc/dhcp/dhcpd.conf', content) todo = write_when_new('/etc/dhcp/dhcpd.conf', content)
if todo: if todo:
print('change ', ifname) print('change dhcpd.conf')
to_start['dhcpd'] = 'restart'
show.dirty = True show.dirty = True
elif doit:
unix_cmd('systemctl stop dhcpd')
unix_cmd('systemctl disable dhcpd')
content = [] content = []
dirty = False dirty = False
for section in parser.sections(): for section in parser.sections():
@ -389,7 +399,6 @@ def handle_config():
raise raise
return False return False
to_start = {}
reload_systemd = False reload_systemd = False
for service, template_func in SERVICES.items(): for service, template_func in SERVICES.items():
section = service.upper() section = service.upper()
@ -428,9 +437,13 @@ def handle_config():
unix_cmd('systemctl daemon-reload') unix_cmd('systemctl daemon-reload')
for service, action in to_start.items(): for service, action in to_start.items():
show.dirty = True show.dirty = True
if action == 'restart': if action == 'if_restart':
unix_cmd('systemctl restart %s' % service) unix_cmd('ifdown %s' % service)
unix_cmd('systemctl enable %s' % service) unix_cmd('ifup %s' % service)
else:
if action == 'restart':
unix_cmd('systemctl restart %s' % service)
unix_cmd('systemctl enable %s' % service)
return True return True