launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #09826
[Merge] lp:~rvb/maas/dns-setup-command3 into lp:maas
Raphaël Badin has proposed merging lp:~rvb/maas/dns-setup-command3 into lp:maas with lp:~rvb/maas/dns-setup-command2 as a prerequisite.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~rvb/maas/dns-setup-command3/+merge/114404
This branch adds a new Django command: get_named_conf. By default, it will simply print out the named configuration snippet that one must append to the system's named.conf.local in order to get MAAS to manage its DNS zones with the system's BIND.
= Notes =
- I've added an option to actually edit a given configuration file rather than printing out the snippet. This is only a convenience that will be used in a dev environment.
- the test performed in test_get_include_snippet_returns_snippet might look a little bit strange but I wanted to prepare for the fact that we might want to improve the snippet in the future, so I'm simply checking that the include seems right and that the snippet looks right to be appended to an existing config file (i.e. that is starts end ends with '\n').
--
https://code.launchpad.net/~rvb/maas/dns-setup-command3/+merge/114404
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/dns-setup-command3 into lp:maas.
=== added file 'src/maasserver/management/commands/get_named_conf.py'
--- src/maasserver/management/commands/get_named_conf.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/management/commands/get_named_conf.py 2012-07-11 12:10:23 +0000
@@ -0,0 +1,59 @@
+# Copyright 2012 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Django command: get the named configuration snippet used to hook
+up MAAS' DNS configuration files with an existing DNS server.
+"""
+
+from __future__ import (
+ absolute_import,
+ print_function,
+ unicode_literals,
+ )
+
+__metaclass__ = type
+__all__ = []
+
+
+from optparse import make_option
+
+from django.core.management.base import BaseCommand
+from provisioningserver.dns.config import DNSConfig
+
+
+INCLUDE_SNIPPET_COMMENT = """
+# Append the following content to your local BIND configuration
+# file (usually /etc/bind/named.conf.local) in order to allow
+# MAAS to manage its DNS zones.
+"""
+
+
+class Command(BaseCommand):
+ option_list = BaseCommand.option_list + (
+ make_option(
+ '--edit', action='store_true', dest='edit',
+ default=False,
+ help="Edit the configuration file instead of simply "
+ "printing the snippet."),
+ make_option(
+ '--config_path', dest='config_path',
+ default='/etc/bind/named.conf.local',
+ help="Specifies the configuration file location ("
+ "used in conjonction with --edit). Defaults to "
+ "/etc/bind/named.conf.local."),
+ )
+ help = (
+ "Return the named configuration snippet used to include "
+ "MAAS' DNS configuration in an existing named "
+ "configuration.")
+
+ def handle(self, *args, **options):
+ edit = options.get('edit')
+ config_path = options.get('config_path')
+ include_snippet = DNSConfig().get_include_snippet()
+
+ if edit is True:
+ with open(config_path, "a") as conf_file:
+ conf_file.write(include_snippet)
+ else:
+ return INCLUDE_SNIPPET_COMMENT + include_snippet
=== added file 'src/maasserver/tests/test_commands_get_named_conf.py'
--- src/maasserver/tests/test_commands_get_named_conf.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/tests/test_commands_get_named_conf.py 2012-07-11 12:10:23 +0000
@@ -0,0 +1,43 @@
+# Copyright 2012 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Tests for the get_named_conf command."""
+
+from __future__ import (
+ absolute_import,
+ print_function,
+ unicode_literals,
+ )
+
+__metaclass__ = type
+__all__ = []
+
+from codecs import getwriter
+from io import BytesIO
+
+from django.core.management import call_command
+from maasserver.testing.testcase import TestCase
+from testtools.matchers import (
+ Contains,
+ FileContains,
+ )
+
+
+class TestGenerateEnlistmentPXE(TestCase):
+
+ def test_get_named_conf_returns_snippet(self):
+ out = BytesIO()
+ stdout = getwriter("UTF-8")(out)
+ call_command('get_named_conf', stdout=stdout)
+ result = stdout.getvalue()
+ # Just check that the returned snippet looks all right.
+ self.assertIn('include "', result)
+
+ def test_get_named_conf_appends_to_config_file(self):
+ file_path = self.make_file()
+ call_command(
+ 'get_named_conf', edit=True, config_path=file_path)
+ self.assertThat(
+ file_path,
+ FileContains(
+ matcher=Contains('include "')))
=== modified file 'src/provisioningserver/dns/config.py'
--- src/provisioningserver/dns/config.py 2012-07-11 12:10:23 +0000
+++ src/provisioningserver/dns/config.py 2012-07-11 12:10:23 +0000
@@ -175,6 +175,9 @@
'named_rndc_conf_path': get_named_rndc_conf_path()
}
+ def get_include_snippet(self):
+ return '\ninclude "%s";\n' % self.target_path
+
class InactiveDNSConfig(DNSConfig):
"""A specialized version of DNSConfig that simply writes a blank/empty
=== modified file 'src/provisioningserver/dns/tests/test_config.py'
--- src/provisioningserver/dns/tests/test_config.py 2012-07-11 12:10:23 +0000
+++ src/provisioningserver/dns/tests/test_config.py 2012-07-11 12:10:23 +0000
@@ -18,7 +18,10 @@
from celery.conf import conf
from maastesting.factory import factory
from maastesting.fakemethod import FakeMethod
-from maastesting.matchers import ContainsAll
+from maastesting.matchers import (
+ ContainsAll,
+ MatchesAll,
+ )
from maastesting.testcase import TestCase
from provisioningserver.dns import config
from provisioningserver.dns.config import (
@@ -33,7 +36,12 @@
TEMPLATES_PATH,
)
import tempita
-from testtools.matchers import FileContains
+from testtools.matchers import (
+ Contains,
+ EndsWith,
+ FileContains,
+ StartsWith,
+ )
class TestRNDCUtilities(TestCase):
@@ -123,6 +131,19 @@
'named.conf.rndc',
])))
+ def test_get_include_snippet_returns_snippet(self):
+ target_dir = self.make_dir()
+ self.patch(DNSConfig, 'target_dir', target_dir)
+ dnsconfig = DNSConfig()
+ snippet = dnsconfig.get_include_snippet()
+ self.assertThat(
+ snippet,
+ MatchesAll(
+ StartsWith('\n'),
+ EndsWith('\n'),
+ Contains(target_dir),
+ Contains('include "%s"' % dnsconfig.target_path)))
+
class TestInactiveDNSConfig(TestCase):
"""Tests for InactiveDNSConfig."""