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

Version 2011-05-11 10:06

M  src/hostinfo.py
M  src/hostinfo/dhcpd.py
M  src/hostinfo/ifconfig.py
M  src/hostinfo/macosx_auto.py
M  src/hostinfo/named.py
M  src/hostinfo/samba.py
M  src/hostinfo/util.py
M  src/hostinfo/yp.py
parent a7d80063
......@@ -99,21 +99,25 @@ attr_weight = {
def host_order(a,b):
def at_top(n):
# Place hosts without ethernet or ip address at top
for i in n._interface_:
if i.ip[0]:
return False
if i.ethernet[0]:
for i1 in n._interface_:
if i1.ethernet[0]:
return False
for i2 in i1._ip_:
if i2.address[0]:
return False
pass
pass
return True
def at_bottom(n):
# Place hosts with ethernet but without ip address at bottom
result = False
for i in n._interface_:
if i.ip[0]:
return False
if i.ethernet[0]:
for i1 in n._interface_:
if i1.ethernet[0]:
result = True
for i2 in i1._ip_:
if i2.address[0]:
return False
pass
pass
return result
if (at_top(a) == at_top(b) and at_bottom(a) == at_bottom(b)):
......@@ -144,11 +148,13 @@ tag_weight = {
None),
('interface', 'kickstart') : (1,
None),
('interface', 'alias') : (2,
('interface', 'ip') : (2,
None),
('interface', 'alias') : (3,
None),
('interface', 'dhcpserver') : (3,
('interface', 'dhcpserver') : (4,
None),
('interface', 'nameserver') : (4,
('interface', 'nameserver') : (5,
None),
('automount', 'entry') : (1,
lambda a,b: cmp(a.key[0],b.key[0])),
......@@ -294,9 +300,9 @@ if __name__ == '__main__':
file["%s/ksinfo" % options.pxelinux] = ksinfo
if options.pretty:
result = "<?xml version='1.0' encoding='iso8859-1'?>\n\n"
result = "<?xml version='1.0' encoding='utf-8'?>\n\n"
result += tree._xml(attr_sort=attr_sort, tag_sort=tag_sort)
print result.encode("iso8859-1")
print result.encode("utf-8")
if options.yp:
for (f, c) in hostinfo.yp.generate(tree, options.yp_auto_domain):
......
......@@ -47,7 +47,6 @@ class "AppleNBI-ppc" {
""" % (dhcphost, dhcphost)
def conf(tree, dhcphost, kickstart, next_server=None):
print next_server
#
# A. Get interfaces to serve
#
......@@ -55,8 +54,10 @@ def conf(tree, dhcphost, kickstart, next_server=None):
for ds in tree._host_._interface_._dhcpserver_:
if dhcphost in ds.name[0::1]:
# Host is dhcpserver for this interface
dhcpip = ds.ip[0:]
dhcpip = ds._parent._ip_[0].address[0]
interface.append(ds._parent)
pass
pass
if not interface:
raise Exception("%s is not a dhcpserver" % dhcphost)
......@@ -73,27 +74,29 @@ def conf(tree, dhcphost, kickstart, next_server=None):
for i in interface:
address = []
vlan = []
if i.ip[0]:
# Main address
address.append(i.ip[0])
for ip in i._ip_:
if ip.alias[0] and ip.address[0]:
# Alias addresses
address.append(ip.address[0])
pass
elif ip.vlan[0] and ip.address[0]:
vlan.append(ip.address[0])
pass
elif ip.address[0]:
address.append(ip.address[0])
pass
pass
result += emit_network(tree, dhcphost, address, kickstart,
i.ethernet[0], next_server=next_server)
for v in vlan:
result += emit_network(tree, dhcphost, [ v ], kickstart,
None, next_server=next_server)
pass
pass
return result
def emit_network(tree, dhcphost, addr, kickstart, ethernet=None,
next_server=None):
print next_server
result = ""
network = {}
for n in tree._subnet_:
......@@ -120,7 +123,6 @@ def emit_network(tree, dhcphost, addr, kickstart, ethernet=None,
def emit_subnet(tree, n, dhcphost, kickstart, next_server=None):
result = ""
print next_server,dhcphost
if next_server == None:
next_server = dhcphost
pass
......@@ -128,32 +130,46 @@ def emit_subnet(tree, n, dhcphost, kickstart, next_server=None):
subnet = aton(n.network[0]) & netmask
static = {}
dynamic = {}
for i in tree._host_._interface_:
never = []
for ip in tree._host_._interface_._ip_:
# Find all hosts that belong to this network
if i.ip[0]:
net = aton(i.ip[0]) & netmask
if ip.address[0]:
net = aton(ip.address[0]) & netmask
if net == subnet:
if i.dynamic[0] == dhcphost:
if ip.dynamic[0] == dhcphost:
# Dynamic address served by this host
dynamic[aton(i.ip[0])] = i
dynamic[aton(ip.address[0])] = ip
pass
else:
static[i.name[0:]] = i
static[ip.name[0:]] = ip
pass
pass
pass
if ip.never[0]:
net = aton(ip.never[0]) & netmask
if net == subnet:
never.append(ip.ethernet[0:])
pass
pass
pass
result += "subnet %s netmask %s {\n" % (n.network[0], n.netmask[0])
result += " option subnet-mask %s;\n" % n.netmask[0]
if n.broadcast[0]:
result += " option broadcast-address %s;\n" % n.broadcast[0]
pass
if n.gateway[0]:
result += " option routers %s;\n" % n.gateway[0]
pass
if n.domain[0]:
result += " option domain-name \"%s\";\n" % n.domain[0]
pass
if n.name_servers[0]:
result += " option domain-name-servers %s;\n" % (
n.name_servers[0])
result += " option domain-name-servers %s;\n" % n.name_servers[0]
pass
if n.ntp_servers[0]:
result += " option ntp-servers %s;\n" % (n.ntp_servers[0])
pass
#
# Emit dynamic hosts
#
......@@ -166,13 +182,18 @@ def emit_subnet(tree, n, dhcphost, kickstart, next_server=None):
for d in dk:
if d == max + 1:
max = d
pass
else:
if min:
result += " range %s %s;\n" % (ntoa(min), ntoa(max))
pass
min = d
max = d
pass
pass
if min:
result += " range %s %s;\n" % (ntoa(min), ntoa(max))
pass
result += " get-lease-hostnames true;\n"
result += " use-host-decl-names on;\n"
......@@ -186,17 +207,17 @@ def emit_subnet(tree, n, dhcphost, kickstart, next_server=None):
sk = static.keys()
sk.sort()
for i in [static[x] for x in sk]:
ether = i.ethernet[0]
ip = i.ip[0]
ether = i._parent.ethernet[0]
ip = i.address[0]
if ether:
result += " host %s.%s {\n"% (i.name[0:], n.domain[0])
result += " hardware ethernet %s;\n" % ether
result += " fixed-address %s;\n" % ip
result += " option host-name \"%s\";\n" % i.name[0:]
if i._kickstart_:
if i._parent._kickstart_:
if not kickstart:
raise Exception("--kickstart needed for %s" % i.name[0:])
kf = i._kickstart_[0].file[0]
kf = i._parent._kickstart_[0].file[0]
result += " server-name \"%s\";\n" % dhcphost
result += " next-server %s;\n" % next_server
result += " if substring(option vendor-class-identifier, "
......@@ -206,11 +227,20 @@ def emit_subnet(tree, n, dhcphost, kickstart, next_server=None):
result += " } else {\n"
result += " filename \"%s/%s\";\n" % (kickstart, kf)
result += " }\n"
pass
for d in i._dhcp_:
result += " %s\n" % d.value[0]
pass
result += " }\n"
pass
pass
for e in never:
result += " host never_%s {\n" % e.replace(':', '')
result += " hardware ethernet %s;\n" % e
result += " ignore booting;\n"
result += " }\n"
pass
result += " }\n"
result += "}\n"
......
......@@ -3,96 +3,199 @@ import os
import re
def generate(tree, host):
result = []
for h in filter(lambda h: h.name[0] == host, tree._host_):
for i, d in map(lambda i: (i, device(i.ethernet[0])),
filter(lambda i: 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"
pass
nameservers = []
for s, ip in map(lambda ip: (subnet(tree, ip.address[0]), ip),
filter(lambda ip: (ip.address[0] and
subnet(tree, ip.address[0]) and
(static or
(not ip.vlan[0] and
ip.alias[0]))),
i._ip_)):
config += "%sIPADDR%s=%s\n" % (pre, index, ip.address[0])
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])
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
index = int("0%s" % index)+1
pass
if i.defroute[0]:
config += "DEFROUTE=%s\n" % i.defroute[0]
pass
if static:
# Nameservers
index = 0
for n in nameservers:
index += 1
config += "DNS%d=%s\n" % (index, n)
pass
pass
result.append(("ifcfg-%s" % d, config))
pass
pass
return result
def generate_old(tree, host):
result = []
for h in tree._host_:
if h.name[0] == host:
for i in h._interface_:
d = device(i.ethernet[0])
if d:
# Ordinary ethernet
for ip in h._interface_._ip_:
d = device(ip._parent.ethernet[0])
if not d:
continue
if ip.alias[0]:
# Alias
name = "%s:%s" % (d, ip.alias[0])
config = "DEVICE=%s\n" % name
config += "HWADDR=%s\n" % ip._parent.ethernet[0].upper()
config += "ONPARENT=yes\n"
if not ip._parent._dhcpserver_:
config += "BOOTPROTO=dhcp\n"
pass
else:
s = subnet(tree, ip.address[0])
config += "IPADDR=%s\n" % ip.address[0]
if s and s.netmask[0]:
config += "NETMASK=%s\n" % s.netmask[0]
pass
if s and s.network[0]:
config += "NETWORK=%s\n" % s.network[0]
pass
if s and s.broadcast[0]:
config += "BROADCAST=%s\n" % s.broadcast[0]
pass
pass
result.append(("ifcfg-%s" % name , config))
pass
elif ip.vlan[0]:
# VLAN
name = "%s.%s" % (d, ip.vlan[0])
config = "DEVICE=%s\n" % name
config += "HWADDR=%s\n" % ip._parent.ethernet[0].upper()
config += "ONBOOT=yes\n"
config += "VLAN=yes\n"
if not ip._parent._dhcpserver_:
config += "BOOTPROTO=dhcp\n"
pass
else:
s = subnet(tree, ip.address[0])
config += "IPADDR=%s\n" % ip.address[0]
if s and s.netmask[0]:
config += "NETMASK=%s\n" % s.netmask[0]
pass
if s and s.network[0]:
config += "NETWORK=%s\n" % s.network[0]
pass
if s and s.broadcast[0]:
config += "BROADCAST=%s\n" % s.broadcast[0]
pass
pass
result.append(("ifcfg-%s" % name , config))
pass
elif ip.address[0]:
# Ordinary address
config = "DEVICE=%s\n" % d
config += "HWADDR=%s\n" % i.ethernet[0].upper()
config += "HWADDR=%s\n" % ip._parent.ethernet[0].upper()
config += "ONBOOT=yes\n"
config += "TYPE=Ethernet\n"
if i._dhcpserver_ or i._static_:
if i._dhcpserver_:
s = subnet(tree, i.ip[0])
if not ip._parent._dhcpserver_ and not ip._parent._static_:
config += "BOOTPROTO=dhcp\n"
pass
else:
if ip._parent._dhcpserver_:
s = subnet(tree, ip.address[0])
pass
else:
s = i._static_[0]
s = ip._parent._static_[0]
pass
if s and s.name_servers[0]:
name_servers = re.split("[, ]+", s.name_servers[0])
name_servers = re.split("[, ]+",
s.name_servers[0])
pass
else:
name_servers = []
pass
if s:
for n in i._nameserver_:
for n in ip._parent._nameserver_:
if n.domain[0] == s.domain[0]:
name_servers = [i.ip[0]] + name_servers
name_servers.insert(0, ip.address[0])
pass
pass
pass
config += "BOOTPROTO=none\n"
config += "IPADDR=%s\n" % i.ip[0]
if s and s.gateway[0] and s.gateway[0] != i.ip[0]:
config += "IPADDR=%s\n" % ip.address[0]
if s and s.gateway[0] and s.gateway[0] != ip.address[0]:
config += "GATEWAY=%s\n" % s.gateway[0]
pass
if s and s.netmask[0]:
config += "NETMASK=%s\n" % s.netmask[0]
pass
if s and s.network[0]:
config += "NETWORK=%s\n" % s.network[0]
pass
if s and s.broadcast[0]:
config += "BROADCAST=%s\n" % s.broadcast[0]
pass
if s and name_servers:
ni = 0
for n in name_servers:
ni += 1
config += "DNS%d=%s\n" % (ni, n)
pass
pass
if s and s.domain[0]:
config += "DOMAIN=%s\n" % s.domain[0]
else:
config += "BOOTPROTO=dhcp\n"
if i.defroute[0]:
config += "DEFROUTE=%s\n" % i.defroute[0]
pass
pass
if ip.defroute[0]:
config += "DEFROUTE=%s\n" % ip.defroute[0]
pass
result.append(("ifcfg-%s" % d, config))
# Aliases and VLANs
for ip in i._ip_:
if ip.alias[0]:
name = "%s:%s" % (d, ip.alias[0])
config = "DEVICE=%s\n" % name
config += "HWADDR=%s\n" % i.ethernet[0].upper()
config += "ONPARENT=yes\n"
if not i._dhcpserver_:
config += "BOOTPROTO=dhcp\n"
else:
s = subnet(tree, ip.address[0])
config += "IPADDR=%s\n" % ip.address[0]
if s and s.netmask[0]:
config += "NETMASK=%s\n" % s.netmask[0]
if s and s.network[0]:
config += "NETWORK=%s\n" % s.network[0]
if s and s.broadcast[0]:
config += "BROADCAST=%s\n" % s.broadcast[0]
result.append(("ifcfg-%s" % name , config))
elif ip.vlan[0]:
name = "%s.%s" % (d, ip.vlan[0])
config = "DEVICE=%s\n" % name
config += "HWADDR=%s\n" % i.ethernet[0].upper()
config += "ONBOOT=yes\n"
config += "VLAN=yes\n"
if not i._dhcpserver_:
config += "BOOTPROTO=dhcp\n"
else:
s = subnet(tree, ip.address[0])
config += "IPADDR=%s\n" % ip.address[0]
if s and s.netmask[0]:
config += "NETMASK=%s\n" % s.netmask[0]
if s and s.network[0]:
config += "NETWORK=%s\n" % s.network[0]
if s and s.broadcast[0]:
config += "BROADCAST=%s\n" % s.broadcast[0]
result.append(("ifcfg-%s" % name , config))
pass
pass
pass
pass
return result
......
from hostinfo.util import fqn
def generate(tree):
maps = {}
for am in tree._host_._automount_:
......
......@@ -11,17 +11,21 @@ def generate(tree, host):
if ns.name[0:] == host:
# Given host is a nameserver
emit = True
pass
pass
if not emit:
raise Exception("%s is not a nameserver" % host)
#
# B. Read named header
#
for h in tree._named_header_:
header = h.get_char()
print header
raise Exception("Is this ever used")
pass
#
# C. Generate named.conf and named/* files
#
......@@ -105,7 +109,8 @@ def header(tree, domain, origin=None):
for ns in tree._host_._interface_._nameserver_:
if ns.domain[0] == domain and ns.primary[0] == 'yes':
result += " IN NS %s\n" % fqn(tree, ns)
result += " IN NS %s\n" % (
fqn(tree, ns._parent))
result += ";\n"
return result
......@@ -120,15 +125,15 @@ def forward(tree, domain):
for m in tree._host_._interface_._mailhost_:
if m.domain[0] == domain.domain[0]:
pri = int(m.priority[0] or 0)
result += "%-16sIN MX %d %s\n" % ("", pri ,fqn(tree, m))
result += "%-16sIN MX %d %s\n" % ("", pri ,fqn(tree, m._parent))
result += ";\n"
host = {}
for i in tree._host_._interface_:
for i in tree._host_._interface_._ip_:
# Find all hosts that belong to this network
for (n, m) in net:
if i.ip[0] and aton(i.ip[0]) & m == n:
host[i.name[1:]] = "A %s" % i.ip[0]
if i.address[0] and aton(i.address[0]) & m == n:
host[i.name[1:]] = "A %s" % i.address[0]
for a in i._alias_:
host[a.name[0]] = "CNAME %s" % i.name[1:]
for s in i._srv_:
......@@ -162,16 +167,17 @@ def reverse(tree, net):
host = {}
m = aton(net.netmask[0])
n = aton(net.network[0])
for i in tree._host_._interface_:
for i in tree._host_._interface_._ip_:
# Find all hosts that belong to this network
if i.ip[0] and aton(i.ip[0]) & m == n:
addr = aton(i.ip[0]) & ~m
if i.address[0] and aton(i.address[0]) & m == n:
addr = aton(i.address[0]) & ~m
host[addr] = "PTR %s" % fqn(tree, i)
hk = host.keys()
hk.sort()
for h in hk:
result += "%-16dIN %s\n" % (h, host[h])
pass
return result
def reverse_local(tree, nameserver):
......@@ -191,7 +197,9 @@ def reverse_local(tree, nameserver):
t._add(net)
# Add localhost
h = hostinfo.parser.Node("host", 0, { "name" : "localhost." })
i = hostinfo.parser.Node("interface", 0, { "ip" : "127.0.0.1" })
i = hostinfo.parser.Node("interface", 0)
ip = hostinfo.parser.Node("ip", 0, { "address" : "127.0.0.1" })
i._add(ip)
h._add(i)
t._add(h)
# Add nameserver (with extra domain entry)
......@@ -200,8 +208,14 @@ def reverse_local(tree, nameserver):
h = copy.copy(ns._parent._parent)
i = copy.copy(ns._parent)