Merge pull request #509 from xiasf/3.0-fix-ssh-timeout

F 修复执行发布前任务可能出现response timeout的问题
pull/517/head
vapao 2022-06-27 13:08:30 +08:00 committed by GitHub
commit 9ba9f5782e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions

View File

@ -31,7 +31,7 @@ class SSH:
self.sftp = None self.sftp = None
self.exec_file = None self.exec_file = None
self.eof = 'Spug EOF 2108111926' self.eof = 'Spug EOF 2108111926'
self.default_env = self._make_env_command(default_env) self.default_env = default_env
self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?') self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?')
self.arguments = { self.arguments = {
'hostname': hostname, 'hostname': hostname,
@ -83,7 +83,7 @@ class SSH:
def exec_command(self, command, environment=None): def exec_command(self, command, environment=None):
channel = self._get_channel() channel = self._get_channel()
command = self._handle_command(command, environment) command = self._handle_command(command, environment)
channel.send(command) channel.sendall(command)
out, exit_code = '', -1 out, exit_code = '', -1
for line in self.stdout: for line in self.stdout:
match = self.regex.search(line) match = self.regex.search(line)
@ -112,7 +112,7 @@ class SSH:
def exec_command_with_stream(self, command, environment=None): def exec_command_with_stream(self, command, environment=None):
channel = self._get_channel() channel = self._get_channel()
command = self._handle_command(command, environment) command = self._handle_command(command, environment)
channel.send(command) channel.sendall(command)
exit_code, line = -1, '' exit_code, line = -1, ''
while True: while True:
line = self._decode(channel.recv(8196)) line = self._decode(channel.recv(8196))
@ -153,14 +153,14 @@ class SSH:
counter = 0 counter = 0
self.channel = self.client.invoke_shell() self.channel = self.client.invoke_shell()
command = 'set +o history\nset +o zle\nset -o no_nomatch\nexport PS1= && stty -echo\n' command = 'set +o history\nset +o zle\nset -o no_nomatch\nexport PS1= && stty -echo\n'
if self.default_env: command = self._handle_command(command, self.default_env)
command += f'{self.default_env}\n' self.channel.sendall(command)
command += f'echo {self.eof} $?\n' out = line = ''
self.channel.send(command.encode())
while True: while True:
if self.channel.recv_ready(): if self.channel.recv_ready():
line = self._decode(self.channel.recv(8196)) line = self._decode(self.channel.recv(8196))
if self.regex.search(line): out += line
if self.regex.search(out):
self.stdout = self.channel.makefile('r') self.stdout = self.channel.makefile('r')
break break
elif counter >= 100: elif counter >= 100: