baremetal: support network_data when building configdrive
This change adds support for network_data.json as defined in: https://specs.openstack.org/openstack/nova-specs/specs/liberty/implemented/metadata-service-network-info.html Also make user_data options, since it is actually optional. Change-Id: I28b7152e47bb6648b63d9216fbcdad22c1649888 Story: #2005083
This commit is contained in:
parent
79effadf07
commit
3c0f44f62d
@ -25,12 +25,14 @@ import six
|
|||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def populate_directory(metadata, user_data, versions=None):
|
def populate_directory(metadata, user_data=None, versions=None,
|
||||||
|
network_data=None):
|
||||||
"""Populate a directory with configdrive files.
|
"""Populate a directory with configdrive files.
|
||||||
|
|
||||||
:param dict metadata: Metadata.
|
:param dict metadata: Metadata.
|
||||||
:param bytes user_data: Vendor-specific user data.
|
:param bytes user_data: Vendor-specific user data.
|
||||||
:param versions: List of metadata versions to support.
|
:param versions: List of metadata versions to support.
|
||||||
|
:param dict network_data: Networking configuration.
|
||||||
:return: a context manager yielding a directory with files
|
:return: a context manager yielding a directory with files
|
||||||
"""
|
"""
|
||||||
d = tempfile.mkdtemp()
|
d = tempfile.mkdtemp()
|
||||||
@ -44,6 +46,11 @@ def populate_directory(metadata, user_data, versions=None):
|
|||||||
with open(os.path.join(subdir, 'meta_data.json'), 'w') as fp:
|
with open(os.path.join(subdir, 'meta_data.json'), 'w') as fp:
|
||||||
json.dump(metadata, fp)
|
json.dump(metadata, fp)
|
||||||
|
|
||||||
|
if network_data:
|
||||||
|
with open(os.path.join(subdir, 'network_data.json'),
|
||||||
|
'w') as fp:
|
||||||
|
json.dump(network_data, fp)
|
||||||
|
|
||||||
if user_data:
|
if user_data:
|
||||||
with open(os.path.join(subdir, 'user_data'), 'wb') as fp:
|
with open(os.path.join(subdir, 'user_data'), 'wb') as fp:
|
||||||
fp.write(user_data)
|
fp.write(user_data)
|
||||||
@ -53,7 +60,7 @@ def populate_directory(metadata, user_data, versions=None):
|
|||||||
shutil.rmtree(d)
|
shutil.rmtree(d)
|
||||||
|
|
||||||
|
|
||||||
def build(metadata, user_data, versions=None):
|
def build(metadata, user_data=None, versions=None, network_data=None):
|
||||||
"""Make a configdrive compatible with the Bare Metal service.
|
"""Make a configdrive compatible with the Bare Metal service.
|
||||||
|
|
||||||
Requires the genisoimage utility to be available.
|
Requires the genisoimage utility to be available.
|
||||||
@ -61,6 +68,7 @@ def build(metadata, user_data, versions=None):
|
|||||||
:param dict metadata: Metadata.
|
:param dict metadata: Metadata.
|
||||||
:param user_data: Vendor-specific user data.
|
:param user_data: Vendor-specific user data.
|
||||||
:param versions: List of metadata versions to support.
|
:param versions: List of metadata versions to support.
|
||||||
|
:param dict network_data: Networking configuration.
|
||||||
:return: configdrive contents as a base64-encoded string.
|
:return: configdrive contents as a base64-encoded string.
|
||||||
"""
|
"""
|
||||||
with populate_directory(metadata, user_data, versions) as path:
|
with populate_directory(metadata, user_data, versions) as path:
|
||||||
|
@ -24,16 +24,27 @@ from openstack.baremetal import configdrive
|
|||||||
|
|
||||||
|
|
||||||
class TestPopulateDirectory(testtools.TestCase):
|
class TestPopulateDirectory(testtools.TestCase):
|
||||||
def _check(self, metadata, user_data=None):
|
def _check(self, metadata, user_data=None, network_data=None):
|
||||||
with configdrive.populate_directory(metadata, user_data) as d:
|
with configdrive.populate_directory(metadata,
|
||||||
|
user_data=user_data,
|
||||||
|
network_data=network_data) as d:
|
||||||
for version in ('2012-08-10', 'latest'):
|
for version in ('2012-08-10', 'latest'):
|
||||||
with open(os.path.join(d, 'openstack', version,
|
with open(os.path.join(d, 'openstack', version,
|
||||||
'meta_data.json')) as fp:
|
'meta_data.json')) as fp:
|
||||||
actual_metadata = json.load(fp)
|
actual_metadata = json.load(fp)
|
||||||
|
|
||||||
self.assertEqual(metadata, actual_metadata)
|
self.assertEqual(metadata, actual_metadata)
|
||||||
|
network_data_file = os.path.join(d, 'openstack', version,
|
||||||
|
'network_data.json')
|
||||||
user_data_file = os.path.join(d, 'openstack', version,
|
user_data_file = os.path.join(d, 'openstack', version,
|
||||||
'user_data')
|
'user_data')
|
||||||
|
|
||||||
|
if network_data is None:
|
||||||
|
self.assertFalse(os.path.exists(network_data_file))
|
||||||
|
else:
|
||||||
|
with open(network_data_file) as fp:
|
||||||
|
self.assertEqual(network_data, json.load(fp))
|
||||||
|
|
||||||
if user_data is None:
|
if user_data is None:
|
||||||
self.assertFalse(os.path.exists(user_data_file))
|
self.assertFalse(os.path.exists(user_data_file))
|
||||||
else:
|
else:
|
||||||
@ -49,6 +60,9 @@ class TestPopulateDirectory(testtools.TestCase):
|
|||||||
def test_with_user_data(self):
|
def test_with_user_data(self):
|
||||||
self._check({'foo': 42}, b'I am user data')
|
self._check({'foo': 42}, b'I am user data')
|
||||||
|
|
||||||
|
def test_with_network_data(self):
|
||||||
|
self._check({'foo': 42}, network_data={'networks': {}})
|
||||||
|
|
||||||
|
|
||||||
@mock.patch('subprocess.Popen', autospec=True)
|
@mock.patch('subprocess.Popen', autospec=True)
|
||||||
class TestPack(testtools.TestCase):
|
class TestPack(testtools.TestCase):
|
||||||
|
6
releasenotes/notes/network-data-deb5772edc111428.yaml
Normal file
6
releasenotes/notes/network-data-deb5772edc111428.yaml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- |
|
||||||
|
Adds support for `network_data
|
||||||
|
<https://specs.openstack.org/openstack/nova-specs/specs/liberty/implemented/metadata-service-network-info.html>`_
|
||||||
|
when building baremetal configdrives.
|
Loading…
Reference in New Issue
Block a user