do not consider network installs when not specified

This commit is contained in:
2025-04-09 09:45:12 +02:00
parent e48e1302c0
commit 2dfc71001a
2 changed files with 47 additions and 46 deletions

View File

@ -15,6 +15,7 @@ import filecmp
import re import re
import types import types
import socket import socket
import tempfile
from pathlib import Path from pathlib import Path
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from ipaddress import IPv4Interface from ipaddress import IPv4Interface
@ -520,9 +521,9 @@ def handle_config():
box.typ = 'bare-apu' box.typ = 'bare-apu'
elif box.typ == 'cm4': elif box.typ == 'cm4':
box.typ = 'dual-eth-rpi' box.typ = 'dual-eth-rpi'
print('This is a cm4, so guess its a dual-eth-rpi - please check type in cfg file') print('This is a cm4, so guess its a dual-eth-rpi - please check type in created cfg file')
elif box.typ == 'cm3': elif box.typ == 'cm3':
print('This is a cm3 so guess its a ionopimax - please check type in cfg file') print('This is a cm3 so guess its a ionopimax - please check type in created cfg file')
box.typ = 'ionopimax' box.typ = 'ionopimax'
template = TEMPLATES.get(box.typ) template = TEMPLATES.get(box.typ)
if template is None: if template is None:
@ -542,10 +543,9 @@ def handle_config():
config = box.read_config() config = box.read_config()
cfgfile = box.cfgfile cfgfile = box.cfgfile
if box.hostname_changed or box.hostname != newhostname: if box.hostname_changed or box.hostname != newhostname:
print('changed', box.hostname, newhostname)
box.hostname_changed = True box.hostname_changed = True
if cfgfile: if cfgfile:
newhostname = cfgfile.stem.rpartition('_')[0] newhostname = cfgfile.stem.split('_')[0]
if doit: if doit:
print('bash sethostname.sh') print('bash sethostname.sh')
unix_cmd('bash', f'{TOOLS}/sethostname.sh') unix_cmd('bash', f'{TOOLS}/sethostname.sh')
@ -561,48 +561,49 @@ def handle_config():
ifname = '' ifname = ''
try: try:
netcfg = config.get('NETWORK', {}) netcfg = config.get('NETWORK', {})
for name in netcfg: if netcfg: # when not network is specified, do not handle network at all
if name not in box.network_interfaces: for name in netcfg:
print(f'{name} is not a valid network interface name') if name not in box.network_interfaces:
raise RuntimeError('network interface name system does not match') print(f'{name} is not a valid network interface name')
for ifname in box.network_interfaces: raise RuntimeError('network interface name system does not match')
content = create_if(ifname, netcfg.get(ifname, 'off')) for ifname in box.network_interfaces:
# content = '\n'.join('%s=%s' % kv for kv in content.items()) content = create_if(ifname, netcfg.get(ifname, 'off'))
todo = write_when_new(f'/etc/network/interfaces.d/{ifname}', content, as_root=True) # content = '\n'.join('%s=%s' % kv for kv in content.items())
if todo and not more_info: todo = write_when_new(f'/etc/network/interfaces.d/{ifname}', content, as_root=True)
print('change', ifname) if todo and not more_info:
show.dirty = True print('change', ifname)
to_start[ifname] = 'if_restart' show.dirty = True
if dhcp_server_cfg: to_start[ifname] = 'if_restart'
content = [DHCP_HEADER] if dhcp_server_cfg:
for subnet, rangelist in dhcp_server_cfg: content = [DHCP_HEADER]
for subnet, rangelist in dhcp_server_cfg:
try:
adr, mask = subnet.split('/')
except Exception as e:
print(subnet, repr(e))
continue
content.append('subnet %s netmask %s {\n' % (adr, mask))
#content.append(' option netmask %s;\n' % mask)
for rng in rangelist:
content.append(' range %s %s;\n' % rng)
content.append('}\n')
content = ''.join(content)
todo = write_when_new('/etc/dhcp/dhcpd.conf', content, as_root=True)
if todo:
print('change dhcpd.conf')
to_start['isc-dhcp-server'] = 'restart'
show.dirty = True
try: try:
adr, mask = subnet.split('/') replace_in_file('/etc/default/isc-dhcp-server',
except Exception as e: r'INTERFACESv4="(.*)"', ' '.join(
print(subnet, repr(e)) [n for n in box.network_interfaces if n != box.main_if]))
continue except FileNotFoundError:
content.append('subnet %s netmask %s {\n' % (adr, mask)) if 'yes'.startswith(input('install isc-dhcp-server? [y]')):
#content.append(' option netmask %s;\n' % mask) unix_cmd('apt-get install isc-dhcp-server')
for rng in rangelist: exit()
content.append(' range %s %s;\n' % rng) elif doit:
content.append('}\n') unix_cmd('systemctl stop isc-dhcp-server')
content = ''.join(content) unix_cmd('systemctl disable isc-dhcp-server')
todo = write_when_new('/etc/dhcp/dhcpd.conf', content, as_root=True)
if todo:
print('change dhcpd.conf')
to_start['isc-dhcp-server'] = 'restart'
show.dirty = True
try:
replace_in_file('/etc/default/isc-dhcp-server',
r'INTERFACESv4="(.*)"', ' '.join(
[n for n in box.network_interfaces if n != box.main_if]))
except FileNotFoundError:
if 'yes'.startswith(input('install isc-dhcp-server? [y]')):
unix_cmd('apt-get install isc-dhcp-server')
exit()
elif doit:
unix_cmd('systemctl stop isc-dhcp-server')
unix_cmd('systemctl disable isc-dhcp-server')
displaycfg = config.get('DISPLAY') displaycfg = config.get('DISPLAY')
if displaycfg and display_update(displaycfg): if displaycfg and display_update(displaycfg):
to_start['display'] = 'restart' to_start['display'] = 'restart'

View File

@ -115,7 +115,7 @@ class BoxInfo:
break break
if not cfgfiles: if not cfgfiles:
cfgfiles = list(self.CFGDIR.glob(f'*_{self.id:06x}.cfg*')) cfgfiles = list(self.CFGDIR.glob(f'*_{self.id:06x}.cfg*'))
self.oldcfg = True self.oldcfg = bool(cfgfiles)
if len(cfgfiles) > 1: if len(cfgfiles) > 1:
raise AmbiguousConfigFile('ambiguous cfgfile: %r' % cfgfiles) raise AmbiguousConfigFile('ambiguous cfgfile: %r' % cfgfiles)
if section and not cfgfiles: if section and not cfgfiles: