Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emr/configutils.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
import logging
14
import os
15
16
from awscli.customizations.configure.writer import ConfigFileWriter
17
from awscli.customizations.emr.constants import EC2_ROLE_NAME
18
from awscli.customizations.emr.constants import EMR_ROLE_NAME
19
20
LOG = logging.getLogger(__name__)
21
22
23
def get_configs(session):
24
return session.get_scoped_config().get('emr', {})
25
26
27
def get_current_profile_name(session):
28
profile_name = session.get_config_variable('profile')
29
return 'default' if profile_name is None else profile_name
30
31
32
def get_current_profile_var_name(session):
33
return _get_profile_str(session, '.')
34
35
36
def _get_profile_str(session, separator):
37
profile_name = session.get_config_variable('profile')
38
return 'default' if profile_name is None \
39
else 'profile%c%s' % (separator, profile_name)
40
41
42
def is_any_role_configured(session):
43
parsed_configs = get_configs(session)
44
return True if ('instance_profile' in parsed_configs or
45
'service_role' in parsed_configs) \
46
else False
47
48
49
def update_roles(session):
50
if is_any_role_configured(session):
51
LOG.debug("At least one of the roles is already associated with "
52
"your current profile ")
53
else:
54
config_writer = ConfigWriter(session)
55
config_writer.update_config('service_role', EMR_ROLE_NAME)
56
config_writer.update_config('instance_profile', EC2_ROLE_NAME)
57
LOG.debug("Associated default roles with your current profile")
58
59
60
class ConfigWriter(object):
61
62
def __init__(self, session):
63
self.session = session
64
self.section = _get_profile_str(session, ' ')
65
self.config_file_writer = ConfigFileWriter()
66
67
def update_config(self, key, value):
68
config_filename = \
69
os.path.expanduser(self.session.get_config_variable('config_file'))
70
updated_config = {'__section__': self.section,
71
'emr': {key: value}}
72
self.config_file_writer.update_config(updated_config, config_filename)
73
74