Skip to content

Instantly share code, notes, and snippets.

@robinbowes
Created December 8, 2017 15:43
Show Gist options
  • Save robinbowes/c52f0a8de96cc91ab2bcb7f293bd39d1 to your computer and use it in GitHub Desktop.
Save robinbowes/c52f0a8de96cc91ab2bcb7f293bd39d1 to your computer and use it in GitHub Desktop.
import click
import mps.aws
import mps.config
from mps.exceptions import NoCredentialsException
# create module-specific logger
logger = logging.getLogger(__name__)
class AppContext(click.Context): # pylint: disable=too-few-public-methods
""" context object """
def __init__(self):
self.__aws = {}
def aws(self, stage):
"""
Returns an aws client for the specified stage
"""
if stage not in self.__aws:
region = mps.config.stage_region(stage)
logger.debug("Creating new aws object for stage %s in region %s", stage, region)
self.__aws[stage] = mps.aws.Aws.initialize(region)
return self.__aws[stage]
def check_credentials(self):
"""
Make sure AWS credentials are found
"""
try:
mps.aws.Aws.check_credentials()
except NoCredentialsException as e:
raise click.ClickException(str(e))
pass_ctx = click.make_pass_decorator(AppContext, ensure=True)
import logging
import click
import mps.config
import mps.options
from mps.context import pass_ctx
from mps.utils import print_tree
# create module-specific logger
logger = logging.getLogger(__name__)
@click.command('set', short_help='Set version lifecycle settings')
@mps.options.component(allow_multiple=True)
@mps.options.stage(allow_multiple=True)
@click.option(
'--max-count-enabled/--no-max-count-enabled', 'max_count_enabled',
default=False
)
@click.option(
'--max-count', 'max_count',
type=int,
default=200
)
@click.option(
'--max-age-enabled/--no-max-age-enabled', 'max_age_enabled',
default=False
)
@click.option(
'--max-age', 'max_age',
type=int,
default=180
)
@pass_ctx
def set_cmd(ctx, component, stage, max_count_enabled, max_count, max_age_enabled, max_age):
"""
Set the application version lifecycle settings for the specified
components in the specified stage(s)
"""
ctx.check_credentials()
logger.debug('stage is %s', stage)
logger.debug('component is %s', component)
if 'all' in component:
component = mps.config.components()
if 'all' in stage:
stage = mps.config.stages()
click.echo(dir(ctx))
ctx.fail('Exiting')
# make sure one choice has been specified
if not (max_age_enabled or max_count_enabled):
click.UsageError("At least one of '--[no-]max_age_enabled' and '--[no-]max_count_enabled' must be specified.")
output = {}
for this_stage in stage:
output[this_stage] = {}
for this_component in component:
response = ctx.aws(this_stage).set_app_version_lifecycle_settings(
this_component,
max_count_enabled, max_count, max_age_enabled, max_age
)
output[this_stage][this_component] = response
print_tree(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment