def test_execute_user_data_script(self, mock_get_command, _):
     mock_get_command.return_value.return_value = (
         mock.sentinel.output, mock.sentinel.error, -1
     )
     retval = userdatautils.execute_user_data_script(
         mock.sentinel.user_data)
     self.assertEqual(-1, retval)
 def test_execute_user_data_script(self, mock_get_command, _):
     mock_get_command.return_value.return_value = (
         mock.sentinel.output, mock.sentinel.error, -1
     )
     retval = userdatautils.execute_user_data_script(
         mock.sentinel.user_data)
     self.assertEqual(-1, retval)
    def test_execute_user_data_script_fails(self, mock_get_command, _):
        mock_get_command.return_value.side_effect = ValueError

        retval = userdatautils.execute_user_data_script(
            mock.sentinel.user_data)

        self.assertEqual(0, retval)
Esempio n. 4
0
    def process(self, part):
        file_name = os.path.join(CONF.heat_config_dir, part.get_filename())
        self._check_dir(file_name)

        encoding.write_file(file_name, part.get_payload())

        if part.get_filename() == self._heat_user_data_filename:
            return userdatautils.execute_user_data_script(part.get_payload())
Esempio n. 5
0
    def process(self, part):
        file_name = os.path.join(CONF.heat_config_dir, part.get_filename())
        self._check_dir(file_name)

        encoding.write_file(file_name, part.get_payload())

        if part.get_filename() == self._heat_user_data_filename:
            return userdatautils.execute_user_data_script(part.get_payload())
    def test_execute_user_data_script_no_commands(self, _):
        with testutils.LogSnatcher('cloudbaseinit.plugins.common.'
                                   'userdatautils') as snatcher:
            retval = userdatautils.execute_user_data_script(b"unknown")

        expected_logging = ['Unsupported user_data format']
        self.assertEqual(0, retval)
        self.assertEqual(expected_logging, snatcher.output)
Esempio n. 7
0
    def _process_non_multi_part(self, user_data):
        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)
        else:
            ret_val = userdatautils.execute_user_data_script(user_data)

        return self._get_plugin_return_value(ret_val)
Esempio n. 8
0
    def _process_non_multi_part(self, user_data):
        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)
        else:
            ret_val = userdatautils.execute_user_data_script(user_data)

        return self._get_plugin_return_value(ret_val)
    def test_execute_user_data_script_no_commands(self, _):
        with testutils.LogSnatcher('cloudbaseinit.plugins.common.'
                                   'userdatautils') as snatcher:
            retval = userdatautils.execute_user_data_script(b"unknown")

        expected_logging = [
            'Unsupported user_data format'
        ]
        self.assertEqual(0, retval)
        self.assertEqual(expected_logging, snatcher.output)
Esempio n. 10
0
    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)
Esempio n. 11
0
    def test_execute_user_data_script_fails(self, mock_get_command, _):
        mock_command = mock.Mock()
        mock_command.execute.side_effect = ValueError
        mock_get_command.return_value = mock_command
        with testutils.LogSnatcher('cloudbaseinit.plugins.common.'
                                   'userdatautils') as snatcher:
            retval = userdatautils.execute_user_data_script(
                mock.sentinel.user_data)

        expected_logging = [
            "An error occurred during user_data execution: ''",
            'User_data script ended with return code: 0']
        self.assertEqual(0, retval)
        self.assertEqual(expected_logging, snatcher.output)
Esempio n. 12
0
    def process(self, part):
        file_name = os.path.join(CONF.heat_config_dir, part.get_filename())
        self._check_dir(file_name)

        payload = part.get_payload(decode=True)
        encoding.write_file(file_name, payload)

        if part.get_filename() == self._heat_user_data_filename:
            # Normalize the payload to bytes, since `execute_user_data_script`
            # operates on bytes and `get_payload` returns a string on
            # Python 3.
            if isinstance(payload, six.text_type):
                payload = payload.encode()
            return userdatautils.execute_user_data_script(payload)
Esempio n. 13
0
    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)
Esempio n. 14
0
    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
 def test_execute_user_data_script_no_commands(self, _):
     retval = userdatautils.execute_user_data_script(b"unknown")
     self.assertEqual(0, retval)