Skip to content

Commit

Permalink
Merge pull request #563 from remind101/profile-flag
Browse files Browse the repository at this point in the history
Add a --profile flag to specify default profile
  • Loading branch information
ejholmes committed Mar 16, 2018
2 parents 50a7ad3 + e40a972 commit ae2beee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions stacker/commands/stacker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ...context import Context
from ...providers.aws import default
from ... import __version__
from ... import session_cache

logger = logging.getLogger(__name__)

Expand All @@ -30,6 +31,8 @@ def configure(self, options, **kwargs):
environment=options.environment,
validate=True)

session_cache.default_profile = options.profile

options.provider_builder = default.ProviderBuilder(
region=options.region,
interactive=options.interactive,
Expand Down
7 changes: 6 additions & 1 deletion stacker/commands/stacker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ def add_arguments(self, parser):
"more than once.")
parser.add_argument(
"-r", "--region",
help="The AWS region to launch in.")
help="The default AWS region to use for all AWS API calls.")
parser.add_argument(
"-p", "--profile",
help="The default AWS profile to use for all AWS API calls. If "
"not specified, the default will be according to http://bo"
"to3.readthedocs.io/en/latest/guide/configuration.html.")
parser.add_argument(
"-v", "--verbose", action="count", default=0,
help="Increase output verbosity. May be specified up to twice.")
Expand Down
3 changes: 0 additions & 3 deletions stacker/providers/aws/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,6 @@ def __init__(self, region=None, **kwargs):
self.kwargs = kwargs

def build(self, region=None, profile=None):
# TODO(ejholmes): This class _could_ cache built providers, however,
# the building of boto3 clients is _not_ threadsafe. See
# https://github.com/boto/boto3/issues/801#issuecomment-245455979
if not region:
region = self.region
session = get_session(region=region, profile=profile)
Expand Down
10 changes: 10 additions & 0 deletions stacker/session_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# https://docs.python.org/3/glossary.html#term-global-interpreter-lock
credential_cache = {}

default_profile = None


def get_session(region, profile=None):
"""Creates a boto3 session with a cache
Expand All @@ -23,6 +25,14 @@ def get_session(region, profile=None):
:class:`boto3.session.Session`: A boto3 session with
credential caching
"""
if profile is None:
logger.debug("No AWS profile explicitly provided. "
"Falling back to default.")
profile = default_profile

logger.debug("Building session using profile \"%s\" in region \"%s\""
% (profile, region))

session = boto3.Session(region_name=region, profile_name=profile)
c = session._session.get_component('credential_provider')
provider = c.get_provider('assume-role')
Expand Down

0 comments on commit ae2beee

Please sign in to comment.