removed old dns and dhcp scripts

This commit is contained in:
Tor Martin Slåen committed 2011-04-22 18:11:27 +02:00
1 parent d22730b378
commit 148f650b89
2 files changed
-224

No files matched your search

-106
View File
@@ -1,106 +0,0 @@
#!/usr/bin/env python
# coding: utf-8
import datetime, os, commands, sys, shutil
from django.core.management import setup_environ
import settings
setup_environ(settings)
from mdb.models import *
to_be_updated = []
dhcp_filename = "dhcpd.conf"
dhcp_file_temp_dir = "dhcp"
#dhcp_file_dir = "/etc/dhcp"
dhcp_file_dir = "dhcp2"
dhcp_orig_file = "%s/%s" % (dhcp_file_dir, dhcp_filename)
dhcp_temp_file = "%s/%s" % (dhcp_file_temp_dir, dhcp_filename)
dhcp_orig_serial = 0
if not os.path.isdir(dhcp_file_temp_dir):
os.mkdir(dhcp_file_temp_dir)
if not os.path.isdir(dhcp_file_dir):
os.mkdir(dhcp_file_dir)
# read the current dhcp configs first line to get the serial
if os.path.exists(dhcp_orig_file):
dhcp_orig_fd = open(dhcp_orig_file, "r")
dhcp_orig_serial = int(dhcp_orig_fd.readline().split(":")[1])
dhcp_orig_fd.close()
# fetch configuration option #1
# we don't support more then one at this time.
config = DhcpConfig.objects.all()[0]
if config.serial <= dhcp_orig_serial:
print "no update nesecarry"
# sys.exit(0)
# open a temporary file to build the new configuration
dhcp_temp_fd = open(dhcp_temp_file, "w")
# write the new serial to the file
dhcp_temp_fd.write("; serial:%s\n" % config.serial)
# write if the dhcp server is authoritative
if config.authoritative:
dhcp_temp_fd.write("authoritative;\n")
# write default lease time
dhcp_temp_fd.write("default-lease-time %d;\n" % config.default_lease_time)
# write max lease time
dhcp_temp_fd.write("max-lease-time %d;\n" % config.max_lease_time)
# write log facility
dhcp_temp_fd.write("log-facility %s;\n" % config.log_facility)
# write update style
dhcp_temp_fd.write("ddns-update-style %s;\n" % config.ddns_update_style)
for subnet in config.ip4subnet_set.all():
dhcp_temp_fd.write("\n# %s\n" % subnet.name)
dhcp_temp_fd.write("subnet %s netmask %s {\n" % (subnet.network, subnet.netmask))
for option in subnet.dhcpoption_set.all():
dhcp_temp_fd.write("\toption %s %s;\n" % (option.key, option.value))
for option in subnet.dhcpcustomfield_set.all():
dhcp_temp_fd.write("\t%s;\n" % option.value)
if subnet.dhcp_dynamic:
dhcp_temp_fd.write("\trange %s %s;\n"
% (subnet.dhcp_dynamic_start, subnet.dhcp_dynamic_end))
dhcp_temp_fd.write("}\n")
for subnet in config.ip4subnet_set.all():
if not subnet.dhcp_dynamic: continue
for ip4address in subnet.ip4address_set.all():
if ip4address.interface_set.count() == 0: continue
interface = ip4address.interface_set.get()
if not interface.dhcp_client: continue
dhcp_temp_fd.write("\nhost %s {\n" % interface.host.hostname)
dhcp_temp_fd.write("\thardware ethernet %s;\n" % interface.macaddr)
dhcp_temp_fd.write("\tfixed-address %s.%s;\n"
% (interface.host.hostname, interface.domain.domain_name))
dhcp_temp_fd.write("}\n")
# print "found interface %s\n" % interface
dhcp_temp_fd.close()
shutil.copy(dhcp_temp_file, dhcp_orig_file)
sys.exit(0)
-118
View File
@@ -1,118 +0,0 @@
#!/usr/bin/env python
# coding: utf-8
import datetime, os, commands, sys, shutil
from django.core.management import setup_environ
#from dns_mdb import settings
import settings
setup_environ(settings)
from mdb.models import *
to_be_updated = []
zones = {}
zone_file_temp_dir = "temp"
zone_file_dir = "/var/lib/bind"
zone_command = "/usr/sbin/named-checkzone %s %s"
if not os.path.isdir(zone_file_temp_dir):
os.mkdir(zone_file_temp_dir)
class Log():
filename = None
started = False
def startlog(self):
if self.started:
return
logfile = open(self.filename, "a")
logfile.write("\nStarting log %s\n" % str(datetime.datetime.now()))
logfile.close()
self.started = True
def setFilename(self, filename):
self.filename = filename
def debug(self, string):
self.startlog()
logfile = open(self.filename, "a")
logfile.write("%s\n" % string)
logfile.close()
print string
log = Log()
log.setFilename("dns_sync.log")
def valid_zone(zone, filename):
status, output = commands.getstatusoutput(zone_command % (zone, filename))
if status != 0:
print "Could not validate zone:\n%s" % output
return False
else:
return True
# this will produce the zone files for the domains
domains = Domain.objects.all()
for domain in domains:
current_serial = 0
if os.path.exists("%s/%s" % (zone_file_dir, domain.domain_filename)):
zonefile = open("%s/%s" % (zone_file_dir,domain.domain_filename), "r")
firstline = zonefile.readline()
current_serial = int( firstline.split(":")[1])
zonefile.close()
if current_serial < domain.domain_serial:
log.debug("Updating zone %s from %d -> %d" %
(domain.domain_name, current_serial, domain.domain_serial))
zone_contents = domain.zone_file_contents()
zonefile = open("%s/%s" % (zone_file_temp_dir, domain.domain_filename),"w")
zonefile.write(zone_contents)
zonefile.close()
if valid_zone(domain.domain_name, "%s/%s" %
(zone_file_temp_dir, domain.domain_filename)):
to_be_updated.append( [ domain.domain_name, "%s/%s" %
(zone_file_temp_dir, domain.domain_filename) ] )
# this will produce the zone files for the reverse lookup subnets
subnets = Ip4Subnet.objects.all()
for domain in subnets:
current_serial = 0
if os.path.exists("%s/%s" % (zone_file_dir, domain.domain_filename)):
zonefile = open("%s/%s" % (zone_file_dir,domain.domain_filename), "r")
firstline = zonefile.readline()
current_serial = int( firstline.split(":")[1])
zonefile.close()
if current_serial < domain.domain_serial:
log.debug("Updating zone %s from %d -> %d" %
(domain.domain_name, current_serial, domain.domain_serial))
zone_contents = domain.zone_file_contents()
zonefile = open("%s/%s" % (zone_file_temp_dir, domain.domain_filename),"w")
zonefile.write(zone_contents)
zonefile.close()
if valid_zone(domain.domain_name, "%s/%s" %
(zone_file_temp_dir, domain.domain_filename)):
to_be_updated.append( [ domain.domain_name, "%s/%s" %
(zone_file_temp_dir, domain.domain_filename) ] )
if len(to_be_updated) == 0:
# nothing to do
sys.exit(0)
for upd in to_be_updated:
shutil.copy(upd[1], zone_file_dir)
status, output = commands.getstatusoutput("/etc/init.d/bind9 reload")
if status != 0:
print output
sys.exit(1)
sys.exit(0)