# Path: ferry/fabric/com.py
# def robust_com(cmd):
#     # All the possible errors that might happen when
#     # we try to connect via ssh. 
#     route_closed = re.compile('.*No route to host.*', re.DOTALL)
#     conn_closed = re.compile('.*Connection closed.*', re.DOTALL)
#     refused_closed = re.compile('.*Connection refused.*', re.DOTALL)
#     timed_out = re.compile('.*timed out*', re.DOTALL)
#     permission = re.compile('.*Permission denied.*', re.DOTALL)
# 
#     num_tries = 0
#     while(True):
#         proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
#         output = proc.stdout.read()
#         err = proc.stderr.read()
#         if route_closed.match(err) or conn_closed.match(err) or refused_closed.match(err) or timed_out.match(err) or permission.match(err):            
#             if num_tries < MAX_COM_RETRIES:
#                 logging.warning("com error, trying again...")
#                 num_tries += 1
#                 time.sleep(10 * num_tries)
#             else: 
#                 logging.error("could not communicate")
#                 return None, None, False
#         else:
#             logging.warning("com msg: " + err)
#             break
#             
#     return output, err, True


from ferry.fabric.com import robust_com
from subprocess import Popen, PIPE
import json
import logging
import os
import re
import sys
        """

        if not server:
            # The server is not supplied, so just execute
            # the command locally. 
            proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
            if read_output:
                out = proc.stdout.read()
                err = proc.stderr.read()
        else:
            # Do not store results in hosts file or warn about 
            # changing ssh keys. Also use the key given to us by the fabric. 
            flags = " -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "
            flags += " -i " + self.key

            # If the user is given explicitly use that. Otherwise use the
            # default user (which is probably root). 
            if user:
                ip = user + '@' + server
            else:
                ip = self.docker_user + '@' + server
            flags += " -t -t " + ip

            # Wrap the command around an ssh command. 
            ssh = 'ssh ' + flags + ' \'%s\'' % cmd
            logging.warning(ssh)

            # All the possible errors that might happen when
            # we try to connect via ssh. 
            if read_output:

# Path: ferry/fabric/com.py
# def robust_com(cmd):
#     # All the possible errors that might happen when
#     # we try to connect via ssh. 
#     route_closed = re.compile('.*No route to host.*', re.DOTALL)
#     conn_closed = re.compile('.*Connection closed.*', re.DOTALL)
#     refused_closed = re.compile('.*Connection refused.*', re.DOTALL)
#     timed_out = re.compile('.*timed out*', re.DOTALL)
#     permission = re.compile('.*Permission denied.*', re.DOTALL)
# 
#     num_tries = 0
#     while(True):
#         proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
#         output = proc.stdout.read()
#         err = proc.stderr.read()
#         if route_closed.match(err) or conn_closed.match(err) or refused_closed.match(err) or timed_out.match(err) or permission.match(err):            
#             if num_tries < MAX_COM_RETRIES:
#                 logging.warning("com error, trying again...")
#                 num_tries += 1
#                 time.sleep(10 * num_tries)
#             else: 
#                 logging.error("could not communicate")
#                 return None, None, False
#         else:
#             logging.warning("com msg: " + err)
#             break
#             
#     return output, err, True


# Path: ferry/docker/docker.py
from ferry.fabric.com import robust_com
from subprocess import Popen, PIPE
import json
import logging
import os
import re
import sys

        """

        if not server:
            # The server is not supplied, so just execute
            # the command locally. 
            proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
            if read_output:
                out = proc.stdout.read()
                err = proc.stderr.read()
        else:
            # Do not store results in hosts file or warn about 
            # changing ssh keys. Also use the key given to us by the fabric. 
            flags = " -o ConnectTimeout=10 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "
            flags += " -i " + self.key

            # If the user is given explicitly use that. Otherwise use the
            # default user (which is probably root). 
            if user:
                ip = user + '@' + server
            else:
                ip = self.docker_user + '@' + server
            flags += " -t -t " + ip

            # Wrap the command around an ssh command. 
            ssh = 'ssh ' + flags + ' \'%s\'' % cmd
            logging.warning(ssh)

            # All the possible errors that might happen when
            # we try to connect via ssh. 
            if read_output:

                out, err, success = robust_com(ssh)