Ejemplo n.º 1
0
def monitor_machines_for(provider_id,
                         limit_machines=[],
                         print_logs=False,
                         dry_run=False):
    """
    Run the set of tasks related to monitoring machines 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.

    NEW LOGIC:
    """
    provider = Provider.objects.get(id=provider_id)

    if print_logs:
        console_handler = _init_stdout_logging()

    account_driver = get_account_driver(provider)
    #Bail out if account driver is invalid
    if not account_driver:
        _exit_stdout_logging(console_handler)
        return []

    cloud_machines = account_driver.list_all_images()
    if limit_machines:
        cloud_machines = [
            cm for cm in cloud_machines if cm.id in limit_machines
        ]
    db_machines = []
    # ASSERT: All non-end-dated machines in the DB can be found in the cloud
    # if you do not believe this is the case, you should call 'prune_machines_for'
    for cloud_machine in cloud_machines:
        if not machine_is_valid(cloud_machine, account_driver):
            continue
        owner_project = _get_owner(account_driver, cloud_machine)
        #STEP 1: Get the application, version, and provider_machine registered in Atmosphere
        (db_machine, created) = convert_glance_image(cloud_machine,
                                                     provider.uuid,
                                                     owner_project)
        db_machines.append(db_machine)
        #STEP 2: For any private cloud_machine, convert the 'shared users' as known by cloud
        update_image_membership(account_driver, cloud_machine, db_machine)

        # into DB relationships: ApplicationVersionMembership, ProviderMachineMembership
        #STEP 3: if ENFORCING -- occasionally 're-distribute' any ACLs that are *listed on DB but not on cloud* -- removals should be done explicitly, outside of this function
        if settings.ENFORCING:
            distribute_image_membership(account_driver, cloud_machine,
                                        provider)
        # ASSERTIONS about this method:
        # 1) We will never 'remove' membership,
        # 2) We will never 'remove' a public or private flag as listed in application.
        # 2b) Future: Individual versions/machines as described by relationships above dictate whats shown in the application.

    if print_logs:
        _exit_stdout_logging(console_handler)
    return db_machines
def monitor_machines_for(
    provider_id,
    limit_machines=[],
    print_logs=False,
    dry_run=False,
    validate=True
):
    """
    Run the set of tasks related to monitoring machines 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.

    NEW LOGIC:
    """
    provider = Provider.objects.get(id=provider_id)

    if print_logs:
        console_handler = _init_stdout_logging()

    account_driver = get_account_driver(provider)
    #Bail out if account driver is invalid
    if not account_driver:
        if print_logs:
            _exit_stdout_logging(console_handler)
        return []

    if account_driver.user_manager.version == 2:
        #Old providers need to use v1 glance to get owner information.
        cloud_machines_dict = account_driver.image_manager.list_v1_images()
        cloud_machines = account_driver.list_all_images()
        account_driver.add_owner_to_machine(cloud_machines, cloud_machines_dict)
    else:
        cloud_machines = account_driver.list_all_images()

    if limit_machines:
        cloud_machines = [
            cm for cm in cloud_machines if cm.id in limit_machines
        ]
    db_machines = []
    # ASSERT: All non-end-dated machines in the DB can be found in the cloud
    # if you do not believe this is the case, you should call 'prune_machines_for'
    machine_validator = MachineValidationPluginManager.get_validator(
        account_driver
    )
    for cloud_machine in cloud_machines:
        if validate and not machine_validator.machine_is_valid(cloud_machine):
            continue
        owner = cloud_machine.get('owner')
        if owner:
            owner_project = account_driver.get_project_by_id(owner)
        else:
            owner = cloud_machine.get('application_owner')
            owner_project = account_driver.get_project(owner)
        #STEP 1: Get the application, version, and provider_machine registered in Atmosphere
        (db_machine, created) = convert_glance_image(
            account_driver, cloud_machine, provider.uuid, owner_project
        )
        if not db_machine:
            continue
        db_machines.append(db_machine)
        #STEP 2: For any private cloud_machine, convert the 'shared users' as known by cloud
        #        into DB relationships: ApplicationVersionMembership, ProviderMachineMembership
        update_image_membership(account_driver, cloud_machine, db_machine)

        # STEP 3: if ENFORCING -- occasionally 're-distribute' any ACLs that
        # are *listed on DB but not on cloud* -- removals should be done
        # explicitly, outside of this function
        if settings.ENFORCING:
            distribute_image_membership(account_driver, cloud_machine, provider)

        # ASSERTIONS about this method:
        # 1) We will never 'remove' membership,
        # 2) We will never 'remove' a public or private flag as listed in application.
        # 2b) Future: Individual versions/machines as described by relationships above dictate whats shown in the application.

    if print_logs:
        _exit_stdout_logging(console_handler)
    return db_machines