Pep8 and assorted cleanups on multi-part userdata
General cleanup and minor refactoring.
This commit is contained in:
parent
3c8cd5b39f
commit
0a0b58f673
@ -14,21 +14,22 @@
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
|
||||
LOG = logging.getLogger("cloudbaseinit")
|
||||
|
||||
|
||||
def get_plugin(parent_set):
|
||||
return CloudConfigHandler(parent_set)
|
||||
|
||||
|
||||
class CloudConfigHandler:
|
||||
|
||||
def __init__(self, parent_set):
|
||||
LOG.info("Cloud-config part handler is loaded.")
|
||||
self.type = "text/cloud-config"
|
||||
self.name = "Cloud-config userdata plugin"
|
||||
return
|
||||
|
||||
def process(self, part):
|
||||
return
|
||||
|
||||
pass
|
||||
|
@ -15,33 +15,25 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import re
|
||||
import tempfile
|
||||
import uuid
|
||||
import email
|
||||
import tempfile
|
||||
import os
|
||||
import errno
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
from cloudbaseinit.osutils.factory import *
|
||||
from cloudbaseinit.plugins.windows.userdata import handle
|
||||
from cloudbaseinit.plugins.windows import userdata
|
||||
|
||||
LOG = logging.getLogger("cloudbaseinit")
|
||||
|
||||
|
||||
def get_plugin(parent_set):
|
||||
return HeatUserDataHandler(parent_set)
|
||||
|
||||
|
||||
class HeatUserDataHandler:
|
||||
|
||||
def __init__(self, parent_set):
|
||||
LOG.info("Heat user data part handler is loaded.")
|
||||
self.type = "text/x-cfninitdata"
|
||||
self.name = "Heat userdata plugin"
|
||||
return
|
||||
|
||||
def process(self, part):
|
||||
#Only user-data part of Heat multipart data is supported. All other cfinitdata part will be skipped
|
||||
#Only user-data part of Heat multipart data is supported.
|
||||
#All other cfinitdata part will be skipped
|
||||
if part.get_filename() == "cfn-userdata":
|
||||
handle(part.get_payload())
|
||||
return
|
||||
|
||||
userdata.handle(part.get_payload())
|
||||
|
@ -14,15 +14,19 @@
|
||||
# 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
|
||||
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
|
||||
LOG = logging.getLogger("cloudbaseinit")
|
||||
|
||||
|
||||
def get_plugin(parent_set):
|
||||
return PartHandlerScriptHandler(parent_set)
|
||||
|
||||
|
||||
def load_from_file(filepath, function):
|
||||
class_inst = None
|
||||
|
||||
@ -35,25 +39,22 @@ def load_from_file(filepath, function):
|
||||
py_mod = imp.load_compiled(mod_name, filepath)
|
||||
|
||||
if hasattr(py_mod, function):
|
||||
callable = getattr(__import__(mod_name),function)
|
||||
return getattr(__import__(mod_name), function)
|
||||
|
||||
return callable
|
||||
|
||||
class PartHandlerScriptHandler:
|
||||
|
||||
def __init__(self, parent_set):
|
||||
LOG.info("Part-handler script part handler is loaded.")
|
||||
self.type = "text/part-handler"
|
||||
self.name = "Part-handler userdata plugin"
|
||||
self.parent_set = parent_set
|
||||
return
|
||||
|
||||
def process(self, part):
|
||||
handler_path = self.parent_set.path + "/part-handler/"+part.get_filename()
|
||||
handler_path = (self.parent_set.path + "/part-handler/" +
|
||||
part.get_filename())
|
||||
with open(handler_path, "wb") as f:
|
||||
f.write(part.get_payload())
|
||||
|
||||
|
||||
list_types = load_from_file(handler_path, "list_types")
|
||||
handle_part = load_from_file(handler_path, "handle_part")
|
||||
|
||||
@ -63,5 +64,3 @@ class PartHandlerScriptHandler:
|
||||
LOG.info("Installing new custom handler for type: %s", part)
|
||||
self.parent_set.custom_handlers[part] = handle_part
|
||||
self.parent_set.has_custom_handlers = True
|
||||
return
|
||||
|
@ -15,32 +15,27 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import re
|
||||
import tempfile
|
||||
import uuid
|
||||
import email
|
||||
import tempfile
|
||||
import os
|
||||
import errno
|
||||
import tempfile
|
||||
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
from cloudbaseinit.osutils.factory import *
|
||||
from cloudbaseinit.osutils import factory as osutils_factory
|
||||
|
||||
LOG = logging.getLogger("cloudbaseinit")
|
||||
|
||||
|
||||
def get_plugin(parent_set):
|
||||
return ShellScriptHandler(parent_set)
|
||||
|
||||
class ShellScriptHandler:
|
||||
|
||||
class ShellScriptHandler:
|
||||
def __init__(self, parent_set):
|
||||
LOG.info("Shell-script part handler is loaded.")
|
||||
self.type = "text/x-shellscript"
|
||||
self.name = "Shell-script userdata plugin"
|
||||
return
|
||||
|
||||
def process(self, part):
|
||||
osutils = OSUtilsFactory().get_os_utils()
|
||||
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
|
||||
|
||||
file_name = part.get_filename()
|
||||
target_path = os.path.join(tempfile.gettempdir(), file_name)
|
||||
@ -69,10 +64,10 @@ class ShellScriptHandler:
|
||||
LOG.debug('User_data stdout:\n%s' % out)
|
||||
LOG.debug('User_data stderr:\n%s' % err)
|
||||
except Exception, ex:
|
||||
LOG.warning('An error occurred during user_data execution: \'%s\'' % ex)
|
||||
LOG.warning('An error occurred during user_data execution: \'%s\''
|
||||
% ex)
|
||||
finally:
|
||||
if os.path.exists(target_path):
|
||||
os.remove(target_path)
|
||||
|
||||
return False
|
||||
|
@ -14,19 +14,18 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import email
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
import uuid
|
||||
import email
|
||||
import errno
|
||||
|
||||
from cloudbaseinit.metadata.services import base as metadata_services_base
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
from cloudbaseinit.openstack.common import cfg
|
||||
from cloudbaseinit.osutils import factory as osutils_factory
|
||||
from cloudbaseinit.plugins import base
|
||||
from cloudbaseinit.plugins.windows.userdata_plugins import PluginSet
|
||||
from cloudbaseinit.openstack.common import cfg
|
||||
from cloudbaseinit.plugins.windows import userdata_plugins
|
||||
|
||||
|
||||
opts = [
|
||||
@ -34,7 +33,6 @@ opts = [
|
||||
help='Specifies a folder to store multipart data files.'),
|
||||
]
|
||||
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.register_opts(opts)
|
||||
|
||||
@ -42,15 +40,14 @@ LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class UserDataPlugin(base.BasePlugin):
|
||||
def __init__(self, cfg=CONF):
|
||||
self.cfg = cfg
|
||||
def __init__(self):
|
||||
self.msg = None
|
||||
self.plugin_set = PluginSet(self.get_plugin_path())
|
||||
self.plugin_set = userdata_plugins.PluginSet(self._get_plugin_path())
|
||||
self.plugin_set.reload()
|
||||
return
|
||||
|
||||
def get_plugin_path(self):
|
||||
return os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))),
|
||||
def _get_plugin_path(self):
|
||||
return os.path.join(os.path.dirname(
|
||||
os.path.dirname(os.path.realpath(__file__))),
|
||||
"windows/userdata-plugins")
|
||||
|
||||
def execute(self, service):
|
||||
@ -62,70 +59,70 @@ class UserDataPlugin(base.BasePlugin):
|
||||
if not user_data:
|
||||
return (base.PLUGIN_EXECUTION_DONE, False)
|
||||
|
||||
self.process_userdata(user_data)
|
||||
self._process_userdata(user_data)
|
||||
return (base.PLUGIN_EXECUTION_DONE, False)
|
||||
|
||||
def process_userdata(self, user_data):
|
||||
def _process_userdata(self, user_data):
|
||||
LOG.debug('User data content:\n%s' % user_data)
|
||||
if user_data.startswith('Content-Type: multipart'):
|
||||
for part in self.parse_MIME(user_data):
|
||||
self.process_part(part)
|
||||
for part in self._parse_mime(user_data):
|
||||
self._process_part(part)
|
||||
else:
|
||||
handle(user_data)
|
||||
return
|
||||
|
||||
def process_part(self, part):
|
||||
part_handler = self.get_part_handler(part)
|
||||
def _process_part(self, part):
|
||||
part_handler = self._get_part_handler(part)
|
||||
if part_handler is not None:
|
||||
try:
|
||||
self.begin_part_process_event(part)
|
||||
LOG.info("Processing part %s filename: %s with handler: %s", part.get_content_type(), part.get_filename(), part_handler.name)
|
||||
self._begin_part_process_event(part)
|
||||
LOG.info("Processing part %s filename: %s with handler: %s",
|
||||
part.get_content_type(),
|
||||
part.get_filename(),
|
||||
part_handler.name)
|
||||
part_handler.process(part)
|
||||
self.end_part_process_event(part)
|
||||
self._end_part_process_event(part)
|
||||
except Exception, e:
|
||||
LOG.error('Exception during multipart part handling: %s %s \n %s' , part.get_content_type(), part.get_filename(), e)
|
||||
return
|
||||
LOG.error('Exception during multipart part handling: '
|
||||
'%s %s \n %s', part.get_content_type(),
|
||||
part.get_filename(), e)
|
||||
|
||||
def begin_part_process_event(self, part):
|
||||
handler = self.get_custom_handler(part)
|
||||
def _begin_part_process_event(self, part):
|
||||
handler = self._get_custom_handler(part)
|
||||
if handler is not None:
|
||||
try:
|
||||
handler("","__begin__", part.get_filename(), part.get_payload())
|
||||
handler("", "__begin__", part.get_filename(),
|
||||
part.get_payload())
|
||||
except Exception, e:
|
||||
LOG.error("Exception occurred during custom handle script invocation (__begin__): %s ", e)
|
||||
return
|
||||
LOG.error("Exception occurred during custom handle script "
|
||||
"invocation (__begin__): %s ", e)
|
||||
|
||||
def end_part_process_event(self, part):
|
||||
handler = self.get_custom_handler(part)
|
||||
def _end_part_process_event(self, part):
|
||||
handler = self._get_custom_handler(part)
|
||||
if handler is not None:
|
||||
try:
|
||||
handler("", "__end__", part.get_filename(), part.get_payload())
|
||||
except Exception, e:
|
||||
LOG.error("Exception occurred during custom handle script invocation (__end__): %s ", e)
|
||||
return
|
||||
LOG.error("Exception occurred during custom handle script "
|
||||
"invocation (__end__): %s ", e)
|
||||
|
||||
|
||||
def get_custom_handler(self, part):
|
||||
def _get_custom_handler(self, part):
|
||||
if self.plugin_set.has_custom_handlers:
|
||||
if part.get_content_type() in self.plugin_set.custom_handlers:
|
||||
handler = self.plugin_set.custom_handlers[part.get_content_type()]
|
||||
handler = self.plugin_set.custom_handlers[
|
||||
part.get_content_type()]
|
||||
return handler
|
||||
return None
|
||||
|
||||
def get_part_handler(self, part):
|
||||
def _get_part_handler(self, part):
|
||||
if part.get_content_type() in self.plugin_set.set:
|
||||
handler = self.plugin_set.set[part.get_content_type()]
|
||||
return handler
|
||||
else:
|
||||
return None
|
||||
|
||||
def parse_MIME(self, user_data):
|
||||
def _parse_mime(self, user_data):
|
||||
self.msg = email.message_from_string(user_data)
|
||||
return self.msg.walk()
|
||||
|
||||
|
||||
|
||||
def handle(self, user_data):
|
||||
def handle(user_data):
|
||||
osutils = osutils_factory.OSUtilsFactory().get_os_utils()
|
||||
|
||||
target_path = os.path.join(tempfile.gettempdir(), str(uuid.uuid4()))
|
||||
@ -178,4 +175,3 @@ def handle(self, user_data):
|
||||
os.remove(target_path)
|
||||
|
||||
return (base.PLUGIN_EXECUTION_DONE, False)
|
||||
|
||||
|
@ -15,14 +15,16 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import glob
|
||||
import imp
|
||||
import os
|
||||
import sys, glob
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
from cloudbaseinit.openstack.common import log as logging
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
global builders
|
||||
|
||||
|
||||
def load_from_file(filepath, parent_set):
|
||||
class_inst = None
|
||||
@ -36,8 +38,8 @@ def load_from_file(filepath, parent_set):
|
||||
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)
|
||||
clb = getattr(__import__(mod_name), "get_plugin")
|
||||
class_inst = clb(parent_set)
|
||||
|
||||
return class_inst
|
||||
|
||||
@ -52,7 +54,7 @@ class PluginSet:
|
||||
self.custom_handlers = {}
|
||||
|
||||
def get_plugin(self, content_type, file_name):
|
||||
return
|
||||
pass
|
||||
|
||||
def load(self):
|
||||
files = glob.glob(self.path + '/*.py')
|
||||
@ -61,21 +63,19 @@ class PluginSet:
|
||||
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)
|
||||
for f in files:
|
||||
LOG.debug("Trying to load user data plug-in from file: %s", f)
|
||||
try:
|
||||
plugin = load_from_file(file, self)
|
||||
plugin = load_from_file(f, 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.error('Can`t load plugin from the file %s. Skip it.', f)
|
||||
LOG.debug(repr(traceback.format_exception(exc_type, exc_value,
|
||||
exc_traceback)))
|
||||
|
||||
|
||||
def reload(self):
|
||||
self.set = {}
|
||||
self.load()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user