make install work also for rpi boxes
- in addition to to_system, we have now to_<box> with every type of box - install is not needed to run under sudo anymore
This commit is contained in:
38
utils.py
38
utils.py
@ -3,6 +3,7 @@ import socket
|
||||
import threading
|
||||
import re
|
||||
from glob import glob
|
||||
from pathlib import Path
|
||||
from configparser import ConfigParser
|
||||
from netifaces import interfaces, ifaddresses, gateways, AF_INET, AF_LINK
|
||||
from subprocess import Popen, PIPE
|
||||
@ -19,13 +20,18 @@ else:
|
||||
os.system(cmd)
|
||||
|
||||
|
||||
class UndefinedConfigFile(Exception):
|
||||
"""config file not found or ambiguous"""
|
||||
|
||||
|
||||
class BoxInfo:
|
||||
TOOLS = '/home/l_samenv/boxtools'
|
||||
CFGPATH = f'{TOOLS}/cfg/%s_%06x.cfg'
|
||||
TOOLS = Path('/home/l_samenv/boxtools')
|
||||
CFGPATH = str(TOOLS / 'cfg' / '%s_%06x.cfg')
|
||||
BOX_TYPES = {
|
||||
'00:0d:b9': 'apu', # bare apu or control box
|
||||
'b8:27:eb': 'cm3', # iono pi
|
||||
'd8:3a:dd': 'cm4', # dual-eth-rpi
|
||||
'b8:27:eb': 'cm3', # guess iono pi max
|
||||
'e4:5f:01': 'ionopi', # guess iono pi
|
||||
'd8:3a:dd': 'cm4', # guess dual-eth-rpi
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
@ -50,6 +56,7 @@ class BoxInfo:
|
||||
self.id = int(''.join(addr.split(':')[-3:]), 16) & 0xffffff
|
||||
self.typ = self.BOX_TYPES.get(addr[:8])
|
||||
self.main_if = ifname
|
||||
self.hwtype = self.typ # this is one of the values in BOX_TYPE and will not change
|
||||
|
||||
def get_macaddr(self):
|
||||
return self.macaddr.get(self.main_if)
|
||||
@ -57,9 +64,9 @@ class BoxInfo:
|
||||
def read_config(self, section=None):
|
||||
cfgfiles = glob(self.CFGPATH % ('*', self.id))
|
||||
if len(cfgfiles) > 1:
|
||||
raise ValueError('ambiguous cfgfile: %r' % cfgfiles)
|
||||
raise AmbiguousConfigFile('ambiguous cfgfile: %r' % cfgfiles)
|
||||
if section and not cfgfiles:
|
||||
raise ValueError('no cfg file found for %s' % self.id)
|
||||
raise UndefinedConfigFile('no cfg file found for %s' % self.id)
|
||||
if cfgfiles:
|
||||
self.cfgfile = cfgfiles[0]
|
||||
else:
|
||||
@ -141,14 +148,16 @@ class MainIf:
|
||||
return self.carrier, self.ip, self.hostnameresult[0], self.gateway
|
||||
|
||||
|
||||
def unix_cmd(command, execute=None, stdout=PIPE):
|
||||
if execute != False: # None or True
|
||||
def unix_cmd(cmd, *args, execute=None, stdout=PIPE, sudo=True):
|
||||
command = cmd.split() + list(args)
|
||||
sudo = ['sudo'] if sudo else []
|
||||
if execute is not False: # None or True
|
||||
if execute:
|
||||
print('$ %s' % command)
|
||||
result = Popen(command.split(), stdout=stdout).communicate()[0]
|
||||
print('$', *command)
|
||||
result = Popen(sudo + command, stdout=stdout).communicate()[0]
|
||||
return (result or b'').decode()
|
||||
else:
|
||||
print('> %s' % command)
|
||||
print('>', *command)
|
||||
|
||||
|
||||
def check_service(service, set_on=None, execute=None):
|
||||
@ -185,15 +194,12 @@ def change_firewall(set_on, ports, execute=None):
|
||||
ports.add(22) # always add ssh
|
||||
active, enabled = check_service('nftables')
|
||||
if not set_on:
|
||||
if os.geteuid() == 0:
|
||||
check_service('nftables', False, execute)
|
||||
else:
|
||||
print('need sudo rights to modify firewall')
|
||||
check_service('nftables', False, execute)
|
||||
return active or enabled
|
||||
|
||||
pattern = re.compile('(tcp dport ({.*}) ct state new accept)', re.MULTILINE | re.DOTALL)
|
||||
|
||||
for filename in FIREWALL_CONF, os.path.join(BoxInfo.TOOLS, 'nftables.conf'):
|
||||
for filename in FIREWALL_CONF, BoxInfo.TOOLS / 'nftables.conf':
|
||||
with open(filename) as f:
|
||||
content = f.read()
|
||||
|
||||
|
Reference in New Issue
Block a user