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

Version 2013-11-07 16:13

M  src/hostinfo/dhcpd.py
M  src/hostinfo/util.py
M  src/hostinfo/yp.py
parent a42d3838
...@@ -90,15 +90,15 @@ class "pxeclient" { ...@@ -90,15 +90,15 @@ class "pxeclient" {
} else { } else {
filename "pxelinux.0"; filename "pxelinux.0";
} }
} }"""
"""
PXE_DENY=""" PXE_DENY="""
authoritative;
class "pxeclient" { class "pxeclient" {
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient"; match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
ignore booting; ignore booting;
} }"""
"""
def MacOS_NETBOOT(dhcphost): def MacOS_NETBOOT(dhcphost):
if not os.path.exists('/local/macos'): if not os.path.exists('/local/macos'):
...@@ -152,7 +152,6 @@ def generate(tree, options): ...@@ -152,7 +152,6 @@ def generate(tree, options):
result.append_lines(""" result.append_lines("""
|ddns-update-style none; |ddns-update-style none;
|authoritative;
|get-lease-hostnames true; |get-lease-hostnames true;
|use-host-decl-names on; |use-host-decl-names on;
""") """)
...@@ -210,7 +209,10 @@ def emit_network(tree, options, subnet, dhcp): ...@@ -210,7 +209,10 @@ def emit_network(tree, options, subnet, dhcp):
assert first in net, '%s not part of %s' % (first, net) assert first in net, '%s not part of %s' % (first, net)
assert last in net, '%s not part of %s' % (last, net) assert last in net, '%s not part of %s' % (last, net)
if pxeboot == 'no': if pxeboot == 'no':
result += " range %s %s;" % (first, last) result.append_lines("""
| pool {
| range %(first)s %(last)s;
| }""" % dict(first=first, last=last))
pass pass
elif pxeboot == 'only': elif pxeboot == 'only':
result.append_lines(""" result.append_lines("""
...@@ -239,7 +241,6 @@ def emit_subnet_info(subnet): ...@@ -239,7 +241,6 @@ def emit_subnet_info(subnet):
if subnet.ntp_servers[0]: if subnet.ntp_servers[0]:
result += "option ntp-servers %s;" % (subnet.ntp_servers[0]) result += "option ntp-servers %s;" % (subnet.ntp_servers[0])
pass pass
result += ''
return result return result
def emit_hosts(tree, options, networks): def emit_hosts(tree, options, networks):
...@@ -248,28 +249,31 @@ def emit_hosts(tree, options, networks): ...@@ -248,28 +249,31 @@ def emit_hosts(tree, options, networks):
return filter(lambda n: a in n, networks) return filter(lambda n: a in n, networks)
static = {} static = {}
never = {} never = {}
for ip in tree._host_._interface_._ip_: for interface in tree._host_._interface_:
# Find all hosts that associated with this network ethernet = interface.ethernet[0:]
ethernet = ip.ethernet[0:]
if not ethernet: if not ethernet:
continue continue
if ethernet.lower() != ethernet: if ethernet.lower() != ethernet:
raise util.HostinfoException('%s not lower-case' % ethernet) raise util.HostinfoException('%s not lower-case' % ethernet)
if ip.never[0]: for ip in interface._ip_:
if match(util.address(ip.never[0])): # Find all hosts that associated with this network
if not ethernet in never: if ip.never[0]:
never[ethernet] = [] if match(util.address(ip.never[0])):
if not ethernet in never:
never[ethernet] = []
pass
never[ethernet].append(ip)
pass pass
never[ethernet].append(ip) continue
pass if match(util.address(ip)):
continue if not ethernet in static:
if match(util.address(ip)): static[ethernet] = []
if not ethernet in static: pass
static[ethernet] = [] static[ethernet].append(ip)
pass continue
static[ethernet].append(ip)
pass pass
pass pass
def by_name(ether_ip_dict): def by_name(ether_ip_dict):
result = {} result = {}
for e in ether_ip_dict: for e in ether_ip_dict:
...@@ -278,10 +282,11 @@ def emit_hosts(tree, options, networks): ...@@ -278,10 +282,11 @@ def emit_hosts(tree, options, networks):
raise util.HostinfoException('Multiple names %s' % name, raise util.HostinfoException('Multiple names %s' % name,
where=ether_ip_dict[e]) where=ether_ip_dict[e])
name = name.pop() name = name.pop()
result[name] = (e, ether_ip_dict[e]) key = '%s_%s' % (name, e)
result[key] = (name, e, ether_ip_dict[e])
pass pass
for name in sorted(result): for key in sorted(result):
ether, ip = result[name] name, ether, ip = result[key]
yield name, ether, ip yield name, ether, ip
pass pass
pass pass
...@@ -293,8 +298,7 @@ def emit_hosts(tree, options, networks): ...@@ -293,8 +298,7 @@ def emit_hosts(tree, options, networks):
| max-lease-time 315360000; # 10 years | max-lease-time 315360000; # 10 years
|""") |""")
for name, ether, ip in by_name(static): for name, ether, ip in by_name(static):
if ether in never: if ether in never: never.pop(ether)
never.pop(ether)
result.append_lines(""" result.append_lines("""
| host %(id)s { | host %(id)s {
| hardware ethernet %(ethernet)s; | hardware ethernet %(ethernet)s;
......
...@@ -19,6 +19,17 @@ def address(ip): ...@@ -19,6 +19,17 @@ def address(ip):
else: else:
return None return None
def subnet(tree, addr):
def match(subnet):
net = network(subnet)
return net and addr in net
result = filter(match, tree._subnet_)
if len(result) > 1:
raise HostinfoException('%s matches multiple networks' % (addr),
where=result)
return result and result[0] or None
def aton(addr): def aton(addr):
result = long(0) result = long(0)
for s in addr.split('.'): for s in addr.split('.'):
...@@ -48,15 +59,13 @@ def fqn(tree, host): ...@@ -48,15 +59,13 @@ def fqn(tree, host):
raise Exception('Unexpected tag: %s', host._tag) raise Exception('Unexpected tag: %s', host._tag)
if ip_addr: if ip_addr:
for s in tree._subnet_: s = subnet(tree, ip_addr)
net = network(s) if not s or not s.domain[0]:
if net and ip_addr in net: return None
return "%s.%s." % (host.name[1:], s.domain[0]) return "%s.%s." % (host.name[1:], s.domain[0])
pass
raise Exception("No subnet declaration for '%s' (%s)" %
(host.name[0:], ip_addr))
else: else:
raise Exception("'%s' not FQN, but has no ip" % host.name[0:]) raise Exception("'%s' not FQN, but has no ip %s" %
(host.name[0:], host))
pass pass
def by_ip(a, b): def by_ip(a, b):
......
from hostinfo.util import fqn, aton, ntoa, by_ip, by_mac #from hostinfo.util import fqn, aton, ntoa, by_ip, by_mac
import hostinfo.util as util
def generate(tree, auto_domain): def generate(tree, auto_domain):
result = [] result = []
...@@ -10,50 +11,35 @@ def generate(tree, auto_domain): ...@@ -10,50 +11,35 @@ def generate(tree, auto_domain):
return result return result
def ypsuffix(tree, ip): def ypsuffix(tree, ip):
for n in tree._subnet_: s = util.subnet(tree, util.address(ip))
try: return s and s.yp_suffix[0:] or ''
netmask = aton(n.netmask[0])
subnet = aton(n.network[0]) & netmask
ipnet = aton(ip) & netmask
if subnet == ipnet and n.yp_suffix[0:]:
return n.yp_suffix[0:]
pass
except:
pass
pass
return ''
def hosts(tree): def hosts(tree):
result = "" result = ""
host = {} host = {}
for i in tree._host_._interface_._ip_: for i,address in [ (i, util.address(i))
if i.address[0]: for i in tree._host_._interface_._ip_
host[i.address[0]] = i.name[0:] + ypsuffix(tree, i.address[0:]) if util.address(i) ]:
for a in i._alias_: host[address] = i.name[0:] + ypsuffix(tree, i.address[0:])
host[i.address[0]] += " %s" % a.name[0] for a in i._alias_:
key = host.keys() host[address] += " %s" % a.name[0]
key.sort(by_ip) for k in sorted(host):
for k in key:
result += "%-15s %s\n" % (k, host[k]) result += "%-15s %s\n" % (k, host[k])
return result return result
def ethers(tree): def ethers(tree):
result = "" result = ""
host = {} host = {}
for i in tree._host_._interface_: for i in filter(lambda i: i.ethernet[0], tree._host_._interface_):
if i.ethernet[0]: host[i.ethernet[0]] = [ i.name[0:] ]
host[i.ethernet[0]] = [ i.name[0:] ] for ip in filter(util.address, i._ip_):
for ip in i._ip_: fqn = util.fqn(tree, ip)
try: if fqn:
host[i.ethernet[0]].append(fqn(tree, ip)[0:-1]) host[i.ethernet[0]].append(fqn[0:-1])
except:
pass
pass pass
pass pass
pass pass
key = host.keys() for k in sorted(host, cmp=util.by_mac):
key.sort(by_mac)
for k in key:
for h in host[k]: for h in host[k]:
result += "%-15s %s\n" % (k, h) result += "%-15s %s\n" % (k, h)
return result return result
...@@ -95,16 +81,14 @@ def netgroup(tree): ...@@ -95,16 +81,14 @@ def netgroup(tree):
elif m.host[0]: elif m.host[0]:
entry = "(%s,,)" % m.host[0] entry = "(%s,,)" % m.host[0]
pass pass
try: if not m.name[1] in netgroup:
netgroup[m.name[1]].append(entry) netgroup[m.name[1]] = [ ]
pass
except:
netgroup[m.name[1]] = [ entry ]
pass pass
netgroup[m.name[1]].append(entry)
pass pass
for g in tree._host_._interface_._netgroup_: for g in tree._host_._interface_._netgroup_:
def exclude(ip): def exclude(ip):
if ip.alias[0] or ip.vlan[0] or ip.never[0]: if not util.address(ip):
return False return False
if ip.exclude_netgroups[0]: if ip.exclude_netgroups[0]:
if g.name[0] in ip.exclude_netgroups[0].split(','): if g.name[0] in ip.exclude_netgroups[0].split(','):
...@@ -116,7 +100,7 @@ def netgroup(tree): ...@@ -116,7 +100,7 @@ def netgroup(tree):
if not g.name[0:] in netgroup: if not g.name[0:] in netgroup:
netgroup[g.name[0:]] = [ ] netgroup[g.name[0:]] = [ ]
pass pass
entry = "(%s,,)" % fqn(tree, ip)[0:-1] entry = "(%s,,)" % util.fqn(tree, ip)[0:-1]
netgroup[g.name[0:]].append(entry) netgroup[g.name[0:]].append(entry)
pass pass
pass pass
...@@ -124,7 +108,7 @@ def netgroup(tree): ...@@ -124,7 +108,7 @@ def netgroup(tree):
for ip in filter(lambda ip: (not ip.alias[0] and not ip.vlan[0] and for ip in filter(lambda ip: (not ip.alias[0] and not ip.vlan[0] and
not ip.never[0]), not ip.never[0]),
k._parent._ip_): k._parent._ip_):
entry = "(%s,,)" % fqn(tree, ip)[0:-1] entry = "(%s,,)" % util.fqn(tree, ip)[0:-1]
key = "ks-%s" % k.file[0:] key = "ks-%s" % k.file[0:]
if not key in netgroup: if not key in netgroup:
netgroup[key] = [] netgroup[key] = []
......
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