Skip to content

Commit

Permalink
Implement cloud-config runcmd
Browse files Browse the repository at this point in the history
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: cloudbase#27

Change-Id: Ie307e08f8c4108c7bf9108543cc90b6a7fa2e7ae
  • Loading branch information
ader1990 committed Dec 4, 2019
1 parent aebec0f commit 19025c5
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cloudbaseinit/osutils/base.py
Expand Up @@ -209,3 +209,7 @@ def set_path_admin_acls(self, path):

def take_path_ownership(self, path, username=None):
raise NotImplementedError()

def get_execution_environment_header(self):
"""File header where the cloud-config runcmd will be aggregated."""
raise NotImplementedError()
3 changes: 3 additions & 0 deletions cloudbaseinit/osutils/windows.py
Expand Up @@ -1742,3 +1742,6 @@ def get_file_version(self, path):
ls = info['FileVersionLS']
return (win32api.HIWORD(ms), win32api.LOWORD(ms),
win32api.HIWORD(ls), win32api.LOWORD(ls))

def get_execution_environment_header(self):
return 'rem cmd\n'
Expand Up @@ -28,6 +28,8 @@
'cloudconfigplugins.set_hostname.SetHostnamePlugin',
'hostname': 'cloudbaseinit.plugins.common.userdataplugins.'
'cloudconfigplugins.set_hostname.SetHostnamePlugin',
'runcmd': 'cloudbaseinit.plugins.common.userdataplugins.'
'cloudconfigplugins.runcmd.RunCmdPlugin',
}


Expand Down
@@ -0,0 +1,84 @@
# 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.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

entries = 0
for command in commands:
if isinstance(command, six.string_types):
script_content = "%s%s%s" % (script_content, command,
os.linesep)
entries += 1
if isinstance(command, (list, tuple)):
subcommand_content = []
for subcommand in command:
subcommand_content.append("%s" % subcommand)
script_content = "%s%s%s" % (script_content,
' '.join(subcommand_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 were received.')
return

LOG.info("Running cloud-config runcmd entries.")
osutils = factory.get_os_utils()
env_header = osutils.get_execution_environment_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
@@ -0,0 +1,56 @@
# Copyright 2016 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 unittest

try:
import unittest.mock as mock
except ImportError:
import mock

from oslo_config import cfg

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()

@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_os_util = mock.MagicMock()
mock_os_util.get_execution_environment_header.return_value = "test"
mock_os_utils.return_value = mock_os_util
mock_userdata.return_value = 1003
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)
self.assertEqual(expected_logging, snatcher.output)
self.assertEqual(result_process, True)

0 comments on commit 19025c5

Please sign in to comment.