diff --git a/package/cloudshell/cm/customscript/domain/linux_script_executor.py b/package/cloudshell/cm/customscript/domain/linux_script_executor.py index a9253d8..c6e9c67 100644 --- a/package/cloudshell/cm/customscript/domain/linux_script_executor.py +++ b/package/cloudshell/cm/customscript/domain/linux_script_executor.py @@ -6,6 +6,7 @@ from multiprocessing.pool import ThreadPool from threading import Thread import binascii +import threading import time from paramiko import SSHClient, AutoAddPolicy, RSAKey @@ -98,7 +99,7 @@ def create_temp_folder(self): """ :rtype str """ - result = self._run_cancelable('mktemp -d') + result = self._run_cancelable('mktemp -d', None) #TBD: decide if to pass None or output_writer if not result.success: raise Exception(ErrorMsg.CREATE_TEMP_FOLDER % result.std_err) return result.std_out.rstrip('\n') @@ -137,10 +138,10 @@ def run_script(self, tmp_folder, script_file, env_vars, output_writer, print_out code += 'export %s=%s;' % (self.PasswordEnvVarName, self._escape(self.target_host.password)) code += 'sh '+tmp_folder+'/'+script_file.name print(code) - result = self._run_cancelable(code) - if print_output: - output_writer.write(result.std_out) - output_writer.write(result.std_err) + result = self._run_cancelable(code, output_writer) + #if print_output: + # output_writer.write(result.std_out) + # output_writer.write(result.std_err) if not result.success: raise Exception(ErrorMsg.RUN_SCRIPT % result.std_err) @@ -148,16 +149,31 @@ def delete_temp_folder(self, tmp_folder): """ :type tmp_folder: str """ - result = self._run_cancelable('rm -rf '+tmp_folder) + result = self._run_cancelable('rm -rf '+tmp_folder, None) #TBD: decide if to pass None or output_writer if not result.success: raise Exception(ErrorMsg.DELETE_TEMP_FOLDER % result.std_err) - def _run(self, code): + def _run(self, code, output_writer): self.logger.debug('BashScript:' + code) #stdin, stdout, stderr = self._run_cancelable(code) - stdin, stdout, stderr = self.session.exec_command(code) - + stdin, stdout, stderr = self.session.exec_command("stdbuf -oL " + code) + + chan = self.session.get_transport().open_session() + chan.exec_command(code) + def output_reader(chan): + while not chan.exit_status_ready(): + if chan.recv_ready(): + data = chan.recv(1024) + #print "Indside stdout" + while data: + output_writer(data) + data = chan.recv(1024) + print("yay!") + + + t = threading.Thread(target=output_reader, args=(chan,)) + t.start() exit_code = stdout.channel.recv_exit_status() stdout_txt = ''.join(stdout.readlines()) stderr_txt = ''.join(stderr.readlines()) @@ -168,8 +184,8 @@ def _run(self, code): return LinuxScriptExecutor.ExecutionResult(exit_code, stdout_txt, stderr_txt) - def _run_cancelable(self, txt, *args): - async_result = self.pool.apply_async(self._run, kwds={'code': txt % args}) + def _run_cancelable(self, txt, *args, output_writer): + async_result = self.pool.apply_async(self._run, kwds={'code': txt % args, 'output_writer':output_writer}) while not async_result.ready(): if self.cancel_sampler.is_cancelled(): diff --git a/package/cloudshell/cm/customscript/domain/windows_script_executor.py b/package/cloudshell/cm/customscript/domain/windows_script_executor.py index a4c67a0..93a7d08 100644 --- a/package/cloudshell/cm/customscript/domain/windows_script_executor.py +++ b/package/cloudshell/cm/customscript/domain/windows_script_executor.py @@ -9,7 +9,7 @@ import winrm from logging import Logger import xml.etree.ElementTree as ET -from winrm.exceptions import WinRMTransportError +from winrm.exceptions import WinRMOperationTimeoutError, WinRMTransportError from cloudshell.cm.customscript.domain.reservation_output_writer import ReservationOutputWriter from cloudshell.cm.customscript.domain.script_configuration import HostConfiguration @@ -111,7 +111,7 @@ def create_temp_folder(self): New-Item $fullPath -type directory | Out-Null Write-Output $fullPath """ - result = self._run_cancelable(code) + result = self._run_cancelable(code, None) #TBD: decide if to pass None or output_writer if result.status_code != 0: raise Exception(ErrorMsg.CREATE_TEMP_FOLDER % result.std_err) return result.std_out.decode('utf-8').rstrip('\r\n') @@ -133,7 +133,7 @@ def copy_script(self, tmp_folder, script_file): $data = [System.Convert]::FromBase64String("{2}") Add-Content -value $data -encoding byte -path $path """.format(tmp_folder, script_file.name, encoded_bulk.decode('utf-8')) - result = self._run_cancelable(code) + result = self._run_cancelable(code, None) #TBD: decide if to pass None or output_writer if result.status_code != 0: raise Exception(ErrorMsg.COPY_SCRIPT % result.std_err) @@ -152,10 +152,10 @@ def run_script(self, tmp_folder, script_file, env_vars, output_writer, print_out $path = Join-Path "{0}" "{1}" Invoke-Expression "& '$path'" """.format(tmp_folder, script_file.name) - result = self._run_cancelable(code) - if print_output: - output_writer.write(result.std_out) - output_writer.write(result.std_err) + result = self._run_cancelable(code, output_writer) # TBD: need to pass print_output + # if print_output: + # output_writer.write(result.std_out) + # output_writer.write(result.std_err) if result.status_code != 0: raise Exception(ErrorMsg.RUN_SCRIPT % result.std_err) @@ -167,7 +167,7 @@ def delete_temp_folder(self, tmp_folder): $path = "%s" Remove-Item $path -recurse """ - result = self._run_cancelable(code % tmp_folder) + result = self._run_cancelable(code % tmp_folder, None)#TBD: decide if to pass None or output_writer if result.status_code != 0: raise Exception(ErrorMsg.DELETE_TEMP_FOLDER % result.std_err) @@ -177,8 +177,38 @@ def delete_temp_folder(self, tmp_folder): # self.logger.debug('Stdout:' + result.std_out) # self.logger.debug('Stderr:' + result.std_err) # return result - - def _run_cancelable(self, ps_code): + def get_command_output1(self, shell_id, command_id, output_writer): + """ + Get the Output of the given shell and command + @param string shell_id: The shell id on the remote machine. + See #open_shell + @param string command_id: The command id on the remote machine. + See #run_command + #@return [Hash] Returns a Hash with a key :exitcode and :data. + Data is an Array of Hashes where the cooresponding key + # is either :stdout or :stderr. The reason it is in an Array so so + we can get the output in the order it ocurrs on + # the console. + """ + #session.WriteMessageToReservationOutput('61c860dd-3a09-4087-9368-3c122fdf48d4', "ofir2") + stdout_buffer, stderr_buffer = [], [] + command_done = False + while not command_done: + try: + stdout, stderr, return_code, command_done = \ + self.session.protocol._raw_get_command_output(shell_id, command_id) + if output_writer: + output_writer.write("ofir") + output_writer.write(stdout) + #output_writer.write(self._try_decode_error_xml(stderr.decode('utf-8')) ) + stdout_buffer.append(stdout) + stderr_buffer.append(stderr) + except WinRMOperationTimeoutError: + # this is an expected error when waiting for a long-running process, just silently retry + pass + return b''.join(stdout_buffer), b''.join(stderr_buffer), return_code + + def _run_cancelable(self, ps_code, output_writer): """ :type ps_code: str """ @@ -188,7 +218,9 @@ def _run_cancelable(self, ps_code): shell_id = self.session.protocol.open_shell() command_id = self.session.protocol.run_command(shell_id, bat_code) - async_result = self.pool.apply_async(self.session.protocol.get_command_output, kwds={'shell_id': shell_id, 'command_id': command_id}) + #async_result = self.pool.apply_async(self.session.protocol.get_command_output, kwds={'shell_id': shell_id, 'command_id': command_id}) + async_result = self.pool.apply_async(self.get_command_output1, kwds={'shell_id': shell_id, 'command_id': command_id, 'output_writer':output_writer}) + try: while not async_result.ready(): if self.cancel_sampler.is_cancelled():