Esempio n. 1
0
def collapse_environment_health_data(environment_health):
    result = dict()
    request_count = environment_health.get('ApplicationMetrics', {}) \
        .get('RequestCount', 0)

    latency_dict = environment_health.get('ApplicationMetrics', {})\
        .pop('Latency', {})
    result.update(_format_latency_dict(latency_dict, request_count))

    result['requests'] = request_count/10.0
    statuses = environment_health.get('ApplicationMetrics', {})\
        .pop('StatusCodes', {})
    for k, v in six.iteritems(statuses):
        convert_data_to_percentage(statuses, k, request_count)
    result.update(statuses)
    result.update(environment_health.pop('ApplicationMetrics', {}))
    total = 0
    for k, v in six.iteritems(environment_health.get('InstancesHealth', {})):
        total += v
    result['Total'] = total
    result.update(environment_health.pop('InstancesHealth', {}))
    result.update(environment_health)

    causes = result.get('Causes', [])
    cause = causes[0] if causes else ''
    result['Cause'] = cause

    return result
def collapse_environment_health_data(environment_health):
    result = dict()
    request_count = environment_health.get('ApplicationMetrics', {}) \
        .get('RequestCount', 0)

    latency_dict = environment_health.get('ApplicationMetrics', {})\
        .pop('Latency', {})
    result.update(_format_latency_dict(latency_dict, request_count))

    result['requests'] = request_count / 10.0
    statuses = environment_health.get('ApplicationMetrics', {})\
        .pop('StatusCodes', {})
    for k, v in six.iteritems(statuses):
        convert_data_to_percentage(statuses, k, request_count)
    result.update(statuses)
    result.update(environment_health.pop('ApplicationMetrics', {}))
    total = 0
    for k, v in six.iteritems(environment_health.get('InstancesHealth', {})):
        total += v
    result['Total'] = total
    result.update(environment_health.pop('InstancesHealth', {}))
    result.update(environment_health)

    causes = result.get('Causes', [])
    cause = causes[0] if causes else ''
    result['Cause'] = cause

    return result
Esempio n. 3
0
    def from_str(cls, envvars_str, variable_delim=ENV_DELIM,
                 kv_delim=ENV_KV_DELIM):
        get_and_validate_envars(envvars_str)
        if not envvars_str:
            return cls()

        all_envvars = dict(e.split(kv_delim) for e in
                           envvars_str.split(variable_delim))

        # All envvars with empty strs are ones to remove
        envvars_map = {k: v for k, v in six.iteritems(all_envvars) if v}
        envvars_to_remove = {k for k, v in six.iteritems(all_envvars) if not v}

        return cls(envvars_map, envvars_to_remove)
Esempio n. 4
0
    def draw_banner_info_lines(self, lines, data):
        if lines > 2:
            tier_type = self.env_data['Tier']['Name']
            tier = '{}'.format(tier_type)
            solutionstack = SolutionStack(self.env_data['SolutionStackName'])
            platform = ' {}'.format(solutionstack.version)
            term.echo_line('{tier}{pad}{platform} '.format(
                tier=tier,
                platform=platform,
                pad=' '*(term.width() - len(tier) - len(platform))
            ))

            lines -= 1
        if lines > 3:
            # Get instance health count
            instance_counts = OrderedDict([
                ('total', data.get('Total', 0)),
                ('ok', data.get('Ok', 0)),
                ('warning', data.get('Warning', 0)),
                ('degraded', data.get('Degraded', 0)),
                ('severe', data.get('Severe', 0)),
                ('info', data.get('Info', 0)),
                ('pending', data.get('Pending', 0)),
                ('unknown', data.get('Unknown', 0) + data.get('NoData', 0)),
            ])
            column_size = max(len(k) for k in instance_counts) + 1
            term.echo_line(
                ''.join((s.center(column_size)
                          for s in instance_counts)))
            term.echo_line(
                ''.join((io.bold((str(v).center(column_size)))
                                 for k, v in six.iteritems(instance_counts))))
            lines -= 2

        return lines
Esempio n. 5
0
    def draw_banner_info_lines(self, lines, data):
        if lines > 2:
            tier_type = self.env_data['Tier']['Name']
            tier = '{}'.format(tier_type)
            solutionstack = SolutionStack(self.env_data['SolutionStackName'])
            platform = ' {}'.format(solutionstack.version)
            term.echo_line('{tier}{pad}{platform} '.format(
                tier=tier,
                platform=platform,
                pad=' ' * (term.width() - len(tier) - len(platform))))

            lines -= 1
        if lines > 3:
            # Get instance health count
            instance_counts = OrderedDict([
                ('total', data.get('Total', 0)),
                ('ok', data.get('Ok', 0)),
                ('warning', data.get('Warning', 0)),
                ('degraded', data.get('Degraded', 0)),
                ('severe', data.get('Severe', 0)),
                ('info', data.get('Info', 0)),
                ('pending', data.get('Pending', 0)),
                ('unknown', data.get('Unknown', 0) + data.get('NoData', 0)),
            ])
            column_size = max(len(k) for k in instance_counts) + 1
            term.echo_line(''.join(
                (s.center(column_size) for s in instance_counts)))
            term.echo_line(''.join(
                (io.bold((str(v).center(column_size)))
                 for k, v in six.iteritems(instance_counts))))
            lines -= 2

        return lines
Esempio n. 6
0
def create_envvars_list(var_list, as_option_settings=True):
    namespace = 'aws:elasticbeanstalk:application:environment'

    options = dict()
    options_to_remove = set()
    for pair in var_list:
        ## validate
        if not re.match('^[\w\\_.:/+@-][^=]*=.*$', pair):
            raise InvalidOptionsError(strings['setenv.invalidformat'])

        try:
            option_name, value = pair.split('=', 1)
        except ValueError:
            raise InvalidOptionsError(strings['setenv.invalidformat'])

        if value:
            options[option_name] = value
        else:
            options_to_remove.add(option_name)

    if as_option_settings:
        option_dict = options
        options = list()
        remove_list = options_to_remove
        options_to_remove = list()
        for k, v in six.iteritems(option_dict):
            options.append(dict(Namespace=namespace, OptionName=k, Value=v))

        for k in remove_list:
            options_to_remove.append(dict(Namespace=namespace, OptionName=k))

    return options, options_to_remove
Esempio n. 7
0
    def filtered(self):
        """
        Return new Envvarcollector with all environment variables in self.map that
        are not in to_remove
        :return EnvvarCollector
        """

        filtered_envvars = {k: v for k, v in six.iteritems(self.map) if k not in
                            self.to_remove}
        return EnvvarCollector(filtered_envvars)
def _format_latency_dict(latency_dict, request_count):
    new_dict = copy(latency_dict)
    for k, v in six.iteritems(latency_dict):
        new_dict[k + '_sort'] = v
        representation = format_float(v, 3)

        if (k == 'P99' and request_count < 100) or \
                (k == 'P90' and request_count < 10):
            representation += '*'
        elif k in ['P99', 'P90']:
            representation += ' '
        new_dict[k] = representation

    return new_dict
Esempio n. 9
0
def _format_latency_dict(latency_dict, request_count):
    new_dict = copy(latency_dict)
    for k, v in six.iteritems(latency_dict):
        new_dict[k + '_sort'] = v
        representation = format_float(v, 3)

        if (k == 'P99' and request_count < 100) or \
                (k == 'P90' and request_count < 10):
            representation += '*'
        elif k in ['P99', 'P90']:
            representation += ' '
        new_dict[k] = representation

    return new_dict
Esempio n. 10
0
def cleanup_application_versions(app_name):
    io.echo('Removing application versions from s3.')
    versions = elasticbeanstalk.get_application_versions(app_name)['ApplicationVersions']
    buckets = defaultdict(list)
    for version in versions:
        bundle = version.get('SourceBundle', {})
        bucket = bundle.get('S3Bucket')
        key = bundle.get('S3Key')
        if bucket and key:
            buckets[bucket].append(key)

    for bucket, keys in six.iteritems(buckets):
        try:
            s3.delete_objects(bucket, keys)
        except NotAuthorizedError:
            io.log_warning(
                'Error deleting application versions from bucket "{0}"'.format(bucket)
            )
Esempio n. 11
0
def create_envvars_list(var_list, as_option_settings=True):
    namespace = 'aws:elasticbeanstalk:application:environment'

    options = dict()
    options_to_remove = set()
    for pair in var_list:
        ## validate
        if not re.match('^[\w\\_.:/+@-][^=]*=([\w\\_.:/+@-].*)?$', pair):
            raise InvalidOptionsError(strings['setenv.invalidformat'])

        try:
            option_name, value = pair.split('=', 1)
        except ValueError:
            raise InvalidOptionsError(strings['setenv.invalidformat'])

        if value:
            options[option_name] = value
        else:
            options_to_remove.add(option_name)

    if as_option_settings:
        option_dict = options
        options = list()
        remove_list = options_to_remove
        options_to_remove = list()
        for k, v in six.iteritems(option_dict):
            options.append(
                dict(Namespace=namespace,
                     OptionName=k,
                     Value=v))

        for k in remove_list:
            options_to_remove.append(
                dict(Namespace=namespace,
                     OptionName=k))

    return options, options_to_remove
Esempio n. 12
0
 def get_cid_hostport_pairs(self):
     return utils.flatten(
         [[(cid, p) for p in hostports] for cid, hostports in six.iteritems(self.get_cid_hostports_map())]
     )
Esempio n. 13
0
def _get_opts(_map, opt_name, val_format):
    _map = _map or {}
    kv_pairs = six.iteritems(_map)
    return utils.flatten([[opt_name, val_format.format(k, v)]
                          for k, v in kv_pairs])
Esempio n. 14
0
def collect_changes(api_model, usr_model):
    """
    Grabs all things in the usr_model that are different and
   returns just the changes
    :param api_model: Model from api with Namespace keys
    :param usr_model: User model, key-value style
    :return: api_model
    """

    api_model = remove_unwanted_settings(api_model)
    ignore_default_resource_names(api_model)

    changes = []
    remove = []

    option_settings = api_model['OptionSettings']
    usr_options = usr_model['settings']
    for setting in option_settings:
        # Compare value for given optionName
        namespace = setting['Namespace']
        option = setting['OptionName']
        resource_name = None
        if 'ResourceName' in setting:
            resource_name = setting['ResourceName']
            key = resource_name + '.' + namespace
        else:
            key = namespace

        try:
            usr_value = usr_options[key][option]
            del usr_options[key][option]

            # If they dont match, take the user value
            if 'Value' in setting:
                if setting['Value'] != usr_value:
                    setting['Value'] = usr_value
                    if usr_value:
                        changes.append(setting)
                    else:
                        remove.append(
                            _get_option_setting_dict(namespace, option,
                                                     None, resource_name))
            else:
                if usr_value is not None:
                    setting['Value'] = usr_value
                    changes.append(setting)

        except KeyError:
            # user removed setting. We want to add to remove list
            d = _get_option_setting_dict(namespace, option,
                                         None, resource_name)
            remove.append(d)

    # Now we look for added options:
    for namespace, options in six.iteritems(usr_options):
        if options:
            namespace, resource_name = \
                _get_namespace_and_resource_name(namespace)
            for option, value in six.iteritems(options):
                changes.append(_get_option_setting_dict(namespace, option,
                                                        value, resource_name))

    return changes, remove
Esempio n. 15
0
def _get_opts(_map, opt_name, val_format):
    _map = _map or {}
    kv_pairs = six.iteritems(_map)
    return utils.flatten([[opt_name, val_format.format(k, v)] for k, v
                          in kv_pairs])
Esempio n. 16
0
    def collect_changes(self, usr_model):
        """
        Grabs all things in the usr_model that are different and
       returns just the changes
        :param usr_model: User model, key-value style
        :return: api_model
        """

        self.api_model = self.remove_unwanted_settings()
        self.ignore_default_resource_names()

        changes = []
        remove = []

        option_settings = self.api_model['OptionSettings']
        usr_options = usr_model['settings']
        for setting in option_settings:
            # Compare value for given optionName
            namespace = setting['Namespace']
            option = setting['OptionName']
            resource_name = None
            if 'ResourceName' in setting:
                resource_name = setting['ResourceName']
                key = resource_name + '.' + namespace
            else:
                key = namespace

            try:
                usr_value = usr_options[key][option]
                del usr_options[key][option]

                # If they dont match, take the user value
                if 'Value' in setting:
                    if setting['Value'] != usr_value:
                        setting['Value'] = usr_value
                        if usr_value:
                            changes.append(setting)
                        else:
                            remove.append(
                                _get_option_setting_dict(
                                    namespace, option, None, resource_name))
                else:
                    if usr_value is not None:
                        setting['Value'] = usr_value
                        changes.append(setting)

            except KeyError:
                # user removed setting. We want to add to remove list
                d = _get_option_setting_dict(namespace, option, None,
                                             resource_name)
                remove.append(d)

        # Now we look for added options:
        for namespace, options in six.iteritems(usr_options):
            if options:
                namespace, resource_name = \
                    _get_namespace_and_resource_name(namespace)
                for option, value in six.iteritems(options):
                    if value is not None:
                        changes.append(
                            _get_option_setting_dict(namespace, option, value,
                                                     resource_name))

        return changes, remove
Esempio n. 17
0
 def get_cid_hostport_pairs(self):
     return utils.flatten([[
         (cid, p) for p in hostports
     ] for cid, hostports in six.iteritems(self.get_cid_hostports_map())])