From 11d6e7d8e7ee527649b0b00fd3852d27fe6fb662 Mon Sep 17 00:00:00 2001 From: LIN SE Date: Tue, 27 Apr 2021 14:47:34 +0200 Subject: [PATCH] ask for hostname and create cfg file if not found + remove NETWORK.address setting --- install.py | 42 +++++++++++++++++++++------------- servercfg/apumaster_591fac.cfg | 1 - servercfg/apuslave1_5920a8.cfg | 1 - servercfg/apuslave3_5a4c90.cfg | 1 - servercfg/dilsc_5a58e4.cfg | 1 - sethostname.sh | 3 ++- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/install.py b/install.py index a6dfbbe..c1f9338 100755 --- a/install.py +++ b/install.py @@ -1,7 +1,8 @@ #!/usr/bin/python3 """install.py -copy files from to_system into system directories +- copy files from to_system into system directories +- set host name / network settings from aputools/servercfg file """ import os @@ -15,7 +16,16 @@ from os.path import join, getmtime, exists, basename os.chdir('/root/aputools/to_system') DEL = '__to_delete__' -CFGPATH = '/root/aputools/servercfg/%s.cfg' +CFGPATH = '/root/aputools/servercfg/%s_%6.6x.cfg' +TEMPLATE_CFG = """[NETWORK] +enp1s0=192.168.127.1/24 +enp2s0=192.168.2.1/24 +enp3s0=192.168.3.1/24 +enp4s0=dhcp + +[ROUTER] +3001=192.168.127.254:3001 +""" def write_when_new(filename, content, doit): @@ -143,29 +153,29 @@ def network(doit): cfgfiles = [] for i in range(4): # goodie: look for mac addresses of all 4 ports - cfgfiles += glob(CFGPATH % ('*_%6.6x' % (apuid + i))) + cfgfiles += glob(CFGPATH % ('*', apuid + i)) + with open('/etc/hostname') as f: + hostname = f.read().strip() if not cfgfiles: - print('no cfg file found for %s' % netaddr) + print('no cfg file found for %s' % hostname) + newname = input('enter host name: ') + if not newname: + print('no hostname given') + return False + cfgfile = CFGPATH % (newname, apuid) + with open(cfgfile, 'w') as f: + f.write(TEMPLATE_CFG) elif len(cfgfiles) > 1: print('ERROR: ambiguous cfg files: %s' % ', '.join(cfgfiles)) else: cfgfile = cfgfiles[0] - for ntry in range(1 + bool(doit)): - with open('/etc/hostname') as f: - hostname = f.read().strip() - if cfgfile == CFGPATH % ('%s_%6.6x' % (hostname, apuid)): - break + if cfgfile != CFGPATH % (hostname, apuid): if doit: - if ntry == 0: - os.system('sh sethostname.sh') - else: - print('ERROR: can not set host name') - return False + os.system('sh ../sethostname.sh') else: if cfgfile: - print('host name does not match %r -> %r' % (hostname, basename(cfgfile).rpartition('_')[0])) + print('replace host name %r by %r' % (hostname, basename(cfgfile).rpartition('_')[0])) show.dirty = True - break if cfgfile is None: return False ifname = '' diff --git a/servercfg/apumaster_591fac.cfg b/servercfg/apumaster_591fac.cfg index a9de8a7..ab8febd 100644 --- a/servercfg/apumaster_591fac.cfg +++ b/servercfg/apumaster_591fac.cfg @@ -1,5 +1,4 @@ [NETWORK] -address=00:0d:b9:59:1f:ac enp1s0=192.168.127.1/24 enp2s0=192.168.2.1/24 enp3s0=192.168.3.1/24 diff --git a/servercfg/apuslave1_5920a8.cfg b/servercfg/apuslave1_5920a8.cfg index f3fa589..b7008ab 100644 --- a/servercfg/apuslave1_5920a8.cfg +++ b/servercfg/apuslave1_5920a8.cfg @@ -1,5 +1,4 @@ [NETWORK] -address=00:0d:b9:59:20:a8 enp1s0=dhcp enp2s0=192.168.127.2/24 enp3s0=192.168.2.3/24 diff --git a/servercfg/apuslave3_5a4c90.cfg b/servercfg/apuslave3_5a4c90.cfg index 3739da9..fae2fab 100644 --- a/servercfg/apuslave3_5a4c90.cfg +++ b/servercfg/apuslave3_5a4c90.cfg @@ -1,5 +1,4 @@ [NETWORK] -address=00:0d:b9:5a:4c:90 enp1s0=dhcp enp2s0=192.168.2.2/24 enp3s0=192.168.2.3/24 diff --git a/servercfg/dilsc_5a58e4.cfg b/servercfg/dilsc_5a58e4.cfg index 438e92c..243597e 100644 --- a/servercfg/dilsc_5a58e4.cfg +++ b/servercfg/dilsc_5a58e4.cfg @@ -1,5 +1,4 @@ [NETWORK] -address=00:0d:b9:5a:58:e4 enp1s0=192.168.1.1/24 enp2s0=192.168.2.2/24 enp3s0=192.168.3.3/24 diff --git a/sethostname.sh b/sethostname.sh index 70ac614..aaab415 100644 --- a/sethostname.sh +++ b/sethostname.sh @@ -1,7 +1,7 @@ ETHNAME=$(cat /sys/class/net/enp1s0/address) ETHNAME=${ETHNAME//:/} APUID=${ETHNAME: -6} -FOUND=( /root/aputools/servercfg/*_$APUID.cfg ) +FOUND="$(shopt -s nullglob; echo /root/aputools/servercfg/*_$APUID.cfg)" if [ -z "$FOUND" ]; then HOSTNAME=apu$APUID else @@ -10,5 +10,6 @@ else if [ $HOSTNAME != "apu$APUID" ]; then HOSTNAME=${HOSTNAME%_*} fi + echo "hostname $HOSTNAME" fi echo $HOSTNAME > /etc/hostname