wip unify rpitools with boxtools

install.py and router.py
This commit is contained in:
l_samenv
2024-03-19 11:42:50 +01:00
parent d6e68aef7d
commit 0a0009a096
2 changed files with 34 additions and 16 deletions

View File

@ -20,15 +20,20 @@ from glob import glob
from ipaddress import IPv4Interface from ipaddress import IPv4Interface
from configparser import ConfigParser from configparser import ConfigParser
from os.path import join, getmtime, exists, basename from os.path import join, getmtime, exists, basename
from utils import get_config from utils import get_config, ifname_mapping
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.")
TOOLS = '/home/l_samenv/boxtools' TOOLS = '/home/l_samenv/boxtools'
BOX_TYPES = {
'00:0d:b9': 'apu', # bare apu or control box
'b8:27:eb': 'cm3', # iono pi
'd8:3a:dd': 'cm4', # dual-eth-rpi
}
more_info = False more_info = False
os.chdir(f'{TOOLS}/to_system')
DEL = '__to_delete__' DEL = '__to_delete__'
CFGPATH = f'{TOOLS}/cfg/%s_%6.6x.cfg' CFGPATH = f'{TOOLS}/cfg/%s_%6.6x.cfg'
@ -125,7 +130,15 @@ for netif in os.scandir('/sys/class/net'):
net_addr[netif.name] = addr net_addr[netif.name] = addr
sorted_if = sorted(net_addr) sorted_if = sorted(net_addr)
apuid = int(''.join(net_addr[sorted_if[0]].split(':')[-3:]), 16) & 0xfffffc boxaddr = net_addr[sorted_if[0]]
boxid = int(''.join(boxaddr.split(':')[-3:]), 16) & 0xffffff
boxtype = BOX_TYPES.get(boxaddr[:8])
if boxtype != 'apu':
ifname_mapping.clear()
TO_SYSTEM = f'{TOOLS}/to_{boxtype}'
if not exists(TO_SYSTEM):
TO_SYSTEM = f'{TOOLS}/to_system'
os.chdir(TO_SYSTEM)
def frappy(cfg=None, port=None, requirements='', **kwds): def frappy(cfg=None, port=None, requirements='', **kwds):
@ -144,7 +157,7 @@ def router(**opts):
if not opts: if not opts:
return None return None
try: try:
os.remove(join(TOOLS, 'to_system/etc/nftables.conf')) os.remove(join(TO_SYSTEM, '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:
@ -282,6 +295,7 @@ def create_if(name, cfg):
else: else:
cfgip = IPv4Interface(cfg) cfgip = IPv4Interface(cfg)
network = cfgip.network network = cfgip.network
print('N', network, network.prefixlen)
if network.prefixlen == 32: # or no prefix specified if network.prefixlen == 32: # or no prefix specified
otherip = IPv4Interface('%s/24' % cfgip.ip) otherip = IPv4Interface('%s/24' % cfgip.ip)
network = otherip.network network = otherip.network
@ -350,6 +364,7 @@ class Show(Walker):
print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) print('%s %s:\n %s' % (title, self.syspath, ' '.join(files)))
def show(self, title, dirpath, files): def show(self, title, dirpath, files):
self.dirty = True
if more_info: if more_info:
for f in files: for f in files:
print('cat %s' % join(dirpath, f)) print('cat %s' % join(dirpath, f))
@ -391,8 +406,9 @@ def handle_config():
cfgfile = None cfgfile = None
cfgfiles = [] cfgfiles = []
dhcp_server_cfg.clear() dhcp_server_cfg.clear()
for file in glob(CFGPATH % ('*', apuid)): for file in glob(CFGPATH % ('*', boxid)):
cfgfiles.append(file) cfgfiles.append(file)
if boxtype == 'apu':
for i in [1, 2, 3]: for i in [1, 2, 3]:
bad = glob(CFGPATH % ('*', apuid+i)) bad = glob(CFGPATH % ('*', apuid+i))
if bad: if bad:
@ -415,19 +431,19 @@ def handle_config():
# main_info['mainif'] = sorted_if[-1] # main_info['mainif'] = sorted_if[-1]
typ = 'bare apu' typ = 'bare apu'
template = CONFIG_TEMPLATE template = CONFIG_TEMPLATE
print('no cfg file found for this', typ, f'with id {apuid:%6.6x} (hostname={hostname})') print('no cfg file found for this', typ, f'with id {boxid:%6.6x} (hostname={hostname})')
newhostname = input('enter host name: ') newhostname = input('enter host name: ')
if not newhostname: if not newhostname:
print('no hostname given') print('no hostname given')
return False return False
cfgfile = CFGPATH % (newhostname, apuid) cfgfile = CFGPATH % (newhostname, boxid)
with open(cfgfile, 'w') as f: with open(cfgfile, 'w') as f:
f.write(template) f.write(template)
elif len(cfgfiles) > 1: elif len(cfgfiles) > 1:
print('ERROR: ambiguous cfg files: %s' % ', '.join(cfgfiles)) print('ERROR: ambiguous cfg files: %s' % ', '.join(cfgfiles))
else: else:
cfgfile = cfgfiles[0] cfgfile = cfgfiles[0]
if cfgfile != CFGPATH % (newhostname, apuid): if cfgfile != CFGPATH % (newhostname, boxid):
if cfgfile: if cfgfile:
newhostname = basename(cfgfile).rpartition('_')[0] newhostname = basename(cfgfile).rpartition('_')[0]
if doit: if doit:
@ -453,6 +469,7 @@ def handle_config():
show.dirty = True show.dirty = True
to_start[ifname] = 'if_restart' to_start[ifname] = 'if_restart'
if dhcp_server_cfg: if dhcp_server_cfg:
print(dhcp_server_cfg)
content = [DHCP_HEADER] content = [DHCP_HEADER]
for subnet, rangelist in dhcp_server_cfg: for subnet, rangelist in dhcp_server_cfg:
try: try:

View File

@ -410,8 +410,9 @@ class Service:
try: try:
((prevline, prevports),) = pattern.findall(content) ((prevline, prevports),) = pattern.findall(content)
except TypeError: except (TypeError, ValueError):
raise ValueError(f'{FIREWALL_CONF} does not contain expected pattern for open ports') print(f'{FIREWALL_CONF} does not contain expected pattern for open ports - firewall off?')
return
# parse previous port set # parse previous port set
prevportset = {int(p) for p in prevports[1:-1].split(',')} prevportset = {int(p) for p in prevports[1:-1].split(',')}