copying cfg file to central repo, fix ssh host key

+ some other improvements
This commit is contained in:
2026-08-20 17:43:47 +02:00
parent 2bf5c24dd9
commit e81d0db1ca
2 changed files with 44 additions and 11 deletions
+37 -7
View File
@@ -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 <service> of <action>, <as_root>
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')
+7 -4
View File
@@ -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)