ask for hostname and create cfg file if not found

+ remove NETWORK.address setting
This commit is contained in:
2021-04-27 14:47:34 +02:00
parent 147bd4efa5
commit 11d6e7d8e7
6 changed files with 28 additions and 21 deletions

View File

@ -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 = ''

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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