コード例 #1
0
def monitor_sizes_for(provider_id, print_logs=False):
    """
    Run the set of tasks related to monitoring sizes for a provider.
    Optionally, provide a list of usernames to monitor
    While debugging, print_logs=True can be very helpful.
    start_date and end_date allow you to search a 'non-standard' window of time.
    """
    from service.driver import get_admin_driver

    if print_logs:
        console_handler = _init_stdout_logging()

    provider = Provider.objects.get(id=provider_id)
    admin_driver = get_admin_driver(provider)
    # Non-End dated sizes on this provider
    db_sizes = Size.objects.filter(only_current(), provider=provider)
    all_sizes = admin_driver.list_sizes()
    seen_sizes = []
    for cloud_size in all_sizes:
        core_size = convert_esh_size(cloud_size, provider.uuid)
        seen_sizes.append(core_size)

    now_time = timezone.now()
    needs_end_date = [size for size in db_sizes if size not in seen_sizes]
    for size in needs_end_date:
        celery_logger.debug("End dating inactive size: %s" % size)
        size.end_date = now_time
        size.save()

    # Find home for 'Unknown Size'
    unknown_sizes = Size.objects.filter(
        provider=provider, name__contains='Unknown Size'
    )
    for size in unknown_sizes:
        # Lookup sizes may not show up in 'list_sizes'
        if size.alias == 'N/A':
            continue    # This is a sentinal value added for a separate purpose.
        try:
            libcloud_size = admin_driver.get_size(
                size.alias, forced_lookup=True
            )
        except BaseHTTPError as error:
            if error.code == 404:
                # The size may have been truly deleted
                continue
        if not libcloud_size:
            continue
        cloud_size = OSSize(libcloud_size)
        core_size = convert_esh_size(cloud_size, provider.uuid)

    if print_logs:
        _exit_stdout_logging(console_handler)
    for size in seen_sizes:
        size.esh = None
    return seen_sizes
コード例 #2
0
def _esh_instance_size_to_core(esh_driver, esh_instance, provider_uuid):
    # NOTE: Querying for esh_size because esh_instance
    # Only holds the alias, not all the values.
    # As a bonus this is a cached-call
    esh_size = esh_instance.size
    if isinstance(esh_size, MockSize):
        # MockSize includes only the Alias/ID information
        # so a lookup on the size is required to get accurate
        # information.
        # TODO: Switch to 'get_cached_size!'
        lc_size = esh_driver.get_size(esh_size.id, forced_lookup=True)
        new_size = OSSize(lc_size)
        if new_size:
            esh_size = new_size
    core_size = convert_esh_size(esh_size, provider_uuid)
    return core_size