summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lib/Server/Hostbase/templates/dhcpd.tmpl2
-rw-r--r--src/lib/Server/Hostbase/templates/hosts.tmpl4
-rw-r--r--src/lib/Server/Hostbase/templates/hostsappend.tmpl24
-rw-r--r--src/lib/Server/Hostbase/templates/named.tmpl21
-rw-r--r--src/lib/Server/Hostbase/templates/reverseappend.tmpl16
-rw-r--r--src/lib/Server/Hostbase/templates/reversesoa.tmpl21
-rw-r--r--src/lib/Server/Hostbase/templates/zone.tmpl29
-rw-r--r--src/lib/Server/Plugins/Hostbase.py101
8 files changed, 97 insertions, 121 deletions
diff --git a/src/lib/Server/Hostbase/templates/dhcpd.tmpl b/src/lib/Server/Hostbase/templates/dhcpd.tmpl
index d4fd29447..309d5f961 100644
--- a/src/lib/Server/Hostbase/templates/dhcpd.tmpl
+++ b/src/lib/Server/Hostbase/templates/dhcpd.tmpl
@@ -3,7 +3,7 @@
# DO NOT EDIT IT BY HAND!
#
# This file contains {{ numips }} IP addresses
-# Generated on: {{ timecreated }}
+# Generated on: {% now "r" %}
#
{% include "dhcpd.conf.head" %}
diff --git a/src/lib/Server/Hostbase/templates/hosts.tmpl b/src/lib/Server/Hostbase/templates/hosts.tmpl
index e104333c9..4fded9d55 100644
--- a/src/lib/Server/Hostbase/templates/hosts.tmpl
+++ b/src/lib/Server/Hostbase/templates/hosts.tmpl
@@ -3,7 +3,7 @@
#
# This file is generated automatically - DO NOT EDIT IT.
#
-# Generated on: {{ timecreated }}
+# Generated on: {% now "r" %}
#
127.0.0.1 localhost.mcs.anl.gov localhost
@@ -22,5 +22,5 @@
{% for octet in three_octets_data %}{{ octet.0 }} {{ octet.1 }}
{% endfor %}
#
-# Total host interfaces (ip addresses) in this file: {{ three_octets }}
+# Total host interfaces (ip addresses) in this file: {{ num_ips }}
diff --git a/src/lib/Server/Hostbase/templates/hostsappend.tmpl b/src/lib/Server/Hostbase/templates/hostsappend.tmpl
index 0ebd67aee..00e0d5d04 100644
--- a/src/lib/Server/Hostbase/templates/hostsappend.tmpl
+++ b/src/lib/Server/Hostbase/templates/hostsappend.tmpl
@@ -1,19 +1,5 @@
-#raw##########################################################################
-#end raw
-# Hosts on subnet: ${subnet[0]}
-# total hosts: ${subnet[1]}
-#for $ip in $ips
-${ip[0]} ${ip[1]} #slurp
-#if $ip[4] and not $ip[3]:
-# $ip[5]
-#else
-#for $name in $ip[2]
-${name.split('.', 1)[0]} #slurp
-#end for
-#for $cname in $ip[3]
-${cname.split('.', 1)[0]} #slurp
-#end for
-# $ip[5]
-#end if
-#end for
-
+##########################################################################
+# Hosts on subnet: {{ subnet.0 }}
+# total hosts: {{ subnet.1 }}
+{% for ip in ips %}{{ ip.0 }} {{ ip.1 }}{% if ip.4 and not ip.3 %} # {{ ip.5 }}{% else %}{% for name in ip.2 %} {{ name }}{% endfor %}{% for cname in ip.3 %} {{ cname }}{% endfor %} # {{ ip.5 }}{% endif %}
+{% endfor %}
diff --git a/src/lib/Server/Hostbase/templates/named.tmpl b/src/lib/Server/Hostbase/templates/named.tmpl
index 5b45e2158..7a227c04e 100644
--- a/src/lib/Server/Hostbase/templates/named.tmpl
+++ b/src/lib/Server/Hostbase/templates/named.tmpl
@@ -15,14 +15,12 @@ zone "." {
// be authoritative for the localhost forward and reverse zones, and for
// broadcast zones as per RFC 1912
-
-#for $zone in $zones
-zone "${zone[1]}" {
+{% for zone in zones %}
+zone "{{ zone.1 }}" {
type master;
- file "/etc/bind/${zone[1]}";
-};
+ file "/etc/bind/{{ zone.1 }}";
+};{% endfor %}
-#end for
zone "localhost" {
type master;
file "/etc/bind/db.local";
@@ -42,14 +40,11 @@ zone "255.in-addr.arpa" {
type master;
file "/etc/bind/db.255";
};
-
-#for $reverse in $reverses
-zone "${reverse[0]}.in-addr.arpa" {
+{% for reverse in reverses %}
+zone "{{ reverse.0 }}.in-addr.arpa" {
type master;
- file "/etc/bind/${reverse[0]}.rev";
-};
-
-#end for
+ file "/etc/bind/{{ reverse.0 }}.rev";
+};{% endfor %}
// zone "com" { type delegation-only; };
// zone "net" { type delegation-only; };
diff --git a/src/lib/Server/Hostbase/templates/reverseappend.tmpl b/src/lib/Server/Hostbase/templates/reverseappend.tmpl
index e4f667d37..64efe6e15 100644
--- a/src/lib/Server/Hostbase/templates/reverseappend.tmpl
+++ b/src/lib/Server/Hostbase/templates/reverseappend.tmpl
@@ -1,12 +1,4 @@
-#if $fileorigin != None
-\$ORIGIN ${fileorigin}.in-addr.arpa.
-#end if
-\$ORIGIN ${inaddr}.in-addr.arpa.
-#for $host in $hosts
-#if $host[2] == 0
-${host[3][3]} PTR ${host[0]}.
-#else
-${host[3][3]} PTR ${host[4][0]}-${host[3][2]}.${host[4][1]}.
-#end if
-#end for
-
+{% if fileorigin %}$ORIGIN {{ fileorigin }}.in-addr.arpa.{% endif %}
+$ORIGIN {{ inaddr }}.in-addr.arpa.
+{% for host in hosts %}{% ifequal host.2 0 %}{{ host.3.3 }} PTR {{ host.0 }}.{% else %}{{ host.3.3 }} PTR {{ host.4.0 }}-{{ host.3.2 }}.{{ host.4.1 }}.{% endifequal %}
+{% endfor %}
diff --git a/src/lib/Server/Hostbase/templates/reversesoa.tmpl b/src/lib/Server/Hostbase/templates/reversesoa.tmpl
index 0b775757f..d142eaf7f 100644
--- a/src/lib/Server/Hostbase/templates/reversesoa.tmpl
+++ b/src/lib/Server/Hostbase/templates/reversesoa.tmpl
@@ -1,14 +1,13 @@
-\$ORIGIN .
-\$TTL $zone[8]
-${inaddr}.in-addr.arpa IN SOA $zone[4]. $zone[3] (
- $zone[2] ; serial
- $zone[7] ; refresh interval
- $zone[6] ; retry interval
- $zone[5] ; expire interval
- $zone[8] ; min ttl
+$ORIGIN .
+$TTL {{ zone.8 }}
+{{ inaddr }}.in-addr.arpa IN SOA {{ zone.4 }}. {{ zone.3 }} (
+ {{ zone.2 }} ; serial
+ {{ zone.7 }} ; refresh interval
+ {{ zone.6 }} ; retry interval
+ {{ zone.5 }} ; expire interval
+ {{ zone.8 }} ; min ttl
)
- #for $ns in $nameservers
- NS $ns[0]
- #end for
+ {% for ns in nameservers %}NS {{ ns.0 }}
+ {% endfor %}
diff --git a/src/lib/Server/Hostbase/templates/zone.tmpl b/src/lib/Server/Hostbase/templates/zone.tmpl
index 142d4dd77..aad48d179 100644
--- a/src/lib/Server/Hostbase/templates/zone.tmpl
+++ b/src/lib/Server/Hostbase/templates/zone.tmpl
@@ -1,19 +1,18 @@
$ORIGIN .
$TTL {{ zone.8 }}
-{{ zone.1 }}. IN SOA {{ zone.4 }}. {{ zone.3 }}. (
- {{ zone.2 }} ; serial
- {{ zone.7 }} ; refresh interval
- {{ zone.6 }} ; retry interval
- {{ zone.5 }} ; expire interval
- {{ zone.8 }} ; min ttl
- )
-
- {% for ns in nameservers %}
- NS {{ ns.0 }}{% endfor %}
- {% for a in addresses %}
- A {{ a.0 }}{% endfor %}
- {% for mx in mxs %}
- MX {{ mx.0 }} {{ mx.1 }}{% endfor %}
+{{ zone.1 }}. IN SOA {{ zone.4 }}. {{ zone.3 }}. (
+ {{ zone.2 }} ; serial
+ {{ zone.7 }} ; refresh interval
+ {{ zone.6 }} ; retry interval
+ {{ zone.5 }} ; expire interval
+ {{ zone.8 }} ; min ttl
+ )
+ {% for ns in nameservers %}NS {{ ns.0 }}
+ {% endfor %}
+ {% for a in addresses %}A {{ a.0 }}
+ {% endfor %}
+ {% for mx in mxs %}MX {{ mx.0 }} {{ mx.1 }}
+ {% endfor %}
$ORIGIN {{ zone.1 }}.
-localhost A 127.0.0.1
+localhost A 127.0.0.1
diff --git a/src/lib/Server/Plugins/Hostbase.py b/src/lib/Server/Plugins/Hostbase.py
index cd9d572b7..d217b723e 100644
--- a/src/lib/Server/Plugins/Hostbase.py
+++ b/src/lib/Server/Plugins/Hostbase.py
@@ -4,10 +4,11 @@ __revision__ = '$Revision$'
import sys, os
from lxml.etree import Element, SubElement
from syslog import syslog, LOG_INFO
-from Cheetah.Template import Template
from Bcfg2.Server.Plugin import Plugin, PluginExecutionError, PluginInitError, DirectoryBacked
from time import strftime
from sets import Set
+from django.template import Context, loader
+from django.db import connection
import re
## class DataNexus(DirectoryBacked):
@@ -48,7 +49,6 @@ class Hostbase(Plugin):
files = ['zone.tmpl', 'reversesoa.tmpl', 'named.tmpl', 'reverseappend.tmpl',
'dhcpd.tmpl', 'hosts.tmpl', 'hostsappend.tmpl']
os.environ['DJANGO_SETTINGS_MODULE'] = 'Hostbase.settings'
- from django.template import loader
## try:
## self.repository = DataNexus(self.data + '/templates/',
## files, self.core.fam)
@@ -58,13 +58,13 @@ class Hostbase(Plugin):
self.filedata = {}
self.dnsservers = []
self.dhcpservers = []
- self.templates = {'zone':Template(open(self.data + '/templates/' + 'zone.tmpl').read()),
- 'reversesoa':Template(open(self.data + '/templates/' + 'reversesoa.tmpl').read()),
- 'named':Template(open(self.data + '/templates/' + 'named.tmpl').read()),
- 'reverseapp':Template(open(self.data + '/templates/' + 'reverseappend.tmpl').read()),
+ self.templates = {'zone':loader.get_template('zone.tmpl'),
+ 'reversesoa':loader.get_template('reversesoa.tmpl'),
+ 'named':loader.get_template('named.tmpl'),
+ 'reverseapp':loader.get_template('reverseappend.tmpl'),
'dhcp':loader.get_template('dhcpd.tmpl'),
'hosts':loader.get_template('hosts.tmpl'),
- 'hostsapp':Template(open(self.data + '/templates/' + 'hostsappend.tmpl').read()),
+ 'hostsapp':loader.get_template('hostsappend.tmpl'),
}
self.Entries['ConfigFile'] = {}
self.__rmi__ = ['rebuildState']
@@ -108,7 +108,6 @@ class Hostbase(Plugin):
def buildZones(self):
'''Pre-build and stash zone files'''
- from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT id, serial FROM hostbase_zone")
@@ -142,20 +141,25 @@ class Hostbase(Plugin):
for zone in zones:
- self.templates['zone'].zone = zone
cursor.execute("""SELECT n.name FROM hostbase_zone_nameservers z
INNER JOIN hostbase_nameserver n ON z.nameserver_id = n.id
WHERE z.zone_id = \'%s\'""" % zone[0])
- self.templates['zone'].nameservers = cursor.fetchall()
+ nameservers = cursor.fetchall()
cursor.execute("""SELECT i.ip_addr FROM hostbase_zone_addresses z
INNER JOIN hostbase_zoneaddress i ON z.zoneaddress_id = i.id
WHERE z.zone_id = \'%s\'""" % zone[0])
- self.templates['zone'].addresses = cursor.fetchall()
+ addresses = cursor.fetchall()
cursor.execute("""SELECT m.priority, m.mx FROM hostbase_zone_mxs z
INNER JOIN hostbase_mx m ON z.mx_id = m.id
WHERE z.zone_id = \'%s\'""" % zone[0])
- self.templates['zone'].mxs = cursor.fetchall()
- self.filedata[zone[1]] = str(self.templates['zone'])
+ mxs = cursor.fetchall()
+ context = Context({
+ 'zone': zone,
+ 'nameservers': nameservers,
+ 'addresses': addresses,
+ 'mxs': mxs
+ })
+ self.filedata[zone[1]] = self.templates['zone'].render(context)
querystring = """SELECT h.hostname, p.ip_addr,
n.name, c.cname, m.priority, m.mx
@@ -216,16 +220,21 @@ class Hostbase(Plugin):
towrite = filename.split('.')
towrite.reverse()
reversename = '.'.join(towrite)
- self.templates['reversesoa'].inaddr = reversename
- self.templates['reversesoa'].zone = reversezone
- self.templates['reversesoa'].nameservers = mcs_nameservers
- self.filedata['%s.rev' % reversename] = str(self.templates['reversesoa'])
+ context = Context({
+ 'inaddr': reversename,
+ 'zone': reversezone,
+ 'nameservers': mcs_nameservers,
+ })
+
+ self.filedata['%s.rev' % reversename] = self.templates['reversesoa'].render(context)
reversenames.append((reversename, filename))
## here's where the named.conf file gets written
- self.templates['named'].zones = zones
- self.templates['named'].reverses = reversenames
- self.filedata['named.conf'] = str(self.templates['named'])
+ context = Context({
+ 'zones': zones,
+ 'reverses': reversenames,
+ })
+ self.filedata['named.conf'] = self.templates['named'].render(context)
self.Entries['ConfigFile']['%s/named.conf' % self.filepath] = self.FetchFile
reversenames.sort()
@@ -248,26 +257,27 @@ class Hostbase(Plugin):
hosts = [host.__add__((host[1].split("."), host[0].split(".", 1)))
for host in reversehosts
if host[1].rstrip('0123456789').rstrip('.') == origin[1]]
- self.templates['reverseapp'].hosts = hosts
- self.templates['reverseapp'].inaddr = origin[0]
- self.templates['reverseapp'].fileorigin = filename[0]
- self.filedata['%s.rev' % filename[0]] += str(self.templates['reverseapp'])
+ context = Context({
+ 'hosts': hosts,
+ 'inaddr': origin[0],
+ 'fileorigin': filename[0],
+ })
+ self.filedata['%s.rev' % filename[0]] += self.templates['reverseapp'].render(context)
else:
originlist = [filename[0]]
hosts = [host.__add__((host[1].split("."), host[0].split(".", 1)))
for host in reversehosts]
- self.templates['reverseapp'].hosts = hosts
- self.templates['reverseapp'].inaddr = filename[0]
- self.templates['reverseapp'].fileorigin = None
- self.filedata['%s.rev' % filename[0]] += str(self.templates['reverseapp'])
+ context = Context({
+ 'hosts': hosts,
+ 'inaddr': filename[0],
+ 'fileorigin': None,
+ })
+ self.filedata['%s.rev' % filename[0]] += self.templates['reverseapp'].render(context)
self.Entries['ConfigFile']['%s/%s.rev' % (self.filepath, filename[0])] = self.FetchFile
def buildDHCP(self):
'''Pre-build dhcpd.conf and stash in the filedata table'''
- from django.db import connection
- from django.template import Context
-
# fetches all the hosts with DHCP == True
cursor = connection.cursor()
cursor.execute("""
@@ -307,7 +317,6 @@ class Hostbase(Plugin):
context = Context({
'hosts': hosts,
'numips': len(hosts),
- 'timecreated': strftime("%a %b %d %H:%M:%S %Z %Y"),
})
self.filedata['dhcpd.conf'] = self.templates['dhcp'].render(context)
@@ -316,8 +325,6 @@ class Hostbase(Plugin):
def buildHosts(self):
'''Pre-build and stash /etc/hosts file'''
- from django.db import connection
- from django.template import Context
append_data = []
@@ -365,7 +372,7 @@ class Hostbase(Plugin):
'three_octets_data': three_octets_data,
'two_octets_data': two_octets_data,
'three_octets': three_octets,
- 'timecreated': strftime("%a %b %d %H:%M:%S %Z %Y"),
+ 'num_ips': len(three_octets),
})
self.filedata['hosts'] = self.templates['hosts'].render(context)
@@ -373,17 +380,17 @@ class Hostbase(Plugin):
for subnet in append_data:
ips = []
simple = True
- namelist = [subnet[1][0][3]]
+ namelist = [name.split('.', 1)[0] for name in [subnet[1][0][3]]]
cnamelist = []
if subnet[1][0][4]:
- cnamelist.append(subnet[1][0][4])
+ cnamelist.append(subnet[1][0][4].split('.', 1)[0])
simple = False
appenddata = subnet[1][0]
for ip in subnet[1][1:]:
if appenddata[2] == ip[2]:
- namelist.append(ip[3])
+ namelist.append(ip[3].split('.', 1)[0])
if ip[4]:
- cnamelist.append(ip[4])
+ cnamelist.append(ip[4].split('.', 1)[0])
simple = False
appenddata = ip
else:
@@ -393,16 +400,18 @@ class Hostbase(Plugin):
cnamelist, simple, appenddata[1]))
appenddata = ip
simple = True
- namelist = [ip[3]]
+ namelist = [ip[3].split('.', 1)[0]]
cnamelist = []
if ip[4]:
- cnamelist.append(ip[4])
+ cnamelist.append(ip[4].split('.', 1)[0])
simple = False
ips.append((appenddata[2], appenddata[0], namelist,
cnamelist, simple, appenddata[1]))
- self.templates['hostsapp'].subnet = subnet[0]
- self.templates['hostsapp'].ips = ips
- self.filedata['hosts'] += str(self.templates['hostsapp'])
+ context = Context({
+ 'subnet': subnet[0],
+ 'ips': ips,
+ })
+ self.filedata['hosts'] += self.templates['hostsapp'].render(context)
self.Entries['ConfigFile']['/mcs/etc/hosts'] = self.FetchFile
def buildPrinters(self):
@@ -412,7 +421,6 @@ class Hostbase(Plugin):
Name Room User Type Notes
============== ========== ============================== ======================== ====================
"""
- from django.db import connection
cursor = connection.cursor()
# fetches all the printers from the database
@@ -442,8 +450,6 @@ Name Room User Type
def buildHostsLPD(self):
"""Creates the /mcs/etc/hosts.lpd file"""
- from django.db import connection
-
# this header needs to be changed to be more generic
header = """+@machines
+@all-machines
@@ -496,7 +502,6 @@ olivia.ctd.anl.gov\n\n"""
#
# Number of hosts in '%s' machine netgroup: %i
#\n\n"""
- from django.db import connection
cursor = connection.cursor()
# fetches all the hosts that with valid netgroup entries