Georgy Okrokvertskhov fe4e46aa1b 1. Changed UserDataPlugin class to handle multipart user data
2. Added PluginSet class which dynamically loads all available plugins
in userdata-plugins subfolder
3. Added standard plugins to handle typical content types in multipart
data
2013-07-22 15:35:27 -07:00

82 lines
2.5 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 Mirantis Inc.
# All Rights Reserved.
#
# 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.
import imp
import os
import sys, glob
from cloudbaseinit.openstack.common import log as logging
import traceback
LOG = logging.getLogger(__name__)
global builders
def load_from_file(filepath, parent_set):
class_inst = None
mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
if file_ext.lower() == '.py':
py_mod = imp.load_source(mod_name, filepath)
elif file_ext.lower() == '.pyc':
py_mod = imp.load_compiled(mod_name, filepath)
if hasattr(py_mod, "get_plugin"):
callable = getattr(__import__(mod_name), "get_plugin")
class_inst = callable(parent_set)
return class_inst
class PluginSet:
def __init__(self, path):
self.path = path
sys.path.append(self.path)
self.set = {}
self.has_custom_handlers = False
self.custom_handlers = {}
def get_plugin(self, content_type, file_name):
return
def load(self):
files = glob.glob(self.path + '/*.py')
if len(files) == 0:
LOG.debug("No user data plug-ins found in %s:", self.path)
return
for file in files:
LOG.debug("Trying to load user data plug-in from file: %s", file)
try:
plugin = load_from_file(file, self)
if plugin is not None:
LOG.info("Plugin '%s' loaded.", plugin.name)
self.set[plugin.type] = plugin
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
LOG.error('Can`t load plugin from the file %s. Skip it.', file)
LOG.debug(repr(traceback.format_exception(exc_type, exc_value,
exc_traceback)))
def reload(self):
self.set = {}
self.load()