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
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 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) 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)
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 _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 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)
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
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
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)