/awscli/customizations/emr/ssh.py

https://gitlab.com/github-cloud-corp/aws-cli
Python | 187 lines | 151 code | 23 blank | 13 comment | 20 complexity | 349ec9ebf9650b1e1706223d0c9a6452 MD5 | raw file
  1. # Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://aws.amazon.com/apache2.0/
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. import os
  14. import subprocess
  15. import tempfile
  16. from awscli.customizations.emr import constants
  17. from awscli.customizations.emr import emrutils
  18. from awscli.customizations.emr import sshutils
  19. from awscli.customizations.emr.command import Command
  20. KEY_PAIR_FILE_HELP_TEXT = '\nA value for the variable Key Pair File ' \
  21. 'can be set in the AWS CLI config file using the "aws configure set" ' \
  22. 'command.\n'
  23. class Socks(Command):
  24. NAME = 'socks'
  25. DESCRIPTION = ('Create a socks tunnel on port 8157 from your machine '
  26. 'to the master.\n%s' % KEY_PAIR_FILE_HELP_TEXT)
  27. ARG_TABLE = [
  28. {'name': 'cluster-id', 'required': True,
  29. 'help_text': 'Cluster Id of cluster you want to ssh into'},
  30. {'name': 'key-pair-file', 'required': True,
  31. 'help_text': 'Private key file to use for login'},
  32. ]
  33. def _run_main_command(self, parsed_args, parsed_globals):
  34. try:
  35. master_dns = sshutils.validate_and_find_master_dns(
  36. session=self._session,
  37. parsed_globals=parsed_globals,
  38. cluster_id=parsed_args.cluster_id)
  39. key_file = parsed_args.key_pair_file
  40. sshutils.validate_ssh_with_key_file(key_file)
  41. f = tempfile.NamedTemporaryFile(delete=False)
  42. if (emrutils.which('ssh') or emrutils.which('ssh.exe')):
  43. command = ['ssh', '-o', 'StrictHostKeyChecking=no', '-o',
  44. 'ServerAliveInterval=10', '-ND', '8157', '-i',
  45. parsed_args.key_pair_file, constants.SSH_USER +
  46. '@' + master_dns]
  47. else:
  48. command = ['putty', '-ssh', '-i', parsed_args.key_pair_file,
  49. constants.SSH_USER + '@' + master_dns, '-N', '-D',
  50. '8157']
  51. print(' '.join(command))
  52. rc = subprocess.call(command)
  53. return rc
  54. except KeyboardInterrupt:
  55. print('Disabling Socks Tunnel.')
  56. return 0
  57. class SSH(Command):
  58. NAME = 'ssh'
  59. DESCRIPTION = ('SSH into master node of the cluster.\n%s' %
  60. KEY_PAIR_FILE_HELP_TEXT)
  61. ARG_TABLE = [
  62. {'name': 'cluster-id', 'required': True,
  63. 'help_text': 'Cluster Id of cluster you want to ssh into'},
  64. {'name': 'key-pair-file', 'required': True,
  65. 'help_text': 'Private key file to use for login'},
  66. {'name': 'command', 'help_text': 'Command to execute on Master Node'}
  67. ]
  68. def _run_main_command(self, parsed_args, parsed_globals):
  69. master_dns = sshutils.validate_and_find_master_dns(
  70. session=self._session,
  71. parsed_globals=parsed_globals,
  72. cluster_id=parsed_args.cluster_id)
  73. key_file = parsed_args.key_pair_file
  74. sshutils.validate_ssh_with_key_file(key_file)
  75. f = tempfile.NamedTemporaryFile(delete=False)
  76. if (emrutils.which('ssh') or emrutils.which('ssh.exe')):
  77. command = ['ssh', '-o', 'StrictHostKeyChecking=no', '-o',
  78. 'ServerAliveInterval=10', '-i',
  79. parsed_args.key_pair_file, constants.SSH_USER +
  80. '@' + master_dns, '-t']
  81. if parsed_args.command:
  82. command.append(parsed_args.command)
  83. else:
  84. command = ['putty', '-ssh', '-i', parsed_args.key_pair_file,
  85. constants.SSH_USER + '@' + master_dns, '-t']
  86. if parsed_args.command:
  87. f.write(parsed_args.command)
  88. f.write('\nread -n1 -r -p "Command completed. Press any key."')
  89. command.append('-m')
  90. command.append(f.name)
  91. f.close()
  92. print(' '.join(command))
  93. rc = subprocess.call(command)
  94. os.remove(f.name)
  95. return rc
  96. class Put(Command):
  97. NAME = 'put'
  98. DESCRIPTION = ('Put file onto the master node.\n%s' %
  99. KEY_PAIR_FILE_HELP_TEXT)
  100. ARG_TABLE = [
  101. {'name': 'cluster-id', 'required': True,
  102. 'help_text': 'Cluster Id of cluster you want to put file onto'},
  103. {'name': 'key-pair-file', 'required': True,
  104. 'help_text': 'Private key file to use for login'},
  105. {'name': 'src', 'required': True,
  106. 'help_text': 'Source file path on local machine'},
  107. {'name': 'dest', 'help_text': 'Destination file path on remote host'}
  108. ]
  109. def _run_main_command(self, parsed_args, parsed_globals):
  110. master_dns = sshutils.validate_and_find_master_dns(
  111. session=self._session,
  112. parsed_globals=parsed_globals,
  113. cluster_id=parsed_args.cluster_id)
  114. key_file = parsed_args.key_pair_file
  115. sshutils.validate_scp_with_key_file(key_file)
  116. if (emrutils.which('scp') or emrutils.which('scp.exe')):
  117. command = ['scp', '-r', '-o StrictHostKeyChecking=no',
  118. '-i', parsed_args.key_pair_file, parsed_args.src,
  119. constants.SSH_USER + '@' + master_dns]
  120. else:
  121. command = ['pscp', '-scp', '-r', '-i', parsed_args.key_pair_file,
  122. parsed_args.src, constants.SSH_USER + '@' + master_dns]
  123. # if the instance is not terminated
  124. if parsed_args.dest:
  125. command[-1] = command[-1] + ":" + parsed_args.dest
  126. else:
  127. command[-1] = command[-1] + ":" + parsed_args.src.split('/')[-1]
  128. print(' '.join(command))
  129. rc = subprocess.call(command)
  130. return rc
  131. class Get(Command):
  132. NAME = 'get'
  133. DESCRIPTION = ('Get file from master node.\n%s' % KEY_PAIR_FILE_HELP_TEXT)
  134. ARG_TABLE = [
  135. {'name': 'cluster-id', 'required': True,
  136. 'help_text': 'Cluster Id of cluster you want to get file from'},
  137. {'name': 'key-pair-file', 'required': True,
  138. 'help_text': 'Private key file to use for login'},
  139. {'name': 'src', 'required': True,
  140. 'help_text': 'Source file path on remote host'},
  141. {'name': 'dest', 'help_text': 'Destination file path on your machine'}
  142. ]
  143. def _run_main_command(self, parsed_args, parsed_globals):
  144. master_dns = sshutils.validate_and_find_master_dns(
  145. session=self._session,
  146. parsed_globals=parsed_globals,
  147. cluster_id=parsed_args.cluster_id)
  148. key_file = parsed_args.key_pair_file
  149. sshutils.validate_scp_with_key_file(key_file)
  150. if (emrutils.which('scp') or emrutils.which('scp.exe')):
  151. command = ['scp', '-r', '-o StrictHostKeyChecking=no', '-i',
  152. parsed_args.key_pair_file, constants.SSH_USER + '@' +
  153. master_dns + ':' + parsed_args.src]
  154. else:
  155. command = ['pscp', '-scp', '-r', '-i', parsed_args.key_pair_file,
  156. constants.SSH_USER + '@' + master_dns + ':' +
  157. parsed_args.src]
  158. if parsed_args.dest:
  159. command.append(parsed_args.dest)
  160. else:
  161. command.append(parsed_args.src.split('/')[-1])
  162. print(' '.join(command))
  163. rc = subprocess.call(command)
  164. return rc