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
- set host name / network settings from boxtools/cfg file
- and many more
"""
if bytes == str:
@ -12,18 +13,39 @@ import sys
import os
import filecmp
import shutil
import serial
import re
import types
import socket
from subprocess import Popen, PIPE
from ipaddress import IPv4Interface
from os.path import join, getmtime, exists, basename
from utils import BoxInfo, check_service, unix_cmd, change_firewall
if os.geteuid() != 0:
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
more_info = False
@ -108,10 +130,10 @@ pip_requirements = {
box = BoxInfo()
box.hostname_changed = False
dhcp_server_cfg = []
TO_SYSTEM = f'{TOOLS}/{box.typ}_system'
os.chdir(TO_SYSTEM)
TO_SYSTEM = [f'{TOOLS}/to_system', f'{TOOLS}/{box.typ}_system']
def do_cmd(command):
@ -151,7 +173,7 @@ def router(firewall=False, **opts):
if not opts:
return None
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:
pip_requirements['root']['tools'] = f.read()
except FileNotFoundError:
@ -333,34 +355,36 @@ def create_if(name, cfg):
def walk(action):
for dirpath, _, files in os.walk('.'):
syspath = dirpath[1:] # remove leading '.'
action.dirpath = dirpath
action.syspath = syspath
if files:
match, mismatch, missing = filecmp.cmpfiles(dirpath, syspath, files)
if mismatch:
newer = [f for f in mismatch if getmtime(join(syspath, f)) > getmtime(join(dirpath, f))]
if newer:
action.newer(newer)
if len(newer) < len(mismatch):
newer = set(newer)
action.older([f for f in mismatch if f not in newer])
if 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)
for rootpath in TO_SYSTEM:
os.chdir(rootpath)
for dirpath, _, files in os.walk('.'):
syspath = dirpath[1:] # remove leading '.'
action.dirpath = dirpath
action.syspath = syspath
if files:
match, mismatch, missing = filecmp.cmpfiles(dirpath, syspath, files)
if mismatch:
newer = [f for f in mismatch if getmtime(join(syspath, f)) > getmtime(join(dirpath, f))]
if newer:
action.newer(newer)
if len(newer) < len(mismatch):
newer = set(newer)
action.older([f for f in mismatch if f not in newer])
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:
@ -449,11 +473,13 @@ def handle_config():
cfgfile = BoxInfo.CFGPATH % (newhostname, box.id)
with open(cfgfile, 'w') as f:
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:
newhostname = basename(cfgfile).rpartition('_')[0]
if doit:
os.system('sh sethostname.sh')
print('bash sethostname.sh')
os.system(f'bash {TOOLS}/sethostname.sh')
else:
if cfgfile:
print('replace host name %r by %r' % (box.hostname, newhostname))
@ -497,8 +523,13 @@ def handle_config():
print('change dhcpd.conf')
to_start['isc-dhcp-server'] = 'restart'
show.dirty = True
replace_in_file('/etc/default/isc-dhcp-server',
r'INTERFACESv4="(.*)"', ' '.join([n for n in box.macaddr if n != box.main_if]))
try:
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:
check_service('isc-dhcp-server', False)
content = []