wip unify rpitools with boxtools
install.py and router.py
This commit is contained in:
45
install.py
45
install.py
@ -20,15 +20,20 @@ from glob import glob
|
||||
from ipaddress import IPv4Interface
|
||||
from configparser import ConfigParser
|
||||
from os.path import join, getmtime, exists, basename
|
||||
from utils import get_config
|
||||
from utils import get_config, ifname_mapping
|
||||
|
||||
if os.geteuid() != 0:
|
||||
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
|
||||
|
||||
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
|
||||
os.chdir(f'{TOOLS}/to_system')
|
||||
|
||||
DEL = '__to_delete__'
|
||||
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
|
||||
|
||||
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):
|
||||
@ -144,7 +157,7 @@ def router(**opts):
|
||||
if not opts:
|
||||
return None
|
||||
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:
|
||||
pip_requirements['root']['tools'] = f.read()
|
||||
except FileNotFoundError:
|
||||
@ -282,6 +295,7 @@ def create_if(name, cfg):
|
||||
else:
|
||||
cfgip = IPv4Interface(cfg)
|
||||
network = cfgip.network
|
||||
print('N', network, network.prefixlen)
|
||||
if network.prefixlen == 32: # or no prefix specified
|
||||
otherip = IPv4Interface('%s/24' % cfgip.ip)
|
||||
network = otherip.network
|
||||
@ -350,6 +364,7 @@ class Show(Walker):
|
||||
print('%s %s:\n %s' % (title, self.syspath, ' '.join(files)))
|
||||
|
||||
def show(self, title, dirpath, files):
|
||||
self.dirty = True
|
||||
if more_info:
|
||||
for f in files:
|
||||
print('cat %s' % join(dirpath, f))
|
||||
@ -391,14 +406,15 @@ def handle_config():
|
||||
cfgfile = None
|
||||
cfgfiles = []
|
||||
dhcp_server_cfg.clear()
|
||||
for file in glob(CFGPATH % ('*', apuid)):
|
||||
for file in glob(CFGPATH % ('*', boxid)):
|
||||
cfgfiles.append(file)
|
||||
for i in [1, 2, 3]:
|
||||
bad = glob(CFGPATH % ('*', apuid+i))
|
||||
if bad:
|
||||
print('cfg files found with bad apu id (use net addr of leftmost plug)')
|
||||
print(bad)
|
||||
return False
|
||||
if boxtype == 'apu':
|
||||
for i in [1, 2, 3]:
|
||||
bad = glob(CFGPATH % ('*', apuid+i))
|
||||
if bad:
|
||||
print('cfg files found with bad apu id (use net addr of leftmost plug)')
|
||||
print(bad)
|
||||
return False
|
||||
newhostname = main_info['hostname']
|
||||
if not cfgfiles:
|
||||
# determine if display is present
|
||||
@ -415,19 +431,19 @@ def handle_config():
|
||||
# main_info['mainif'] = sorted_if[-1]
|
||||
typ = 'bare apu'
|
||||
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: ')
|
||||
if not newhostname:
|
||||
print('no hostname given')
|
||||
return False
|
||||
cfgfile = CFGPATH % (newhostname, apuid)
|
||||
cfgfile = CFGPATH % (newhostname, boxid)
|
||||
with open(cfgfile, 'w') as f:
|
||||
f.write(template)
|
||||
elif len(cfgfiles) > 1:
|
||||
print('ERROR: ambiguous cfg files: %s' % ', '.join(cfgfiles))
|
||||
else:
|
||||
cfgfile = cfgfiles[0]
|
||||
if cfgfile != CFGPATH % (newhostname, apuid):
|
||||
if cfgfile != CFGPATH % (newhostname, boxid):
|
||||
if cfgfile:
|
||||
newhostname = basename(cfgfile).rpartition('_')[0]
|
||||
if doit:
|
||||
@ -453,6 +469,7 @@ def handle_config():
|
||||
show.dirty = True
|
||||
to_start[ifname] = 'if_restart'
|
||||
if dhcp_server_cfg:
|
||||
print(dhcp_server_cfg)
|
||||
content = [DHCP_HEADER]
|
||||
for subnet, rangelist in dhcp_server_cfg:
|
||||
try:
|
||||
|
@ -410,8 +410,9 @@ class Service:
|
||||
|
||||
try:
|
||||
((prevline, prevports),) = pattern.findall(content)
|
||||
except TypeError:
|
||||
raise ValueError(f'{FIREWALL_CONF} does not contain expected pattern for open ports')
|
||||
except (TypeError, ValueError):
|
||||
print(f'{FIREWALL_CONF} does not contain expected pattern for open ports - firewall off?')
|
||||
return
|
||||
|
||||
# parse previous port set
|
||||
prevportset = {int(p) for p in prevports[1:-1].split(',')}
|
||||
|
Reference in New Issue
Block a user