Commit f188b34e authored by zolliker's avatar zolliker
Browse files

changed naming of cfg files

+ create ifcfg files again directly
parent 8215efe9
......@@ -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,
......@@ -36,7 +50,7 @@ def create_if(name, mac, cfg):
interface = IPv4Interface(cfg)
if '/' not in cfg:
if interface < IPv4Interface('128.0.0.0'):
cfg += '/8'
cfg += '/8'
elif interface < IPv4Interface('192.0.0.0'):
cfg += '/16'
else:
......@@ -45,7 +59,7 @@ def create_if(name, mac, cfg):
result['IPADDR'], result['NETMASK'] = interface.with_netmask.split('/')
_, result['PREFIX'] = interface.with_prefixlen.split('/')
return result
def walk(action):
for dirpath, _, files in os.walk('.'):
......@@ -77,37 +91,39 @@ def walk(action):
if missing:
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):
self.show('get from', files)
def older(self, files):
self.show('replace in', files)
def missing(self, files):
self.show('install in', files)
def delete(self, files):
self.show('remove from', files)
class Do:
def newer(self, files):
for file in files:
shutil.copy(join(self.syspath, file), join(self.dirpath, file))
def older(self, files):
for file in files:
shutil.copy(join(self.dirpath, file), join(self.syspath, file))
def missing(self, files):
self.older(files)
def delete(self, files):
for file in files:
os.remove(join(self.syspath, file))
......@@ -115,65 +131,58 @@ 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:
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()
result = network(False)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment