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