Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emr/ssh.py
1567 views
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
14
import os
15
import subprocess
16
import tempfile
17
18
from awscli.customizations.emr import constants
19
from awscli.customizations.emr import emrutils
20
from awscli.customizations.emr import sshutils
21
from awscli.customizations.emr.command import Command
22
23
KEY_PAIR_FILE_HELP_TEXT = '\nA value for the variable Key Pair File ' \
24
'can be set in the AWS CLI config file using the ' \
25
'"aws configure set emr.key_pair_file <value>" command.\n'
26
27
28
class Socks(Command):
29
NAME = 'socks'
30
DESCRIPTION = ('Create a socks tunnel on port 8157 from your machine '
31
'to the master.\n%s' % KEY_PAIR_FILE_HELP_TEXT)
32
ARG_TABLE = [
33
{'name': 'cluster-id', 'required': True,
34
'help_text': 'Cluster Id of cluster you want to ssh into'},
35
{'name': 'key-pair-file', 'required': True,
36
'help_text': 'Private key file to use for login'},
37
]
38
39
def _run_main_command(self, parsed_args, parsed_globals):
40
try:
41
master_dns = sshutils.validate_and_find_master_dns(
42
session=self._session,
43
parsed_globals=parsed_globals,
44
cluster_id=parsed_args.cluster_id)
45
46
key_file = parsed_args.key_pair_file
47
sshutils.validate_ssh_with_key_file(key_file)
48
f = tempfile.NamedTemporaryFile(delete=False)
49
if (emrutils.which('ssh') or emrutils.which('ssh.exe')):
50
command = ['ssh', '-o', 'StrictHostKeyChecking=no', '-o',
51
'ServerAliveInterval=10', '-ND', '8157', '-i',
52
parsed_args.key_pair_file, constants.SSH_USER +
53
'@' + master_dns]
54
else:
55
command = ['putty', '-ssh', '-i', parsed_args.key_pair_file,
56
constants.SSH_USER + '@' + master_dns, '-N', '-D',
57
'8157']
58
59
print(' '.join(command))
60
rc = subprocess.call(command)
61
return rc
62
except KeyboardInterrupt:
63
print('Disabling Socks Tunnel.')
64
return 0
65
66
67
class SSH(Command):
68
NAME = 'ssh'
69
DESCRIPTION = ('SSH into master node of the cluster.\n%s' %
70
KEY_PAIR_FILE_HELP_TEXT)
71
ARG_TABLE = [
72
{'name': 'cluster-id', 'required': True,
73
'help_text': 'Cluster Id of cluster you want to ssh into'},
74
{'name': 'key-pair-file', 'required': True,
75
'help_text': 'Private key file to use for login'},
76
{'name': 'command', 'help_text': 'Command to execute on Master Node'}
77
]
78
79
def _run_main_command(self, parsed_args, parsed_globals):
80
master_dns = sshutils.validate_and_find_master_dns(
81
session=self._session,
82
parsed_globals=parsed_globals,
83
cluster_id=parsed_args.cluster_id)
84
85
key_file = parsed_args.key_pair_file
86
sshutils.validate_ssh_with_key_file(key_file)
87
f = tempfile.NamedTemporaryFile(delete=False)
88
if (emrutils.which('ssh') or emrutils.which('ssh.exe')):
89
command = ['ssh', '-o', 'StrictHostKeyChecking=no', '-o',
90
'ServerAliveInterval=10', '-i',
91
parsed_args.key_pair_file, constants.SSH_USER +
92
'@' + master_dns, '-t']
93
if parsed_args.command:
94
command.append(parsed_args.command)
95
else:
96
command = ['putty', '-ssh', '-i', parsed_args.key_pair_file,
97
constants.SSH_USER + '@' + master_dns, '-t']
98
if parsed_args.command:
99
f.write(parsed_args.command)
100
f.write('\nread -n1 -r -p "Command completed. Press any key."')
101
command.append('-m')
102
command.append(f.name)
103
104
f.close()
105
print(' '.join(command))
106
rc = subprocess.call(command)
107
os.remove(f.name)
108
return rc
109
110
111
class Put(Command):
112
NAME = 'put'
113
DESCRIPTION = ('Put file onto the master node.\n%s' %
114
KEY_PAIR_FILE_HELP_TEXT)
115
ARG_TABLE = [
116
{'name': 'cluster-id', 'required': True,
117
'help_text': 'Cluster Id of cluster you want to put file onto'},
118
{'name': 'key-pair-file', 'required': True,
119
'help_text': 'Private key file to use for login'},
120
{'name': 'src', 'required': True,
121
'help_text': 'Source file path on local machine'},
122
{'name': 'dest', 'help_text': 'Destination file path on remote host'}
123
]
124
125
def _run_main_command(self, parsed_args, parsed_globals):
126
master_dns = sshutils.validate_and_find_master_dns(
127
session=self._session,
128
parsed_globals=parsed_globals,
129
cluster_id=parsed_args.cluster_id)
130
131
key_file = parsed_args.key_pair_file
132
sshutils.validate_scp_with_key_file(key_file)
133
if (emrutils.which('scp') or emrutils.which('scp.exe')):
134
command = ['scp', '-r', '-o StrictHostKeyChecking=no',
135
'-i', parsed_args.key_pair_file, parsed_args.src,
136
constants.SSH_USER + '@' + master_dns]
137
else:
138
command = ['pscp', '-scp', '-r', '-i', parsed_args.key_pair_file,
139
parsed_args.src, constants.SSH_USER + '@' + master_dns]
140
141
# if the instance is not terminated
142
if parsed_args.dest:
143
command[-1] = command[-1] + ":" + parsed_args.dest
144
else:
145
command[-1] = command[-1] + ":" + parsed_args.src.split('/')[-1]
146
print(' '.join(command))
147
rc = subprocess.call(command)
148
return rc
149
150
151
class Get(Command):
152
NAME = 'get'
153
DESCRIPTION = ('Get file from master node.\n%s' % KEY_PAIR_FILE_HELP_TEXT)
154
ARG_TABLE = [
155
{'name': 'cluster-id', 'required': True,
156
'help_text': 'Cluster Id of cluster you want to get file from'},
157
{'name': 'key-pair-file', 'required': True,
158
'help_text': 'Private key file to use for login'},
159
{'name': 'src', 'required': True,
160
'help_text': 'Source file path on remote host'},
161
{'name': 'dest', 'help_text': 'Destination file path on your machine'}
162
]
163
164
def _run_main_command(self, parsed_args, parsed_globals):
165
master_dns = sshutils.validate_and_find_master_dns(
166
session=self._session,
167
parsed_globals=parsed_globals,
168
cluster_id=parsed_args.cluster_id)
169
170
key_file = parsed_args.key_pair_file
171
sshutils.validate_scp_with_key_file(key_file)
172
if (emrutils.which('scp') or emrutils.which('scp.exe')):
173
command = ['scp', '-r', '-o StrictHostKeyChecking=no', '-i',
174
parsed_args.key_pair_file, constants.SSH_USER + '@' +
175
master_dns + ':' + parsed_args.src]
176
else:
177
command = ['pscp', '-scp', '-r', '-i', parsed_args.key_pair_file,
178
constants.SSH_USER + '@' + master_dns + ':' +
179
parsed_args.src]
180
181
if parsed_args.dest:
182
command.append(parsed_args.dest)
183
else:
184
command.append(parsed_args.src.split('/')[-1])
185
print(' '.join(command))
186
rc = subprocess.call(command)
187
return rc
188
189