diff --git a/install.py b/install.py index d723de2..7eca789 100755 --- a/install.py +++ b/install.py @@ -15,6 +15,7 @@ import filecmp import re import types import socket +import tempfile from pathlib import Path from subprocess import Popen, PIPE from ipaddress import IPv4Interface @@ -520,9 +521,9 @@ def handle_config(): box.typ = 'bare-apu' elif box.typ == 'cm4': 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': - 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' template = TEMPLATES.get(box.typ) if template is None: @@ -542,10 +543,9 @@ def handle_config(): config = box.read_config() cfgfile = box.cfgfile if box.hostname_changed or box.hostname != newhostname: - print('changed', box.hostname, newhostname) box.hostname_changed = True if cfgfile: - newhostname = cfgfile.stem.rpartition('_')[0] + newhostname = cfgfile.stem.split('_')[0] if doit: print('bash sethostname.sh') unix_cmd('bash', f'{TOOLS}/sethostname.sh') @@ -561,48 +561,49 @@ def handle_config(): ifname = '' try: netcfg = config.get('NETWORK', {}) - for name in netcfg: - if name not in box.network_interfaces: - print(f'{name} is not a valid network interface name') - raise RuntimeError('network interface name system does not match') - for ifname in box.network_interfaces: - content = create_if(ifname, netcfg.get(ifname, 'off')) - # content = '\n'.join('%s=%s' % kv for kv in content.items()) - todo = write_when_new(f'/etc/network/interfaces.d/{ifname}', content, as_root=True) - if todo and not more_info: - print('change', ifname) - show.dirty = True - to_start[ifname] = 'if_restart' - if dhcp_server_cfg: - content = [DHCP_HEADER] - for subnet, rangelist in dhcp_server_cfg: + if netcfg: # when not network is specified, do not handle network at all + for name in netcfg: + if name not in box.network_interfaces: + print(f'{name} is not a valid network interface name') + raise RuntimeError('network interface name system does not match') + for ifname in box.network_interfaces: + content = create_if(ifname, netcfg.get(ifname, 'off')) + # content = '\n'.join('%s=%s' % kv for kv in content.items()) + todo = write_when_new(f'/etc/network/interfaces.d/{ifname}', content, as_root=True) + if todo and not more_info: + print('change', ifname) + show.dirty = True + to_start[ifname] = 'if_restart' + if 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: - 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: - 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') + 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') if displaycfg and display_update(displaycfg): to_start['display'] = 'restart' diff --git a/utils.py b/utils.py index 4828f13..824640b 100644 --- a/utils.py +++ b/utils.py @@ -115,7 +115,7 @@ class BoxInfo: break if not cfgfiles: cfgfiles = list(self.CFGDIR.glob(f'*_{self.id:06x}.cfg*')) - self.oldcfg = True + self.oldcfg = bool(cfgfiles) if len(cfgfiles) > 1: raise AmbiguousConfigFile('ambiguous cfgfile: %r' % cfgfiles) if section and not cfgfiles: