Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ omit =
package/setup.py,
*/__init__.py,
package/tests/*.py
package/cloudshell/cm/customscript/domain/script_file
package/cloudshell/cm/customscript/domain/script_executor.py,
33 changes: 27 additions & 6 deletions package/cloudshell/cm/customscript/customscript_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from cloudshell.shell.core.session.logging_session import LoggingSessionContext

from cloudshell.cm.customscript.domain.linux_script_executor import LinuxScriptExecutor
from cloudshell.cm.customscript.domain.reservation_output_writer import ReservationOutputWriter
from cloudshell.cm.customscript.domain.script_configuration import ScriptConfigurationParser, ScriptRepository, \
HostConfiguration
from cloudshell.cm.customscript.domain.script_downloader import ScriptDownloader, HttpAuth
from cloudshell.cm.customscript.domain.script_executor import ReservationOutputWriter, IScriptExecutor
from cloudshell.cm.customscript.domain.script_executor import IScriptExecutor
from cloudshell.cm.customscript.domain.script_file import ScriptFile
from cloudshell.cm.customscript.domain.windows_script_executor import WindowsScriptExecutor

Expand Down Expand Up @@ -47,15 +48,14 @@ def execute_script(self, command_context, script_conf_json):
logger.info('Running script on target machine.')
with CloudShellSessionContext(command_context) as session:
output_writer = ReservationOutputWriter(session, command_context)
service.run_script(tmp_folder, script_file.name, output_writer)
service.run_script(tmp_folder, script_file, output_writer)
logger.info('Done.')

finally:
logger.info('Deleting temp folder from target machine.')
service.delete_temp_folder(tmp_folder)
logger.info('Done.')

@staticmethod
def _download_script(self, script_repo, logger):
"""
:type script_repo: ScriptRepository
Expand All @@ -68,14 +68,35 @@ def _download_script(self, script_repo, logger):
auth = HttpAuth(script_repo.username, script_repo.password)
return ScriptDownloader(logger).download(url, auth)

@staticmethod
def _create_script_executor(self, host_conf, logger):
"""
:type host_conf: HostConfiguration
:type logger: Logger
:rtype IScriptExecutor
"""
if host_conf.connection_method.lower() == 'ssh':
if host_conf.connection_method == 'ssh':
return LinuxScriptExecutor(logger, host_conf)
else:
return WindowsScriptExecutor(logger, host_conf)
return WindowsScriptExecutor(logger, host_conf)


# conf = '''{
# "repositoryDetails": {
# "url": "http://192.168.30.108:8081/artifactory/LinuxScripts/ls.sh"
# },
# "hostDetails": {
# "ip": "192.168.85.20",
# "username": "root",
# "password": "qs1234",
# "connectionMethod": "ssh"
# }
# }'''
# context = ResourceCommandContext()
# context.resource = ResourceContextDetails()
# context.resource.name = 'TEST Resource'
# context.reservation = ReservationContextDetails()
# context.reservation.reservation_id = '8cc5bc1a-ae62-43c6-8772-3cd2bde5dbd8'
#
# shell = CustomScriptShell()
#
# shell.execute_script(context, conf)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def create_temp_folder(self):
result = self._run('mktemp -d')
if not result.success:
raise Exception(ErrorMsg.CREATE_TEMP_FOLDER % result.std_err)
return result.std_out
return result.std_out.rstrip('\n')

def copy_script(self, tmp_folder, script_file):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def json_to_object(json_str):
script_conf.host_conf = HostConfiguration()
script_conf.host_conf.ip = json_obj['hostDetails'].get('ip')
script_conf.host_conf.connection_method = json_obj['hostDetails'].get('connectionMethod')
if script_conf.host_conf.connection_method:
script_conf.host_conf.connection_method = script_conf.host_conf.connection_method.lower()
script_conf.host_conf.connection_secured = bool_parse(json_obj['hostDetails'].get('connectionSecured'))
script_conf.host_conf.username = json_obj['hostDetails'].get('username')
script_conf.host_conf.password = json_obj['hostDetails'].get('password')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def download(self, url, auth):
"""
self.logger.info('Downloading file from \'%s\' ...' % url)
response = requests.get(url, auth=(auth.username, auth.password) if auth else None, stream=True)
file_name = self.get_filename.get_filename(response)
file_name = self._get_filename(response)
file_txt = ''

for chunk in response.iter_content(ScriptDownloader.CHUNK_SIZE):
Expand All @@ -44,7 +44,7 @@ def download(self, url, auth):

return ScriptFile(name=file_name, text=file_txt)

def get_filename(self, response):
def _get_filename(self, response):
file_name = None
for header_value, pattern in self.filename_patterns.iteritems():
matching = re.match(pattern, response.headers.get(header_value, ""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ def create_temp_folder(self):
:rtype str
"""
code = """
$folderName = [System.Guid]::NewGuid().ToString()
$parent = $env:Temp
$fullPath = Join-Path $parent $folderName
New-Item $path -type directory | Out-Null
$fullPath = Join-Path $env:Temp ([System.Guid]::NewGuid().ToString())
New-Item $fullPath -type directory | Out-Null
Write-Output $fullPath
"""
result = self._run_ps(code)
if result.status_code != 0:
raise Exception(ErrorMsg.CREATE_TEMP_FOLDER % result.std_err)
return result.std_out
return result.std_out.rstrip('\r\n')

def copy_script(self, tmp_folder, script_file):
"""
Expand Down
30 changes: 29 additions & 1 deletion package/tests/test_script_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,32 @@ def test_cannot_parse_json_with_host_with_an_empty_connection_method(self):
json = '{"repositoryDetails":{"url":"someurl"},"hostDetails":{"ip":"x.x.x.x", "connectionMethod":""}}'
with self.assertRaises(SyntaxError) as context:
ScriptConfigurationParser.json_to_object(json)
self.assertIn('Missing/Empty "hostDetails.connectionMethod" node.', context.exception.message)
self.assertIn('Missing/Empty "hostDetails.connectionMethod" node.', context.exception.message)

def test_sanity(self):
json = """
{
"repositoryDetails" : {
"url": "B",
"username": "C",
"password": "D"
},
"hostDetails": {
"ip": "E",
"username": "F",
"password": "G",
"accessKey": "H",
"connectionMethod": "IiIiI",
"parameters": [{"name":"K11","value":"K12"}, {"name":"K21","value":"K22"}]
}
}"""
conf = ScriptConfigurationParser.json_to_object(json)
self.assertEquals("B", conf.script_repo.url)
self.assertEquals("C", conf.script_repo.username)
self.assertEquals("D", conf.script_repo.password)
self.assertEquals("F", conf.host_conf.username)
self.assertEquals("G", conf.host_conf.password)
self.assertEquals("H", conf.host_conf.access_key)
self.assertEquals("iiiii", conf.host_conf.connection_method)
self.assertItemsEqual('K12', conf.host_conf.parameters['K11'])
self.assertItemsEqual('K22', conf.host_conf.parameters['K21'])