def create_provider_machine(identifier, provider_uuid, app, created_by_identity=None, version=None):
    # Attempt to match machine by provider alias
    # Admin identity used until the real owner can be identified.
    provider = Provider.objects.get(uuid=provider_uuid)
    if not created_by_identity:
        created_by_identity = provider.admin

    try:
        source = InstanceSource.objects.get(provider=provider, identifier=identifier)
        source.created_by_identity = created_by_identity
        source.created_by = created_by_identity.created_by
    except InstanceSource.DoesNotExist:
        source = InstanceSource.objects.create(
            provider=provider,
            identifier=identifier,
            created_by_identity=created_by_identity,
            created_by=created_by_identity.created_by,
        )
    if not version:
        version = create_app_version(app)
    logger.debug("Provider %s" % provider)
    logger.debug("App %s" % app)
    logger.debug("Version %s" % version)
    logger.debug("Source %s" % source.identifier)
    provider_machine = ProviderMachine.objects.create(instance_source=source, application_version=version)
    provider_machine_update_hook(provider_machine, provider_uuid, identifier)
    logger.info("New ProviderMachine created: %s" % provider_machine)
    add_to_cache(provider_machine)
    return provider_machine
Exemple #2
0
def create_provider_machine(identifier, provider_uuid, app,
                            created_by_identity=None, version=None):
    # Attempt to match machine by provider alias
    # Admin identity used until the real owner can be identified.
    provider = Provider.objects.get(uuid=provider_uuid)
    if not created_by_identity:
        created_by_identity = provider.admin

    try:
        source = InstanceSource.objects.get(
            provider=provider,
            identifier=identifier)
        source.created_by_identity = created_by_identity
        source.created_by = created_by_identity.created_by
    except InstanceSource.DoesNotExist:
        source = InstanceSource.objects.create(
            provider=provider,
            identifier=identifier,
            created_by_identity=created_by_identity,
            created_by=created_by_identity.created_by,
        )
    if not version:
        version = create_app_version(app)
    logger.debug("Provider %s" % provider)
    logger.debug("App %s" % app)
    logger.debug("Version %s" % version)
    logger.debug("Source %s" % source.identifier)
    provider_machine = ProviderMachine.objects.create(
        instance_source=source,
        application_version=version,
    )
    read_cloud_machine_hook(provider_machine, provider_uuid, identifier)
    logger.info("New ProviderMachine created: %s" % provider_machine)
    add_to_cache(provider_machine)
    return provider_machine
def get_or_create_provider_machine(image_id, machine_name, provider_uuid, app=None, version=None):
    """
    Guaranteed Return of ProviderMachine.
    1. Load provider machine from DB
    2. If 'Miss':
       * Lookup application based on PM uuid
       If 'Miss':
         * Create application based on PM uuid
    3. Using application from 2. Create provider machine
    """
    provider_machine = get_provider_machine(image_id, provider_uuid)
    if provider_machine:
        return provider_machine
    if not app:
        app = get_application(provider_uuid, image_id, machine_name)
    # ASSERT: If no application here, this is a new image (Found on instance)
    # that was created on a seperate server. We need to make a new one.
    if not app:
        app = create_application(provider_uuid, image_id, machine_name)

    if not version:
        version = get_version_for_machine(provider_uuid, image_id, fuzzy=True)
    if not version:
        version = create_app_version(app, "1.0", provider_machine_id=image_id)
    # TODO: fuzzy=True returns a list, but call comes through as a .get()?
    #      this line will cover that edge-case.
    if type(version) in [models.QuerySet, list]:
        version = version[0]

    return create_provider_machine(image_id, provider_uuid, app, version=version)
Exemple #4
0
def get_or_create_provider_machine(image_id, machine_name,
                                   provider_uuid, app=None, version=None,
                                   version_name="1.0"):
    """
    Guaranteed Return of ProviderMachine.
    1. Load provider machine from DB
    2. If 'Miss':
       * Lookup application based on PM uuid
       If 'Miss':
         * Create application based on PM uuid
    3. Using application from 2. Create provider machine
    """
    provider_machine = get_provider_machine(image_id, provider_uuid)
    if provider_machine:
        return provider_machine
    if not app:
        app = get_application(provider_uuid, image_id, machine_name)
    # ASSERT: If no application here, this is a new image (Found on instance)
    # that was created on a seperate server. We need to make a new one.
    if not app:
        app = create_application(provider_uuid, image_id, machine_name)

    if not version:
        version = get_version_for_machine(provider_uuid, image_id, fuzzy=True)
        if not version:
            version = create_app_version(app, version_name, provider_machine_id=image_id)
    if type(version) in [models.QuerySet, list]:
        version = version[0]

    return create_provider_machine(
        image_id,
        provider_uuid,
        app,
        version=version)
Exemple #5
0
def get_or_create_provider_machine(image_id, machine_name,
                                   provider_uuid, app=None, version=None):
    """
    Guaranteed Return of ProviderMachine.
    1. Load provider machine from DB
    2. If 'Miss':
       * Lookup application based on PM uuid
       If 'Miss':
         * Create application based on PM uuid
    3. Using application from 2. Create provider machine
    """
    provider_machine = get_provider_machine(image_id, provider_uuid)
    if provider_machine:
        return provider_machine
    if not app:
        app = get_application(provider_uuid, image_id, machine_name)
    # ASSERT: If no application here, this is a new image (Found on instance)
    # that was created on a seperate server. We need to make a new one.
    if not app:
        app = create_application(provider_uuid, image_id, machine_name)

    if not version:
        version = get_version_for_machine(provider_uuid, image_id)
    if not version:
        version = create_app_version(app, "1.0", provider_machine_id=image_id)

    return create_provider_machine(
        image_id,
        provider_uuid,
        app,
        version=version)
Exemple #6
0
def convert_glance_image(glance_image, provider_uuid, owner=None):
    """
    Guaranteed Return of ProviderMachine.
    1. Load provider machine from DB and return
    2a. If 'Miss':
        * Lookup application based on glance_machine metadata for application_uuid
        If 'Miss':
          * Create application based on available glance_machine metadata
    2b. Using application from 2. Create provider machine
    """
    from core.models import AtmosphereUser
    image_id = glance_image.id
    machine_name = glance_image.name
    application_name = machine_name  # Future: application_name will partition at the 'Application Version separator'.. and pass the version_name to create_version
    provider_machine = get_provider_machine(image_id, provider_uuid)
    if provider_machine:
        return (provider_machine, False)
    app_kwargs = collect_image_metadata(glance_image)
    if owner and hasattr(owner,'name'):
        owner_name = owner.name
    else:
        owner_name = glance_image.get('application_owner')
    user = AtmosphereUser.objects.filter(username=owner_name).first()
    if user:
        identity = Identity.objects.filter(
            provider__uuid=provider_uuid, created_by=user).first()
    else:
        identity = None
    app_kwargs.update({
        'created_by': user,
        'created_by_identity': identity
    })
    app = create_application(
        provider_uuid, image_id, application_name,
        **app_kwargs)
    version = get_version_for_machine(provider_uuid, image_id, fuzzy=True)
    if not version:
        version_kwargs = {
            'version_str': glance_image.get('application_version', '1.0'),
            'created_by': user,
            'created_by_identity': identity,
            'provider_machine_id': image_id
        }
        version = create_app_version(app, **version_kwargs)
    #TODO: fuzzy=True returns a list, but call comes through as a .get()?
    #      this line will cover that edge-case.
    if type(version) in [models.QuerySet, list]:
        version = version[0]

    machine_kwargs = {
        'created_by_identity': identity,
        'version': version
    }
    provider_machine = create_provider_machine(
        image_id, provider_uuid,
        app, **machine_kwargs)
    return (provider_machine, True)
def convert_glance_image(
    account_driver, glance_image, provider_uuid, owner=None
):
    """
    Guaranteed Return of ProviderMachine.
    1. Load provider machine from DB and return
    2a. If 'Miss':
        * Lookup application based on glance_machine metadata for application_uuid
        If 'Miss':
          * Create application based on available glance_machine metadata
    2b. Using application from 2. Create provider machine
    """
    if hasattr(glance_image, 'id'):
        image_id = glance_image.id
    elif type(glance_image) == dict:
        image_id = glance_image.get('id')
    else:
        raise ValueError("Unexpected glance_image: %s" % glance_image)

    provider_machine = get_provider_machine(image_id, provider_uuid)
    if provider_machine:
        if provider_machine.is_end_dated():
            return (None, False)
        update_instance_source_size(
            provider_machine.instance_source, glance_image.get('size')
        )
        return (provider_machine, False)
    (app_kwargs, version_kwargs
    ) = _application_and_version_from_metadata(account_driver, glance_image)
    # TODO: use version_kwargs in method below?
    version = get_version_for_machine(provider_uuid, image_id, fuzzy=True)
    if version:
        app = version.application
    else:
        app = create_application(**app_kwargs)
        version = create_app_version(app=app, **version_kwargs)

    #TODO: fuzzy=True returns a list, but call comes through as a .get()?
    #      this line will cover that edge-case.
    if type(version) in [models.QuerySet, list]:
        version = version[0]

    machine_kwargs = {
        'created_by_identity': version_kwargs.get('created_by_identity'),
        'version': version
    }
    provider_machine = create_provider_machine(
        image_id, provider_uuid, app, **machine_kwargs
    )
    update_instance_source_size(
        provider_machine.instance_source, glance_image.get('size')
    )
    return (provider_machine, True)
Exemple #8
0
def convert_glance_image(account_driver,
                         glance_image,
                         provider_uuid,
                         owner=None):
    """
    Guaranteed Return of ProviderMachine.
    1. Load provider machine from DB and return
    2a. If 'Miss':
        * Lookup application based on glance_machine metadata for application_uuid
        If 'Miss':
          * Create application based on available glance_machine metadata
    2b. Using application from 2. Create provider machine
    """
    if hasattr(glance_image, 'id'):
        image_id = glance_image.id
    elif type(glance_image) == dict:
        image_id = glance_image.get('id')
    else:
        raise ValueError("Unexpected glance_image: %s" % glance_image)

    provider_machine = get_provider_machine(image_id, provider_uuid)
    if provider_machine:
        if provider_machine.is_end_dated():
            return (None, False)
        update_instance_source_size(provider_machine.instance_source,
                                    glance_image.get('size'))
        return (provider_machine, False)
    (app_kwargs, version_kwargs) = _application_and_version_from_metadata(
        account_driver, glance_image)
    # TODO: use version_kwargs in method below?
    version = get_version_for_machine(provider_uuid, image_id, fuzzy=True)
    if version:
        app = version.application
    else:
        app = create_application(**app_kwargs)
        version = create_app_version(app=app, **version_kwargs)

    #TODO: fuzzy=True returns a list, but call comes through as a .get()?
    #      this line will cover that edge-case.
    if type(version) in [models.QuerySet, list]:
        version = version[0]

    machine_kwargs = {
        'created_by_identity': version_kwargs.get('created_by_identity'),
        'version': version
    }
    provider_machine = create_provider_machine(image_id, provider_uuid, app,
                                               **machine_kwargs)
    update_instance_source_size(provider_machine.instance_source,
                                glance_image.get('size'))
    return (provider_machine, True)
Exemple #9
0
def process_machine_request(machine_request, new_image_id, update_cloud=True):
    """
    NOTE: Current process accepts instance with source of 'Image' ONLY!
          VOLUMES CANNOT BE IMAGED until this function is updated!
    """
    # Based on original instance -- You'll need this:
    parent_mach = machine_request.instance.provider_machine
    parent_version = parent_mach.application_version
    # Based on data provided in MR:
    new_provider = machine_request.new_machine_provider
    new_owner = machine_request.new_machine_owner
    owner_identity = _get_owner(new_provider, new_owner)
    tags = _match_tags_to_names(machine_request.new_version_tags)
    # TODO: Use it or remove it
    # membership = _match_membership_to_access(
    #     machine_request.access_list,
    #     machine_request.new_version_membership)
    if machine_request.new_version_forked:
        application = create_application(
            new_provider.uuid,
            new_image_id,
            machine_request.new_application_name,
            created_by_identity=owner_identity,
            description=machine_request.new_application_description,
            private=not machine_request.is_public(),
            tags=tags)
    else:
        application = update_application(
            parent_version.application, machine_request.new_application_name,
            tags, machine_request.new_application_description)
    #FIXME: Either *add* system_files here, or *remove* the entire field.
    app_version = create_app_version(application,
                                     machine_request.new_version_name,
                                     new_owner,
                                     owner_identity,
                                     machine_request.new_version_change_log,
                                     machine_request.new_version_allow_imaging,
                                     provider_machine_id=new_image_id)

    # 2. Create the new InstanceSource and appropriate Object, relations,
    # Memberships..
    if models.ProviderMachine.test_existence(new_provider, new_image_id):
        pm = models.ProviderMachine.objects.get(
            instance_source__identifier=new_image_id,
            instance_source__provider=new_provider)
        pm = update_provider_machine(
            pm,
            new_created_by_identity=owner_identity,
            new_created_by=machine_request.new_machine_owner,
            new_application_version=app_version)
    else:
        pm = create_provider_machine(new_image_id, new_provider.uuid,
                                     application, owner_identity, app_version)
        provider_machine_write_hook(pm)

    # Must be set in order to ask for threshold information
    machine_request.new_application_version = app_version
    machine_request.new_machine = pm
    machine_request.save()

    # 3. Associate additional attributes to new application
    if machine_request.has_threshold():
        machine_request.update_threshold()

    # 4a. Add new *Memberships For new ProviderMachine//Application
    if not machine_request.is_public():
        upload_privacy_data(machine_request, pm)

    # 4b. If new boot scripts have been associated,
    # add them to the new version.
    if machine_request.new_version_scripts.count():
        for script in machine_request.new_version_scripts.all():
            app_version.boot_scripts.add(script)

    # 4c. If new licenses have been associated, add them to the new version.
    if machine_request.new_version_licenses.count():
        for license in machine_request.new_version_licenses.all():
            app_version.licenses.add(license)
    return machine_request
def process_machine_request(machine_request, new_image_id, update_cloud=True):
    """
    NOTE: Current process accepts instance with source of 'Image' ONLY!
          VOLUMES CANNOT BE IMAGED until this function is updated!
    """
    # Based on original instance -- You'll need this:
    parent_mach = machine_request.instance.provider_machine
    parent_version = parent_mach.application_version
    # Based on data provided in MR:
    new_provider = machine_request.new_machine_provider
    new_owner = machine_request.new_machine_owner
    owner_identity = _get_owner(new_provider, new_owner)
    tags = _match_tags_to_names(machine_request.new_version_tags)
    # TODO: Use it or remove it
    # membership = _match_membership_to_access(
    #     machine_request.access_list,
    #     machine_request.new_version_membership)
    if machine_request.new_version_forked:
        application = create_application(
            new_provider.uuid,
            new_image_id,
            machine_request.new_application_name,
            created_by_identity=owner_identity,
            description=machine_request.new_application_description,
            private=not machine_request.is_public(),
            tags=tags)
    else:
        application = update_application(
            parent_version.application,
            machine_request.new_application_name,
            tags,
            machine_request.new_application_description)
    app_version = create_app_version(
        application, machine_request.new_version_name,
        new_owner, owner_identity, machine_request.new_version_change_log,
        machine_request.new_version_allow_imaging, provider_machine_id=new_image_id)

    # 2. Create the new InstanceSource and appropriate Object, relations,
    # Memberships..
    if ProviderMachine.test_existence(new_provider, new_image_id):
        pm = ProviderMachine.objects.get(
            instance_source__identifier=new_image_id,
            instance_source__provider=new_provider)
        pm = update_provider_machine(
            pm,
            new_created_by_identity=owner_identity,
            new_created_by=machine_request.new_machine_owner,
            new_application_version=app_version)
    else:
        pm = create_provider_machine(new_image_id, new_provider.uuid, application, owner_identity, app_version)
        provider_machine_write_hook(pm)

    # Must be set in order to ask for threshold information
    machine_request.new_application_version = app_version
    machine_request.new_machine = pm
    machine_request.save()

    # 3. Associate additional attributes to new application
    if machine_request.has_threshold():
        machine_request.update_threshold()

    # 4a. Add new *Memberships For new ProviderMachine//Application
    if not machine_request.is_public():
        upload_privacy_data(machine_request, pm)

    # 4b. If new boot scripts have been associated,
    # add them to the new version.
    if machine_request.new_version_scripts.count():
        for script in machine_request.new_version_scripts.all():
            app_version.boot_scripts.add(script)

    # 4c. If new licenses have been associated, add them to the new version.
    if machine_request.new_version_licenses.count():
        for license in machine_request.new_version_licenses.all():
            app_version.licenses.add(license)

    # 5. Advance the state of machine request
    # After processing, validate the image.
    machine_request.status = 'validating'
    machine_request.save()
    return machine_request