From 53af769c3f9b894c55b2546ac8cb178a932b8cfe Mon Sep 17 00:00:00 2001 From: LIN SE Date: Wed, 24 Mar 2021 16:10:46 +0100 Subject: [PATCH] improved install.py install.py is now also dealing with ip addresses --- install.py | 132 +++++++++++++++++++++++++++-- servercfg/apumaster.cfg | 1 + servercfg/apuslave1.cfg | 5 +- servercfg/apuslave2.cfg | 11 +++ servercfg/dilsc.cfg | 11 +++ to_system/etc/profile.d/welcome.sh | 3 +- 6 files changed, 154 insertions(+), 9 deletions(-) mode change 100644 => 100755 install.py create mode 100644 servercfg/apuslave2.cfg create mode 100644 servercfg/dilsc.cfg diff --git a/install.py b/install.py old mode 100644 new mode 100755 index 3a4457b..c53f92a --- a/install.py +++ b/install.py @@ -1,4 +1,5 @@ -"""install.py: +#!/usr/bin/python3 +"""install.py copy files from to_system into system directories """ @@ -6,11 +7,15 @@ copy files from to_system into system directories import os import filecmp import shutil +from glob import glob +from ipaddress import IPv4Interface +from configparser import ConfigParser from os.path import join, getmtime, exists os.chdir('to_system') DEL = '__to_delete__' +CFGPATH = '/root/aputools/servercfg/%s.cfg' def walk(action): for dirpath, _, files in os.walk('.'): @@ -43,7 +48,10 @@ def walk(action): action.missing(missing) class Show: + dirty = False + def show(self, title, files): + self.dirty = True print('%s %s:\n %s' % (title, self.syspath, ' '.join(files))) def newer(self, files): @@ -74,7 +82,121 @@ class Do: for file in files: os.remove(join(self.syspath, file)) -walk(Show()) -answer = input('do above?') -if answer.lower().startswith('y'): - walk(Do()) + +def network(doit): + result = None + dirty = None + with open('/sys/class/net/enp1s0/address') as f: + netaddr = f.read().strip().lower() + action = 'doit' if doit else 'check' + addrdict = {} + for cfgfile in glob(CFGPATH % '*'): + parser = ConfigParser() + try: + parser.read(cfgfile) + address = parser['NETWORK']['address'].lower() + if address in addrdict: + print(cfgfile) + print(addrdict[address]) + print('ERROR: duplicate address %s in above files' % address) + result = 'failed' + addrdict[address] = cfgfile + except Exception as e: + print('ERROR: can not read %s: %r' % (cfgfile, e)) + result = 'failed' + cfgfile = addrdict.get(netaddr) + if cfgfile is None: + print('can not find cfg file for %s' % netaddr) + result = 'failed' + for ntry in range(1 + bool(doit)): + with open('/etc/hostname') as f: + hostname = f.read().strip() + if CFGPATH % hostname == cfgfile: + break + if doit: + if ntry == 0: + os.system('sh sethostname.sh') + else: + print('ERROR: can not set host name') + result = 'failed' + else: + print('host name does not match') + dirty = 'dirty' + break + if result: + return False + ifname = '' + parser = ConfigParser() + try: + parser.read(CFGPATH % hostname) + network = dict(parser['NETWORK']) + address = network.pop('address', None) + main = None + for ifname, value in network.items(): + try: + with open('/etc/sysconfig/network-scripts/ifcfg-%s' % ifname) as f: + original = f.read().strip().split('\n') + except FileNotFoundError: + raise ValueError('unknown interface %s' % ifname) + content = {} + for line in original: + key, _, val = line.strip().partition('=') + if val: + content[key] = val + if content['NAME'] != ifname: + print('ERROR: name %s does not match' % content['NAME']) + result = 'failed' + original = dict(content) + if value == 'dhcp': + if main: + print('ERROR: only one interface with DHCP allowed') + result = 'failed' + main = ifname + content.pop('IPADDR', None) + content['ONBOOT'] = 'yes' + content['BOOTPROTO'] = 'dhcp' + else: + interface = IPv4Interface(value) + if '/' not in value: + if interface < IPv4Interface('128.0.0.0'): + value += '/8' + elif interface < IPv4Interface('192.0.0.0'): + value += '/16' + else: + value += '/24' + interface = IPv4Interface(value) + content['IPADDR'], content['NETMASK'] = interface.with_netmask.split('/') + _, content['PREFIX'] = interface.with_prefixlen.split('/') + content['ONBOOT'] = 'yes' + content['BOOTPROTO'] = 'none' + if content != original: + if doit: + with open('/etc/sysconfig/network-scripts/ifcfg-%s' % ifname, 'w') as f: + f.write('%s\n' % '\n'.join('%s=%s' % kv for kv in content.items())) + else: + dirty = 'dirty' + print('%s changed:' % ifname) + diff = dict(content) + diff.update({k: '' for k in original if k not in diff}) + print(' %s' % ', '.join('%s=%s' % (k, v) for k, v in diff.items() if v != original.get(k))) + except Exception as e: + print('ERROR: can not handle %s %s: %r' % (hostname, ifname, e)) + result = 'failed' + return result or dirty + + +print('---') +show = Show() +walk(show) +result = network(False) + +if result == 'failed': + print('fix first above errors') +elif result == 'dirty' or show.dirty: + print('---') + answer = input('do above? ') + if answer.lower().startswith('y'): + network(True) + walk(Do()) +else: + print('nothing to do') diff --git a/servercfg/apumaster.cfg b/servercfg/apumaster.cfg index 703529a..525b36d 100644 --- a/servercfg/apumaster.cfg +++ b/servercfg/apumaster.cfg @@ -1,5 +1,6 @@ [NETWORK] address=00:0d:b9:59:1f:ac +enp1s0=dhcp enp2s0=192.168.127.2/24 enp3s0=192.168.2.3/24 enp4s0=192.168.2.4/24 diff --git a/servercfg/apuslave1.cfg b/servercfg/apuslave1.cfg index 03edce8..f3fa589 100644 --- a/servercfg/apuslave1.cfg +++ b/servercfg/apuslave1.cfg @@ -1,7 +1,6 @@ -[HOSTNAME] -apuslave1=00:0d:b9:59:20:a8 - [NETWORK] +address=00:0d:b9:59:20:a8 +enp1s0=dhcp enp2s0=192.168.127.2/24 enp3s0=192.168.2.3/24 enp4s0=192.168.2.4/24 diff --git a/servercfg/apuslave2.cfg b/servercfg/apuslave2.cfg new file mode 100644 index 0000000..8ae37bb --- /dev/null +++ b/servercfg/apuslave2.cfg @@ -0,0 +1,11 @@ +[NETWORK] +address=00:0d:b9:5a:58:e4 +enp1s0=dhcp +enp2s0=192.168.2.2/24 +enp3s0=192.168.2.3/24 +enp4s0=192.168.127.4/24 + +[ROUTER] +3000=/dev/ttyUSB0 +5900=192.168.2.33 +8080=192.168.127.254:80 diff --git a/servercfg/dilsc.cfg b/servercfg/dilsc.cfg new file mode 100644 index 0000000..3739da9 --- /dev/null +++ b/servercfg/dilsc.cfg @@ -0,0 +1,11 @@ +[NETWORK] +address=00:0d:b9:5a:4c:90 +enp1s0=dhcp +enp2s0=192.168.2.2/24 +enp3s0=192.168.2.3/24 +enp4s0=192.168.127.4/24 + +[ROUTER] +3000=/dev/ttyUSB0 +5900=192.168.2.33 +8080=192.168.127.254:80 diff --git a/to_system/etc/profile.d/welcome.sh b/to_system/etc/profile.d/welcome.sh index 4f6f331..a6dd1c0 100755 --- a/to_system/etc/profile.d/welcome.sh +++ b/to_system/etc/profile.d/welcome.sh @@ -1,2 +1,3 @@ -echo "Welcome to $HOSTNAME $(hostname -I) $(cat /sys/class/net/enp1s0/address)" +echo "Welcome to $HOSTNAME $(hostname -I)" +echo "$(cat /sys/class/net/enp1s0/address)" export EDITOR=nano