From c3124259e76774badd06f066091b31ded8877131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Martin=20Sl=C3=A5en?= Date: Fri, 22 Apr 2011 17:59:10 +0200 Subject: [PATCH] created dhcpd configuration export function --- mdb/models.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/mdb/models.py b/mdb/models.py index 5105df6..c0ccba5 100644 --- a/mdb/models.py +++ b/mdb/models.py @@ -138,6 +138,7 @@ class DomainCnameRecord(models.Model): class DhcpConfig(models.Model): serial = models.IntegerField() + active_serial = models.IntegerField() name = models.CharField(max_length=255) authoritative = models.BooleanField() ddns_update_style = models.CharField(max_length=63) @@ -145,10 +146,56 @@ class DhcpConfig(models.Model): max_lease_time = models.IntegerField(default=7200) log_facility = models.CharField(max_length=255) + def dhcpd_configuration(self): + content = "# Autogenerated configuration %s\n" % datetime.datetime.now() + if self.authoritative: + content += "authoritative;\n" + content += "default-lease-time %d;\n" % self.default_lease_time + content += "max-lease-time %d;\n" % self.max_lease_time + content += "log-facility %s;\n" % self.log_facility + content += "ddns-update-style %s;\n" % self.ddns_update_style + + + # time to write the subnet definitions + for subnet in self.ip4subnet_set.all(): + content += "\n# %s\n" % subnet.name + content += "subnet %s netmask %s {\n" % (subnet.network, subnet.netmask) + + for option in subnet.dhcpoption_set.all(): + content += "\toption %s %s;\n" % (option.key, option.value) + + for option in subnet.dhcpcustomfield_set.all(): + content += "\t%s;\n" % option.value + + if subnet.dhcp_dynamic: + content += "\trange %s %s;\n" \ + % (subnet.dhcp_dynamic_start, subnet.dhcp_dynamic_end) + + content += "}\n" + + # time to write host definitions + for subnet in self.ip4subnet_set.all(): + 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 + content += "\nhost %s {\n" % interface.host.hostname + content += "\thardware ethernet %s;\n" % interface.macaddr + content += "\tfixed-address %s.%s;\n" % \ + (interface.host.hostname, interface.domain.domain_name) + if len(interface.pxe_filename) > 0: + content += "\tfilename \"%s\";\n" % interface.pxe_filename + content += "}\n" + + return content + def __unicode__(self): return self.name class Ip4Subnet(models.Model): + name = models.CharField(max_length=256) netmask = models.IPAddressField() network = models.IPAddressField()