diff --git a/README.md b/README.md index d4f3fa3..aaa5f68 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ A Django app for managing computer equipment. ## Installation - sudo apt-get install libmysqlclient-dev python-dev libldap2-dev libsasl2-dev bind9utils + sudo apt install libmysqlclient-dev python-dev libldap2-dev libsasl2-dev bind9utils virtualenv venv . venv/bin/activate pip install -r requirements.txt diff --git a/mdb/fixtures/test_data.json b/mdb/fixtures/test_data.json index 0ef9f9a..5b2e24f 100644 --- a/mdb/fixtures/test_data.json +++ b/mdb/fixtures/test_data.json @@ -39,7 +39,7 @@ "domain_serial": 2015100301, "domain_refresh": 28800, "domain_name": "edb.technology", - "domain_filename": "/tmp/mdb/bind/edb.technology", + "domain_filename": "/tmp/bind-db.edb.technology", "domain_minimum_ttl": 86400, "domain_expire": 604800, "domain_soa": "sega.neuf.no", @@ -81,7 +81,7 @@ "netmask": "255.255.255.0", "dhcp_dynamic_start": "10.0.3.2", "domain_admin": "sysadmin@neuf.no", - "domain_filename": "/tmp/mdb/bind/3.0.10.in-addr.arpa", + "domain_filename": "/tmp/bind-db.3.0.10.in-addr.arpa", "domain_minimum_ttl": 86400, "domain_expire": 604800, "dhcp_dynamic": true, diff --git a/mdb/models.py b/mdb/models.py index 26b1bc3..0a8ed52 100644 --- a/mdb/models.py +++ b/mdb/models.py @@ -222,17 +222,15 @@ class DhcpConfig(models.Model): # 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: + if not hasattr(ip4address, 'interface') or not ip4address.interface.dhcp_client: 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 + _if = ip4address.interface + content += "\nhost %s {\n" % _if.host.hostname + content += "\thardware ethernet %s;\n" % _if.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 + (_if.host.hostname, _if.domain.domain_name) + if len(_if.pxe_filename) > 0: + content += "\tfilename \"%s\";\n" % _if.pxe_filename content += "}\n" return content @@ -388,26 +386,18 @@ class Ip4Subnet(models.Model): content += ";\n" for addr in self.ip4address_set.all(): - if addr.interface_set.count() == 0 and generate_unassigned: + """ Get PTR records for each v4 address in subnet """ + if hasattr(addr, 'interface') and addr.interface.domain: + hostname = "%s.%s" % ( + addr.interface.host.hostname, + addr.interface.domain.domain_name) + content += "%-20s\tIN\tPTR\t%s.\n" % \ + (addr.address.split(".")[3], hostname) + elif not hasattr(addr, 'interface') and generate_unassigned: content += "%s\tIN\tPTR\t%s.%s\n" % ( addr.address, addr.address.split(".")[3], "dhcp.neuf.no.") - continue - - for interface in addr.interface_set.all(): - if interface.domain is None and generate_unassigned: - content += "%s\tIN\tPTR\t%s.%s.\n" % ( - addr.address, - addr.address.split(".")[3], - "dhcp.neuf.no") - - else: - hostname = "%s.%s" % ( - interface.host.hostname, - interface.domain.domain_name) - content += "%-20s\tIN\tPTR\t%s.\n" % \ - (addr.address.split(".")[3], hostname) return content diff --git a/mdb/tests.py b/mdb/tests.py index 6c975cf..7581525 100644 --- a/mdb/tests.py +++ b/mdb/tests.py @@ -1,6 +1,8 @@ from django.contrib.auth.models import User +from django.core.management import call_command from django.core.urlresolvers import reverse -from django.test import TestCase +from django.test import TestCase, LiveServerTestCase +from django.utils import six from rest_framework.authtoken.models import Token from rest_framework.test import APITestCase @@ -36,3 +38,19 @@ class UnitTests(TestCase): def test_host_as_pxe_files(self): host = Host.objects.first() self.assertEqual(len(host.as_pxe_files()), 1) + + +class RunManagementCommands(TestCase): + fixtures = ['test_data'] + + def test_zone_sync(self): + expected_zone_file = '' + with six.BytesIO() as f: + call_command('zone-sync', debug=True, stdout=f) + # TODO check expected_zone_file + + def test_dhcp_sync(self): + expected_dhcp_file = '' + with six.BytesIO() as f: + call_command('dhcp-sync', ('--debug',), stdout=f) + # TODO check expected_dhcp_file