Skip to content

Commit

Permalink
added config options
Browse files Browse the repository at this point in the history
Change-Id: Ibc6fce3fe398fc2c68a71b285ff8790c87e8f71c
  • Loading branch information
ader1990 committed Jan 8, 2024
1 parent c18a36b commit f944d48
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 28 deletions.
1 change: 1 addition & 0 deletions cloudbaseinit/conf/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'cloudbaseinit.conf.packet.PacketOptions',
'cloudbaseinit.conf.vmwareguestinfo.VMwareGuestInfoConfigOptions',
'cloudbaseinit.conf.gce.GCEOptions',
'cloudbaseinit.conf.nocloud.NoCloudOptions',
)


Expand Down
47 changes: 47 additions & 0 deletions cloudbaseinit/conf/nocloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2016 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

"""Config options available for the OpenStack metadata service."""

from oslo_config import cfg

from cloudbaseinit.conf import base as conf_base


class NoCloudOptions(conf_base.Options):

"""Config options available for the OpenStack metadata service."""

def __init__(self, config):
super(NoCloudOptions, self).__init__(config, group="nocloud")
self._options = [
cfg.StrOpt(
"metadata_file", default="meta-data",
help="The file name where the service looks for"
"metadata"),
cfg.StrOpt(
"userdata_file", default="user-data",
help="The file name where the service looks for"
"userdata"),
]

def register(self):
"""Register the current options to the global ConfigOpts object."""
group = cfg.OptGroup(self.group_name, title='NoCloud Options')
self._config.register_group(group)
self._config.register_opts(self._options, group=group)

def list(self):
"""Return a list which contains all the available options."""
return self._options
1 change: 0 additions & 1 deletion cloudbaseinit/metadata/services/baseconfigdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ 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
3 changes: 2 additions & 1 deletion cloudbaseinit/metadata/services/nocloudservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ class NoCloudConfigDriveService(baseconfigdrive.BaseConfigDriveService):

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

def get_user_data(self):
Expand Down
32 changes: 15 additions & 17 deletions cloudbaseinit/metadata/services/osconfigdrive/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# under the License.


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

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

if os.path.exists(metadata_file):
return True

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:
metadata_name = self._meta_data_file_exists(drive, metadata_file)
if metadata_name:
LOG.info('Config Drive found on %s', drive)
return metadata_name
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
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,12 +151,11 @@ 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():
meta_name = self._check_for_config_drive(drive_letter, drive_label,
metadata_file)
if meta_name:
if self._check_for_config_drive(drive_letter, drive_label,
metadata_file):
os.rmdir(self.target_path)
shutil.copytree(drive_letter, self.target_path)
return meta_name
return True

return False

Expand Down Expand Up @@ -187,12 +186,11 @@ 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:
meta_name = self._check_for_config_drive(volume, drive_label,
metadata_file)
if meta_name:
if self._check_for_config_drive(volume, drive_label,
metadata_file):
os.rmdir(self.target_path)
shutil.copytree(volume, self.target_path)
return meta_name
return True
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,14 +64,12 @@ def setUp(self):
self._fake_label = 'config-2'
self._fake_metadata_file = 'fake_metadata_file'

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

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

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

0 comments on commit f944d48

Please sign in to comment.