* when httplib.Connection fails to get response it prevents further requests on

the connection - shallow copy and create a new connection object when this
happens

* timeout the SSH client to speed up tests
This commit is contained in:
Prasanna Santhanam 2012-01-22 22:06:50 +05:30
parent 0102e3f61d
commit 8efacc97a6
2 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,7 @@
import httplib
import urllib
import base64
import copy
import hmac
import hashlib
import json
@ -49,9 +50,16 @@ class cloudConnection(object):
sig = urllib.quote_plus(base64.encodestring(hmac.new(self.securityKey, hashStr, hashlib.sha1).digest()).strip())
requestUrl += "&signature=%s"%sig
self.connection.request("GET", "/client/api?%s"%requestUrl)
return self.connection.getresponse().read()
try:
response = self.connection.getresponse().read()
except httplib.HTTPException:
self.close()
self = copy.copy(self)
else:
return response
return
def make_request_without_auth(self, command, requests={}):
requests["command"] = command
@ -121,10 +129,10 @@ class cloudConnection(object):
else:
result = self.make_request_without_auth(commandName, requests)
if self.logging is not None:
self.logging.debug("got result: " + result)
if result is None:
return None
if self.logging is not None:
self.logging.debug("got result: " + result)
result = jsonHelper.getResultObj(result, response)
if raw or isAsync == "false":
@ -138,4 +146,4 @@ if __name__ == '__main__':
xml = '<?xml version="1.0" encoding="ISO-8859-1"?><deployVirtualMachineResponse><virtualmachine><id>407</id><name>i-1-407-RS3</name><displayname>i-1-407-RS3</displayname><account>system</account><domainid>1</domainid><domain>ROOT</domain><created>2011-07-30T14:45:19-0700</created><state>Running</state><haenable>false</haenable><zoneid>1</zoneid><zonename>CA1</zonename><hostid>3</hostid><hostname>kvm-50-205</hostname><templateid>4</templateid><templatename>CentOS 5.5(64-bit) no GUI (KVM)</templatename><templatedisplaytext>CentOS 5.5(64-bit) no GUI (KVM)</templatedisplaytext><passwordenabled>false</passwordenabled><serviceofferingid>1</serviceofferingid><serviceofferingname>Small Instance</serviceofferingname><cpunumber>1</cpunumber><cpuspeed>500</cpuspeed><memory>512</memory><guestosid>112</guestosid><rootdeviceid>0</rootdeviceid><rootdevicetype>NetworkFilesystem</rootdevicetype><nic><id>380</id><networkid>203</networkid><netmask>255.255.255.0</netmask><gateway>65.19.181.1</gateway><ipaddress>65.19.181.110</ipaddress><isolationuri>vlan://65</isolationuri><broadcasturi>vlan://65</broadcasturi><traffictype>Guest</traffictype><type>Direct</type><isdefault>true</isdefault><macaddress>06:52:da:00:00:08</macaddress></nic><hypervisor>KVM</hypervisor></virtualmachine></deployVirtualMachineResponse>'
conn = cloudConnection(None)
print conn.paraseReturnXML(xml, deployVirtualMachine.deployVirtualMachineResponse())
print conn.paraseReturnXML(xml, deployVirtualMachine.deployVirtualMachineResponse())

View File

@ -1,7 +1,7 @@
import paramiko
import cloudstackException
class remoteSSHClient(object):
def __init__(self, host, port, user, passwd):
def __init__(self, host, port, user, passwd, timeout=120):
self.host = host
self.port = port
self.user = user
@ -9,7 +9,7 @@ class remoteSSHClient(object):
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
self.ssh.connect(str(host),int(port), user, passwd)
self.ssh.connect(str(host),int(port), user, passwd, timeout=timeout)
except paramiko.SSHException, sshex:
raise cloudstackException.InvalidParameterException(repr(sshex))
@ -33,4 +33,4 @@ class remoteSSHClient(object):
if __name__ == "__main__":
ssh = remoteSSHClient("192.168.137.2", 22, "root", "password")
print ssh.execute("ls -l")
print ssh.execute("rm x")
print ssh.execute("rm x")