Honeycomb setup and utils
[csit.git] / resources / libraries / python / ssh.py
index ad5fb27..6914d52 100644 (file)
@@ -10,6 +10,7 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import socket
 import paramiko
 from paramiko import RSAKey
 import StringIO
@@ -67,6 +68,17 @@ class SSH(object):
                 format(self._ssh.get_transport().getpeername()))
         logger.debug('Connections: {0}'.format(str(SSH.__existing_connections)))
 
+    def disconnect(self, node):
+        """Close SSH connection to the node.
+
+        :param node: The node to disconnect from.
+        :type node: dict
+        """
+        node_hash = self._node_hash(node)
+        if node_hash in SSH.__existing_connections:
+            ssh = SSH.__existing_connections.pop(node_hash)
+            ssh.close()
+
     def exec_command(self, cmd, timeout=10):
         """Execute SSH command on a new channel on the connected Node.
 
@@ -84,11 +96,17 @@ class SSH(object):
             self._ssh.get_transport().getpeername(), end-start))
 
         stdout = ""
-        while True:
-            buf = chan.recv(self.__MAX_RECV_BUF)
-            stdout += buf
-            if not buf:
-                break
+        try:
+            while True:
+                buf = chan.recv(self.__MAX_RECV_BUF)
+                stdout += buf
+                if not buf:
+                    break
+        except socket.timeout:
+            logger.error('Caught timeout exception, current contents '
+                         'of buffer: {0}'.format(stdout))
+            raise
+
 
         stderr = ""
         while True: