From e81d0db1ca8dad6fb9e180fc0f64fca02b334be6 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 20 Aug 2026 17:41:11 +0200 Subject: [PATCH] copying cfg file to central repo, fix ssh host key + some other improvements --- install.py | 44 +++++++++++++++++++++++++++++++++++++------- utils.py | 11 +++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/install.py b/install.py index b10fbc4..d3f385a 100755 --- a/install.py +++ b/install.py @@ -44,6 +44,8 @@ except ImportError as e: exit('') +CENTRAL_BOXTOOLS_REPO = 'l_samenv@linse-c:boxtools/' + main_ports = {22} # ssh router_ports = set() @@ -281,6 +283,26 @@ SERVICES = dict(router=router, display=display, frappy=frappy, boxweb=boxweb) AS_ROOT = {'router', 'display'} +def update_host_key(): + hostname = Path('/etc/hostname').read_text().strip().split('.')[0] + try: + for pubkey in Path('/etc/ssh').glob('ssh_host*key.pub'): + pubhost = pubkey.read_text().split('\n')[0].rsplit('@')[-1] + if pubhost == hostname: + continue # no need to change keys + print('-- need new ssh host key:', pubhost, '!=', hostname) + break + else: + return # no changed hostname found + do_cmd('mkdir -p /etc/ssh/host_key_backup') + for file in Path('/etc/ssh').glob('ssh_host*key*'): + do_cmd('mv', file, '/etc/ssh/host_key_backup/') + do_cmd(f'ssh-keygen -A') + except BaseException: + for file in Path('/etc/ssh/host_key_backup').glob('*'): + do_cmd('mv', file, '/etc/ssh/') + + def write_when_new(filename, content, as_root=False, ignore_reduction=False): if content is None: lines = [] @@ -289,11 +311,15 @@ def write_when_new(filename, content, as_root=False, ignore_reduction=False): content += '\n' lines = content.split('\n') try: - os.makedirs(Path(filename).parent, exist_ok=True) - for parent in reversed(Path(filename).absolute().parents): - unix_cmd('chmod', 'o+rx', parent, sudo=True) - with open(filename) as fil: - old = fil.read() + filepath = Path(filename) + try: + filepath.parent.mkdir(parents=True) + # os.makedirs(Path(filename).parent, exist_ok=True) + for parent in reversed(Path(filename).absolute().parents): + unix_cmd('chmod', 'o+rx', parent, sudo=True) + except FileExistsError: + pass # do not complain when dir exists, and do not set mode + old = filepath.read_text() except FileNotFoundError: old = None #except Exception as e: @@ -527,10 +553,13 @@ def handle_config(): print(f'{box.cfgfile} not found', e) if box.oldcfg: - print(f'convert {box.cfgfile} to {convert_cfg(box.cfgfile, box.macaddr)}') + converted = convert_cfg(box.cfgfile, box.macaddr) + print(f'convert {box.cfgfile} to {converted}') + box.cfgfile = converted config = box.read_config() cfgfile = box.cfgfile + unix_cmd('scp', str(cfgfile), f'{CENTRAL_BOXTOOLS_REPO}/cfg/', sudo=False) newhostname = box.hostname if cfgfile: if box.hwtype != 'apu': @@ -605,13 +634,14 @@ def handle_config(): show.dirty = True config = box.read_config() box.hostname = newhostname + update_host_key() if cfgfile is None: return False to_start = {} # dict of , ifname = '' try: netcfg = config.get('NETWORK', {}) - if netcfg: # when not network is specified, do not handle network at all + if netcfg: # when no network is specified, do not handle network at all for name in netcfg: if name not in box.network_interfaces: print(f'{name} is currently not a valid network interface - skip') diff --git a/utils.py b/utils.py index 779cbd1..a4e0a1e 100644 --- a/utils.py +++ b/utils.py @@ -258,13 +258,16 @@ def unix_cmd(cmd, *args, execute=None, stdout=PIPE, sudo=True): command.insert(1, '--user') if sudo: command.insert(0, 'sudo') - # sudo = ['sudo'] if sudo else [] if execute is not False: # None or True if execute: print('$', *command) - # result = Popen(sudo + command, stdout=stdout).communicate()[0] - result = Popen(command, stdout=stdout).communicate()[0] - return (result or b'').decode() + try: + result = Popen(command, stdout=stdout).communicate()[0] + return (result or b'').decode() + except Exception as e: + print(f'error {e!r}') + finally: + pass else: print('>', *command)