示例#1
0
 def calls_agent_handler_close_if_enabled(self, Handler, client):
     c = Connection("host", forward_agent=True)
     c.create_session()
     c.close()
     # NOTE: this will need to change if, for w/e reason, we ever want
     # to run multiple handlers at once
     Handler.return_value.close.assert_called_once_with()
示例#2
0
 def calls_agent_handler_close_if_enabled(self, Handler, client):
     c = Connection('host', forward_agent=True)
     c.create_session()
     c.close()
     # NOTE: this will need to change if, for w/e reason, we ever want
     # to run multiple handlers at once
     Handler.return_value.close.assert_called_once_with()
示例#3
0
文件: ssh.py 项目: pnwairfire/afaws
class SshClient(object):
    def __init__(self, ssh_key, ip):
        self._ssh_key = ssh_key
        self._ip = ip
        self.client = None

    def __enter__(self):
        self._create_client()
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def _create_client(self):
        if self.client is None:
            self.client = Connection(
                host=self._ip,
                user="******",
                connect_kwargs={"key_filename": self._ssh_key})

        return self.client

    async def execute(self, cmd, ignore_errors=False):
        logging.info("About to run %s on %s", cmd, self._ip)
        try:
            return await run_in_loop_executor(self.client.run, cmd, hide=True)
        except UnexpectedExit as e:
            if ignore_errors:
                return e.result
            raise

        # TODO: handle other exceptions?

    async def put(self, local_file_path, remote_file_path):
        """Uploads local file(s) to remote server, recursively if passed a
        directory.
        """
        if os.path.isdir(local_file_path):
            await self.execute('mkdir {}'.format(remote_file_path))
            for f in os.listdir(local_file_path):
                await self.put(os.path.join(local_file_path, f),
                               os.path.join(remote_file_path, f))
        else:
            await run_in_loop_executor(self.client.put,
                                       local_file_path,
                                       remote=remote_file_path)

    async def get(self, remote_file_path, local_file_path):
        """Downloads remote files to local file system

        TODO: Support recusrive mode if passed a directory
        """
        await run_in_loop_executor(self.client.get,
                                   remote_file_path,
                                   local=local_file_path)

    def close(self):
        if self.client and self.client.is_connected:
            self.client.close()
示例#4
0
 def short_circuits_if_not_connected(self, client):
     c = Connection("host")
     # Won't trigger close() on client because it'll already think it's
     # closed (due to no .transport & the behavior of .is_connected)
     c.close()
     assert not client.close.called
示例#5
0
 def calls_SSHClient_close(self, client):
     "calls paramiko.SSHClient.close()"
     c = Connection("host")
     c.open()
     c.close()
     client.close.assert_called_with()
示例#6
0
 def has_no_required_args_and_returns_None(self, client):
     c = Connection("host")
     c.open()
     assert c.close() is None
示例#7
0
 def short_circuits_if_not_connected(self, client):
     c = Connection('host')
     # Won't trigger close() on client because it'll already think it's
     # closed (due to no .transport & the behavior of .is_connected)
     c.close()
     assert not client.close.called
示例#8
0
 def calls_SSHClient_close(self, client):
     "calls paramiko.SSHClient.close()"
     c = Connection('host')
     c.open()
     c.close()
     client.close.assert_called_with()
示例#9
0
 def has_no_required_args_and_returns_None(self, client):
     c = Connection('host')
     c.open()
     assert c.close() is None