Refactoring factory methods in functions

Classes are not need in the existing factories.
This commit is contained in:
Alessandro Pilotti 2014-02-19 17:57:57 +02:00
parent d8009a3d46
commit 62ce24e218
16 changed files with 46 additions and 53 deletions

View File

@ -97,19 +97,17 @@ class InitManager(object):
return supported
def configure_host(self):
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
osutils.wait_for_boot_completion()
mdsf = metadata_factory.MetadataServiceFactory()
service = mdsf.get_metadata_service()
service = metadata_factory.get_metadata_service()
LOG.info('Metadata service loaded: \'%s\'' %
service.get_name())
instance_id = service.get_instance_id()
LOG.debug('Instance id: %s', instance_id)
plugins = plugins_factory.PluginFactory().load_plugins()
plugins = plugins_factory.load_plugins()
plugins_shared_data = {}
reboot_required = False

View File

@ -38,16 +38,15 @@ CONF.register_opts(opts)
LOG = logging.getLogger(__name__)
class MetadataServiceFactory(object):
def get_metadata_service(self):
# Return the first service that loads correctly
cl = classloader.ClassLoader()
for class_path in CONF.metadata_services:
service = cl.load_class(class_path)()
try:
if service.load():
return service
except Exception, ex:
LOG.error('Failed to load metadata service \'%(class_path)s\'')
LOG.exception(ex)
raise Exception("No available service found")
def get_metadata_service():
# Return the first service that loads correctly
cl = classloader.ClassLoader()
for class_path in CONF.metadata_services:
service = cl.load_class(class_path)()
try:
if service.load():
return service
except Exception, ex:
LOG.error("Failed to load metadata service '%s'" % class_path)
LOG.exception(ex)
raise Exception("No available service found")

View File

@ -47,7 +47,7 @@ class HttpService(baseopenstackservice.BaseOpenStackService):
'''
Workaround for: https://bugs.launchpad.net/quantum/+bug/1174657
'''
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
if osutils.check_os_version(6, 0):
# 169.254.x.x addresses are not getting routed starting from

View File

@ -43,7 +43,7 @@ class WindowsConfigDriveManager(base.BaseConfigDriveManager):
return l
def _get_config_drive_cdrom_mount_point(self):
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
for drive in osutils.get_cdrom_drives():
label = osutils.get_volume_label(drive)

View File

@ -19,12 +19,11 @@ import os
from cloudbaseinit.utils import classloader
class OSUtilsFactory(object):
def get_os_utils(self):
osutils_class_paths = {
'nt': 'cloudbaseinit.osutils.windows.WindowsUtils',
'posix': 'cloudbaseinit.osutils.posix.PosixUtils'
}
def get_os_utils():
osutils_class_paths = {
'nt': 'cloudbaseinit.osutils.windows.WindowsUtils',
'posix': 'cloudbaseinit.osutils.posix.PosixUtils'
}
cl = classloader.ClassLoader()
return cl.load_class(osutils_class_paths[os.name])()
cl = classloader.ClassLoader()
return cl.load_class(osutils_class_paths[os.name])()

View File

@ -44,10 +44,9 @@ CONF = cfg.CONF
CONF.register_opts(opts)
class PluginFactory(object):
def load_plugins(self):
plugins = []
cl = classloader.ClassLoader()
for class_path in CONF.plugins:
plugins.append(cl.load_class(class_path)())
return plugins
def load_plugins():
plugins = []
cl = classloader.ClassLoader()
for class_path in CONF.plugins:
plugins.append(cl.load_class(class_path)())
return plugins

View File

@ -45,7 +45,7 @@ class CreateUserPlugin(base.BasePlugin):
user_name = CONF.username
shared_data[constants.SHARED_DATA_USERNAME] = user_name
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
password = self._get_password(osutils)
if osutils.user_exists(user_name):

View File

@ -66,7 +66,7 @@ class NetworkConfigPlugin(base.BasePlugin):
gateway = m.group('gateway')
dnsnameservers = m.group('dnsnameservers').strip().split(' ')
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
network_adapter_name = CONF.network_adapter
if not network_adapter_name:

View File

@ -36,7 +36,7 @@ NETBIOS_HOST_NAME_MAX_LEN = 15
class SetHostNamePlugin(base.BasePlugin):
def execute(self, service, shared_data):
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
metadata_host_name = service.get_host_name()
if not metadata_host_name:

View File

@ -93,7 +93,7 @@ class SetUserPasswordPlugin(base.BasePlugin):
if service.can_post_password and service.is_password_set:
LOG.debug('User\'s password already set in the instance metadata')
else:
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
if osutils.user_exists(user_name):
password = self._set_password(service, osutils, user_name)
# TODO(alexpilotti): encrypt with DPAPI

View File

@ -35,7 +35,7 @@ class SetUserSSHPublicKeysPlugin(base.BasePlugin):
username = CONF.username
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
user_home = osutils.get_user_home(username)
if not user_home:

View File

@ -48,8 +48,7 @@ class UserDataPlugin(base.BasePlugin):
LOG.debug('User data content:\n%s' % user_data)
if user_data.startswith('Content-Type: multipart'):
user_data_plugins_factory = factory.UserDataPluginsFactory()
user_data_plugins = user_data_plugins_factory.load_plugins()
user_data_plugins = factory.load_plugins()
user_handlers = {}
for part in self._parse_mime(user_data):

View File

@ -40,11 +40,10 @@ CONF = cfg.CONF
CONF.register_opts(opts)
class UserDataPluginsFactory(object):
def load_plugins(self):
plugins = {}
cl = classloader.ClassLoader()
for class_path in CONF.user_data_plugins:
plugin = cl.load_class(class_path)()
plugins[plugin.get_mime_type()] = plugin
return plugins
def load_plugins():
plugins = {}
cl = classloader.ClassLoader()
for class_path in CONF.user_data_plugins:
plugin = cl.load_class(class_path)()
plugins[plugin.get_mime_type()] = plugin
return plugins

View File

@ -28,7 +28,7 @@ class ShellScriptPlugin(base.BaseUserDataPlugin):
super(ShellScriptPlugin, self).__init__("text/x-shellscript")
def process(self, part):
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
file_name = part.get_filename()
target_path = os.path.join(tempfile.gettempdir(), file_name)

View File

@ -24,7 +24,7 @@ LOG = logging.getLogger(__name__)
def execute_user_data_script(user_data):
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
target_path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))
if re.search(r'^rem cmd\s', user_data, re.I):

View File

@ -61,7 +61,7 @@ class ConfigWinRMListenerPlugin(base.BasePlugin):
return True
def execute(self, service, shared_data):
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
osutils = osutils_factory.get_os_utils()
if not self._check_winrm_service(osutils):
return (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False)