Beispiel #1
0
    def usage_reset(self, context, resources):
        """
        Reset the usage records for a particular user on a list of
        resources.  This will force that user's usage records to be
        refreshed the next time a reservation is made.

        Note: this does not affect the currently outstanding
        reservations the user has; those reservations must be
        committed or rolled back (or expired).

        :param context: The request context, for access checks.
        :param resources: A list of the resource names for which the
                          usage must be reset.
        """

        # We need an elevated context for the calls to
        # quota_usage_update()
        elevated = context.elevated()

        for resource in resources:
            try:
                # Reset the usage to -1, which will force it to be
                # refreshed
                db.quota_usage_update(elevated, context.project_id,
                                      resource, in_use=-1)
            except exception.QuotaUsageNotFound:
                # That means it'll be refreshed anyway
                pass
Beispiel #2
0
    def usage_reset(self, context, resources):
        """
        Reset the usage records for a particular user on a list of
        resources.  This will force that user's usage records to be
        refreshed the next time a reservation is made.

        Note: this does not affect the currently outstanding
        reservations the user has; those reservations must be
        committed or rolled back (or expired).

        :param context: The request context, for access checks.
        :param resources: A list of the resource names for which the
                          usage must be reset.
        """

        # We need an elevated context for the calls to
        # quota_usage_update()
        elevated = context.elevated()

        for resource in resources:
            try:
                # Reset the usage to -1, which will force it to be
                # refreshed
                db.quota_usage_update(elevated,
                                      context.project_id,
                                      resource,
                                      in_use=-1)
            except exception.QuotaUsageNotFound:
                # That means it'll be refreshed anyway
                pass
def fix_usage(cntxt, tenant):

    # Get per-user data for this tenant since usage is now per-user
    filter_object = {'project_id': tenant}
    instance_info = db.instance_get_all_by_filters(cntxt, filter_object)

    usage_by_resource = {}
    #resource_types = ['instances', 'cores', 'ram', 'security_groups']
    states_to_ignore = ['error', 'deleted', 'building']

    for instance in instance_info:
        user = instance['user_id']
        # We need to build a list of users who have launched vm's even if the user
        # no longer exists. We can't use keystone here.
        if not usage_by_resource.has_key(user):
            usage_by_resource[user] = {
            }  # Record that this user has once used resources
        if not instance['vm_state'] in states_to_ignore:
            user_resource = usage_by_resource[user]
            user_resource['instances'] = user_resource.get('instances', 0) + 1
            user_resource['cores'] = user_resource.get('cores',
                                                       0) + instance['vcpus']
            user_resource['ram'] = user_resource.get('ram',
                                                     0) + instance['memory_mb']

    secgroup_list = db.security_group_get_by_project(cntxt, tenant)
    for group in secgroup_list:
        user = group.user_id
        if not usage_by_resource.has_key(user):
            usage_by_resource[user] = {
            }  # Record that this user has once used resources
        user_resource = usage_by_resource[user]
        user_resource['security_groups'] = user_resource.get(
            'security_groups', 0) + 1

    # Correct the quota usage in the database
    for user in usage_by_resource:
        for resource in resource_types:
            usage = usage_by_resource[user].get(resource, 0)
            try:
                db.quota_usage_update(cntxt,
                                      tenant,
                                      user,
                                      resource,
                                      in_use=usage)
            except exception.QuotaUsageNotFound as e:
                print e
                print 'db.quota_usage_update(cntxt, %s, %s, %s, in_use=%s)' % \
                      (tenant, user, resource, usage)
def fix_usage(cntxt, tenant):

    # Get per-user data for this tenant since usage is now per-user
    filter_object = {'project_id': tenant}
    instance_info = db.instance_get_all_by_filters(cntxt, filter_object)

    usage_by_resource = {}
    #resource_types = ['instances', 'cores', 'ram', 'security_groups']
    states_to_ignore = ['error', 'deleted', 'building']

    for instance in instance_info:
        user = instance['user_id']
        # We need to build a list of users who have launched vm's even if the user
        # no longer exists. We can't use keystone here.
        if not usage_by_resource.has_key(user):
            usage_by_resource[user] = {} # Record that this user has once used resources
        if not instance['vm_state'] in states_to_ignore:
            user_resource = usage_by_resource[user]
            user_resource['instances'] = user_resource.get('instances', 0) + 1
            user_resource['cores'] = user_resource.get('cores', 0) + instance['vcpus']
            user_resource['ram'] = user_resource.get('ram', 0) + instance['memory_mb']

    secgroup_list = db.security_group_get_by_project(cntxt, tenant)
    for group in secgroup_list:
        user = group.user_id
        if not usage_by_resource.has_key(user):
            usage_by_resource[user] = {} # Record that this user has once used resources
        user_resource = usage_by_resource[user]
        user_resource['security_groups'] = user_resource.get('security_groups', 0) + 1

    # Correct the quota usage in the database
    for user in usage_by_resource:
        for resource in resource_types:
            usage = usage_by_resource[user].get(resource, 0)
            try:
                db.quota_usage_update(cntxt, tenant, user, resource, in_use=usage)
            except exception.QuotaUsageNotFound as e:
                print e
                print 'db.quota_usage_update(cntxt, %s, %s, %s, in_use=%s)' % \
                      (tenant, user, resource, usage)
Beispiel #5
0
def fix_usage(cntxt, tenant, actual_table_name, incorrect_table_name):
    print "\nUpdating quota usage to reflect actual usage..\n"
    # Calculate differences
    existing_usage = db.quota_usage_get_all_by_project(cntxt, tenant)
    actual = get_actual_usage(cntxt, tenant)

    # it's possible for accounts to use nothing but the system default
    # group, which doesn't get tracked, so default to 0
    try:
        security_groups = existing_usage["security_groups"]["in_use"]
        secgroup_difference = security_groups - actual["actual_secgroup_count"]
    except KeyError:
        security_groups = None

    instance_difference = \
        existing_usage["instances"]["in_use"] - actual["actual_instance_count"]
    core_difference = \
        existing_usage["cores"]["in_use"] - actual["actual_core_count"]
    ram_difference = \
        existing_usage["ram"]["in_use"] - actual["actual_ram_count"]
    # Actual_fixed_ips should be the same as actual_instance_count since there is no
    # Reason in our environment for someone to use two fixed IPs
    # existing_usage["fixed_ips"]["in_use"]-actual["actual_fixed_ips"]
    fixedips_difference = \
        existing_usage["fixed_ips"]["in_use"] - actual["actual_instance_count"]
    # Quota_usage_update(context, project_id, resource, **kwargs)
    # Update ram.
    db.quota_usage_update(cxt,
                          tenant,
                          'ram',
                          in_use=existing_usage["ram"]["in_use"] -
                          ram_difference)
    # Update instances
    db.quota_usage_update(cxt,
                          tenant,
                          'instances',
                          in_use=existing_usage["instances"]["in_use"] -
                          instance_difference)
    # Update cores
    db.quota_usage_update(cxt,
                          tenant,
                          'cores',
                          in_use=existing_usage["cores"]["in_use"] - core_difference)
    # Update fixed IPs
    """
    db.quota_usage_update(cxt,
                          tenant,
                          'fixed_ips',
                          in_use=existing_usage["fixed_ips"]["in_use"] - fixedips_difference)
    """

    # Update security groups
    if security_groups is not None:
        db.quota_usage_update(cxt,
                              tenant,
                              'security_groups',
                              in_use=security_groups - secgroup_difference)

    print "############### Actual Usage (including non-active instances) ###############"
    print make_table(actual_table_name, get_actual_usage(cxt, tenant).values())
    print "############### Corrected Database Usage ###############"
    print make_table(incorrect_table_name, get_incorrect_usage(cxt, tenant).values())