From ce7421831574b62b8cae63309f14d703c10fc34c Mon Sep 17 00:00:00 2001 From: Adrian Vladu Date: Mon, 2 Dec 2019 16:05:35 +0200 Subject: [PATCH] Implement cloud-config runcmd If the userdata is of type cloud-config, the runcmd entry can contain multiple entries with commands that will be executed, in the order of their definition. The commands can be given as a string or as an array of strings, the first item being the binary to be executed and the rest being the parameters of that binary. The commands will be aggregated and written into one single shell file, in the order of their definition. On Windows, the file will be executed by the native Windows shell cmd.exe. Example userdata file: runcmd: - 'dir C:\\' - ['echo', '1'] Fixes: https://github.com/cloudbase/cloudbase-init/issues/27 Change-Id: Ie307e08f8c4108c7bf9108543cc90b6a7fa2e7ae --- cloudbaseinit/constant.py | 2 + cloudbaseinit/osutils/base.py | 7 ++ cloudbaseinit/osutils/windows.py | 4 + .../cloudconfigplugins/factory.py | 2 + .../cloudconfigplugins/runcmd.py | 86 +++++++++++++++++ .../cloudconfigplugins/test_runcmd.py | 96 +++++++++++++++++++ 6 files changed, 197 insertions(+) create mode 100644 cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/runcmd.py create mode 100644 cloudbaseinit/tests/plugins/common/userdataplugins/cloudconfigplugins/test_runcmd.py diff --git a/cloudbaseinit/constant.py b/cloudbaseinit/constant.py index b6ddda56..3a7b772a 100644 --- a/cloudbaseinit/constant.py +++ b/cloudbaseinit/constant.py @@ -46,3 +46,5 @@ VOL_ACT_AVMA = "AVMA" CERT_LOCATION_LOCAL_MACHINE = "LocalMachine" CERT_LOCATION_CURRENT_USER = "CurrentUser" + +SCRIPT_HEADER_CMD = 'rem cmd' diff --git a/cloudbaseinit/osutils/base.py b/cloudbaseinit/osutils/base.py index 6773380a..0bc795b7 100644 --- a/cloudbaseinit/osutils/base.py +++ b/cloudbaseinit/osutils/base.py @@ -209,3 +209,10 @@ class BaseOSUtils(object): def take_path_ownership(self, path, username=None): raise NotImplementedError() + + def get_default_script_exec_header(self): + """File header where the cloud-config runcmd will be aggregated. + + Example: `#!/bin/bash` for bash or `rem cmd` for cmd. + """ + raise NotImplementedError() diff --git a/cloudbaseinit/osutils/windows.py b/cloudbaseinit/osutils/windows.py index 558efee8..e9715a5c 100644 --- a/cloudbaseinit/osutils/windows.py +++ b/cloudbaseinit/osutils/windows.py @@ -36,6 +36,7 @@ import win32security import win32service import winerror +from cloudbaseinit import constant from cloudbaseinit import exception from cloudbaseinit.osutils import base from cloudbaseinit.utils import classloader @@ -1742,3 +1743,6 @@ class WindowsUtils(base.BaseOSUtils): ls = info['FileVersionLS'] return (win32api.HIWORD(ms), win32api.LOWORD(ms), win32api.HIWORD(ls), win32api.LOWORD(ls)) + + def get_default_script_exec_header(self): + return constant.SCRIPT_HEADER_CMD diff --git a/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/factory.py b/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/factory.py index 6812f2cf..7de21b17 100644 --- a/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/factory.py +++ b/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/factory.py @@ -30,6 +30,8 @@ PLUGINS = { 'cloudconfigplugins.set_hostname.SetHostnamePlugin', 'ntp': 'cloudbaseinit.plugins.common.userdataplugins.' 'cloudconfigplugins.set_ntp.SetNtpPlugin', + 'runcmd': 'cloudbaseinit.plugins.common.userdataplugins.' + 'cloudconfigplugins.runcmd.RunCmdPlugin', } diff --git a/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/runcmd.py b/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/runcmd.py new file mode 100644 index 00000000..6cd62065 --- /dev/null +++ b/cloudbaseinit/plugins/common/userdataplugins/cloudconfigplugins/runcmd.py @@ -0,0 +1,86 @@ +# Copyright 2019 Cloudbase Solutions Srl +# +# 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 os +import six + +from oslo_log import log as oslo_logging + +from cloudbaseinit import exception +from cloudbaseinit.osutils import factory +from cloudbaseinit.plugins.common import execcmd +from cloudbaseinit.plugins.common.userdataplugins.cloudconfigplugins import ( + base +) +from cloudbaseinit.plugins.common import userdatautils + +LOG = oslo_logging.getLogger(__name__) + + +class RunCmdPlugin(base.BaseCloudConfigPlugin): + """Aggregate and execute cloud-config runcmd entries in a shell. + + The runcmd entries can be a string or an array of strings. + The prefered shell is given by the OS platform. + + Example for Windows, where cmd.exe is the prefered shell: + + #cloud-config + + runcmd: + - ['dir', 'C:\'] + - 'dir C:\' + """ + + @staticmethod + def _unify_scripts(commands, env_header): + script_content = env_header + os.linesep + + entries = 0 + for command in commands: + if isinstance(command, six.string_types): + script_content += command + elif isinstance(command, (list, tuple)): + subcommand_content = [] + for subcommand in command: + subcommand_content.append("%s" % subcommand) + script_content += ' '.join(subcommand_content) + else: + raise exception.CloudbaseInitException( + "Unrecognized type '%r' in cmd content" % type(command)) + + script_content += os.linesep + entries += 1 + + LOG.info("Found %d cloud-config runcmd entries." % entries) + return script_content + + 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 diff --git a/cloudbaseinit/tests/plugins/common/userdataplugins/cloudconfigplugins/test_runcmd.py b/cloudbaseinit/tests/plugins/common/userdataplugins/cloudconfigplugins/test_runcmd.py new file mode 100644 index 00000000..49bfdba2 --- /dev/null +++ b/cloudbaseinit/tests/plugins/common/userdataplugins/cloudconfigplugins/test_runcmd.py @@ -0,0 +1,96 @@ +# Copyright 2019 Cloudbase Solutions Srl +# +# 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 os +import unittest + +try: + import unittest.mock as mock +except ImportError: + import mock + +from oslo_config import cfg + +from cloudbaseinit import exception +from cloudbaseinit.plugins.common.userdataplugins.cloudconfigplugins import ( + runcmd +) + +from cloudbaseinit.tests import testutils + +CONF = cfg.CONF + + +class RunCmdPluginTest(unittest.TestCase): + + def setUp(self): + self._runcmd_plugin = runcmd.RunCmdPlugin() + + def test_unify_scripts(self): + run_commands = ['echo 1', 'echo 2'] + fake_hader = 'fake_header' + + result = self._runcmd_plugin._unify_scripts(run_commands, fake_hader) + + ln_sep = os.linesep + expected_result = 'fake_header%secho 1%secho 2%s' % (ln_sep, ln_sep, + ln_sep,) + self.assertEqual(result, expected_result) + + def test_unify_scripts_fail(self): + run_commands = [{'cmd': 'fake_cmd'}] + with self.assertRaises(exception.CloudbaseInitException) as cm: + self._runcmd_plugin._unify_scripts(run_commands, 'fake_header') + + expected = ("Unrecognized type '%s' in cmd content" + % type(run_commands[0])) + self.assertEqual(expected, str(cm.exception)) + + @mock.patch('cloudbaseinit.plugins.common.' + 'userdatautils.execute_user_data_script') + @mock.patch('cloudbaseinit.osutils.factory.get_os_utils') + def test_process_basic_data(self, mock_os_utils, mock_userdata): + run_commands = ['echo 1', 'echo 2', ['echo', '1'], 'exit 1003'] + mock_userdata.return_value = 1003 + mock_utils = mock.MagicMock() + mock_utils.get_default_script_exec_header.return_value = 'fake_header' + mock_os_utils.return_value = mock_utils + expected_logging = [ + "Running cloud-config runcmd entries.", + "Found 4 cloud-config runcmd entries.", + ] + with testutils.LogSnatcher('cloudbaseinit.plugins.common.' + 'userdataplugins.cloudconfigplugins.' + 'runcmd') as snatcher: + result_process = self._runcmd_plugin.process(run_commands) + + mock_utils.get_default_script_exec_header.assert_called_with() + self.assertEqual(expected_logging, snatcher.output) + self.assertEqual(result_process, True) + + @mock.patch('cloudbaseinit.osutils.factory.get_os_utils') + def test_process_wrong_cmd_type(self, mock_os_utils): + run_commands = [{'cmd': 'fake_cmd'}] + expected_logging = [ + "Running cloud-config runcmd entries.", + "An error occurred during runcmd execution: 'Unrecognized type " + "'%s' in cmd content'" % type(run_commands[0]) + ] + with testutils.LogSnatcher('cloudbaseinit.plugins.common.' + 'userdataplugins.cloudconfigplugins.' + 'runcmd') as snatcher: + result_process = self._runcmd_plugin.process(run_commands) + + self.assertEqual(expected_logging, snatcher.output) + self.assertEqual(result_process, False)