def get(self, request, provider_uuid, identity_uuid, machine_id): """ Details view for specific machine (Lookup using the given provider/identity) """ user = request.user try: esh_driver = prepare_driver(request, provider_uuid, identity_uuid) except ProviderNotActive as pna: return inactive_provider(pna) except Exception as e: return failure_response(status.HTTP_409_CONFLICT, e.message) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) # TODO: Need to determine that identity_uuid is ALLOWED to # see machine_id. if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine( esh_driver, esh_machine, provider_uuid, user ) serialized_data = ProviderMachineSerializer( core_machine, request_user=request.user ).data response = Response(serialized_data) return response
def put(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides core_machine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff\ and user is not core_machine.application.created_by: logger.error('Non-staff/non-owner trying to update a machine') return failure_response( status.HTTP_401_UNAUTHORIZED, 'Only Staff and the machine Owner ' + 'are allowed to change machine info.') core_machine.application.update(data) serializer = ProviderMachineSerializer(core_machine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response( status.HTTP_400_BAD_REQUEST, serializer.errors)
def get(self, request, provider_uuid, identity_uuid, machine_id): user = request.user try: esh_driver = prepare_driver(request, provider_uuid, identity_uuid) except ProviderNotActive as pna: return inactive_provider(pna) except Exception as e: return failure_response( status.HTTP_409_CONFLICT, e.message) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) # TODO: Need to determine that identity_uuid is ALLOWED to # see machine_id. # if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not core_machine: return failure_response( status.HTTP_400_BAD_REQUEST, "Could not retrieve machine with ID = %s" % machine_id) if not core_machine.application_version.application.icon: return None app_icon = core_machine.application_version.application.icon image_name, image_ext = os.path.splitext(app_icon.name) return Response(app_icon.file)
def get(self, request, provider_uuid, identity_uuid, machine_id): user = request.user try: esh_driver = prepare_driver(request, provider_uuid, identity_uuid) except ProviderNotActive as pna: return inactive_provider(pna) except Exception as e: return failure_response(status.HTTP_409_CONFLICT, e.message) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) # TODO: Need to determine that identity_uuid is ALLOWED to # see machine_id. # if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine( esh_driver, esh_machine, provider_uuid, user ) if not core_machine: return failure_response( status.HTTP_400_BAD_REQUEST, "Could not retrieve machine with ID = %s" % machine_id ) if not core_machine.application_version.application.icon: return None app_icon = core_machine.application_version.application.icon return Response(app_icon.file)
def get(self, request, provider_uuid, identity_uuid, machine_id): """ Details view for specific machine (Lookup using the given provider/identity) """ user = request.user try: esh_driver = prepare_driver(request, provider_uuid, identity_uuid) except ProviderNotActive as pna: return inactive_provider(pna) except Exception as e: return failure_response( status.HTTP_409_CONFLICT, e.message) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) # TODO: Need to determine that identity_uuid is ALLOWED to # see machine_id. if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) serialized_data = ProviderMachineSerializer( core_machine, request_user=request.user).data response = Response(serialized_data) return response
def _update_machine(self, request, provider_uuid, identity_uuid, machine_id): # TODO: Determine who is allowed to edit machines besides # core_machine.owner user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not user.is_staff and user is not core_machine.application_version.application.created_by: logger.warn("%s is Non-staff/non-owner trying to update a machine" % (user.username)) return failure_response( status.HTTP_401_UNAUTHORIZED, "Only Staff and the machine Owner " "are allowed to change machine info." ) partial_update = True if request.method == "PATCH" else False serializer = ProviderMachineSerializer( core_machine, request_user=request.user, data=data, partial=partial_update ) if serializer.is_valid(): logger.info("metadata = %s" % data) update_machine_metadata(esh_driver, esh_machine, data) machine = serializer.save() if "created_by_identity" in request.DATA: identity = machine.created_by_identity update_application_owner(core_machine.application_version.application, identity) logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def convert_instance_source(esh_driver, esh_instance, esh_source, provider_uuid, identity_uuid, user): """ Given the instance source, create the appropriate core REPR and return """ from rtwo.models.volume import BaseVolume from rtwo.models.machine import BaseMachine # TODO: Future Release.. new_source = None if isinstance(esh_source, BaseVolume): core_source = convert_esh_volume(esh_source, provider_uuid, identity_uuid, user) elif isinstance(esh_source, BaseMachine): if isinstance(esh_source, MockMachine): # MockMachine includes only the Alias/ID information # so a lookup on the machine is required to get accurate # information. new_source = esh_driver.get_machine(esh_source.id) if not new_source: core_source = get_or_create_provider_machine( esh_source.id, "Inactive Machine for Instance %s" % esh_instance.id, provider_uuid) else: core_source = convert_esh_machine(esh_driver, new_source, provider_uuid, user) elif not isinstance(esh_source, BaseMachine): raise Exception("Encountered unknown source %s" % esh_source) return core_source
def patch(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides coreMachine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, identity_id) esh_machine = esh_driver.get_machine(machine_id) coreMachine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff and user is not coreMachine.application.created_by: logger.warn('%s is Non-staff/non-owner trying to update a machine' % (user.username)) errorObj = failureJSON([{ 'code': 401, 'message': 'Only Staff and the machine Owner ' + 'are allowed to change machine info.'}]) return Response(errorObj, status=status.HTTP_401_UNAUTHORIZED) coreMachine.application.update(request.DATA) serializer = ProviderMachineSerializer(coreMachine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides core_machine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff\ and user is not core_machine.application.created_by: logger.error('Non-staff/non-owner trying to update a machine') return failure_response( status.HTTP_401_UNAUTHORIZED, 'Only Staff and the machine Owner ' + 'are allowed to change machine info.') core_machine.application.update(data) serializer = ProviderMachineSerializer(core_machine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def convert_instance_source( esh_driver, esh_instance, esh_source, provider_uuid, identity_uuid, user ): """ Given the instance source, create the appropriate core REPR and return """ from rtwo.models.volume import BaseVolume from rtwo.models.machine import BaseMachine # TODO: Future Release.. new_source = None if isinstance(esh_source, BaseVolume): core_source = convert_esh_volume( esh_source, provider_uuid, identity_uuid, user ) elif isinstance(esh_source, BaseMachine): if isinstance(esh_source, MockMachine): # MockMachine includes only the Alias/ID information # so a lookup on the machine is required to get accurate # information. new_source = esh_driver.get_machine(esh_source.id) if not new_source: core_source = get_or_create_provider_machine( esh_source.id, "Inactive Machine for Instance %s" % esh_instance.id, provider_uuid ) else: core_source = convert_esh_machine( esh_driver, new_source, provider_uuid, user ) elif not isinstance(esh_source, BaseMachine): raise Exception("Encountered unknown source %s" % esh_source) return core_source
def list_filtered_machines(esh_driver, provider_uuid, request_user=None): esh_machine_list = esh_driver.list_machines() #logger.info("Total machines from esh:%s" % len(esh_machine_list)) #TODO: I hate this. Make this black_list on MACHINE TYPE ari/aki/eri/eki instead. - SG esh_machine_list = esh_driver.filter_machines( esh_machine_list, black_list=['eki-', 'eri-', 'aki-', 'ari-']) #logger.info("Filtered machines from esh:%s" % len(esh_machine_list)) core_machine_list = [ convert_esh_machine(esh_driver, mach, provider_uuid, request_user) for mach in esh_machine_list ] #logger.info("Core machines :%s" % len(core_machine_list)) filtered_machine_list = [ core_mach for core_mach in core_machine_list if filter_core_machine(core_mach) ] #logger.info("Filtered Core machines :%s" % len(filtered_machine_list)) sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def convert_esh_instance(esh_driver, esh_instance, provider_id, identity_id, user, token=None, password=None): """ """ instance_id = esh_instance.id ip_address = _find_esh_ip(esh_instance) esh_machine = esh_instance.machine core_instance = find_instance(instance_id) if core_instance: _update_core_instance(core_instance, ip_address, password) else: start_date = _find_esh_start_date(esh_instance) logger.debug("Instance: %s" % instance_id) if type(esh_machine) == MockMachine: # MockMachine includes only the Alias/ID information # so a lookup on the machine is required to get accurate # information. esh_machine = esh_driver.get_machine(esh_machine.id) # Ensure that core Machine exists coreMachine = convert_esh_machine(esh_driver, esh_machine, provider_id, user, image_id=esh_instance.image_id) # Use New/Existing core Machine to create core Instance core_instance = create_instance( provider_id, identity_id, instance_id, coreMachine, ip_address, esh_instance.name, user, start_date, token, password, ) # Add 'esh' object core_instance.esh = esh_instance # Confirm instance exists in a project _check_project(core_instance, user) # Update the InstanceStatusHistory # 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 type(esh_size) == MockSize: # MockSize includes only the Alias/ID information # so a lookup on the size is required to get accurate # information. esh_size = esh_driver.get_size(esh_size.id) core_size = convert_esh_size(esh_size, provider_id) # TODO: You are the mole! core_instance.update_history( esh_instance.extra["status"], core_size, esh_instance.extra.get("task") or esh_instance.extra.get("metadata", {}).get("tmp_status"), ) # Update values in core with those found in metadata. core_instance = set_instance_from_metadata(esh_driver, core_instance) return core_instance
def convert_esh_instance(esh_driver, esh_instance, provider_id, identity_id, user, token=None, password=None): """ """ #logger.debug(esh_instance.__dict__) #logger.debug(esh_instance.extra) alias = esh_instance.alias try: ip_address = esh_instance._node.public_ips[0] except IndexError: # no public ip try: ip_address = esh_instance._node.private_ips[0] except IndexError: # no private ip ip_address = '0.0.0.0' eshMachine = esh_instance.machine core_instance = find_instance(alias) if core_instance: core_instance.ip_address = ip_address if password: core_instance.password = password core_instance.save() else: if 'launchdatetime' in esh_instance.extra: create_stamp = esh_instance.extra.get('launchdatetime') elif 'launch_time' in esh_instance.extra: create_stamp = esh_instance.extra.get('launch_time') elif 'created' in esh_instance.extra: create_stamp = esh_instance.extra.get('created') else: raise Exception("Instance does not have a created timestamp. This" "should never happen. Don't cheat and assume it was created just " "now. Get the real launch time, bra.") # create_stamp is an iso 8601 timestamp string that may or may not # include microseconds start_date is a timezone-aware datetime object try: start_date = datetime.strptime(create_stamp, '%Y-%m-%dT%H:%M:%S.%fZ') except ValueError: start_date = datetime.strptime(create_stamp, '%Y-%m-%dT%H:%M:%SZ') start_date = start_date.replace(tzinfo=pytz.utc) logger.debug("Instance %s" % alias) logger.debug("CREATED: %s" % create_stamp) logger.debug("START: %s" % start_date) coreMachine = convert_esh_machine(esh_driver, eshMachine, provider_id, image_id=esh_instance.image_id) core_instance = create_instance(provider_id, identity_id, alias, coreMachine, ip_address, esh_instance.name, user, start_date, token, password) core_instance.esh = esh_instance core_instance = set_instance_from_metadata(esh_driver, core_instance) return core_instance
def list_filtered_machines(esh_driver, provider_uuid, request_user=None): esh_machine_list = esh_driver.list_machines() # TODO: I hate this. Make this black_list on # MACHINE TYPE ari/aki/eri/eki instead. - SG esh_machine_list = esh_driver.filter_machines(esh_machine_list, black_list=["eki-", "eri-", "aki-", "ari-"]) core_machine_list = [ convert_esh_machine(esh_driver, mach, provider_uuid, request_user) for mach in esh_machine_list ] filtered_machine_list = [core_mach for core_mach in core_machine_list if filter_core_machine(core_mach)] sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def get(self, request, provider_id, identity_id, machine_id): """ Lookup the machine information (Lookup using the given provider/identity) Update on server (If applicable) """ esh_driver = prepare_driver(request, identity_id) eshMachine = esh_driver.get_machine(machine_id) coreMachine = convert_esh_machine(esh_driver, eshMachine, provider_id) serialized_data = ProviderMachineSerializer(coreMachine).data response = Response(serialized_data) return response
def _update_machine( self, request, provider_uuid, identity_uuid, machine_id ): # TODO: Determine who is allowed to edit machines besides # core_machine.owner user = request.user data = request.data try: esh_driver = prepare_driver(request, provider_uuid, identity_uuid) except ProviderNotActive as pna: return inactive_provider(pna) except Exception as e: return failure_response(status.HTTP_409_CONFLICT, e.message) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine( esh_driver, esh_machine, provider_uuid, user ) if not user.is_staff and user is not core_machine.application_version.application.created_by: logger.warn( '%s is Non-staff/non-owner trying to update a machine' % (user.username) ) return failure_response( status.HTTP_401_UNAUTHORIZED, "Only Staff and the machine Owner " "are allowed to change machine info." ) partial_update = True if request.method == 'PATCH' else False serializer = ProviderMachineSerializer( core_machine, request_user=request.user, data=data, partial=partial_update ) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) machine = serializer.save() if 'created_by_identity' in request.data: identity = machine.created_by_identity update_application_owner( core_machine.application_version.application, identity ) logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def convert_esh_instance(esh_driver, esh_instance, provider_id, identity_id, user, token=None, password=None): """ """ instance_id = esh_instance.id ip_address = _find_esh_ip(esh_instance) esh_machine = esh_instance.machine core_instance = find_instance(instance_id) if core_instance: _update_core_instance(core_instance, ip_address, password) else: start_date = _find_esh_start_date(esh_instance) logger.debug("Instance: %s" % instance_id) if type(esh_machine) == MockMachine: #MockMachine includes only the Alias/ID information #so a lookup on the machine is required to get accurate #information. esh_machine = esh_driver.get_machine(esh_machine.id) #Ensure that core Machine exists coreMachine = convert_esh_machine(esh_driver, esh_machine, provider_id, user, image_id=esh_instance.image_id) #Use New/Existing core Machine to create core Instance core_instance = create_instance(provider_id, identity_id, instance_id, coreMachine, ip_address, esh_instance.name, user, start_date, token, password) #Add 'esh' object core_instance.esh = esh_instance #Confirm instance exists in a project _check_project(core_instance, user) #Update the InstanceStatusHistory #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 type(esh_size) == MockSize: #MockSize includes only the Alias/ID information #so a lookup on the size is required to get accurate #information. esh_size = esh_driver.get_size(esh_size.id) core_size = convert_esh_size(esh_size, provider_id) core_instance.update_history( esh_instance.extra['status'], core_size, esh_instance.extra.get('task')) #Update values in core with those found in metadata. core_instance = set_instance_from_metadata(esh_driver, core_instance) return core_instance
def list_filtered_machines(esh_driver, provider_id, request_user=None): esh_machine_list = esh_driver.list_machines() #logger.info("Total machines from esh:%s" % len(esh_machine_list)) esh_machine_list = esh_driver.filter_machines( esh_machine_list, black_list=['eki-', 'eri-']) #logger.info("Filtered machines from esh:%s" % len(esh_machine_list)) core_machine_list = [convert_esh_machine(esh_driver, mach, provider_id) for mach in esh_machine_list] #logger.info("Core machines :%s" % len(core_machine_list)) filtered_machine_list = [core_mach for core_mach in core_machine_list if filter_core_machine(core_mach, request_user)] #logger.info("Filtered Core machines :%s" % len(filtered_machine_list)) sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def get(self, request, provider_id, identity_id, machine_id): """ Lookup the machine information (Lookup using the given provider/identity) Update on server (If applicable) """ esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) #TODO: Need to determine that identity_id is ALLOWED to see machine_id. # if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) serialized_data = ProviderMachineSerializer(core_machine).data response = Response(serialized_data) return response
def get(self, request, provider_uuid, identity_uuid, machine_id): """ Details view for specific machine (Lookup using the given provider/identity) """ user = request.user esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) # TODO: Need to determine that identity_uuid is ALLOWED to # see machine_id. if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) serialized_data = ProviderMachineSerializer(core_machine, request_user=request.user).data response = Response(serialized_data) return response
def provider_filtered_machines(request, provider_id, identity_id): """ Return all filtered machines. Uses the most common, default filtering method. """ esh_driver = prepare_driver(request, identity_id) esh_machine_list = esh_driver.list_machines() logger.info("Total machines from esh:%s" % len(esh_machine_list)) esh_machine_list = esh_driver.filter_machines( esh_machine_list, black_list=['eki-', 'eri-']) core_machine_list = [convert_esh_machine(esh_driver, mach, provider_id) for mach in esh_machine_list] filtered_machine_list = filter(filter_core_machine, core_machine_list) sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def list_filtered_machines(esh_driver, provider_uuid, request_user=None): esh_machine_list = esh_driver.list_machines() # TODO: I hate this. Make this black_list on # MACHINE TYPE ari/aki/eri/eki instead. - SG esh_machine_list = esh_driver.filter_machines( esh_machine_list, black_list=['eki-', 'eri-', 'aki-', 'ari-']) core_machine_list = [ convert_esh_machine(esh_driver, mach, provider_uuid, request_user) for mach in esh_machine_list ] filtered_machine_list = [ core_mach for core_mach in core_machine_list if filter_core_machine(core_mach) ] sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def get(self, request, provider_uuid, identity_uuid, machine_id): """ Details view for specific machine (Lookup using the given provider/identity) """ user = request.user esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) #TODO: Need to determine that identity_uuid is ALLOWED to see machine_id. # if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) serialized_data = ProviderMachineSerializer( core_machine, request_user=request.user).data response = Response(serialized_data) return response
def get(self, request, provider_uuid, identity_uuid, machine_id): user = request.user esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) #TODO: Need to determine that identity_uuid is ALLOWED to see machine_id. # if not covered by calling as the users driver.. esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not core_machine: return failure_response( status.HTTP_400_BAD_REQUEST, "Could not retrieve machine with ID = %s" % machine_id) if not core_machine.application.icon: return None app_icon = core_machine.application.icon image_name, image_ext = os.path.splitext(app_icon.name) return Response(app_icon.file)
def list_filtered_machines(esh_driver, provider_id, request_user=None): esh_machine_list = esh_driver.list_machines() #logger.info("Total machines from esh:%s" % len(esh_machine_list)) esh_machine_list = esh_driver.filter_machines(esh_machine_list, black_list=['eki-', 'eri-']) #logger.info("Filtered machines from esh:%s" % len(esh_machine_list)) core_machine_list = [ convert_esh_machine(esh_driver, mach, provider_id, request_user) for mach in esh_machine_list ] #logger.info("Core machines :%s" % len(core_machine_list)) filtered_machine_list = [ core_mach for core_mach in core_machine_list if filter_core_machine(core_mach) ] #logger.info("Filtered Core machines :%s" % len(filtered_machine_list)) sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def convert_instance_source(esh_driver, esh_source, provider_uuid, identity_uuid, user): """ Given the instance source, create the appropriate core REPR and return """ from rtwo.volume import BaseVolume from rtwo.machine import BaseMachine #TODO: Future Release.. #if isinstance(esh_source, BaseSnapShot): # core_source = convert_esh_snapshot(esh_source, provider_uuid, identity_uuid, user) if isinstance(esh_source, BaseVolume): core_source = convert_esh_volume(esh_source, provider_uuid, identity_uuid, user) elif isinstance(esh_source, BaseMachine): if type(esh_source) == MockMachine: #MockMachine includes only the Alias/ID information #so a lookup on the machine is required to get accurate #information. esh_source = esh_driver.get_machine(esh_source.id) core_source = convert_esh_machine(esh_driver, esh_source, provider_uuid, user) elif not isinstance(esh_source, BaseMachine): raise Exception ("Encountered unknown source %s" % esh_source) return core_source
def list_filtered_machines(esh_driver, provider_uuid, request_user=None): esh_machine_list = esh_driver.list_machines() #logger.info("Total machines from esh:%s" % len(esh_machine_list)) #TODO: I hate this. Make this black_list on MACHINE TYPE ari/aki/eri/eki instead. - SG esh_machine_list = esh_driver.filter_machines( esh_machine_list, black_list=['eki-', 'eri-', 'aki-', 'ari-']) #logger.info("Filtered machines from esh:%s" % len(esh_machine_list)) core_machine_list = [convert_esh_machine(esh_driver, mach, provider_uuid, request_user) for mach in esh_machine_list] #logger.info("Core machines :%s" % len(core_machine_list)) filtered_machine_list = [core_mach for core_mach in core_machine_list if filter_core_machine(core_mach)] #logger.info("Filtered Core machines :%s" % len(filtered_machine_list)) sorted_machine_list = sorted(filtered_machine_list, cmp=compare_core_machines) return sorted_machine_list
def convert_esh_instance(esh_driver, esh_instance, provider_id, identity_id, user, token=None, password=None): """ """ #logger.debug(esh_instance.__dict__) #logger.debug(esh_instance.extra) try: ip_address = esh_instance._node.public_ips[0] except IndexError: # no public ip try: ip_address = esh_instance._node.private_ips[0] except IndexError: # no private ip ip_address = '0.0.0.0' eshMachine = esh_instance.machine instance_id = esh_instance.id core_instance = find_instance(instance_id) if core_instance: core_instance.ip_address = ip_address if password: core_instance.password = password core_instance.save() else: if 'launchdatetime' in esh_instance.extra: create_stamp = esh_instance.extra.get('launchdatetime') elif 'launch_time' in esh_instance.extra: create_stamp = esh_instance.extra.get('launch_time') elif 'created' in esh_instance.extra: create_stamp = esh_instance.extra.get('created') else: raise Exception( "Instance does not have a created timestamp. This" "should never happen. Don't cheat and assume it was created just " "now. Get the real launch time, bra.") # create_stamp is an iso 8601 timestamp string that may or may not # include microseconds start_date is a timezone-aware datetime object try: start_date = datetime.strptime(create_stamp, '%Y-%m-%dT%H:%M:%S.%fZ') except ValueError: start_date = datetime.strptime(create_stamp, '%Y-%m-%dT%H:%M:%SZ') start_date = start_date.replace(tzinfo=pytz.utc) logger.debug("Instance %s" % instance_id) logger.debug("CREATED: %s" % create_stamp) logger.debug("START: %s" % start_date) coreMachine = convert_esh_machine(esh_driver, eshMachine, provider_id, image_id=esh_instance.image_id) core_instance = create_instance(provider_id, identity_id, instance_id, coreMachine, ip_address, esh_instance.name, user, start_date, token, password) core_instance.esh = esh_instance core_instance = set_instance_from_metadata(esh_driver, core_instance) return core_instance