split system files into to_system and <boxtyp>_system

- install.py checks for both
- fix hostname
This commit is contained in:
l_samenv
2024-05-14 11:19:15 +02:00
parent eac314346f
commit 03162d43c1
2 changed files with 67 additions and 36 deletions

View File

@ -3,6 +3,7 @@
- copy files from to_system into system directories - copy files from to_system into system directories
- set host name / network settings from boxtools/cfg file - set host name / network settings from boxtools/cfg file
- and many more
""" """
if bytes == str: if bytes == str:
@ -12,18 +13,39 @@ import sys
import os import os
import filecmp import filecmp
import shutil import shutil
import serial
import re import re
import types import types
import socket import socket
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from ipaddress import IPv4Interface from ipaddress import IPv4Interface
from os.path import join, getmtime, exists, basename from os.path import join, getmtime, exists, basename
from utils import BoxInfo, check_service, unix_cmd, change_firewall
if os.geteuid() != 0: if os.geteuid() != 0:
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.") exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
def exit():
print('please restart sudo ./install.py again')
sys.exit()
try:
import serial
except ImportError:
if 'yes'.startswith(input('install pyserial? [y]')):
os.system('pip3 install --break-system-packages pyserial')
serial = None
try:
from utils import BoxInfo, check_service, unix_cmd, change_firewall
except ImportError:
if 'yes'.startswith(input('install netifaces? [y]')):
os.system('pip3 install --break-system-packages netifaces')
serial = None
if serial is None:
print('please restart sudo ./install.py again')
exit()
TOOLS = BoxInfo.TOOLS TOOLS = BoxInfo.TOOLS
more_info = False more_info = False
@ -108,10 +130,10 @@ pip_requirements = {
box = BoxInfo() box = BoxInfo()
box.hostname_changed = False
dhcp_server_cfg = [] dhcp_server_cfg = []
TO_SYSTEM = f'{TOOLS}/{box.typ}_system' TO_SYSTEM = [f'{TOOLS}/to_system', f'{TOOLS}/{box.typ}_system']
os.chdir(TO_SYSTEM)
def do_cmd(command): def do_cmd(command):
@ -151,7 +173,7 @@ def router(firewall=False, **opts):
if not opts: if not opts:
return None return None
try: try:
os.remove(join(TO_SYSTEM, 'etc/nftables.conf')) os.remove(join(TO_SYSTEM[0], 'etc/nftables.conf'))
with open(f'{TOOLS}/requirements.txt') as f: with open(f'{TOOLS}/requirements.txt') as f:
pip_requirements['root']['tools'] = f.read() pip_requirements['root']['tools'] = f.read()
except FileNotFoundError: except FileNotFoundError:
@ -333,34 +355,36 @@ def create_if(name, cfg):
def walk(action): def walk(action):
for dirpath, _, files in os.walk('.'): for rootpath in TO_SYSTEM:
syspath = dirpath[1:] # remove leading '.' os.chdir(rootpath)
action.dirpath = dirpath for dirpath, _, files in os.walk('.'):
action.syspath = syspath syspath = dirpath[1:] # remove leading '.'
if files: action.dirpath = dirpath
match, mismatch, missing = filecmp.cmpfiles(dirpath, syspath, files) action.syspath = syspath
if mismatch: if files:
newer = [f for f in mismatch if getmtime(join(syspath, f)) > getmtime(join(dirpath, f))] match, mismatch, missing = filecmp.cmpfiles(dirpath, syspath, files)
if newer: if mismatch:
action.newer(newer) newer = [f for f in mismatch if getmtime(join(syspath, f)) > getmtime(join(dirpath, f))]
if len(newer) < len(mismatch): if newer:
newer = set(newer) action.newer(newer)
action.older([f for f in mismatch if f not in newer]) if len(newer) < len(mismatch):
if missing: newer = set(newer)
if DEL in missing: action.older([f for f in mismatch if f not in newer])
missing.remove(DEL)
with open(join(dirpath, DEL)) as fil:
to_delete = []
for fname in fil:
fname = fname.strip()
if fname and exists(join(syspath, fname)):
if exists(join(dirpath, fname)):
print('ERROR: %s in %s, but also in repo -> ignored' % (fname, DEL))
else:
to_delete.append(fname)
action.delete(to_delete)
if missing: if missing:
action.missing(missing) if DEL in missing:
missing.remove(DEL)
with open(join(dirpath, DEL)) as fil:
to_delete = []
for fname in fil:
fname = fname.strip()
if fname and exists(join(syspath, fname)):
if exists(join(dirpath, fname)):
print('ERROR: %s in %s, but also in repo -> ignored' % (fname, DEL))
else:
to_delete.append(fname)
action.delete(to_delete)
if missing:
action.missing(missing)
class Walker: class Walker:
@ -449,11 +473,13 @@ def handle_config():
cfgfile = BoxInfo.CFGPATH % (newhostname, box.id) cfgfile = BoxInfo.CFGPATH % (newhostname, box.id)
with open(cfgfile, 'w') as f: with open(cfgfile, 'w') as f:
f.write(template) f.write(template)
if cfgfile != BoxInfo.CFGPATH % (newhostname, box.id): if box.hostname_changed or cfgfile != BoxInfo.CFGPATH % (newhostname, box.id):
box.hostname_changed = True
if cfgfile: if cfgfile:
newhostname = basename(cfgfile).rpartition('_')[0] newhostname = basename(cfgfile).rpartition('_')[0]
if doit: if doit:
os.system('sh sethostname.sh') print('bash sethostname.sh')
os.system(f'bash {TOOLS}/sethostname.sh')
else: else:
if cfgfile: if cfgfile:
print('replace host name %r by %r' % (box.hostname, newhostname)) print('replace host name %r by %r' % (box.hostname, newhostname))
@ -497,8 +523,13 @@ def handle_config():
print('change dhcpd.conf') print('change dhcpd.conf')
to_start['isc-dhcp-server'] = 'restart' to_start['isc-dhcp-server'] = 'restart'
show.dirty = True show.dirty = True
replace_in_file('/etc/default/isc-dhcp-server', try:
r'INTERFACESv4="(.*)"', ' '.join([n for n in box.macaddr if n != box.main_if])) replace_in_file('/etc/default/isc-dhcp-server',
r'INTERFACESv4="(.*)"', ' '.join([n for n in box.macaddr if n != box.main_if]))
except FileNotFoundError:
if 'yes'.startswith(input('install isc-dhcp-server? [y]')):
os.system('apt-get install isc-dhcp-server')
exit()
elif doit: elif doit:
check_service('isc-dhcp-server', False) check_service('isc-dhcp-server', False)
content = [] content = []