Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emr/sshutils.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 logging
15
16
from awscli.customizations.emr import exceptions
17
from awscli.customizations.emr import emrutils
18
from awscli.customizations.emr import constants
19
from botocore.exceptions import WaiterError
20
21
LOG = logging.getLogger(__name__)
22
23
24
def validate_and_find_master_dns(session, parsed_globals, cluster_id):
25
"""
26
Utility method for ssh, socks, put and get command.
27
Check if the cluster to be connected to is
28
terminated or being terminated.
29
Check if the cluster is running.
30
Find master instance public dns of a given cluster.
31
Return the latest created master instance public dns name.
32
Throw MasterDNSNotAvailableError or ClusterTerminatedError.
33
"""
34
cluster_state = emrutils.get_cluster_state(
35
session, parsed_globals, cluster_id)
36
37
if cluster_state in constants.TERMINATED_STATES:
38
raise exceptions.ClusterTerminatedError
39
40
emr = emrutils.get_client(session, parsed_globals)
41
42
try:
43
cluster_running_waiter = emr.get_waiter('cluster_running')
44
if cluster_state in constants.STARTING_STATES:
45
print("Waiting for the cluster to start.")
46
cluster_running_waiter.wait(ClusterId=cluster_id)
47
except WaiterError:
48
raise exceptions.MasterDNSNotAvailableError
49
50
return emrutils.find_master_dns(
51
session=session, cluster_id=cluster_id,
52
parsed_globals=parsed_globals)
53
54
55
def validate_ssh_with_key_file(key_file):
56
if (emrutils.which('putty.exe') or emrutils.which('ssh') or
57
emrutils.which('ssh.exe')) is None:
58
raise exceptions.SSHNotFoundError
59
else:
60
check_ssh_key_format(key_file)
61
62
63
def validate_scp_with_key_file(key_file):
64
if (emrutils.which('pscp.exe') or emrutils.which('scp') or
65
emrutils.which('scp.exe')) is None:
66
raise exceptions.SCPNotFoundError
67
else:
68
check_scp_key_format(key_file)
69
70
71
def check_scp_key_format(key_file):
72
# If only pscp is present and the file format is incorrect
73
if (emrutils.which('pscp.exe') is not None and
74
(emrutils.which('scp.exe') or emrutils.which('scp')) is None):
75
if check_command_key_format(key_file, ['ppk']) is False:
76
raise exceptions.WrongPuttyKeyError
77
else:
78
pass
79
80
81
def check_ssh_key_format(key_file):
82
# If only putty is present and the file format is incorrect
83
if (emrutils.which('putty.exe') is not None and
84
(emrutils.which('ssh.exe') or emrutils.which('ssh')) is None):
85
if check_command_key_format(key_file, ['ppk']) is False:
86
raise exceptions.WrongPuttyKeyError
87
else:
88
pass
89
90
91
def check_command_key_format(key_file, accepted_file_format=[]):
92
if any(key_file.endswith(i) for i in accepted_file_format):
93
return True
94
else:
95
return False
96
97