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

Version 2014-01-02 11:46

M  src/hostinfo.py
M  src/hostinfo/dhcpd_ipv6.py
M  src/hostinfo/ifconfig.py
M  src/hostinfo/parser.py
M  src/hostinfo/yp.py
parent c0a03c32
......@@ -148,7 +148,6 @@ def host_order(a,b):
tag_weight = {
('hostinfo', 'soa') : (1, None),
('hostinfo', 'subnet') : (2, network_order),
# lambda a,b: cmp(a.network[0],b.network[0])),
('hostinfo', 'nameserver') : (3, None),
('hostinfo', 'netgroup') : (4,
lambda a,b: cmp(a.name[0],b.name[0])),
......@@ -158,9 +157,10 @@ tag_weight = {
('host', 'mio') : (3, None),
('interface', 'kickstart') : (1, None),
('interface', 'ip') : (2, None),
('interface', 'alias') : (3, None),
('interface', 'dhcpserver') : (4, None),
('interface', 'nameserver') : (5, None),
('interface', 'ipv6') : (3, None),
('interface', 'alias') : (4, None),
('interface', 'dhcpserver') : (5, None),
('interface', 'nameserver') : (6, None),
('automount', 'entry') : (1, lambda a,b: cmp(a.key[0],b.key[0])),
}
......@@ -282,7 +282,7 @@ if __name__ == '__main__':
options = optParser.parse_args(sys.argv[1:])
hostinfo.options = options
tree = hostinfo.parser.parse(options.hostinfo_xml)
tree = hostinfo.parser.parse(options.hostinfo_xml, include_comments=True)
host = options.host
file = {}
......
......@@ -85,9 +85,6 @@ def emit_network(tree, options, subnet, dhcp):
def emit_subnet_info(subnet):
result = util.StringArray()
if subnet.gateway[0]:
result += "option routers %s;" % subnet.gateway[0]
pass
if subnet.domain[0]:
result += "option domain-name \"%s\";" % subnet.domain[0]
pass
......
......@@ -94,9 +94,11 @@ def generate_ifcfgv6(tree, interface, search, nameservers):
address = []
for ipv6 in filter(util.address, interface._ipv6_):
a = util.address(ipv6)
gateway = None
for s in filter(util.network, tree._subnet_ ):
n = util.network(s)
if a in n:
gateway = s.gateway[0]
address.append('%s/%d' % (a, n.prefixlen))
if s.domain[0]:
search.update(s.domain[0].split())
......@@ -110,6 +112,9 @@ def generate_ifcfgv6(tree, interface, search, nameservers):
config.append('IPV6_DEFROUTE=yes')
config.append('IPV6_FAILURE_FATAL=no')
config.append('IPV6ADDR=%s' % address[0])
if gateway:
config.append('IPV6_DEFAULTGW=%s' % gateway)
pass
pass
if len(address) > 1:
config.append('IPV6ADDR_SECONDARIES="%s"' %
......
import calendar
import os
import stat
import sys
import time
import xml.sax
import xml.parsers.expat
import traceback
import urlparse
import urllib2
__doc__ = """
Package for xml parsing
......@@ -360,12 +364,6 @@ class Node:
def _xml(self, indent=0, attr_sort=None, tag_sort=None, width=80):
"""Generate a prettyprinted xml of the tree rooted in this node"""
if self._tag == '':
# Comment node
result = "%s<!--" % (" " * indent)
result += self._attribute['']
result += "-->\n"
return result
result = ""
line = "%s<%s" % (" " * indent, self._tag)
blanks = " " * len(line)
......@@ -382,7 +380,6 @@ class Node:
result += "%s/>\n" % line
else:
result += "%s>\n" % line
#result += xml_escape('\n'.join(self._char))
if self._char:
result += '\n'.join(self._char)
result += '\n'
......@@ -434,47 +431,63 @@ class Node:
def __repr__(self):
return self._xml()
class Comment:
def __init__(self, content, line=None):
self._content = content
self._line = line
self._parent = None
self._tag = ''
self._attribute = { '': content}
def __copy__(self):
"""Copy self"""
result = Comment(self._content, self._line)
return result
def __deepcopy__(self, memo):
"""Copy node and all children"""
return self.__copy__()
def _xml(self, indent=0, attr_sort=None, tag_sort=None, width=80):
"""Generate a prettyprinted xml of the tree rooted in this node"""
result = "%s<!--" % (" " * indent)
result += self._attribute['']
result += "-->\n"
return result
def __repr__(self):
return self._xml()
class Parser(xml.sax.ContentHandler):
def __init__(self, url):
"""Parse the given url inti .tree
class Parser:
def __init__(self, data, url=None, mtime=None, include_comments=False):
"""Parse the given url into .tree
augment the tree with
._mtime == url modification time if url is a file, 0 otherwise
._mtime == url modification time if url is a file,
current time otherwise
._url == the url that was parsed
"""
self.include_comments = include_comments
self.tree = None
# Create a xml parser
import xml.parsers.expat
self.parser = xml.parsers.expat.ParserCreate()
self.parser.StartElementHandler = self.startElement
self.parser.EndElementHandler = self.endElement
self.parser.CharacterDataHandler = self.characters
self.parser.CommentHandler = self.comment
"""parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
parser.setContentHandler(self)
try:
parser.setFeature(xml.sax.handler.feature_external_ges, 0)
except:
pass
"""
# Parse the url
self.current = [ self ]
self.parser.Parse(open(url).read())
#import urllib2
#parser.Parse(urllib2.urlopen(url).read())
# Set the mtime
mtime = 0
try:
mtime = os.stat(url)[stat.ST_MTIME]
except Exception,e:
mtime = int(time.time())
self.parser.Parse(data)
except xml.parsers.expat.ExpatError:
reason = xml.parsers.expat.ErrorString(self.parser.ErrorCode)
line = self.parser.ErrorLineNumber
column = self.parser.ErrorColumnNumber
raise Exception("%s at line %d, column %d in %s" %
(reason, line, column, url))
pass
self.tree._mtime = mtime
self.tree._url = url
......@@ -504,11 +517,32 @@ class Parser(xml.sax.ContentHandler):
self.current[-1]._append(char)
def comment(self, data):
# Ugly hack, ...
parent = self.current[-1]
parent._add(Node('', self.parser.CurrentLineNumber, { '': data }))
def parse(url):
"""Parse the given 'url' returning an easily traversable tree"""
return Parser(url).tree
if self.include_comments:
parent = self.current[-1]
parent._add(Comment(data, self.parser.CurrentLineNumber))
def parse(source, include_comments=False):
"""Parse the given 'source' returning an easily traversable tree"""
(scheme, host, path, param, query, frag) = urlparse.urlparse(source)
if not scheme:
data = open(source).read()
mtime = os.stat(source)[stat.ST_MTIME]
pass
else:
u = urllib2.urlopen(source)
data = u.read()
try:
modified = u.info()["Last-Modified"]
mtime = calendar.timegm(
time.strptime(modified, "%a, %d %b %Y %H:%M:%S %Z"))
except:
mtime = None
pass
pass
pass
return Parser(data, url=source, mtime=mtime,
include_comments=include_comments).tree
def parse_string(source, include_comments=False):
return Parser(source, url=None, mtime=None,
include_comments=include_comments).tree
......@@ -86,16 +86,13 @@ def netgroup(tree):
pass
netgroup[m.name[1]].append(entry)
pass
def exclude(ip):
if not util.address(ip):
return False
if ip.exclude_netgroups[0] == 'yes':
return False
return True
for g in tree._host_._interface_._netgroup_:
def exclude(ip):
if not util.address(ip):
return False
if ip.exclude_netgroups[0]:
if g.name[0] in ip.exclude_netgroups[0].split(','):
return False
pass
return True
for ip in filter(exclude, g._parent._ip_):
if not g.name[0:] in netgroup:
netgroup[g.name[0:]] = [ ]
......@@ -105,9 +102,7 @@ def netgroup(tree):
pass
pass
for k in tree._host_._interface_._kickstart_:
for ip in filter(lambda ip: (not ip.alias[0] and not ip.vlan[0] and
not ip.never[0]),
k._parent._ip_):
for ip in filter(exclude, k._parent._ip_):
entry = "(%s,,)" % util.fqn(tree, ip)[0:-1]
key = "ks-%s" % k.file[0:]
if not key in netgroup:
......
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