Path: blob/develop/awscli/customizations/emr/configutils.py
1567 views
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import logging13import os1415from awscli.customizations.configure.writer import ConfigFileWriter16from awscli.customizations.emr.constants import EC2_ROLE_NAME17from awscli.customizations.emr.constants import EMR_ROLE_NAME1819LOG = logging.getLogger(__name__)202122def get_configs(session):23return session.get_scoped_config().get('emr', {})242526def get_current_profile_name(session):27profile_name = session.get_config_variable('profile')28return 'default' if profile_name is None else profile_name293031def get_current_profile_var_name(session):32return _get_profile_str(session, '.')333435def _get_profile_str(session, separator):36profile_name = session.get_config_variable('profile')37return 'default' if profile_name is None \38else 'profile%c%s' % (separator, profile_name)394041def is_any_role_configured(session):42parsed_configs = get_configs(session)43return True if ('instance_profile' in parsed_configs or44'service_role' in parsed_configs) \45else False464748def update_roles(session):49if is_any_role_configured(session):50LOG.debug("At least one of the roles is already associated with "51"your current profile ")52else:53config_writer = ConfigWriter(session)54config_writer.update_config('service_role', EMR_ROLE_NAME)55config_writer.update_config('instance_profile', EC2_ROLE_NAME)56LOG.debug("Associated default roles with your current profile")575859class ConfigWriter(object):6061def __init__(self, session):62self.session = session63self.section = _get_profile_str(session, ' ')64self.config_file_writer = ConfigFileWriter()6566def update_config(self, key, value):67config_filename = \68os.path.expanduser(self.session.get_config_variable('config_file'))69updated_config = {'__section__': self.section,70'emr': {key: value}}71self.config_file_writer.update_config(updated_config, config_filename)727374