diff --git a/spug_api/libs/ssh.py b/spug_api/libs/ssh.py index 0366482..989460e 100644 --- a/spug_api/libs/ssh.py +++ b/spug_api/libs/ssh.py @@ -5,7 +5,7 @@ from paramiko.client import SSHClient, AutoAddPolicy from paramiko.rsakey import RSAKey from paramiko.ssh_exception import AuthenticationException from io import StringIO -import base64 +from uuid import uuid4 import time import re @@ -17,8 +17,8 @@ class SSH: self.client = None self.channel = None self.sftp = None + self.exec_file = None self.eof = 'Spug EOF 2108111926' - self.already_init = False self.default_env = self._make_env_command(default_env) self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?') self.arguments = { @@ -116,11 +116,11 @@ class SSH: def put_file(self, local_path, remote_path): sftp = self._get_sftp() - sftp.put(local_path, remote_path) + sftp.put(local_path, remote_path, confirm=False) def put_file_by_fl(self, fl, remote_path, callback=None): sftp = self._get_sftp() - sftp.putfo(fl, remote_path, callback=callback) + sftp.putfo(fl, remote_path, callback=callback, confirm=False) def list_dir_attr(self, path): sftp = self._get_sftp() @@ -180,19 +180,17 @@ class SSH: def _handle_command(self, command, environment): new_command = commands = '' - if not self.already_init: - commands = 'export SPUG_EXEC_FILE=$(mktemp)\n' - commands += 'trap \'rm -f $SPUG_EXEC_FILE\' EXIT\n' - self.already_init = True + if not self.exec_file: + self.exec_file = f'/tmp/{uuid4().hex}' + commands += f'trap \'rm -f {self.exec_file}\' EXIT\n' env_command = self._make_env_command(environment) if env_command: new_command += f'{env_command}\n' new_command += command new_command += f'\necho {self.eof} $?\n' - b64_command = base64.standard_b64encode(new_command.encode()) - commands += f'echo {b64_command.decode()} | base64 -di > $SPUG_EXEC_FILE\n' - commands += 'source $SPUG_EXEC_FILE\n' + self.put_file_by_fl(StringIO(new_command), self.exec_file) + commands += f'source {self.exec_file}\n' return commands def _decode(self, content):