Пример #1
0
def monitor_instances_for_user(provider, username, instances):
    from core.models.instance import convert_esh_instance
    try:
        user = AtmosphereUser.objects.get(username=username)
    except AtmosphereUser.DoesNotExist:
        if instances:
            logger.warn("WARNING: User %s has %s instances, but does not"
            "exist on this database" % username, len(instances))
        return
    for identity in user.identity_set.filter(provider=provider):
        try:
            identity_id = identity.id
            #GATHER STATISTICS FIRST
            #This will be: Calculate time that user has used all instances within a
            #given delta, including the instances listed currently.
            time_period=relativedelta(day=1, months=1)
            allocation = get_allocation(username, identity_id)
            delta_time = get_delta(allocation, time_period)
            time_used = current_instance_time(
                    user, instances,
                    identity_id, delta_time)
            enforce_allocation(identity, user, time_used)
        except:
            logger.exception("Unable to monitor Identity:%s"
                         % (identity,))
Пример #2
0
def enforce_allocation(identity, user, time_used):
    from core.models.instance import convert_esh_instance
    #TODO: When user->group is no longer true,
    #TODO: Is 'group' allowed to use this identity?
    #group = Group.objects.get(name=user.username)
    #im = identity.identitymembership_set.get(member=group)
    allocation = get_allocation(user.username, identity.id)
    if not allocation:
        return False
    max_time_allowed = timedelta(minutes=allocation.threshold)
    time_diff = max_time_allowed - time_used
    over_allocated = time_diff.total_seconds() <= 0
    if not over_allocated:
        return False
    if not settings.ENFORCING:
        logger.info('Settings dictate allocations are NOT enforced')
        return False
    logger.info("%s is OVER their allowed quota by %s" %
                (user.username, time_diff))
    driver = get_esh_driver(identity)
    esh_instances = driver.list_instances()
    for instance in esh_instances:
        try:
            if driver._is_active_instance(instance):
                #Suspend active instances, update the task in the DB
                driver.suspend_instance(instance)
                #Give it a few seconds to suspend
                time.sleep(3)
                updated_esh = driver.get_instance(instance.id)
                updated_core = convert_esh_instance(driver, updated_esh,
                                                    identity.provider.id,
                                                    identity.id, user)
        except Exception, e:
            if 'in vm_state suspended' not in e.message:
                raise
Пример #3
0
def check_usernames(provider_id, usernames):
    for username in usernames:
        ident = Identity.objects.get(provider__id=provider_id,
                                     created_by__username=username)
        print "Provider: %s - Username: %s" % (ident.provider,
                                               ident.created_by.username)
        total_time, instances_map = core_instance_time(
            ident.created_by,
            ident.id,
            get_delta(get_allocation(ident.created_by.username, ident.id),
                      settings.FIXED_WINDOW, timezone.now()), [],
            now_time=timezone.now())
        instance_list = instances_map.keys()
        for instance in instance_list:
            print "Instance:%s Time Used:%s" % (instance.provider_alias,
                                                instance.active_time)
            for history in instances_map[instance]:
                if history.cpu_time > timezone.timedelta(0):
                    print "Status:%s %s*(%s - %s) = %s" % (
                        history.status.name, history.size.cpu,
                        history.start_date, history.end_date, history.cpu_time)
        print "Total Time Given:%s Total time used: %s" % (timezone.timedelta(
            minutes=get_allocation(ident.created_by.username,
                                   ident.id).threshold), total_time)
def check_usernames(provider_id, usernames):
    for username in usernames:
        ident = Identity.objects.get(
            provider__id=provider_id,
            created_by__username=username)
        print "Provider: %s - Username: %s" % (ident.provider, ident.created_by.username)
        total_time, instances_map = core_instance_time(
            ident.created_by, ident.id,
            get_delta(
                get_allocation(ident.created_by.username, ident.id),
                settings.FIXED_WINDOW, timezone.now()),
            [],
            now_time=timezone.now())
        instance_list = instances_map.keys()
        for instance in instance_list:
            print "Instance:%s Time Used:%s" % (instance.provider_alias, instance.active_time)
            for history in instances_map[instance]:
                if history.cpu_time > timezone.timedelta(0):
                    print "Status:%s %s*(%s - %s) = %s" % (history.status.name, history.size.cpu, history.start_date, history.end_date, history.cpu_time)
        print "Total Time Given:%s Total time used: %s" % (timezone.timedelta(minutes=get_allocation(ident.created_by.username, ident.id).threshold), total_time)
Пример #5
0
def monitor_instances_for_user(provider, username, instances,
                               print_logs=False):
    """
    """
    from core.models import IdentityMembership
    try:
        #Note: This username may or may not have an associated
        #Allocation/IdentityMembership
        user = AtmosphereUser.objects.get(username=username)
    except AtmosphereUser.DoesNotExist:
        #if instances:
        #    logger.warn("WARNING: User %s has %s instances, but does not"
        #                "exist on this database" % (username, len(instances)))
        return
    for identity in user.identity_set.filter(provider=provider):
        try:
            identity_id = identity.id
            #GATHER STATISTICS FIRST
            #This will be: Calculate time for all instances within a
            #given delta, including the instances listed currently.
            time_period = settings.FIXED_WINDOW
            allocation = get_allocation(username, identity_id)
            delta_time = get_delta(allocation, time_period)
            time_used, instance_status_map = current_instance_time(
                user, instances,
                identity_id, delta_time)
            if print_logs:
                print_table_row(instance_status_map, user,
                                allocation, time_used)
                return
            enforce_allocation(identity, user, time_used)
        except IdentityMembership.DoesNotExist:
            pass
            #if instances:
            #    logger.warn(
            #        "WARNING: User %s has %s instances, but does not"
            #        "exist on this database" % (username, len(instances)))
        except:
            logger.exception("Unable to monitor Identity:%s"
                             % (identity,))
Пример #6
0
def monitor_instances_for_user(provider,
                               username,
                               instances,
                               print_logs=False):
    """
    """
    from core.models import IdentityMembership
    try:
        #Note: This username may or may not have an associated
        #Allocation/IdentityMembership
        user = AtmosphereUser.objects.get(username=username)
    except AtmosphereUser.DoesNotExist:
        #if instances:
        #    logger.warn("WARNING: User %s has %s instances, but does not"
        #                "exist on this database" % (username, len(instances)))
        return
    for identity in user.identity_set.filter(provider=provider):
        try:
            identity_id = identity.id
            #GATHER STATISTICS FIRST
            #This will be: Calculate time for all instances within a
            #given delta, including the instances listed currently.
            time_period = settings.FIXED_WINDOW
            allocation = get_allocation(username, identity_id)
            delta_time = get_delta(allocation, time_period)
            time_used, instance_status_map = current_instance_time(
                user, instances, identity_id, delta_time)
            if print_logs:
                print_table_row(instance_status_map, user, allocation,
                                time_used)
                return
            enforce_allocation(identity, user, time_used)
        except IdentityMembership.DoesNotExist:
            pass
            #if instances:
            #    logger.warn(
            #        "WARNING: User %s has %s instances, but does not"
            #        "exist on this database" % (username, len(instances)))
        except:
            logger.exception("Unable to monitor Identity:%s" % (identity, ))
Пример #7
0
def enforce_allocation(identity, user, time_used):
    from core.models.instance import convert_esh_instance
    #TODO: When user->group is no longer true,
    # we will need to modify this..
    group = Group.objects.get(name=user.username)
    im = identity.identitymembership_set.get(member=group)
    allocation = get_allocation(user.username, identity.id)
    if not allocation:
        return False
    max_time_allowed = timedelta(minutes=allocation.threshold)
    time_diff = max_time_allowed - time_used
    over_allocated = time_diff.total_seconds() <= 0
    if not over_allocated:
        return False
    #if settings.DEBUG:
    #    logger.info('Do not enforce allocations in DEBUG mode')
    #    return False
    logger.info("%s is OVER their allowed quota by %s" %
                 (user.username, time_diff))
    driver = get_esh_driver(identity)
    esh_instances = driver.list_instances()
    for instance in esh_instances:
        try:
            if driver._is_active_instance(instance):
                #Suspend active instances, update the task in the DB
                driver.suspend_instance(instance)
                #Give it a few seconds to suspend
                time.sleep(3)
                updated_esh = driver.get_instance(instance.id)
                updated_core = convert_esh_instance(
                        driver, updated_esh,
                        identity.provider.id,
                        identity.id,
                        user)
        except Exception, e:
            if 'in vm_state suspended' not in e.message:
                raise