Skip to content

Commit

Permalink
Added support for meta_data and meta-data formats.
Browse files Browse the repository at this point in the history
Change-Id: I312aa26ed9be6f22156ac238f456c3906d93760d
Signed-off-by: Adrian Vladu <[email protected]>
  • Loading branch information
ader1990 committed Jan 8, 2024
1 parent 07cae6e commit 9ce26ae
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
5 changes: 4 additions & 1 deletion cloudbaseinit/metadata/services/baseconfigdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@

class BaseConfigDriveService(base.BaseMetadataService):

def __init__(self, drive_label, metadata_file):
def __init__(self, drive_label, metadata_file,
userdata_file='user-data'):
super(BaseConfigDriveService, self).__init__()
self._drive_label = drive_label
self._metadata_file = metadata_file
self._userdata_file = userdata_file
self._metadata_path = None
self._searched_types = set()
self._searched_locations = set()
Expand Down Expand Up @@ -75,6 +77,7 @@ def load(self):
searched_locations=self._searched_locations)

if found:
self._metadata_file = found
self._metadata_path = self._mgr.target_path
LOG.debug('Metadata copied to folder: %r', self._metadata_path)
return found
Expand Down
7 changes: 4 additions & 3 deletions cloudbaseinit/metadata/services/nocloudservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,18 @@ class NoCloudConfigDriveService(baseconfigdrive.BaseConfigDriveService):

def __init__(self):
super(NoCloudConfigDriveService, self).__init__(
'cidata', 'meta-data')
'cidata', 'meta[-,_]data')
self._meta_data = {}

def get_user_data(self):
return self._get_cache_data("user-data")
return self._get_cache_data(self._userdata_file)

def _get_meta_data(self):
if self._meta_data:
return self._meta_data

raw_meta_data = self._get_cache_data("meta-data", decode=True)
raw_meta_data = self._get_cache_data(
self._metadata_file, decode=True)
try:
self._meta_data = (
serialization.parse_json_yaml(raw_meta_data))
Expand Down
32 changes: 17 additions & 15 deletions cloudbaseinit/metadata/services/osconfigdrive/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# under the License.


import glob
import itertools
import os
import shutil
Expand Down Expand Up @@ -50,21 +51,20 @@ def __init__(self):
self._osutils = osutils_factory.get_os_utils()

def _meta_data_file_exists(self, drive, metadata_file):
metadata_file = os.path.join(drive, metadata_file)

if os.path.exists(metadata_file):
return True
metadata_files = glob.glob(os.path.join(drive, metadata_file))
if len(metadata_files) > 0:
return os.path.split(metadata_files[0])[1]

LOG.debug('%s not found', metadata_file)
return False

def _check_for_config_drive(self, drive, required_drive_label,
metadata_file):
label = self._osutils.get_volume_label(drive)
if label and label.lower() == required_drive_label and \
self._meta_data_file_exists(drive, metadata_file):
LOG.info('Config Drive found on %s', drive)
return True
if label and label.lower() == required_drive_label:
metadata_name = self._meta_data_file_exists(drive, metadata_file)
if metadata_name:
LOG.info('Config Drive found on %s', drive)
return metadata_name
LOG.debug("Looking for a Config Drive with label '%s' on '%s'. "
"Found mismatching label '%s'.",
required_drive_label, drive, label)
Expand Down Expand Up @@ -151,11 +151,12 @@ def _extract_iso_from_devices(self, devices):

def _get_config_drive_from_cdrom_drive(self, drive_label, metadata_file):
for drive_letter in self._osutils.get_cdrom_drives():
if self._check_for_config_drive(drive_letter, drive_label,
metadata_file):
meta_name = self._check_for_config_drive(drive_letter, drive_label,
metadata_file)
if meta_name:
os.rmdir(self.target_path)
shutil.copytree(drive_letter, self.target_path)
return True
return meta_name

return False

Expand Down Expand Up @@ -186,11 +187,12 @@ def _get_config_drive_from_volume(self, drive_label, metadata_file):
"""Look through all the volumes for config drive."""
volumes = self._osutils.get_volumes()
for volume in volumes:
if self._check_for_config_drive(volume, drive_label,
metadata_file):
meta_name = self._check_for_config_drive(volume, drive_label,
metadata_file)
if meta_name:
os.rmdir(self.target_path)
shutil.copytree(volume, self.target_path)
return True
return meta_name
return False

def _get_config_drive_files(self, drive_label, metadata_file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def setUp(self):
self._fake_label = 'config-2'
self._fake_metadata_file = 'fake_metadata_file'

@mock.patch('os.path.exists')
def _test_check_for_config_drive(self, mock_exists, exists=True,
@mock.patch('glob.glob')
def _test_check_for_config_drive(self, glob_exists, exists=True,
label="config-2", fail=False):
drive = "C:\\"
self.osutils.get_volume_label.return_value = label
mock_exists.return_value = exists
glob_exists.return_value = []
if exists:
glob_exists.return_value = ["meta_data"]

with self.snatcher:
response = self._config_manager._check_for_config_drive(
Expand All @@ -84,7 +86,10 @@ def _test_check_for_config_drive(self, mock_exists, exists=True,
"'config-2' on 'C:\\'. Found mismatching "
"label 'None'."],
self.snatcher.output)
self.assertEqual(not fail, response)
if fail:
self.assertEqual(False, response)
else:
self.assertEqual("meta_data", response)

def test_check_for_config_drive_exists(self):
self._test_check_for_config_drive()
Expand Down

0 comments on commit 9ce26ae

Please sign in to comment.