1
0
mirror of https://github.com/ARM-software/workload-automation.git synced 2025-04-20 09:40:50 +01:00

ssh: making execute() thread safe.

This commit is contained in:
Sergei Trofimov 2015-05-05 09:09:57 +01:00
parent 1993007d49
commit a6ef53291b

View File

@ -17,6 +17,7 @@
import logging import logging
import subprocess import subprocess
import re import re
import threading
import pxssh import pxssh
from pexpect import EOF, TIMEOUT, spawn from pexpect import EOF, TIMEOUT, spawn
@ -88,6 +89,7 @@ class SshShell(object):
self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt self.password_prompt = password_prompt if password_prompt is not None else self.default_password_prompt
self.timeout = timeout self.timeout = timeout
self.conn = None self.conn = None
self.lock = threading.Lock()
def login(self, host, username, password=None, keyfile=None, port=None, timeout=None, telnet=False): def login(self, host, username, password=None, keyfile=None, port=None, timeout=None, telnet=False):
# pylint: disable=attribute-defined-outside-init # pylint: disable=attribute-defined-outside-init
@ -118,13 +120,14 @@ class SshShell(object):
return subprocess.Popen(command, stdout=stdout, stderr=stderr, shell=True) return subprocess.Popen(command, stdout=stdout, stderr=stderr, shell=True)
def execute(self, command, timeout=None, check_exit_code=True, as_root=False, strip_colors=True): def execute(self, command, timeout=None, check_exit_code=True, as_root=False, strip_colors=True):
output = self._execute_and_wait_for_prompt(command, timeout, as_root, strip_colors) with self.lock:
if check_exit_code: output = self._execute_and_wait_for_prompt(command, timeout, as_root, strip_colors)
exit_code = int(self._execute_and_wait_for_prompt('echo $?', strip_colors=strip_colors, log=False)) if check_exit_code:
if exit_code: exit_code = int(self._execute_and_wait_for_prompt('echo $?', strip_colors=strip_colors, log=False))
message = 'Got exit code {}\nfrom: {}\nOUTPUT: {}' if exit_code:
raise DeviceError(message.format(exit_code, command, output)) message = 'Got exit code {}\nfrom: {}\nOUTPUT: {}'
return output raise DeviceError(message.format(exit_code, command, output))
return output
def logout(self): def logout(self):
logger.debug('Logging out {}@{}'.format(self.username, self.host)) logger.debug('Logging out {}@{}'.format(self.username, self.host))