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 #!/usr/bin/python3
"""install.py """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 import os
@ -15,7 +16,16 @@ from os.path import join, getmtime, exists, basename
os.chdir('/root/aputools/to_system') os.chdir('/root/aputools/to_system')
DEL = '__to_delete__' 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): def write_when_new(filename, content, doit):
@ -143,29 +153,29 @@ def network(doit):
cfgfiles = [] cfgfiles = []
for i in range(4): for i in range(4):
# goodie: look for mac addresses of all 4 ports # 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: 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: 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]
for ntry in range(1 + bool(doit)): if cfgfile != CFGPATH % (hostname, apuid):
with open('/etc/hostname') as f:
hostname = f.read().strip()
if cfgfile == CFGPATH % ('%s_%6.6x' % (hostname, apuid)):
break
if doit: if doit:
if ntry == 0: os.system('sh ../sethostname.sh')
os.system('sh sethostname.sh')
else:
print('ERROR: can not set host name')
return False
else: else:
if cfgfile: 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 show.dirty = True
break
if cfgfile is None: if cfgfile is None:
return False return False
ifname = '' ifname = ''

View File

@ -1,5 +1,4 @@
[NETWORK] [NETWORK]
address=00:0d:b9:59:1f:ac
enp1s0=192.168.127.1/24 enp1s0=192.168.127.1/24
enp2s0=192.168.2.1/24 enp2s0=192.168.2.1/24
enp3s0=192.168.3.1/24 enp3s0=192.168.3.1/24

View File

@ -1,5 +1,4 @@
[NETWORK] [NETWORK]
address=00:0d:b9:59:20:a8
enp1s0=dhcp enp1s0=dhcp
enp2s0=192.168.127.2/24 enp2s0=192.168.127.2/24
enp3s0=192.168.2.3/24 enp3s0=192.168.2.3/24

View File

@ -1,5 +1,4 @@
[NETWORK] [NETWORK]
address=00:0d:b9:5a:4c:90
enp1s0=dhcp enp1s0=dhcp
enp2s0=192.168.2.2/24 enp2s0=192.168.2.2/24
enp3s0=192.168.2.3/24 enp3s0=192.168.2.3/24

View File

@ -1,5 +1,4 @@
[NETWORK] [NETWORK]
address=00:0d:b9:5a:58:e4
enp1s0=192.168.1.1/24 enp1s0=192.168.1.1/24
enp2s0=192.168.2.2/24 enp2s0=192.168.2.2/24
enp3s0=192.168.3.3/24 enp3s0=192.168.3.3/24

View File

@ -1,7 +1,7 @@
ETHNAME=$(cat /sys/class/net/enp1s0/address) ETHNAME=$(cat /sys/class/net/enp1s0/address)
ETHNAME=${ETHNAME//:/} ETHNAME=${ETHNAME//:/}
APUID=${ETHNAME: -6} APUID=${ETHNAME: -6}
FOUND=( /root/aputools/servercfg/*_$APUID.cfg ) FOUND="$(shopt -s nullglob; echo /root/aputools/servercfg/*_$APUID.cfg)"
if [ -z "$FOUND" ]; then if [ -z "$FOUND" ]; then
HOSTNAME=apu$APUID HOSTNAME=apu$APUID
else else
@ -10,5 +10,6 @@ else
if [ $HOSTNAME != "apu$APUID" ]; then if [ $HOSTNAME != "apu$APUID" ]; then
HOSTNAME=${HOSTNAME%_*} HOSTNAME=${HOSTNAME%_*}
fi fi
echo "hostname $HOSTNAME"
fi fi
echo $HOSTNAME > /etc/hostname echo $HOSTNAME > /etc/hostname