def _process_part(self, part, user_data_plugins, user_handlers): ret_val = None try: content_type = part.get_content_type() handler_func = user_handlers.get(content_type) if handler_func: LOG.debug("Calling user part handler for content type: %s" % content_type) handler_func(None, content_type, part.get_filename(), part.get_payload(decode=True)) else: user_data_plugin = user_data_plugins.get(content_type) if not user_data_plugin: LOG.info("Userdata plugin not found for content type: %s" % content_type) else: LOG.debug("Executing userdata plugin: %s" % user_data_plugin.__class__.__name__) if content_type == self._PART_HANDLER_CONTENT_TYPE: new_user_handlers = user_data_plugin.process(part) self._add_part_handlers(user_data_plugins, user_handlers, new_user_handlers) else: ret_val = user_data_plugin.process(part) except Exception as ex: LOG.error('Exception during multipart part handling: ' '%(content_type)s, %(filename)s' % {'content_type': part.get_content_type(), 'filename': part.get_filename()}) LOG.exception(ex) return execcmd.get_plugin_return_value(ret_val)
def _process_part(self, part, user_data_plugins, user_handlers): ret_val = None try: content_type = part.get_content_type() handler_func = user_handlers.get(content_type) if handler_func: LOG.debug("Calling user part handler for content type: %s" % content_type) handler_func(None, content_type, part.get_filename(), part.get_payload()) else: user_data_plugin = user_data_plugins.get(content_type) if not user_data_plugin: LOG.info("Userdata plugin not found for content type: %s" % content_type) else: LOG.debug("Executing userdata plugin: %s" % user_data_plugin.__class__.__name__) if content_type == self._PART_HANDLER_CONTENT_TYPE: new_user_handlers = user_data_plugin.process(part) self._add_part_handlers(user_data_plugins, user_handlers, new_user_handlers) else: ret_val = user_data_plugin.process(part) except Exception as ex: LOG.error('Exception during multipart part handling: ' '%(content_type)s, %(filename)s' % {'content_type': part.get_content_type(), 'filename': part.get_filename()}) LOG.exception(ex) return execcmd.get_plugin_return_value(ret_val)
def test_get_plugin_return_value(self, _): ret_val_map = { 0: (base.PLUGIN_EXECUTION_DONE, False), 1: (base.PLUGIN_EXECUTION_DONE, False), "invalid": (base.PLUGIN_EXECUTION_DONE, False), 1001: (base.PLUGIN_EXECUTION_DONE, True), 1002: (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False), 1003: (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, True), } for ret_val, expect in ret_val_map.items(): self.assertEqual(expect, execcmd.get_plugin_return_value(ret_val))
def test_get_plugin_return_value(self, _): ret_val_map = { 0: (base.PLUGIN_EXECUTION_DONE, False), 1: (base.PLUGIN_EXECUTION_DONE, False), "invalid": (base.PLUGIN_EXECUTION_DONE, False), 1001: (base.PLUGIN_EXECUTION_DONE, True), 1002: (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, False), 1003: (base.PLUGIN_EXECUTE_ON_NEXT_BOOT, True), } for ret_val, expect in ret_val_map.items(): self.assertEqual(expect, execcmd.get_plugin_return_value(ret_val))
def _process_non_multi_part(self, user_data): ret_val = None if user_data.startswith(b'#cloud-config'): user_data_plugins = factory.load_plugins() cloud_config_plugin = user_data_plugins.get('text/cloud-config') ret_val = cloud_config_plugin.process_non_multipart(user_data) elif user_data.strip().startswith(x509constants.PEM_HEADER.encode()): LOG.debug('Found X509 certificate in userdata') else: ret_val = userdatautils.execute_user_data_script(user_data) return execcmd.get_plugin_return_value(ret_val)
def execute(self, service, shared_data): plugin_status = base.PLUGIN_EXECUTION_DONE reboot = False if CONF.local_scripts_path: for file_path in self._get_files_in_dir(CONF.local_scripts_path): ret_val = fileexecutils.exec_file(file_path) new_plugin_status, new_reboot = \ execcmd.get_plugin_return_value(ret_val) plugin_status = max(plugin_status, new_plugin_status) reboot = reboot or new_reboot if reboot: break return plugin_status, reboot
def execute(self, service, shared_data): plugin_status = base.PLUGIN_EXECUTION_DONE reboot = False if CONF.local_scripts_path: for file_path in self._get_files_in_dir(CONF.local_scripts_path): ret_val = fileexecutils.exec_file(file_path) new_plugin_status, new_reboot = \ execcmd.get_plugin_return_value(ret_val) plugin_status = max(plugin_status, new_plugin_status) reboot = reboot or new_reboot if reboot: break return plugin_status, reboot
def _process_non_multi_part(self, user_data, instance_data={}): ret_val = None if user_data.startswith(b'## template: jinja'): jinja_ident, cloud_config = user_data.split(b"\n", 1) template = Template(cloud_config.decode()) user_data = template.render(**instance_data).encode() if user_data.startswith(b'#cloud-config'): user_data_plugins = factory.load_plugins() cloud_config_plugin = user_data_plugins.get('text/cloud-config') ret_val = cloud_config_plugin.process_non_multipart(user_data) elif user_data.strip().startswith(x509constants.PEM_HEADER.encode()): LOG.debug('Found X509 certificate in userdata') else: ret_val = userdatautils.execute_user_data_script(user_data) return execcmd.get_plugin_return_value(ret_val)
def process(self, data): if not data: LOG.info('No cloud-config runcmd entries found.') return LOG.info("Running cloud-config runcmd entries.") osutils = factory.get_os_utils() env_header = osutils.get_default_script_exec_header() try: ret_val = userdatautils.execute_user_data_script( self._unify_scripts(data, env_header).encode()) _, reboot = execcmd.get_plugin_return_value(ret_val) return reboot except Exception as ex: LOG.warning("An error occurred during runcmd execution: '%s'" % ex) return False