Guest User

Untitled

a guest
Apr 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import logging
  2.  
  3. import pexpect
  4. from airflow.hooks.base_hook import BaseHook
  5.  
  6.  
  7. class SFTP(object):
  8. """
  9. Requires openssh_client. Spawns process to execute sftp command.
  10. """
  11.  
  12. def __init__(self, connection_id):
  13. """
  14. :param connection_id: The airflow connection id to use when connecting running sftp command
  15. """
  16. connection = BaseHook.get_connection(connection_id)
  17. self.username = connection.login
  18. self.host = connection.host
  19. self.port = connection.port
  20. self.password = connection.password
  21.  
  22. def get_files(self, remote_path, local_path):
  23. """
  24. Connect to connection_id and download all files in the remote path.
  25. :param remote_path: The remote path to sync
  26. :param local_path: The local dir to store files
  27. :return: None
  28. """
  29. logging.info(f'sftp get {remote_path} to: {local_path}')
  30.  
  31. child = None
  32.  
  33. try:
  34. logging.debug('spawning child')
  35. child = pexpect.spawn(
  36. f'/usr/bin/sftp -r -P {self.port} {self.username}@{self.host}:{remote_path} {local_path}',
  37. timeout=14400)
  38. self._handle_sftp_prompt(child)
  39. child.expect(pexpect.EOF)
  40. finally:
  41. if child:
  42. child.close()
  43. if child.isalive():
  44. logging.warning('Child did not exit gracefully.')
  45. else:
  46. logging.debug('Child exited gracefully.')
  47. if child:
  48. if child.status > 0:
  49. raise Exception(f'sftp command exited with'
  50. f' status: {child.status}'
  51. f' sigstatus: {child.signalstatus}'
  52. f' exitstatus: {child.exitstatus}')
  53. else:
  54. logging.info('download complete')
  55.  
  56. def _handle_sftp_prompt(self, child):
  57. logging.debug('expecting prompt...')
  58. i = child.expect(['.*password:.*', '.*continue connecting.*', '.*Connected.*'])
  59. if i == 0:
  60. logging.info('supplying pass to sftp server')
  61. child.sendline(self.password)
  62. logging.debug('sent pw')
  63. self._handle_sftp_prompt(child)
  64. elif i == 1:
  65. logging.info('answering yes to host check')
  66. child.sendline('yes')
  67. logging.debug('sent yes')
  68. self._handle_sftp_prompt(child)
  69. elif i == 2:
  70. logging.info('connected to sftp server')
Add Comment
Please, Sign In to add comment