Commit 2fadf63d authored by zolliker's avatar zolliker
Browse files

ifcfg scripts are now created from scratch

- ifcfg script do not contain UUID any more
parent 53af769c
......@@ -17,6 +17,28 @@ os.chdir('to_system')
DEL = '__to_delete__'
CFGPATH = '/root/aputools/servercfg/%s.cfg'
def create_if(name, mac, cfg):
result=dict(TYPE='Ethernet', NAME=name, DEVICE=name,
BOOTPROTO='none', ONBOOT='yes')
if cfg == 'off':
result['ONBOOT']='no'
elif cfg == 'dhcp':
result['BOOTPROTO']='dhcp'
else:
interface = IPv4Interface(cfg)
if '/' not in cfg:
if interface < IPv4Interface('128.0.0.0'):
cfg += '/8'
elif interface < IPv4Interface('192.0.0.0'):
cfg += '/16'
else:
cfg += '/24'
interface = IPv4Interface(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('.'):
syspath = dirpath[1:] # remove leading '.'
......@@ -83,11 +105,15 @@ class Do:
os.remove(join(self.syspath, file))
IFNAMES = ['enp%ds0' % i for i in range(1,5)]
def network(doit):
result = None
dirty = None
with open('/sys/class/net/enp1s0/address') as f:
netaddr = f.read().strip().lower()
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 % '*'):
......@@ -99,15 +125,14 @@ def network(doit):
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'
result = False
cfgfile = addrdict.get(netaddr)
if cfgfile is None:
print('can not find cfg file for %s' % netaddr)
result = 'failed'
result = False
for ntry in range(1 + bool(doit)):
with open('/etc/hostname') as f:
hostname = f.read().strip()
......@@ -118,12 +143,11 @@ def network(doit):
os.system('sh sethostname.sh')
else:
print('ERROR: can not set host name')
result = 'failed'
result = False
else:
print('host name does not match')
dirty = 'dirty'
break
if result:
if not result:
return False
ifname = ''
parser = ConfigParser()
......@@ -132,67 +156,24 @@ def network(doit):
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)))
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')
except Exception as e:
print('ERROR: can not handle %s %s: %r' % (hostname, ifname, e))
result = 'failed'
return result or dirty
result = False
return result
print('---')
show = Show()
walk(show)
result = network(False)
walk(show)
if result == 'failed':
if not result:
print('fix first above errors')
elif result == 'dirty' or show.dirty:
elif show.dirty:
print('---')
answer = input('do above? ')
if answer.lower().startswith('y'):
......
[NETWORK]
address=00:0d:b9:5a:58:e4
address=00:0d:b9:5a:4c:90
enp1s0=dhcp
enp2s0=192.168.2.2/24
enp3s0=192.168.2.3/24
......
[NETWORK]
address=00:0d:b9:5a:4c:90
enp1s0=dhcp
address=00:0d:b9:5a:58:e4
enp1s0=192.168.2.1/24
enp2s0=192.168.2.2/24
enp3s0=192.168.2.3/24
enp4s0=192.168.127.4/24
enp4s0=dhcp
[ROUTER]
3000=/dev/ttyUSB0
......
echo "Welcome to $HOSTNAME $(hostname -I)"
echo "$(cat /sys/class/net/enp1s0/address)"
echo "$(cat /sys/class/net/enp4s0/address)"
export EDITOR=nano
TYPE=Ethernet
NAME=enp1s0
DEVICE=enp1s0
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.2.1
NETMASK=255.255.255.0
PREFIX=24
TYPE=Ethernet
NAME=enp2s0
DEVICE=enp2s0
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.2.2
NETMASK=255.255.255.0
PREFIX=24
TYPE=Ethernet
NAME=enp3s0
DEVICE=enp3s0
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.2.3
NETMASK=255.255.255.0
PREFIX=24
TYPE=Ethernet
NAME=enp4s0
DEVICE=enp4s0
BOOTPROTO=dhcp
ONBOOT=yes
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