def add_past_exists(start, end):
    exists = models.InstanceExists.objects.select_related()\
                                  .filter(audit_period_beginning=start,
                                          audit_period_ending=end)
    i = 0
    for exist in exists:
        i += 1
        print i
        if models.InstanceUsage.objects\
                 .filter(instance=exist.instance).count() == 0:
            # We got an exist record that we don't have any launches for
            values = {
                'instance': exist.instance,
                'launched_at': exist.launched_at,
                'instance_type_id': exist.instance_type_id,
                'request_id': 'req-fake'
            }
            print values
            models.InstanceUsage(**values).save()
示例#2
0
def create_instance_usage(**kwargs):
    return models.InstanceUsage(**kwargs)
示例#3
0
def seed(period_length):
    usages = []
    building_usages = []
    in_flight_usages = []
    deletes = []

    start, end = get_previous_period(datetime.datetime.utcnow(), period_length)

    context = RequestContext(1, 1, is_admin=True)

    print "Selecting all active instances"
    active_instances = get_active_instances(period_length)
    print "Selected all active instances"

    print "Populating active usages, preparing for in-flight"
    for instance in active_instances:
        vm_state = instance['vm_state']
        task_state = instance['task_state']

        if vm_state == 'building':
            if instance['deleted'] != 0 and instance['deleted_at'] >= start:
                building_usages.append(_usage_for_instance(instance))
                deletes.append(_delete_for_instance(instance))
            elif instance['deleted'] == 0:
                building_usages.append(_usage_for_instance(instance))
        else:
            if task_state in in_flight_tasks:
                if (instance['deleted'] != 0
                        and instance['deleted_at'] is not None
                        and instance['deleted_at'] >= start):
                    # Just in case...
                    deletes.append(_delete_for_instance(instance))
                    in_flight_usages.append(
                        _usage_for_instance(instance, task=task_state))
                elif instance['deleted'] == 0:
                    in_flight_usages.append(
                        _usage_for_instance(instance, task=task_state))
            else:
                if (instance['deleted'] != 0
                        and instance['deleted_at'] is not None
                        and instance['deleted_at'] >= start):
                    deletes.append(_delete_for_instance(instance))
                    usages.append(_usage_for_instance(instance))
                elif instance['deleted'] == 0:
                    usages.append(_usage_for_instance(instance))

    print "Populated active instances, processing building"
    for usage in building_usages:
        action = get_action_for_instance(context, usage['instance'], 'create')
        if action is not None:
            usage['request_id'] = action['request_id']

    print "Populated building, processing in-flight"
    for usage in in_flight_usages:
        instance = usage['instance']
        action = None
        if usage['task'] in rebuild_tasks:
            action = get_action_for_instance(context, instance, 'rebuild')
        elif usage['task'] in resize_tasks:
            action = get_action_for_instance(context, instance, 'resize')
        elif usage['task'] in resize_revert_tasks:
            action = get_action_for_instance(context, instance, 'resizeRevert')
        elif usage['task'] in rescue_tasks:
            action = get_action_for_instance(context, instance, 'rescue')

        if action is not None:
            usage['request_id'] = action['request_id']
        del usage['task']

    print "Done cataloging usage"

    print "Saving active instances"
    active_InstanceUsages = map(lambda x: models.InstanceUsage(**x), usages)
    models.InstanceUsage.objects.bulk_create(active_InstanceUsages,
                                             batch_size=100)

    print "Saving building instances"
    building_InstanceUsages = map(lambda x: models.InstanceUsage(**x),
                                  building_usages)
    models.InstanceUsage.objects.bulk_create(building_InstanceUsages,
                                             batch_size=100)

    print "Saving in-flight instances"
    in_flight_InstanceUsages = map(lambda x: models.InstanceUsage(**x),
                                   in_flight_usages)
    models.InstanceUsage.objects.bulk_create(in_flight_InstanceUsages,
                                             batch_size=100)

    print "Saving deletes"
    all_InstanceDeletes = map(lambda x: models.InstanceDeletes(**x), deletes)
    models.InstanceDeletes.objects.bulk_create(all_InstanceDeletes,
                                               batch_size=100)

    return (len(usages), len(building_usages), len(in_flight_usages),
            len(deletes))