示例#1
0
def convert_esh_machine(esh_driver, esh_machine, provider_id, user,
                        image_id=None):
    """
    Takes as input an (rtwo) driver and machine, and a core provider id
    Returns as output a core ProviderMachine
    """
    if image_id and not esh_machine:
        return _convert_from_instance(esh_driver, provider_id, image_id)
    elif not esh_machine:
        return None
    push_metadata = False
    if not esh_machine._image:
        metadata = {}
    else:
        metadata = esh_machine._image.extra.get('metadata',{})
    name = esh_machine.name
    alias = esh_machine.alias

    if metadata and False and has_app_data(metadata):
        #USE CASE: Application data exists on the image
        # and may exist on this DB
        app = get_application(alias, metadata.get('application_uuid'))
        if not app:
            app_kwargs = get_app_data(metadata, provider_id)
            logger.debug("Creating Application for Image %s "
                         "(Based on Application data: %s)"
                         % (alias, app_kwargs))
            app = create_application(alias, provider_id, **app_kwargs)
    else:
        #USE CASE: Application data does NOT exist,
        # This machine is assumed to be its own application, so run the
        # machine alias to retrieve any existing application.
        # otherwise create a new application with the same name as the machine
        # App assumes all default values
        #logger.info("Image %s missing Application data" % (alias, ))
        push_metadata = True
        #TODO: Get application 'name' instead?
        app = get_application(alias)
        if not app:
            logger.debug("Creating Application for Image %s" % (alias, ))
            app = create_application(alias, provider_id, name)
    provider_machine = load_provider_machine(alias, name, provider_id,
                                             app=app, metadata=metadata)

    #If names conflict between OpenStack and Database, choose OpenStack.
    if esh_machine._image and app.name != name:
        logger.debug("Name Conflict! Machine %s named %s, Application named %s"
                     % (alias, name, app.name))
        app.name = name
        app.save()
    _check_project(app, user)
    #if push_metadata and hasattr(esh_driver._connection,
    #                             'ex_set_image_metadata'):
    #    logger.debug("Creating App data for Image %s:%s" % (alias, app.name))
    #    write_app_data(esh_driver, provider_machine)
    provider_machine.esh = esh_machine
    return provider_machine
示例#2
0
def convert_esh_machine(esh_driver, esh_machine, provider_id, user, image_id=None):
    """
    Takes as input an (rtwo) driver and machine, and a core provider id
    Returns as output a core ProviderMachine
    """
    if image_id and not esh_machine:
        return _convert_from_instance(esh_driver, provider_id, image_id)
    elif not esh_machine:
        return None
    push_metadata = False
    if not esh_machine._image:
        metadata = {}
    else:
        metadata = esh_machine._image.extra.get('metadata',{})
    name = esh_machine.name
    alias = esh_machine.alias

    if metadata and False and has_app_data(metadata):
        #USE CASE: Application data exists on the image
        # and may exist on this DB
        app = get_application(alias, metadata.get('application_uuid'))
        if not app:
            app_kwargs = get_app_data(metadata, provider_id)
            logger.debug("Creating Application for Image %s "
                         "(Based on Application data: %s)"
                         % (alias, app_kwargs))
            app = create_application(alias, provider_id, **app_kwargs)
    else:
        #USE CASE: Application data does NOT exist,
        # This machine is assumed to be its own application, so run the
        # machine alias to retrieve any existing application.
        # otherwise create a new application with the same name as the machine
        # App assumes all default values
        #logger.info("Image %s missing Application data" % (alias, ))
        push_metadata = True
        #TODO: Get application 'name' instead?
        app = get_application(alias)
        if not app:
            logger.debug("Creating Application for Image %s" % (alias, ))
            app = create_application(alias, provider_id, name)
    provider_machine = load_provider_machine(alias, name, provider_id,
                                             app=app, metadata=metadata)

    #If names conflict between OpenStack and Database, choose OpenStack.
    if esh_machine._image and app.name != name:
        logger.debug("Name Conflict! Machine %s named %s, Application named %s"
                     % (alias, name, app.name))
        app.name = name
        app.save()
    _check_project(app, user)
    #if push_metadata and hasattr(esh_driver._connection,
    #                             'ex_set_image_metadata'):
    #    logger.debug("Creating App data for Image %s:%s" % (alias, app.name))
    #    write_app_data(esh_driver, provider_machine)
    provider_machine.esh = esh_machine
    return provider_machine
示例#3
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)
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)
def _apply_fix(accounts, provider, pm):
    image_id = pm.identifier
    uuid = _generate_app_uuid(image_id)
    g_img = get_image(accounts, image_id)
    name = g_img.name

    apps = Application.objects.filter(uuid=uuid)
    if not apps.count():
        app = create_application(
            provider.uuid,
            image_id,
            name,
            private=(
                not g_img.is_public),
            uuid=uuid)
    else:
        app = apps[0]

    pm.application = app
    pm.save()
    if NO_META:
        print "Fixed: %s Now points to %s" % (pm.identifier, app)
        return
    app.update_images()
    g_img = get_image(accounts, image_id)
    print "Updated: %s Now points to %s" % (pm.identifier, app)
示例#6
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)
示例#7
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)
示例#8
0
def _create_machine_and_app(esh_machine, provider_uuid):
    app = get_application(esh_machine.alias)
    if not app:
        logger.debug("Creating Application for Image %s" % (esh_machine.alias, ))
        app = create_application(esh_machine.alias, provider_uuid, esh_machine.name)
    #Using what we know about our (possibly new) application
    #and load (or possibly create) the provider machine
    provider_machine = load_provider_machine(esh_machine.alias, esh_machine.name, provider_uuid,
                                             app=app)
    return provider_machine
def _load_machine(esh_machine, provider_uuid):
    name = esh_machine.name
    alias = esh_machine.id
    app = get_application(provider_uuid, alias, name)
    if not app:
        logger.debug("Creating Application for Image %s" % (alias,))
        app = create_application(provider_uuid, alias, name)

    # Using what we know about our (possibly new) application
    # and load (or possibly create) the provider machine
    provider_machine = get_or_create_provider_machine(alias, name, provider_uuid, app=app)
    return provider_machine
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)
示例#11
0
def load_provider_machine(provider_alias, machine_name, provider_id,
                          app=None, metadata={}):
    """
    Returns ProviderMachine
    """
    provider_machine = get_provider_machine(provider_alias, provider_id)
    if provider_machine:
        return provider_machine
    if not app:
        app = get_application(provider_alias, app_uuid=metadata.get('uuid'))
    if not app:
        app = create_application(provider_alias, provider_id, machine_name)
    return create_provider_machine(machine_name, provider_alias, provider_id, app=app, metadata=metadata)
示例#12
0
def _load_machine(esh_machine, provider_uuid):
    name = esh_machine.name
    alias = esh_machine.id
    app = get_application(provider_uuid, alias, name)
    if not app:
        logger.debug("Creating Application for Image %s" % (alias, ))
        app = create_application(provider_uuid, alias, name)

    # Using what we know about our (possibly new) application
    # and load (or possibly create) the provider machine
    provider_machine = get_or_create_provider_machine(
        alias, name, provider_uuid, app=app)
    return provider_machine
示例#13
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)
示例#14
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!
    """
    parent_mach = machine_request.instance.provider_machine
    parent_app = machine_request.instance.provider_machine.application
    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_machine_tags)

    if machine_request.new_machine_forked:
        app = create_application(
            new_image_id,
            new_provider.uuid,
            machine_request.new_machine_name,
            created_by_identity=owner_identity,
            description=machine_request.new_machine_description,
            private=not machine_request.is_public(),
            tags=tags)
    else:
        parent_app = machine_request.instance.source.providermachine.application
        app = update_application(parent_app, machine_request.new_machine_name, machine_request.new_machine_description, tags)

    #2. Create the new InstanceSource and appropriate Object, relations, Memberships..
    if ProviderMachine.test_existence(new_provider, new_image_id):
        pm = ProviderMachine.objects.get(identifier=new_image_id, provider=new_provider)
        pm = update_provider_machine(pm, new_created_by_identity=owner_identity, new_created_by=machine_request.new_machine_owner, new_application=app, new_version=machine_request.new_machine_version, allow_imaging=machine_request.new_machine_allow_imaging)
    else:
        pm = create_provider_machine(new_image_id, new_provider.uuid, app, owner_identity, machine_request.new_machine_version, machine_request.new_machine_allow_imaging)
        provider_machine_write_hook(pm)

    #Must be set in order to ask for threshold information
    machine_request.new_machine = pm

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

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

    #5. Advance the state of machine request
    machine_request.end_date = timezone.now()
    #After processing, validate the image.
    machine_request.status = 'validating'
    machine_request.save()
    return machine_request
示例#15
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!
    """
    parent_mach = machine_request.instance.provider_machine
    parent_app = machine_request.instance.provider_machine.application
    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_machine_tags)

    if machine_request.new_machine_forked:
        app = create_application(
            new_image_id,
            new_provider.uuid,
            machine_request.new_machine_name,
            created_by_identity=owner_identity,
            description=machine_request.new_machine_description,
            private=not machine_request.is_public(),
            tags=tags)
    else:
        parent_app = machine_request.instance.source.providermachine.application
        app = update_application(parent_app, machine_request.new_machine_name, machine_request.new_machine_description, tags)

    #2. Create the new InstanceSource and appropriate Object, relations, Memberships..
    if ProviderMachine.test_existence(new_provider, new_image_id):
        pm = ProviderMachine.objects.get(identifier=new_image_id, provider=new_provider)
        pm = update_provider_machine(pm, new_created_by_identity=owner_identity, new_created_by=machine_request.new_machine_owner, new_application=app, new_version=machine_request.new_machine_version)
    else:
        pm = create_provider_machine(new_image_id, new_provider.uuid, app, owner_identity, machine_request.new_machine_version)
        provider_machine_write_hook(pm)

    #Must be set in order to ask for threshold information
    machine_request.new_machine = pm

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

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

    #5. Advance the state of machine request
    machine_request.end_date = timezone.now()
    #After processing, validate the image.
    machine_request.status = 'validating'
    machine_request.save()
    return machine_request
示例#16
0
def _create_new_application(machine_request, new_image_id, tags=[]):
    new_provider = machine_request.new_machine_provider
    user = machine_request.new_machine_owner
    owner_ident = Identity.objects.get(created_by=user, provider=new_provider)
    # This is a brand new app and a brand new providermachine
    new_app = create_application(
        new_provider.id,
        new_image_id,
        machine_request.new_application_name,
        owner_ident,
        # new_app.Private = False when machine_request.is_public = True
        not machine_request.is_public(),
        machine_request.new_machine_version,
        machine_request.new_machine_description,
        tags)
    return new_app
示例#17
0
def _create_new_application(machine_request, new_image_id, tags=[]):
    new_provider = machine_request.new_machine_provider
    user = machine_request.new_machine_owner
    owner_ident = Identity.objects.get(created_by=user, provider=new_provider)
    # This is a brand new app and a brand new providermachine
    new_app = create_application(
        new_provider.id,
        new_image_id,
        machine_request.new_application_name,
        owner_ident,
        # new_app.Private = False when machine_request.is_public = True
        not machine_request.is_public(),
        machine_request.new_machine_version,
        machine_request.new_machine_description,
        tags)
    return new_app
示例#18
0
def _load_machine(esh_machine, provider_uuid):
    name = esh_machine.name
    alias = esh_machine.id
    app = get_application(provider_uuid, alias, name)
    if not app:
        logger.debug("Creating Application for Image %s" % (alias, ))
        app = create_application(provider_uuid, alias, name)

    # Using what we know about our (possibly new) application
    # and load (or possibly create) the provider machine
    provider_machine = get_or_create_provider_machine(
        alias, name, provider_uuid, app=app)
    lc_machine = esh_machine._image
    image_size = lc_machine.extra.get('image_size')
    update_instance_source_size(provider_machine.instance_source, image_size)
    return provider_machine
示例#19
0
def _load_machine(esh_machine, provider_uuid):
    name = esh_machine.name
    alias = esh_machine.id
    app = get_application(provider_uuid, alias, name)
    if not app:
        logger.debug("Creating Application for Image %s" % (alias, ))
        app = create_application(provider_uuid, alias, name)

    # Using what we know about our (possibly new) application
    # and load (or possibly create) the provider machine
    provider_machine = get_or_create_provider_machine(
        alias, name, provider_uuid, app=app)
    lc_machine = esh_machine._image
    image_size = lc_machine.extra.get('image_size')
    update_instance_source_size(provider_machine.instance_source, image_size)
    return provider_machine
示例#20
0
def _check_for_metadata_update(esh_machine, provider_uuid):
    """
    In this method, we look for specific metadata on an 'esh_machine'
    IF we find the data we are looking for (like application_uuid)
    and we assume that OpenStack is 'the Authority' on this information,
    We can use that to Update/Bootstrap our DB values about the specific
    application and provider machine version.
    """
    name = esh_machine.name
    alias = esh_machine.alias
    if not esh_machine._image:
        metadata = {}
    else:
        metadata = esh_machine._image.extra.get('metadata',{})
    #TODO: lookup the line below and find the 'real' test conditions.
    if metadata and False and has_app_metadata(metadata):
        #USE CASE: Application data exists on the image
        # and may exist on this DB
        app = get_application(alias, metadata.get('application_uuid'))
        if not app:
            app_kwargs = get_app_metadata(metadata, provider_uuid)
            logger.debug("Creating Application for Image %s "
                         "(Based on Application data: %s)"
                         % (alias, app_kwargs))
            app = create_application(alias, provider_uuid, **app_kwargs)
        provider_machine = load_provider_machine(alias, name, provider_uuid,
                                             app=app, metadata=metadata)
        #If names conflict between OpenStack and Database, choose OpenStack.
        if esh_machine._image and app.name != name:
            logger.debug("Name Conflict! Machine %s named %s, Application named %s"
                         % (alias, name, app.name))
            app.name = name
            app.save()
    else:
        #USE CASE: Application data does NOT exist,
        # This machine is assumed to be its own application, so run the
        # machine alias to retrieve any existing application.
        # otherwise create a new application with the same name as the machine
        provider_machine = _create_machine_and_app(
                esh_machine, provider_uuid)
    #TODO: some test to verify when we should be 'pushing back' to cloud
    #push_metadata = True
    #if push_metadata and hasattr(esh_driver._connection,
    #                             'ex_set_image_metadata'):
    #    logger.debug("Writing App data for Image %s:%s" % (alias, app.name))
    #    write_app_to_metadata(esh_driver, provider_machine)
    return provider_machine
示例#21
0
def get_or_create_provider_machine(image_id,
                                   machine_name,
                                   provider_uuid,
                                   app=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 an 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)
    return create_provider_machine(image_id, provider_uuid, app)
示例#22
0
def process_machine_request(machine_request, new_image_id):
    from core.models.machine import add_to_cache
    from core.models.tag import Tag
    from core.models import Identity, ProviderMachine
    #Get all the data you can from the machine request
    new_provider = machine_request.new_machine_provider
    owner_ident = Identity.objects.get(created_by=machine_request.new_machine_owner, provider=new_provider)
    parent_mach = machine_request.instance.provider_machine
    parent_app = machine_request.instance.provider_machine.application
    if machine_request.new_machine_tags:
        tags = [Tag.objects.get(name__iexact=tag) for tag in
                machine_request.new_machine_tags.split(',')]
    else:
        tags = []

    #if machine_request.new_machine_forked:
    #NOTE: Swap these lines when application forking/versioning is supported in the UI
    if True:
        # This is a brand new app and a brand new providermachine
        new_app = create_application(
                new_image_id,
                new_provider.id,
                machine_request.new_machine_name, 
                owner_ident,
                #new_app.Private = False when machine_request.is_public = True
                not machine_request.is_public(),
                machine_request.new_machine_version,
                machine_request.new_machine_description,
                tags)
        app_to_use = new_app
        description = machine_request.new_machine_description
    else:
        #This is NOT a fork, the application to be used is that of your
        # ancestor, and the app owner should not be changed.
        app_to_use = parent_app
        #Include your ancestors tags, description if necessary
        tags.extend(parent_app.tags.all())
        if not machine_request.new_machine_description:
            description = parent_app.description
        else:
            description = machine_request.new_machine_description
        app.private = not machine_request.is_public()

        app.tags = tags
        app.description = description
        app.save()
    #Set application data to an existing/new providermachine
    try:
        new_machine = ProviderMachine.objects.get(identifier=new_image_id)
        new_machine.application = app_to_use
        new_machine.version = machine_request.new_machine_version
        new_machine.created_by = machine_request.new_machine_owner
        new_machine.created_by_identity = owner_ident
        new_machine.save()
    except ProviderMachine.DoesNotExist:
        new_machine = create_provider_machine(
            machine_request.new_machine_name, new_image_id,
            machine_request.new_machine_provider_id, app_to_use,
            {'version' : machine_request.new_machine_version})

    #Be sure to write all this data to openstack metadata
    #So that it can continue to be the 'authoritative source'
    if not machine_request.is_public():
        upload_privacy_data(machine_request, new_machine)
    save_app_data(new_machine.application)
    add_to_cache(new_machine)
    machine_request.new_machine = new_machine
    machine_request.end_date = timezone.now()
    machine_request.status = 'completed'
    machine_request.save()
    return machine_request
示例#23
0
def process_machine_request(machine_request, new_image_id):
    from core.models.machine import add_to_cache
    from core.models.tag import Tag
    from core.models import Identity, ProviderMachine
    #Get all the data you can from the machine request
    new_provider = machine_request.new_machine_provider
    owner_ident = Identity.objects.get(
        created_by=machine_request.new_machine_owner, provider=new_provider)
    parent_mach = machine_request.instance.provider_machine
    parent_app = machine_request.instance.provider_machine.application
    if machine_request.new_machine_tags:
        tags = [
            Tag.objects.get(name__iexact=tag)
            for tag in machine_request.new_machine_tags.split(',')
        ]
    else:
        tags = []

    #if machine_request.new_machine_forked:
    #NOTE: Swap these lines when application forking/versioning is supported in the UI
    if True:
        # This is a brand new app and a brand new providermachine
        new_app = create_application(
            new_image_id,
            new_provider.id,
            machine_request.new_machine_name,
            owner_ident,
            #new_app.Private = False when machine_request.is_public = True
            not machine_request.is_public(),
            machine_request.new_machine_version,
            machine_request.new_machine_description,
            tags)
        app_to_use = new_app
        description = machine_request.new_machine_description
    else:
        #This is NOT a fork, the application to be used is that of your
        # ancestor, and the app owner should not be changed.
        app_to_use = parent_app
        #Include your ancestors tags, description if necessary
        tags.extend(parent_app.tags.all())
        if not machine_request.new_machine_description:
            description = parent_app.description
        else:
            description = machine_request.new_machine_description
        app.private = not machine_request.is_public()

        app.tags = tags
        app.description = description
        app.save()
    #Set application data to an existing/new providermachine
    try:
        new_machine = ProviderMachine.objects.get(identifier=new_image_id)
        new_machine.application = app_to_use
        new_machine.version = machine_request.new_machine_version
        new_machine.created_by = machine_request.new_machine_owner
        new_machine.created_by_identity = owner_ident
        new_machine.save()
    except ProviderMachine.DoesNotExist:
        new_machine = create_provider_machine(
            machine_request.new_machine_name, new_image_id,
            machine_request.new_machine_provider_id, app_to_use,
            {'version': machine_request.new_machine_version})

    #Be sure to write all this data to openstack metadata
    #So that it can continue to be the 'authoritative source'
    if not machine_request.is_public():
        upload_privacy_data(machine_request, new_machine)
    save_app_data(new_machine.application)
    add_to_cache(new_machine)
    machine_request.new_machine = new_machine
    machine_request.end_date = timezone.now()
    machine_request.status = 'completed'
    machine_request.save()
    return machine_request
示例#24
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
示例#25
0
def process_machine_request(machine_request, new_image_id):
    from core.models.machine import add_to_cache
    from core.models.tag import Tag
    from core.models import Identity, ProviderMachine
    #Get all the data you can from the machine request
    owner_ident = Identity.objects.get(created_by=machine_request.new_machine_owner, provider=machine_request.new_machine_provider)
    parent_mach = machine_request.instance.provider_machine
    parent_app = machine_request.instance.provider_machine.application
    if machine_request.new_machine_tags:
        tags = [Tag.objects.get(name__iexact=tag) for tag in
                machine_request.new_machine_tags.split(',')]
    else:
        tags = []

    if machine_request.new_machine_forked:
        # This is a brand new app and a brand new providermachine
        new_app = create_application(
                new_image_id,
                new_machine_provider.id,
                machine_request.new_machine_name, 
                owner_ident,
                machine_request.new_machine_version,
                machine_request.new_machine_description,
                tags)
        app_to_use = new_app
    else:
        #This is NOT a fork, the application to be used is that of your
        # ancestor
        app_to_use = parent_app
        #Include your ancestors tags, description if necessary
        tags.extend(parent_app.tags.all())
        if not machine_request.new_machine_description:
            description = parent_app.description
        else:
            description = machine_request.new_machine_description
    try:
        #Set application data to an existing/new providermachine
        new_machine = ProviderMachine.objects.get(identifier=new_image_id)
        new_machine.application = app_to_use
        new_machine.save()
    except ProviderMachine.DoesNotExist:
        new_machine = create_provider_machine(
            machine_request.new_machine_name, new_image_id,
            machine_request.new_machine_provider_id, app_to_use)

    app = new_machine.application
    #app.created_by = machine_request.new_machine_owner
    #app.created_by_identity = owner_ident
    app.tags = tags
    app.description = description
    app.save()

    new_machine.version = machine_request.new_machine_version
    new_machine.created_by = machine_request.new_machine_owner
    new_machine.created_by_identity = owner_ident
    new_machine.save()
    add_to_cache(new_machine)
    machine_request.new_machine = new_machine
    machine_request.end_date = timezone.now()
    machine_request.status = 'completed'
    machine_request.save()
    return machine_request
示例#26
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)
    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