Refactoring of ShellScriptPlugin part handler

This is needed to allow script execution in other areas
avoiding code duplication
This commit is contained in:
Alessandro Pilotti 2014-07-22 18:22:49 +03:00
parent cf2dec9dc1
commit d27c8f0c8d
2 changed files with 63 additions and 31 deletions

View File

@ -0,0 +1,61 @@
# Copyright 2014 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
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.osutils import factory as osutils_factory
LOG = logging.getLogger(__name__)
def exec_file(file_path):
shell = False
powershell = False
ext = os.path.splitext(file_path)[1][1:].lower()
if ext == "cmd":
args = [file_path]
shell = True
elif ext == "exe":
args = [file_path]
elif ext == "sh":
args = ["bash.exe", file_path]
elif ext == "py":
args = ["python.exe", file_path]
elif ext == "ps1":
powershell = True
else:
# Unsupported
LOG.warning('Unsupported script file type: %s' % ext)
return 0
osutils = osutils_factory.get_os_utils()
try:
if powershell:
(out, err,
ret_val) = osutils.execute_powershell_script(file_path)
else:
(out, err, ret_val) = osutils.execute_process(args, shell)
LOG.info('Script "%(file_path)s" ended with exit code: %(ret_val)d' %
{"file_path": file_path, "ret_val": ret_val})
LOG.debug('User_data stdout:\n%s' % out)
LOG.debug('User_data stderr:\n%s' % err)
return ret_val
except Exception, ex:
LOG.warning('An error occurred during file execution: \'%s\'' % ex)

View File

@ -17,7 +17,7 @@ import tempfile
import os
from cloudbaseinit.openstack.common import log as logging
from cloudbaseinit.osutils import factory as osutils_factory
from cloudbaseinit.plugins.windows import fileexecutils
from cloudbaseinit.plugins.windows.userdataplugins import base
LOG = logging.getLogger(__name__)
@ -28,43 +28,14 @@ class ShellScriptPlugin(base.BaseUserDataPlugin):
super(ShellScriptPlugin, self).__init__("text/x-shellscript")
def process(self, part):
osutils = osutils_factory.get_os_utils()
file_name = part.get_filename()
target_path = os.path.join(tempfile.gettempdir(), file_name)
shell = False
powershell = False
if file_name.endswith(".cmd"):
args = [target_path]
shell = True
elif file_name.endswith(".sh"):
args = ['bash.exe', target_path]
elif file_name.endswith(".py"):
args = ['python.exe', target_path]
elif file_name.endswith(".ps1"):
powershell = True
else:
# Unsupported
LOG.warning('Unsupported script type')
return 0
try:
with open(target_path, 'wb') as f:
f.write(part.get_payload())
if powershell:
(out, err,
ret_val) = osutils.execute_powershell_script(target_path)
else:
(out, err, ret_val) = osutils.execute_process(args, shell)
LOG.info('User_data script ended with return code: %d' % ret_val)
LOG.debug('User_data stdout:\n%s' % out)
LOG.debug('User_data stderr:\n%s' % err)
return ret_val
return fileexecutils.exec_file(target_path)
except Exception, ex:
LOG.warning('An error occurred during user_data execution: \'%s\''
% ex)