'Start an `ssh-agent` and add its socket path and pid to the global
environment so that SSH sub-processes can use it for authentication.
:param FilePath key_file: An SSH private key file which can be used
when authenticating with SSH servers.'

def __init__(self, key_file):

    self._cleanups = []
    output = check_output(['ssh-agent', '-c']).splitlines()
    sock = output[0].split()[2][:(-1)]
    pid = output[1].split()[2][:(-1)]
    self._pid = int(pid)
    def patchdict(k, v):
        if (k in os.environ):
            self._cleanups.append((lambda old=os.environ[k]: setitem(os.environ, k, old)))
        else:
            self._cleanups.append((lambda : delitem(os.environ, k)))
        os.environ[k] = v
    patchdict('SSH_AUTH_SOCK', sock)
    patchdict('SSH_AGENT_PID', pid)
    with open(os.devnull, 'w') as discard:
        check_call(['ssh-add', key_file.path], stdout=discard, stderr=discard)
