changed naming of cfg files

+ create ifcfg files again directly
This commit is contained in:
2021-04-27 10:39:33 +02:00
parent 8215efe9be
commit f188b34e22
5 changed files with 54 additions and 45 deletions

View File

@@ -17,8 +17,22 @@ os.chdir('/root/aputools/to_system')
DEL = '__to_delete__'
CFGPATH = '/root/aputools/servercfg/%s.cfg'
def create_if(name, mac, cfg):
result=dict(
def write_when_new(filename, content, doit):
if not content.endswith('\n'):
content += '\n'
with open(filename) as fil:
old = fil.read()
if old == content:
return False
if doit:
with open(filename, 'w') as fil:
fil.write(content)
return True
def create_if(name, cfg):
result = dict(
TYPE='Ethernet',
NAME=name,
DEVICE=name,
@@ -77,6 +91,7 @@ def walk(action):
if missing:
action.missing(missing)
class Show:
dirty = False
@@ -96,6 +111,7 @@ class Show:
def delete(self, files):
self.show('remove from', files)
class Do:
def newer(self, files):
for file in files:
@@ -115,64 +131,57 @@ class Do:
IFNAMES = ['enp%ds0' % i for i in range(1,5)]
def network(doit):
result = True
netaddr_dict = {}
for ifname in IFNAMES:
with open('/sys/class/net/%s/address' % ifname) as f:
netaddr_dict[ifname] = f.read().strip().lower()
netaddr = netaddr_dict[IFNAMES[0]]
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)
addrdict[address] = cfgfile
except Exception as e:
print('ERROR: can not read %s: %r' % (cfgfile, e))
result = False
cfgfile = addrdict.get(netaddr)
if cfgfile is None:
print('can not find cfg file for %s' % netaddr)
result = False
apuid = int(''.join(netaddr.split(':')[-3:]), 16) & 0xfffffc
cfgfile = None
cfgfiles = []
for i in range(4):
# goodie: look for mac addresses of all 4 ports
cfgfiles += glob(CFGPATH % ('*_%6.6x' % (apuid + i)))
if not cfgfiles:
print('no cfg file found for %s' % netaddr)
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 CFGPATH % hostname == cfgfile:
if hostname == cfgfile[:-7]: # stripped apuid from cfgfile
break
if doit:
if ntry == 0:
os.system('sh sethostname.sh')
else:
print('ERROR: can not set host name')
result = False
return False
else:
print('host name does not match')
break
if not result:
if cfgfile is None:
return False
ifname = ''
parser = ConfigParser()
try:
parser.read(CFGPATH % hostname)
parser.read(cfgfile)
network = dict(parser['NETWORK'])
address = network.pop('address', None)
main = None
for ifname in IFNAMES:
content = create_if(ifname, netaddr_dict[ifname],
network.get(ifname, 'off'))
with open('etc/sysconfig/network-scripts/ifcfg-%s' % ifname, 'w') as f:
f.write('\n'.join('%s=%s' % kv for kv in content.items())+ '\n')
content = create_if(ifname, network.get(ifname, 'off'))
content = '\n'.join('%s=%s' % kv for kv in content.items())
todo = write_when_new('/etc/sysconfig/network-scripts/ifcfg-%s' % ifname, content, doit)
if todo:
print('change ', ifname)
except Exception as e:
print('ERROR: can not handle %s %s: %r' % (hostname, ifname, e))
result = False
return result
return False
return True
print('---')
show = Show()