first version of dhcp settings
create /etc/dhcp/dhcpd.conf in install.py + new cfg file for apuslave3
This commit is contained in:
70
install.py
70
install.py
@ -27,6 +27,19 @@ enp4s0=dhcp
|
||||
3001=192.168.127.254:3001
|
||||
"""
|
||||
|
||||
DHCP_HEADER = """
|
||||
default-lease-time 600;
|
||||
max-lease-time 7200;
|
||||
authoritative;
|
||||
"""
|
||||
|
||||
DHCP_ITEM = """
|
||||
subnet %s netmask %s {
|
||||
option routers %s;
|
||||
option subnet-mask %s;
|
||||
range %s %s;
|
||||
}
|
||||
"""
|
||||
|
||||
def write_when_new(filename, content, doit):
|
||||
if not content.endswith('\n'):
|
||||
@ -38,6 +51,12 @@ def write_when_new(filename, content, doit):
|
||||
if doit:
|
||||
with open(filename, 'w') as fil:
|
||||
fil.write(content)
|
||||
else:
|
||||
print('===')
|
||||
print(old)
|
||||
print('---')
|
||||
print(content)
|
||||
print('===')
|
||||
return True
|
||||
|
||||
|
||||
@ -57,17 +76,44 @@ def create_if(name, cfg, mac):
|
||||
elif cfg == 'dhcp':
|
||||
result['BOOTPROTO']='dhcp'
|
||||
else:
|
||||
interface = IPv4Interface(cfg)
|
||||
if '/' not in cfg:
|
||||
error = None
|
||||
cfgdict = {'own': cfg, 'prefix': '', 'other': ''}
|
||||
if ':' in cfg:
|
||||
for cfgitem in cfg.split(','):
|
||||
k, _, v = cfgitem.partition(':')
|
||||
k = k.strip()
|
||||
if k not in cfgdict:
|
||||
error = 'bad formed'
|
||||
cfgdict[k] = v.strip()
|
||||
ownip, _, prefix = cfgdict['own'].partition('/')
|
||||
prefix = cfgdict['prefix'] or prefix
|
||||
if prefix:
|
||||
try:
|
||||
prefix = int(prefix)
|
||||
if not 16 <= prefix < 32:
|
||||
error = 'illegal prefix in'
|
||||
except ValueError:
|
||||
error = 'illegal prefix in'
|
||||
if error:
|
||||
print('Allowed forms:\n')
|
||||
print('%s=n.n.n.n/24' % name)
|
||||
print('%s=prefix:24,own:n.n.n.n,other:m.m.m.m\n' % name)
|
||||
raise ValueError('%s network config: %s=%s' % (error, name, cfg))
|
||||
if not prefix:
|
||||
interface = IPv4Interface(ownip)
|
||||
if interface < IPv4Interface('128.0.0.0'):
|
||||
cfg += '/8'
|
||||
prefix = 8
|
||||
elif interface < IPv4Interface('192.0.0.0'):
|
||||
cfg += '/16'
|
||||
prefix = 16
|
||||
else:
|
||||
cfg += '/24'
|
||||
interface = IPv4Interface(cfg)
|
||||
prefix = 24
|
||||
interface = IPv4Interface('%s/%d' % (ownip, prefix))
|
||||
result['IPADDR'], result['NETMASK'] = interface.with_netmask.split('/')
|
||||
_, result['PREFIX'] = interface.with_prefixlen.split('/')
|
||||
other = cfgdict['other']
|
||||
if other:
|
||||
adrs = cfgdict['other'].split('-')
|
||||
result['dhcprange'] = (adrs[0], adrs[-1])
|
||||
return result
|
||||
|
||||
|
||||
@ -180,18 +226,30 @@ def network(doit):
|
||||
return False
|
||||
ifname = ''
|
||||
parser = ConfigParser()
|
||||
dh = []
|
||||
try:
|
||||
parser.read(cfgfile)
|
||||
network = dict(parser['NETWORK'])
|
||||
for ifname in IFNAMES:
|
||||
content = create_if(ifname, network.get(ifname, 'off'), netaddr_dict[ifname])
|
||||
dhcprange = content.pop('dhcprange', None)
|
||||
if dhcprange:
|
||||
ip_mask = content['IPADDR'], content['NETMASK']
|
||||
dh.append(ip_mask + ip_mask + dhcprange)
|
||||
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)
|
||||
show.dirty = True
|
||||
if dh:
|
||||
content = DHCP_HEADER + '\n'.join([DHCP_ITEM % d for d in dh])
|
||||
todo = write_when_new('/etc/dhcp/dhcpd.conf', content, doit)
|
||||
if todo:
|
||||
print('change ', ifname)
|
||||
show.dirty = True
|
||||
except Exception as e:
|
||||
print('ERROR: can not handle %s %s: %r' % (hostname, ifname, e))
|
||||
raise
|
||||
return False
|
||||
return True
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
[NETWORK]
|
||||
enp1s0=dhcp
|
||||
enp2s0=192.168.2.2/24
|
||||
enp2s0=own:192.168.2.2,prefix:24,other:192.168.2.15
|
||||
enp3s0=192.168.2.3/24
|
||||
enp4s0=192.168.127.4/24
|
||||
|
||||
|
Reference in New Issue
Block a user