Commit 92143960 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Version 2013-09-17 15:46

M  src/hostinfo.py
M  src/hostinfo/ifconfig.py
parent 5ff6ac00
......@@ -269,7 +269,7 @@ if __name__ == '__main__':
(options, args) = optParser.parse_args(sys.argv[1:])
host = options.host or os.uname()[1]
host = options.host or os.uname()[1].split('.')[0]
if not args:
tree = hostinfo.parser.parse("/etc/hostinfo.xml")
......
......@@ -2,108 +2,150 @@ from hostinfo.util import aton
import subprocess
import re
def generate(tree, host):
result = []
for h in filter(lambda h: h.name[0] == host, tree._host_):
for i, d in filter(lambda (i,d): i and d,
map(lambda i: (i, device(i.ethernet[0])),
h._interface_)):
static = True
if not i._dhcpserver_ and not i._static_:
for ip in i._ip_:
if ip.address[0] and not (ip.alias[0] or ip.vlan[0]):
static = False
pass
pass
pass
config = "DEVICE=%s\n" % d
config += "HWADDR=%s\n" % i.ethernet[0].upper()
config += "ONBOOT=yes\n"
config += "TYPE=Ethernet\n"
pre = ""
index = ""
if not static:
config += "BOOTPROTO=dhcp\n"
pre= "# "
pass
else:
config += "BOOTPROTO=none\n"
def get_uuid(device):
try:
import dbus
obj = dbus.SystemBus().get_object('com.redhat.ifcfgrh1',
'/com/redhat/ifcfgrh1')
interface = dbus.Interface(obj, "com.redhat.ifcfgrh1")
path = '/etc/sysconfig/network-scripts/ifcfg-%s' % device
uuid = interface.GetIfcfgDetails(path)[0]
return uuid
except:
return None
pass
def is_static(interface):
if not interface._dhcpserver_ and not interface._static_:
for ip in interface._ip_:
if ip.address[0] and not (ip.alias[0] or ip.vlan[0]):
return False
pass
nameservers = []
search = []
for s, ip in map(lambda ip: (subnet(tree, ip.address[0]), ip),
filter(lambda ip: (ip.address[0] and
(static or
(not ip.vlan[0] and
ip.alias[0]))),
i._ip_)):
if ip.search[0]:
search.extend(ip.search[0].split())
pass
config += "%sIPADDR%s=%s\n" % (pre, index, ip.address[0])
netmask = (ip.netmask[0] or
s and s.netmask[0] or
'255.255.255.255')
gateway = ip.gateway[0] or s and s.gateway[0]
network = ip.network[0] or s and s.network[0]
broadcast = ip.broadcast[0] or s and s.broadcast[0]
name_servers = ip.name_servers[0] or s and s.name_servers[0]
if netmask:
config += "%sNETMASK%s=%s\n" % (pre, index, netmask)
pass
if gateway:
config += "%sGATEWAY%s=%s\n" % (pre, index, gateway)
pass
if network:
config += "%sNETWORK%s=%s\n" % (pre, index,network)
pass
if broadcast:
config += "%sBROADCAST%s=%s\n" % (pre, index, broadcast)
pass
pass
return True
def generate_ifcfg(tree, interface):
config = []
static_config = []
search = []
nameservers = []
index = ''
for ip in interface._ip_:
if not (ip.address[0] and (is_static(interface) or
(not ip.vlan[0] and
ip.alias[0]))):
continue
sub = subnet(tree, ip.address[0])
if ip.search[0]:
search.extend(ip.search[0].split())
pass
static_config.append('IPADDR%s=%s' % (index, ip.address[0]))
netmask = ip.netmask[0] or sub and sub.netmask[0] or '255.255.255.255'
gateway = ip.gateway[0] or sub and sub.gateway[0]
network = ip.network[0] or sub and sub.network[0]
broadcast = ip.broadcast[0] or sub and sub.broadcast[0]
name_servers = ip.name_servers[0] or sub and sub.name_servers[0]
if netmask:
static_config.append('NETMASK%s=%s' % (index, netmask))
pass
if gateway:
static_config.append('GATEWAY%s=%s' % (index, gateway))
pass
if network:
static_config.append('NETWORK%s=%s' % (index,network))
pass
if broadcast:
static_config.append('BROADCAST%s=%s' % (index, broadcast))
pass
if sub:
for n in re.split('[, ]+', sub.name_servers[0]):
# Domain nameservers
if not n in nameservers:
nameservers.append(n)
pass
#if not s:
# config += "%sNETMASK%s=255.255.255.255\n" % (pre, index)
# pass
#else:
# config += "%sGATEWAY%s=%s\n" % (pre, index, s.gateway[0])
# config += "%sNETMASK%s=%s\n" % (pre, index, s.netmask[0])
# config += "%sNETWORK%s=%s\n" % (pre, index, s.network[0])
# config += "%sBROADCAST%s=%s\n" % (pre, index,
# s.broadcast[0])
if s:
for n in re.split("[, ]+", s.name_servers[0]):
# Domain nameservers
if not n in nameservers:
nameservers.append(n)
pass
pass
for n in i._nameserver_:
if n.domain[0] == s.domain[0]:
if not '127.0.0.1' in nameservers:
# Insert own address first in nameserver list
nameservers.insert(0, '127.0.0.1')
pass
pass
pass
for n in interface._nameserver_:
if n.domain[0] == sub.domain[0]:
if not '127.0.0.1' in nameservers:
# Insert own address first in nameserver list
nameservers.insert(0, '127.0.0.1')
pass
pass
index = int("0%s" % index)+1
pass
if i.defroute[0]:
config += "DEFROUTE=%s\n" % i.defroute[0]
pass
index = int('0%s' % index)+1
pass
if interface.defroute[0]:
config.append('DEFROUTE=%s' % interface.defroute[0])
pass
index = 0
for n in nameservers:
index += 1
static_config.append('DNS%d=%s' % (index, n))
pass
if search:
static_config.append('SEARCH="%s"' % (' '.join(search)))
pass
if not is_static(interface):
config.insert(0, 'BOOTPROTO=dhcp')
config.extend(map(lambda s: '# %s' % s, static_config))
pre= '# '
pass
else:
config.insert(0, 'BOOTPROTO=none')
config.extend(static_config)
pass
return '\n'.join(config) + '\n'
def generate(tree, host):
result = []
for i in tree._host_._interface_:
if i._parent.name[0] != host or not device(i.ethernet[0]):
continue
device_name = device(i.ethernet[0])
get_uuid(device_name)
if i.bridge[0]:
config = 'DEVICE=%s\n' % i.bridge[0]
config += 'NAME="%s"\n' % i.bridge[0]
config += 'ONBOOT=yes\n'
config += 'TYPE=Bridge\n'
config += 'STP=off\n'
uuid = get_uuid(i.bridge[0])
if uuid:
config += 'UUID=%s\n' % uuid
pass
if static:
# Nameservers
index = 0
for n in nameservers:
index += 1
config += "DNS%d=%s\n" % (index, n)
pass
config += generate_ifcfg(tree, i)
result.append(('ifcfg-%s' % i.bridge[0], config))
config = 'DEVICE=%s\n' % device_name
config += 'NAME="%s"\n' % device_name
config += 'HWADDR=%s\n' % i.ethernet[0].upper()
config += 'ONBOOT=yes\n'
config += 'TYPE=Ethernet\n'
config += 'BRIDGE=%s\n' % i.bridge[0]
uuid = get_uuid(device_name)
if uuid:
config += 'UUID=%s\n' % uuid
pass
if search:
config += 'SEARCH="%s"\n' % (" ".join(search))
result.append(('ifcfg-%s' % device_name, config))
pass
elif i.ethernet[0]:
config = 'DEVICE=%s\n' % device_name
config += 'NAME="%s"\n' % device_name
config += 'HWADDR=%s\n' % i.ethernet[0].upper()
config += 'ONBOOT=yes\n'
config += 'TYPE=Ethernet\n'
uuid = get_uuid(device_name)
if uuid:
config += 'UUID=%s\n' % uuid
pass
result.append(("ifcfg-%s" % d, config))
config += generate_ifcfg(tree, i)
result.append(('ifcfg-%s' % device_name, config))
pass
pass
return result
......
Supports Markdown
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