Skip to content

Commit cf7532e

Browse files
Santhosh EdukullaSrikanteswaraRao Talluri
authored andcommitted
Added fix for bug 5056.
The fix contains fixes related to ssh library majorly. Complete description should be available under bug description. Signed-off-by: Santhosh Edukulla <Santhosh.Edukulla@citrix.com> Signed-off-by: SrikanteswaraRao Talluri <talluri@apache.org>
1 parent c76da53 commit cf7532e

3 files changed

Lines changed: 127 additions & 42 deletions

File tree

‎tools/marvin/marvin/codes.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@
4040
FAIL = 0
4141
PASS = 1
4242
MATCH_NOT_FOUND = "ELEMENT NOT FOUND IN THE INPUT"
43+
SUCCESS = "SUCCESS"
44+
EXCEPTION_OCCURRED = "Exception Occurred"

‎tools/marvin/marvin/integration/lib/utils.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,16 @@ def cleanup_resources(api_client, resources):
113113
obj.delete(api_client)
114114

115115

116-
def is_server_ssh_ready(ipaddress, port, username, password, retries=10, timeout=30, keyPairFileLocation=None):
117-
"""Return ssh handle else wait till sshd is running"""
116+
def is_server_ssh_ready(ipaddress, port, username, password, retries=10, retryinterv=30, timeout=3.0, keyPairFileLocation=None):
117+
'''
118+
@Name: is_server_ssh_ready
119+
@Input: timeout: tcp connection timeout flag,
120+
others information need to be added
121+
@Output:object for remoteSSHClient
122+
Name of the function is little misnomer and is not
123+
verifying anything as such mentioned
124+
'''
125+
118126
try:
119127
ssh = remoteSSHClient(
120128
host=ipaddress,
@@ -123,9 +131,10 @@ def is_server_ssh_ready(ipaddress, port, username, password, retries=10, timeout
123131
passwd=password,
124132
keyPairFileLocation=keyPairFileLocation,
125133
retries=retries,
126-
delay=timeout)
134+
delay=retryinterv,
135+
timeout=timeout)
127136
except Exception, e:
128-
raise Exception("Failed to bring up ssh service in time. Waited %ss. Error is %s" % (retries * timeout, e))
137+
raise Exception("SSH connection has Failed. Waited %ss. Error is %s" % (retries * retryinterv, e))
129138
else:
130139
return ssh
131140

‎tools/marvin/marvin/remoteSSHClient.py‎

Lines changed: 112 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,48 @@
2020
import cloudstackException
2121
import contextlib
2222
import logging
23+
from marvin.codes import (
24+
SUCCESS, FAIL, INVALID_INPUT, EXCEPTION_OCCURRED
25+
)
2326
from contextlib import closing
2427

2528

2629
class remoteSSHClient(object):
30+
'''
31+
Added timeout flag for ssh connect calls.Default to 3.0 seconds
32+
'''
2733
def __init__(self, host, port, user, passwd, retries=10, delay=30,
28-
log_lvl=logging.INFO, keyPairFileLocation=None):
29-
self.host = host
30-
self.port = port
34+
log_lvl=logging.INFO, keyPairFiles=None, timeout=3.0):
35+
self.host = None
36+
self.port = 22
3137
self.user = user
3238
self.passwd = passwd
33-
self.keyPairFile = keyPairFileLocation
39+
self.keyPairFiles = keyPairFiles
3440
self.ssh = paramiko.SSHClient()
3541
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
3642
self.logger = logging.getLogger('sshClient')
43+
self.retryCnt = 0
44+
self.delay = 0
45+
self.timeout = 3.0
3746
ch = logging.StreamHandler()
3847
ch.setLevel(log_lvl)
3948
self.logger.addHandler(ch)
4049

41-
retry_count = retries
42-
while retry_count >= 0:
43-
try:
44-
if keyPairFileLocation is None:
45-
self.ssh.connect(str(host), int(port), user, passwd)
46-
self.logger.debug("SSH connect: %s@%s with passwd %s" %
47-
(user, str(host), passwd))
48-
else:
49-
self.ssh.connect(hostname=str(host),
50-
port=int(port),
51-
username=str(user),
52-
key_filename=str(keyPairFileLocation),
53-
look_for_keys=False
54-
)
55-
self.logger.debug(
56-
"connecting to server %s with user %s key %s" %
57-
(str(host), user, keyPairFileLocation))
58-
self.logger.debug("SSH connect: %s@%s with passwd %s" %
59-
(user, str(host), passwd))
60-
#except paramiko.AuthenticationException, authEx:
61-
# raise cloudstackException. \
62-
# InvalidParameterException("Invalid credentials to "
63-
# + "login to %s on port %s" %
64-
# (str(host), port))
65-
except Exception as se:
66-
if retry_count == 0:
67-
raise cloudstackException. \
68-
InvalidParameterException(repr(se))
69-
else:
70-
return
71-
72-
retry_count = retry_count - 1
73-
time.sleep(delay)
50+
#Check invalid host value and raise exception
51+
#Atleast host is required for connection
52+
if host is not None and host != '':
53+
self.host = host
54+
if retries is not None and retries > 0:
55+
self.retryCnt = retries
56+
if delay is not None and delay > 0:
57+
self.delay = delay
58+
if timeout is not None and timeout > 0:
59+
self.timeout = timeout
60+
if port is not None or port >= 0:
61+
self.port = port
62+
if self.createConnection() == FAIL:
63+
raise cloudstackException.\
64+
internalError("Connection Failed")
7465

7566
def execute(self, command):
7667
stdin, stdout, stderr = self.ssh.exec_command(command)
@@ -89,6 +80,88 @@ def execute(self, command):
8980
(command, str(self.host), results))
9081
return results
9182

83+
def createConnection(self):
84+
'''
85+
@Name: createConnection
86+
@Desc: Creates an ssh connection for
87+
retries mentioned,along with sleep mentioned
88+
@Output: SUCCESS on successful connection
89+
FAIL If connection through ssh failed
90+
'''
91+
ret = FAIL
92+
while self.retryCnt >= 0:
93+
try:
94+
self.logger.debug("SSH Connection: Host:%s User:%s\
95+
Port:%s KeyPairFile: %s" %
96+
(self.host, self.user, str(self.port),
97+
str(self.keyPairFiles)))
98+
if self.keyPairFiles is None:
99+
self.ssh.connect(hostname=self.host,
100+
port=self.port,
101+
username=self.user,
102+
password=self.passwd,
103+
timeout=self.timeout)
104+
else:
105+
self.ssh.connect(hostname=self.host,
106+
port=self.port,
107+
username=self.user,
108+
password=self.passwd,
109+
key_filename=self.keyPairFiles,
110+
timeout=self.timeout,
111+
look_for_keys=False
112+
)
113+
ret = SUCCESS
114+
break
115+
except Exception as se:
116+
self.retryCnt = self.retryCnt - 1
117+
if self.retryCnt == 0:
118+
break
119+
time.sleep(self.delay)
120+
return ret
121+
122+
def runCommand(self, command):
123+
'''
124+
@Name: runCommand
125+
@Desc: Runs a command over ssh and
126+
returns the result along with status code
127+
@Input: command to execute
128+
@Output: 1: status of command executed.
129+
Default to None
130+
SUCCESS : If command execution is successful
131+
FAIL : If command execution has failed
132+
EXCEPTION_OCCURRED: Exception occurred while executing
133+
command
134+
INVALID_INPUT : If invalid value for command is passed
135+
2: stdin,stdout,stderr values of command output
136+
'''
137+
excep_msg = ''
138+
ret = {"status": None, "stdin": None, "stdout": None, "stderr": None}
139+
if command is None or command == '':
140+
ret["status"] = INVALID_INPUT
141+
return ret
142+
try:
143+
status_check = 1
144+
stdin, stdout, stderr = self.ssh.exec_command(command)
145+
output = stdout.readlines()
146+
errors = stderr.readlines()
147+
inp = stdin.readlines()
148+
ret["stdin"] = inp
149+
ret["stdout"] = output
150+
ret["stderr"] = errors
151+
if stdout is not None:
152+
status_check = stdout.channel.recv_exit_status()
153+
if status_check == 0:
154+
ret["status"] = SUCCESS
155+
else:
156+
ret["status"] = FAIL
157+
except Exception as e:
158+
excep_msg = str(e)
159+
ret["status"] = EXCEPTION_OCCURRED
160+
finally:
161+
self.logger.debug(" Host: %s Cmd: %s Output:%s Exception: %s" %
162+
(self.host, command, str(ret), excep_msg))
163+
return ret
164+
92165
def scp(self, srcFile, destPath):
93166
transport = paramiko.Transport((self.host, int(self.port)))
94167
transport.connect(username=self.user, password=self.passwd)
@@ -99,7 +172,8 @@ def scp(self, srcFile, destPath):
99172
raise e
100173

101174
def close(self):
102-
self.ssh.close()
175+
if self.ssh is not None:
176+
self.ssh.close()
103177

104178

105179
if __name__ == "__main__":

0 commit comments

Comments
 (0)