launchpad-reviewers team mailing list archive
-
launchpad-reviewers team
-
Mailing list archive
-
Message #08835
[Merge] lp:~rvb/maas/preseed-lookup into lp:maas
Raphaël Badin has proposed merging lp:~rvb/maas/preseed-lookup into lp:maas.
Requested reviews:
Launchpad code reviewers (launchpad-reviewers)
For more details, see:
https://code.launchpad.net/~rvb/maas/preseed-lookup/+merge/110346
This branch adds a first version of the preseed module. The only method it provides right now is 'get_preseed_filenames' which gives a list possible preseed template filenames for a given node, release and type.
Compared to "The Plan", you will note that I forgot about the filename which includes MAC Addresses. I think it's more important to get something in the trunk quickly rather that getting it perfect. Also, I'm pretty sure we will have to iterate on this.
--
https://code.launchpad.net/~rvb/maas/preseed-lookup/+merge/110346
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/preseed-lookup into lp:maas.
=== added file 'src/maasserver/preseed.py'
--- src/maasserver/preseed.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/preseed.py 2012-06-14 14:47:20 +0000
@@ -0,0 +1,69 @@
+# Copyright 2012 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Preseed module."""
+
+from __future__ import (
+ absolute_import,
+ print_function,
+ unicode_literals,
+ )
+
+__metaclass__ = type
+__all__ = []
+
+
+GENERIC_FILENAME = 'generic'
+
+
+# XXX: rvb 2012-06-14 bug=1013146: 'precise' is hardcoded here.
+def get_preseed_filenames(node, type, release='precise'):
+ """List possible preseed template filenames for the given node.
+
+ :param node: The node to return template preseed filenames for.
+ :type node: :class:`maasserver.models.Node`
+ :param type: The preseed type (will be used as a prefix in the template
+ filenames). Usually one of {'', 'enlist', 'commissioning'}.
+ :type type: basestring
+ :param release: The Ubuntu release to be used.
+ :type type: basestring
+
+ Returns a list of possible preseed template filenames using the following
+ lookup order:
+ {type}_{node_architecture}_{node_subarchitecture}_{release}_{node_hostname}
+ {type}_{node_architecture}_{node_subarchitecture}_{release}
+ {type}_{node_architecture}
+ {type}
+ 'generic'
+ """
+ elements = (
+ [type] + split_subarch(node.architecture) + [release, node.hostname])
+ return _create_triange_combination(elements) + [GENERIC_FILENAME]
+
+
+def _create_triange_combination(elements):
+ """Given a list of string elements, return a list of filenames given by
+ composing (using the method 'compose_filename') all the elements, then
+ all elements but the last, etc.
+
+ >>> _create_triange_combination(['A', 'B', 'C'])
+ ['A_B_C', 'A_B', 'A']
+ """
+ filenames = map(
+ compose_filename,
+ [elements[:i + 1] for i in range(len(elements))])
+ filenames.reverse()
+ return filenames
+
+
+def split_subarch(architecture):
+ """Split the architecture and the subarchitecture."""
+ return architecture.split('/')
+
+
+COMPOSE_FILENAME_SEPARATOR = '_'
+
+
+def compose_filename(elements):
+ """Create a preseed filename from a list of elements."""
+ return COMPOSE_FILENAME_SEPARATOR.join(elements)
=== added file 'src/maasserver/tests/test_preseed.py'
--- src/maasserver/tests/test_preseed.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/tests/test_preseed.py 2012-06-14 14:47:20 +0000
@@ -0,0 +1,72 @@
+# Copyright 2012 Canonical Ltd. This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+"""Test preseed module."""
+
+from __future__ import (
+ absolute_import,
+ print_function,
+ unicode_literals,
+ )
+
+__metaclass__ = type
+__all__ = []
+
+from maasserver.preseed import (
+ _create_triange_combination,
+ get_preseed_filenames,
+ split_subarch,
+ )
+from maasserver.testing.factory import factory
+from maastesting.testcase import TestCase
+
+
+class TestPreseedUtilities(TestCase):
+
+ def test_split_subarch_returns_list(self):
+ self.assertEqual(['amd64'], split_subarch('amd64'))
+
+ def test_split_subarch_splits_sub_architecture(self):
+ self.assertEqual(['amd64', 'test'], split_subarch('amd64/test'))
+
+ def test__create_triange_combination(self):
+ self.assertEqual(
+ ['AA_B_CC', 'AA_B', 'AA'],
+ _create_triange_combination(['AA', 'B', 'CC']))
+
+ def test_get_preseed_filenames_returns_filenames(self):
+ hostname = factory.getRandomString()
+ type = factory.getRandomString()
+ release = factory.getRandomString()
+ node = factory.make_node(hostname=hostname)
+ self.assertEqual(
+ [
+ '%s_%s_%s_%s' % (type, node.architecture, release, hostname),
+ '%s_%s_%s' % (type, node.architecture, release),
+ '%s_%s' % (type, node.architecture),
+ '%s' % type,
+ 'generic',
+ ],
+ get_preseed_filenames(node, type, release))
+
+ def test_get_preseed_filenames_returns_filenames_with_subarch(self):
+ arch = factory.getRandomString()
+ subarch = factory.getRandomString()
+ fake_arch = '%s/%s' % (arch, subarch)
+ hostname = factory.getRandomString()
+ type = factory.getRandomString()
+ release = factory.getRandomString()
+ node = factory.make_node(hostname=hostname)
+ # Set an architecture of the form '%s/%s' i.e. with a
+ # sub-architecture.
+ node.architecture = fake_arch
+ self.assertEqual(
+ [
+ '%s_%s_%s_%s_%s' % (type, arch, subarch, release, hostname),
+ '%s_%s_%s_%s' % (type, arch, subarch, release),
+ '%s_%s_%s' % (type, arch, subarch),
+ '%s_%s' % (type, arch),
+ '%s' % type,
+ 'generic',
+ ],
+ get_preseed_filenames(node, type, release))