def enable_cloudwatch_logs(app_name, env_name, cloudwatch_log_source): """ Enables CloudWatch log-streaming for the given environment if the required streaming of the specified `cloudwatch_log_source`s is not already enabled :param app_name: application name :param env_name: environment name :param cloudwatch_log_source: the source of logs. Defaults to 'instance' if value is 'None'. Use - 'instance' to enable instance log0streaming - 'health' to enable health transition log-streaming - 'all': enable streaming of all CloudWatch log sources :return None """ cloudwatch_log_source = cloudwatch_log_source or logs_operations_constants.LOG_SOURCES.INSTANCE_LOG_SOURCE configuration_settings = elasticbeanstalk.describe_configuration_settings( app_name, env_name) option_settings = [] timeout = 5 if cloudwatch_log_source in [ logs_operations_constants.LOG_SOURCES.ALL_LOG_SOURCES, logs_operations_constants.LOG_SOURCES.INSTANCE_LOG_SOURCE ]: if not instance_log_streaming_enabled( app_name, env_name, config_settings=configuration_settings): timeout = 15 option_settings.append(_instance_log_streaming_option_setting()) io.echo(strings['cloudwatch_instance_log_streaming.enable']) else: io.echo( strings['cloudwatch_instance_log_streaming.already_enabled']) if cloudwatch_log_source in [ logs_operations_constants.LOG_SOURCES.ALL_LOG_SOURCES, logs_operations_constants.LOG_SOURCES. ENVIRONMENT_HEALTH_LOG_SOURCE, ]: _raise_if_environment_is_not_using_enhanced_health( configuration_settings) if not environment_health_streaming_enabled( app_name, env_name, config_settings=configuration_settings): option_settings.append( _environment_health_log_streaming_option_setting()) io.echo( strings['cloudwatch_environment_health_log_streaming.enable']) else: io.echo(strings[ 'cloudwatch_environment_health_log_streaming.already_enabled']) if not option_settings: return _echo_link_to_cloudwatch_console(env_name) commonops.update_environment(env_name, changes=option_settings, nohang=False, timeout=timeout)
def _is_single_instance(app_name, env_name): env = elasticbeanstalk.describe_configuration_settings(app_name, env_name) option_settings = env['OptionSettings'] env_type = elasticbeanstalk.get_option_setting( option_settings, namespaces.ENVIRONMENT, option_names.ENVIRONMENT_TYPE) if env_type == 'SingleInstance': return True
def get_and_print_environment_vars(app_name, env_name): settings = elasticbeanstalk.describe_configuration_settings( app_name, env_name)['OptionSettings'] namespace = 'aws:elasticbeanstalk:application:environment' environment_variables = { setting['OptionName']: setting['Value'] for setting in settings if setting["Namespace"] == namespace } print_environment_vars(environment_variables)
def get_and_print_environment_vars(app_name, env_name): settings = elasticbeanstalk.describe_configuration_settings( app_name, env_name )['OptionSettings'] namespace = 'aws:elasticbeanstalk:application:environment' environment_variables = { setting['OptionName']: setting['Value'] for setting in settings if setting["Namespace"] == namespace } print_environment_vars(environment_variables)
def log_streaming_enabled(app_name, env_name): """ Checks if log streaming is enabled for the given environment :param app_name: application name :param env_name: environment name :return: boolean if the given environment has log stremaing enabled """ config_settings = elasticbeanstalk.describe_configuration_settings(app_name, env_name) stream_enabled = elasticbeanstalk.get_specific_configuration(config_settings, namespaces.CLOUDWATCH_LOGS, option_names.STREAM_LOGS) if stream_enabled is not None and stream_enabled == 'true': return True return False
def instance_log_streaming_enabled(app_name, env_name, config_settings=None): """ Checks if log streaming is enabled for the given environment :param app_name: application name :param env_name: environment name :param config_settings: the raw response of a call to describe_configuration_settings :return: boolean if the given environment has log streaming enabled """ config_settings = config_settings or elasticbeanstalk.describe_configuration_settings( app_name, env_name) stream_enabled = elasticbeanstalk.get_specific_configuration( config_settings, namespaces.CLOUDWATCH_LOGS, option_names.STREAM_LOGS) return stream_enabled == 'true'
def enable_cloudwatch_logs(app_name, env_name, cloudwatch_log_source): """ Enables CloudWatch log-streaming for the given environment if the required streaming of the specified `cloudwatch_log_source`s is not already enabled :param app_name: application name :param env_name: environment name :param cloudwatch_log_source: the source of logs. Defaults to 'instance' if value is 'None'. Use - 'instance' to enable instance log0streaming - 'health' to enable health transition log-streaming - 'all': enable streaming of all CloudWatch log sources :return None """ cloudwatch_log_source = cloudwatch_log_source or logs_operations_constants.LOG_SOURCES.INSTANCE_LOG_SOURCE configuration_settings = elasticbeanstalk.describe_configuration_settings(app_name, env_name) option_settings = [] timeout = 5 if cloudwatch_log_source in [ logs_operations_constants.LOG_SOURCES.ALL_LOG_SOURCES, logs_operations_constants.LOG_SOURCES.INSTANCE_LOG_SOURCE ]: if not instance_log_streaming_enabled(app_name, env_name, config_settings=configuration_settings): timeout = 15 option_settings.append(_instance_log_streaming_option_setting()) io.echo(strings['cloudwatch_instance_log_streaming.enable']) else: io.echo(strings['cloudwatch_instance_log_streaming.already_enabled']) if cloudwatch_log_source in [ logs_operations_constants.LOG_SOURCES.ALL_LOG_SOURCES, logs_operations_constants.LOG_SOURCES.ENVIRONMENT_HEALTH_LOG_SOURCE, ]: _raise_if_environment_is_not_using_enhanced_health(configuration_settings) if not environment_health_streaming_enabled(app_name, env_name, config_settings=configuration_settings): option_settings.append(_environment_health_log_streaming_option_setting()) io.echo(strings['cloudwatch_environment_health_log_streaming.enable']) else: io.echo(strings['cloudwatch_environment_health_log_streaming.already_enabled']) if not option_settings: return _echo_link_to_cloudwatch_console(env_name) commonops.update_environment(env_name, changes=option_settings, nohang=False, timeout=timeout)
def environment_health_streaming_enabled(app_name, env_name, config_settings=None): """ Checks if health transition streaming is enabled for the given environment :param app_name: application name :param env_name: environment name :param config_settings: the raw response of a call to describe_configuration_settings :return: boolean if the given environment has health transition streaming enabled """ config_settings = config_settings or elasticbeanstalk.describe_configuration_settings(app_name, env_name) stream_enabled = elasticbeanstalk.get_specific_configuration( config_settings, namespaces.CLOUDWATCH_ENVIRONMENT_HEALTH_LOGS, option_names.CLOUDWATCH_ENVIRONMENT_HEALTH_LOGS_ENABLED ) return stream_enabled == 'true'
def instance_log_streaming_enabled(app_name, env_name, config_settings=None): """ Checks if log streaming is enabled for the given environment :param app_name: application name :param env_name: environment name :param config_settings: the raw response of a call to describe_configuration_settings :return: boolean if the given environment has log streaming enabled """ config_settings = config_settings or elasticbeanstalk.describe_configuration_settings(app_name, env_name) stream_enabled = elasticbeanstalk.get_specific_configuration( config_settings, namespaces.CLOUDWATCH_LOGS, option_names.STREAM_LOGS ) return stream_enabled == 'true'
def environment_health_streaming_enabled(app_name, env_name, config_settings=None): """ Checks if health transition streaming is enabled for the given environment :param app_name: application name :param env_name: environment name :param config_settings: the raw response of a call to describe_configuration_settings :return: boolean if the given environment has health transition streaming enabled """ config_settings = config_settings or elasticbeanstalk.describe_configuration_settings(app_name, env_name) stream_enabled = elasticbeanstalk.get_specific_configuration( config_settings, namespaces.CLOUDWATCH_ENVIRONMENT_HEALTH_LOGS, option_names.CLOUDWATCH_ENVIRONMENT_HEALTH_LOGS_ENABLED ) return stream_enabled == 'true'
def open_app(app_name, env_name): env = elasticbeanstalk.get_environment(app_name=app_name, env_name=env_name) settings = elasticbeanstalk.describe_configuration_settings( app_name, env_name) option_settings = settings.get('OptionSettings', []) http_port = elasticbeanstalk.get_option_setting( option_settings, namespaces.LOAD_BALANCER, option_names.LOAD_BALANCER_HTTP_PORT) if http_port == 'OFF': ssl = True else: ssl = False commonops.open_webpage_in_browser(env.cname, ssl=ssl)
def is_shared_load_balancer(app_name, env_name): namespace = namespaces.ENVIRONMENT load_balancer_is_shared = option_names.LOAD_BALANCER_IS_SHARED load_balancer_type = option_names.LOAD_BALANCER_TYPE app_name = app_name env_name = env_name env_config = elasticbeanstalk.describe_configuration_settings( app_name, env_name) lb_type = elasticbeanstalk.get_specific_configuration( env_config, namespace, load_balancer_type) if lb_type == elb_names.APPLICATION_VERSION: value = elasticbeanstalk.get_specific_configuration( env_config, namespace, load_balancer_is_shared) return value.lower() == 'true' else: return False
def get_quick_link(app_name, env_name): env = elasticbeanstalk.get_environment(app_name=app_name, env_name=env_name) settings = elasticbeanstalk.describe_configuration_settings( app_name, env_name) option_settings = settings['OptionSettings'] environment_type = elasticbeanstalk.get_option_setting( option_settings, 'aws:elasticbeanstalk:environment', 'EnvironmentType') instance_type = elasticbeanstalk.get_option_setting( option_settings, 'aws:autoscaling:launchconfiguration', 'InstanceType') link = 'https://console.aws.amazon.com/elasticbeanstalk/home?' # add region region = aws.get_region_name() link += 'region=' + urllib.parse.quote(region) # add quicklaunch flag link += '#/newApplication' # add application name link += '?applicationName=' + urllib.parse.quote(app_name) # add solution stack link += '&solutionStackName=' + urllib.parse.quote( env.platform.platform_shorthand) # add link += '&tierName=' + env.tier.name if environment_type: link += '&environmentType=' + environment_type if env.version_label: app_version = elasticbeanstalk.get_application_versions( app_name, version_labels=[env.version_label])['ApplicationVersions'][0] source_bundle = app_version['SourceBundle'] source_url = 'https://s3.amazonaws.com/' + source_bundle['S3Bucket'] + \ '/' + source_bundle['S3Key'] link += '&sourceBundleUrl=' + source_url if instance_type: link += '&instanceType=' + instance_type link = _add_database_options(link, option_settings) link = _add_vpc_options(link, option_settings) io.echo(link)
def update_environment_configuration(app_name, env_name, nohang, timeout=None): # get environment setting api_model = elasticbeanstalk.describe_configuration_settings( app_name, env_name) # Convert the raw api return to yaml format env_settings = EnvironmentSettings(api_model) usr_model = env_settings.convert_api_to_usr_model() # Save the yaml in a temp file file_location = fileoperations.save_env_file(usr_model) fileoperations.open_file_for_editing(file_location) platform_arn = None # Update and delete file try: usr_model = fileoperations.get_environment_from_file(env_name) changes, remove = env_settings.collect_changes(usr_model) if api_model['PlatformArn'] != usr_model['PlatformArn']: platform_arn = usr_model['PlatformArn'] fileoperations.delete_env_file(env_name) except InvalidSyntaxError: io.log_error(prompts['update.invalidsyntax']) return if not changes and not remove and not platform_arn: # no changes made, exit io.log_warning('No changes made. Exiting.') return if fileoperations.env_yaml_exists(): io.echo(strings['config.envyamlexists']) commonops.update_environment(env_name, changes, nohang, remove=remove, timeout=timeout, solution_stack_name=None, platform_arn=platform_arn)
def scale(app_name, env_name, number, confirm, timeout=None): options = [] # get environment env = elasticbeanstalk.describe_configuration_settings( app_name, env_name)['OptionSettings'] # if single instance, offer to switch to load-balanced namespace = 'aws:elasticbeanstalk:environment' setting = next((n for n in env if n["Namespace"] == namespace), None) value = setting['Value'] if value == 'SingleInstance': if not confirm: ## prompt to switch to LoadBalanced environment type io.echo(prompts['scale.switchtoloadbalance']) io.log_warning(prompts['scale.switchtoloadbalancewarn']) switch = io.get_boolean_response() if not switch: return options.append({ 'Namespace': namespace, 'OptionName': 'EnvironmentType', 'Value': 'LoadBalanced' }) # change autoscaling min AND max to number namespace = 'aws:autoscaling:asg' max = 'MaxSize' min = 'MinSize' for name in [max, min]: options.append({ 'Namespace': namespace, 'OptionName': name, 'Value': str(number) }) request_id = elasticbeanstalk.update_environment(env_name, options) commonops.wait_for_success_events(request_id, timeout_in_minutes=timeout or 5, can_abort=True)
def update_environment_configuration(app_name, env_name, nohang, timeout=None): # get environment setting api_model = elasticbeanstalk.describe_configuration_settings( app_name, env_name ) # Convert the raw api return to yaml format env_settings = EnvironmentSettings(api_model) usr_model = env_settings.convert_api_to_usr_model() # Save the yaml in a temp file file_location = fileoperations.save_env_file(usr_model) fileoperations.open_file_for_editing(file_location) platform_arn = None # Update and delete file try: usr_model = fileoperations.get_environment_from_file(env_name) changes, remove = env_settings.collect_changes(usr_model) if api_model['PlatformArn'] != usr_model['PlatformArn']: platform_arn = usr_model['PlatformArn'] fileoperations.delete_env_file(env_name) except InvalidSyntaxError: io.log_error(prompts['update.invalidsyntax']) return if not changes and not remove and not platform_arn: # no changes made, exit io.log_warning('No changes made. Exiting.') return if fileoperations.env_yaml_exists(): io.echo(strings['config.envyamlexists']) commonops.update_environment(env_name, changes, nohang, remove=remove, timeout=timeout, solution_stack_name=None, platform_arn=platform_arn)