created dhcpd configuration export function
This commit is contained in:
1 parent
84f446fc49
commit
c3124259e7
1 file changed
+47
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user