Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emr/command.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
from awscli.customizations.commands import BasicCommand
16
from awscli.customizations.emr import config
17
from awscli.customizations.emr import configutils
18
from awscli.customizations.emr import emrutils
19
from awscli.customizations.emr import exceptions
20
21
LOG = logging.getLogger(__name__)
22
23
24
class Command(BasicCommand):
25
region = None
26
27
UNSUPPORTED_COMMANDS_FOR_RELEASE_BASED_CLUSTERS = set([
28
'install-applications',
29
'restore-from-hbase-backup',
30
'schedule-hbase-backup',
31
'create-hbase-backup',
32
'disable-hbase-backups',
33
])
34
35
def supports_arg(self, name):
36
return any((x['name'] == name for x in self.ARG_TABLE))
37
38
def _run_main(self, parsed_args, parsed_globals):
39
40
self._apply_configs(parsed_args,
41
configutils.get_configs(self._session))
42
self.region = emrutils.get_region(self._session, parsed_globals)
43
self._validate_unsupported_commands_for_release_based_clusters(
44
parsed_args, parsed_globals)
45
return self._run_main_command(parsed_args, parsed_globals)
46
47
def _apply_configs(self, parsed_args, parsed_configs):
48
applicable_configurations = \
49
self._get_applicable_configurations(parsed_args, parsed_configs)
50
51
configs_added = {}
52
for configuration in applicable_configurations:
53
configuration.add(self, parsed_args,
54
parsed_configs[configuration.name])
55
configs_added[configuration.name] = \
56
parsed_configs[configuration.name]
57
58
if configs_added:
59
LOG.debug("Updated arguments with configs: %s" % configs_added)
60
else:
61
LOG.debug("No configs applied")
62
LOG.debug("Running command with args: %s" % parsed_args)
63
64
def _get_applicable_configurations(self, parsed_args, parsed_configs):
65
# We need to find the applicable configurations by applying
66
# following filters:
67
# 1. Configurations that are applicable to this command
68
# 3. Configurations that are present in parsed_configs
69
# 2. Configurations that are not present in parsed_args
70
71
configurations = \
72
config.get_applicable_configurations(self)
73
74
configurations = [x for x in configurations
75
if x.name in parsed_configs and
76
not x.is_present(parsed_args)]
77
78
configurations = self._filter_configurations_in_special_cases(
79
configurations, parsed_args, parsed_configs)
80
81
return configurations
82
83
def _filter_configurations_in_special_cases(self, configurations,
84
parsed_args, parsed_configs):
85
# Subclasses can override this method to filter the applicable
86
# configurations further based upon some custom logic
87
# Default behavior is to return the configurations list as is
88
return configurations
89
90
def _run_main_command(self, parsed_args, parsed_globals):
91
# Subclasses should implement this method.
92
# parsed_globals are the parsed global args (things like region,
93
# profile, output, etc.)
94
# parsed_args are any arguments you've defined in your ARG_TABLE
95
# that are parsed.
96
# parsed_args are updated to include any emr specific configuration
97
# from the config file if the corresponding argument is not
98
# explicitly specified on the CLI
99
raise NotImplementedError("_run_main_command")
100
101
def _validate_unsupported_commands_for_release_based_clusters(
102
self, parsed_args, parsed_globals):
103
command = self.NAME
104
105
if (command in self.UNSUPPORTED_COMMANDS_FOR_RELEASE_BASED_CLUSTERS and
106
hasattr(parsed_args, 'cluster_id')):
107
release_label = emrutils.get_release_label(
108
parsed_args.cluster_id, self._session, self.region,
109
parsed_globals.endpoint_url, parsed_globals.verify_ssl)
110
if release_label:
111
raise exceptions.UnsupportedCommandWithReleaseError(
112
command=command,
113
release_label=release_label)
114
115
116
def override_args_required_option(argument_table, args, session, **kwargs):
117
# This function overrides the 'required' property of an argument
118
# if a value corresponding to that argument is present in the config
119
# file
120
# We don't want to override when user is viewing the help so that we
121
# can show the required options correctly in the help
122
need_to_override = False if len(args) == 1 and args[0] == 'help' \
123
else True
124
125
if need_to_override:
126
parsed_configs = configutils.get_configs(session)
127
for arg_name in argument_table.keys():
128
if arg_name.replace('-', '_') in parsed_configs:
129
argument_table[arg_name].required = False
130
131