Exemplo n.º 1
0
 def put(self, request, provider_uuid, identity_uuid, instance_id):
     """Authentication Required, update metadata about the instance"""
     user = request.user
     data = request.data
     # Ensure item exists on the server first
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     if not can_use_instance(user, instance_id, leader_required=True):
         return member_action_forbidden(user.username, instance_id)
     try:
         esh_instance = esh_driver.get_instance(instance_id)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except LibcloudInvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
     if not esh_instance:
         return instance_not_found(instance_id)
     # Gather the DB related item and update
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_uuid, identity_uuid,
                                          user)
     serializer = InstanceSerializer(core_instance,
                                     data=data,
                                     context={"request": request})
     identity = Identity.objects.get(uuid=identity_uuid)
     if serializer.is_valid():
         logger.info('metadata = %s' % data)
         #NOTE: We shouldn't allow 'full replacement' of metadata..
         # We should also validate against potentional updating of 'atmo-used metadata'
         update_metadata.s(esh_driver.__class__,
                           esh_driver.provider,
                           esh_driver.identity,
                           esh_instance.id,
                           data,
                           replace_metadata=False).apply()
         new_instance = serializer.save()
         boot_scripts = data.pop('boot_scripts', [])
         if boot_scripts:
             new_instance = _save_scripts_to_instance(
                 new_instance, boot_scripts)
             serializer = InstanceSerializer(new_instance,
                                             context={"request": request})
         invalidate_cached_instances(identity=identity)
         response = Response(serializer.data)
         logger.info('data = %s' % serializer.data)
         response['Cache-Control'] = 'no-cache'
         return response
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def put(self, request, provider_uuid, identity_uuid, instance_id):
     """Authentication Required, update metadata about the instance"""
     user = request.user
     data = request.DATA
     # Ensure item exists on the server first
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     try:
         esh_instance = esh_driver.get_instance(instance_id)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except InvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT,
                                 str(exc.message))
     if not esh_instance:
         return instance_not_found(instance_id)
     # Gather the DB related item and update
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_uuid, identity_uuid,
                                          user)
     serializer = InstanceSerializer(core_instance, data=data,
                                     context={"request": request})
     if serializer.is_valid():
         logger.info('metadata = %s' % data)
         update_instance_metadata(esh_driver, esh_instance, data)
         serializer.save()
         new_instance = serializer.object
         boot_scripts = data.pop('boot_scripts', [])
         if boot_scripts:
             new_instance = _save_scripts_to_instance(new_instance,
                                                      boot_scripts)
             serializer = InstanceSerializer(
                 new_instance,
                 context={"request": request})
         invalidate_cached_instances(
                 identity=Identity.objects.get(
                     uuid=identity_uuid))
         response = Response(serializer.data)
         logger.info('data = %s' % serializer.data)
         response['Cache-Control'] = 'no-cache'
         return response
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def post(self, request, provider_uuid, identity_uuid, volume_id=None):
        user = request.user
        data = request.data

        missing_keys = valid_launch_data(data)
        if missing_keys:
            return keys_not_found(missing_keys)
        source = None
        name = data.pop('name')
        size_id = data.pop('size')
        key_name = self._select_source_key(data)
        if not key_name:
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                'Source could not be acquired. Did you send: ['
                'snapshot_id/volume_id/image_id] ?')
        try:
            core_instance = create_bootable_volume(
                request.user, provider_uuid, identity_uuid,
                name, size_id, volume_id, source_hint=key_name,
                **data)
        except Exception as exc:
            message = exc.message
            return failure_response(
                status.HTTP_409_CONFLICT,
                message)

        serialized_data = InstanceSerializer(core_instance,
                                             context={'request': request}).data
        response = Response(serialized_data)
        return response
Exemplo n.º 4
0
 def get(self, request, provider_uuid, identity_uuid):
     """
     Returns a list of all instances
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     identity = Identity.objects.get(uuid=identity_uuid)
     try:
         esh_instance_list = get_cached_instances(identity=identity)
     except MalformedResponseError:
         return malformed_response(provider_uuid, identity_uuid)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except InvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     core_instance_list = [convert_esh_instance(esh_driver,
                                                inst,
                                                provider_uuid,
                                                identity_uuid,
                                                user)
                           for inst in esh_instance_list]
     # TODO: Core/Auth checks for shared instances
     serialized_data = InstanceSerializer(core_instance_list,
                                          context={"request": request},
                                          many=True).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
Exemplo n.º 5
0
    def get(self, request, provider_uuid, identity_uuid, instance_id):
        """
        Authentication Required, get instance details.
        """
        user = request.user
        # NOTE: This 'Scheme' should be used across
        #       the ENTIRE API v1 (Machines, Volumes, Sizes)
        # NOTE: Especially the part below, where you end date
        #       all the things that are 'inactive'
        try:
            provider = Provider.objects.get(uuid=provider_uuid)
            if not provider.is_active():
                raise ProviderNotActive(provider)
        except Provider.DoesNotExist:
            return invalid_creds(provider_uuid, identity_uuid)
        except ProviderNotActive as pna:
            return inactive_provider(pna)

        # Cleared provider testing -- ready for driver prep.
        try:
            esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
            if not esh_driver:
                return invalid_creds(provider_uuid, identity_uuid)
            logger.info("InstanceQuery Looking for %s" % instance_id)
            esh_instance = esh_driver.get_instance(instance_id)
            logger.info("InstanceQuery Found instance %s" % esh_instance)
        except (socket_error, ConnectionFailure):
            logger.exception("Connection failure prevented InstanceQuery")
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            logger.exception("Invalid credentialsprevented InstanceQuery")
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))

        # NOTE: Especially THIS part below, where you end date all the
        #       things that are 'inactive'
        if not esh_instance:
            try:
                core_inst = CoreInstance.objects.get(
                    provider_alias=instance_id,
                    source__provider__uuid=provider_uuid,
                    created_by_identity__uuid=identity_uuid)
                core_inst.end_date_all()
            except CoreInstance.DoesNotExist:
                pass
            return instance_not_found(instance_id)

        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_uuid, identity_uuid,
                                             user)
        serialized_data = InstanceSerializer(core_instance,
                                             context={
                                                 "request": request
                                             }).data
        response = Response(serialized_data)
        response['Cache-Control'] = 'no-cache'
        return response
Exemplo n.º 6
0
 def get(self, request, project_uuid):
     user = request.user
     group = get_user_group(user.username)
     project = get_group_project(group, project_uuid)
     if not project:
         return Response(
             "Project with ID=%s does not "
             "exist" % project_uuid,
             status=status.HTTP_400_BAD_REQUEST
         )
     instances = project.instances.filter(
         only_current(), provider_machine__provider__active=True
     )
     active_provider_uuids = [ap.uuid for ap in Provider.get_active()]
     instances = instances.filter(
         pk__in=[
             i.id for i in instances
             if i.instance.provider_uuid() in active_provider_uuids
         ]
     )
     serialized_data = InstanceSerializer(
         instances, many=True, context={
             "request": request
         }
     ).data
     response = Response(serialized_data)
     return response
Exemplo n.º 7
0
 def get(self, request):
     user = request.user
     instances = user.instance_set.filter(
         only_current(),
         source__provider_machine__provider__active=True,
         projects=None)
     serialized_data = InstanceSerializer(
         instances, many=True,
         context={"request": request}).data
     response = Response(serialized_data)
     return response
Exemplo n.º 8
0
    def patch(self, request, provider_uuid, identity_uuid, instance_id):
        """Authentication Required, update metadata about the instance"""
        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)
        try:
            esh_instance = esh_driver.get_instance(instance_id)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
        if not esh_instance:
            return instance_not_found(instance_id)
        # Gather the DB related item and update
        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_uuid, identity_uuid,
                                             user)
        serializer = InstanceSerializer(core_instance,
                                        data=data,
                                        context={"request": request},
                                        partial=True)
        identity = Identity.objects.get(uuid=identity_uuid)
        provider = identity.provider

        if serializer.is_valid():
            logger.info('metadata = %s' % data)

            driver_class = esh_driver.__class__
            update_metadata.s(driver_class,
                              provider,
                              identity,
                              esh_instance.id,
                              data,
                              replace_metadata=False).apply()
            instance = serializer.save()
            boot_scripts = data.pop('boot_scripts', [])
            if boot_scripts:
                _save_scripts_to_instance(instance, boot_scripts)
            invalidate_cached_instances(identity=identity)
            response = Response(serializer.data)
            logger.info('data = %s' % serializer.data)
            response['Cache-Control'] = 'no-cache'
            return response
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 9
0
 def get(self, request, provider_uuid, identity_uuid):
     """
     Returns a list of all instances
     """
     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)
     identity = Identity.objects.get(uuid=identity_uuid)
     # Probably redundant
     if not user.can_use_identity(identity.id):
         return invalid_creds(provider_uuid, identity_uuid)
     try:
         esh_instance_list = get_cached_instances(identity=identity)
     except LibcloudBadResponseError:
         return malformed_response(provider_uuid, identity_uuid)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except LibcloudInvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     core_instance_list = [
         convert_esh_instance(esh_driver, inst, provider_uuid,
                              identity_uuid, user)
         for inst in esh_instance_list
     ]
     # TODO: Core/Auth checks for shared instances
     serialized_data = InstanceSerializer(core_instance_list,
                                          context={
                                              "request": request
                                          },
                                          many=True).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
Exemplo n.º 10
0
 def get(self, request, provider_uuid, identity_uuid, instance_id):
     """
     Authentication Required, get instance details.
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     try:
         esh_instance = esh_driver.get_instance(instance_id)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except InvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT,
                                 str(exc.message))
     if not esh_instance:
         try:
             core_inst = CoreInstance.objects.get(
                 provider_alias=instance_id,
                 source__provider__uuid=provider_uuid,
                 created_by_identity__uuid=identity_uuid)
             core_inst.end_date_all()
         except CoreInstance.DoesNotExist:
             pass
         return instance_not_found(instance_id)
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_uuid, identity_uuid,
                                          user)
     serialized_data = InstanceSerializer(
         core_instance,
         context={"request": request}).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
Exemplo n.º 11
0
    def delete(self, request, provider_uuid, identity_uuid, instance_id):
        """Authentication Required, TERMINATE the instance.

        Be careful, there is no going back once you've deleted an instance.
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        try:
            esh_instance = esh_driver.get_instance(instance_id)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
        try:
            # Test that there is not an attached volume BEFORE we destroy
            task.destroy_instance_task(user, esh_instance, identity_uuid)

            invalidate_cached_instances(identity=Identity.objects.get(
                uuid=identity_uuid))

            existing_instance = esh_driver.get_instance(instance_id)
            if existing_instance:
                # Instance will be deleted soon...
                esh_instance = existing_instance
                if esh_instance.extra\
                   and 'task' not in esh_instance.extra:
                    esh_instance.extra['task'] = 'queueing delete'
        except VolumeAttachConflict as exc:
            message = exc.message
            return failure_response(status.HTTP_409_CONFLICT, message)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except InstanceDoesNotExist as dne:
            return failure_response(
                status.HTTP_404_NOT_FOUND,
                'Instance %s no longer exists' % (dne.message, ))
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))

        try:
            core_instance = convert_esh_instance(esh_driver, esh_instance,
                                                 provider_uuid, identity_uuid,
                                                 user)
            if core_instance:
                core_instance.end_date_all()
            else:
                logger.warn("Unable to find core instance %s." % (instance_id))
            serialized_data = InstanceSerializer(core_instance,
                                                 context={
                                                     "request": request
                                                 }).data
            response = Response(serialized_data, status=status.HTTP_200_OK)
            response['Cache-Control'] = 'no-cache'
            return response
        except (Identity.DoesNotExist) as exc:
            return failure_response(status.HTTP_400_BAD_REQUEST,
                                    "Invalid provider_uuid or identity_uuid.")
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
Exemplo n.º 12
0
    def post(self, request, provider_uuid, identity_uuid, format=None):
        """
        Instance Class:
        Launches an instance based on the params
        Returns a single instance

        Parameters: machine_alias, size_alias, username

        TODO: Create a 'reverse' using the instance-id to pass
        the URL for the newly created instance
        I.e: url = "/provider/1/instance/1/i-12345678"
        """
        data = request.data
        user = request.user
        # Check the data is valid
        missing_keys = valid_post_data(data)
        if missing_keys:
            return keys_not_found(missing_keys)
        # Pass these as args
        size_alias = data.pop("size_alias")
        allocation_source_id = data.pop("allocation_source_id", None)
        machine_alias = data.pop("machine_alias")
        hypervisor_name = data.pop("hypervisor", None)
        if hypervisor_name:
            # Previous method passed this with 'None' but that fails now.
            # This check will only add the ex_ value if it is 'truthy'.
            data['ex_hypervisor_name'] = hypervisor_name
        deploy = data.pop("deploy", True)
        if type(deploy) in [str, unicode] and deploy.lower() == "false":
            deploy = False
        elif not isinstance(deploy, bool):
            deploy = True
        boot_scripts = data.pop("scripts", [])
        try:
            logger.debug(data)
            if not settings.USE_ALLOCATION_SOURCE:
                allocation_source = None
            else:
                allocation_source = AllocationSource.objects.get(
                    source_id=allocation_source_id)
            core_instance = launch_instance(user,
                                            identity_uuid,
                                            size_alias,
                                            machine_alias,
                                            deploy=deploy,
                                            **data)
        except UnderThresholdError as ute:
            return under_threshold(ute)
        except OverQuotaError as oqe:
            return over_quota(oqe)
        except OverAllocationError as oae:
            return over_quota(oae)
        except SizeNotAvailable as snae:
            return size_not_available(snae)
        except SecurityGroupNotCreated:
            return connection_failure(provider_uuid, identity_uuid)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except LibcloudInvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT, str(exc.message))

        serializer = InstanceSerializer(core_instance,
                                        context={"request": request},
                                        data=data)
        if serializer.is_valid():
            instance = serializer.save()
            if boot_scripts:
                _save_scripts_to_instance(instance, boot_scripts)
            instance.change_allocation_source(allocation_source)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 13
0
        except SizeNotAvailable, snae:
            return size_not_availabe(snae)
        except SecurityGroupNotCreated:
            return connection_failure(provider_uuid, identity_uuid)
        except (socket_error, ConnectionFailure):
            return connection_failure(provider_uuid, identity_uuid)
        except InvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        except Exception as exc:
            logger.exception("Encountered a generic exception. "
                             "Returning 409-CONFLICT")
            return failure_response(status.HTTP_409_CONFLICT,
                                    str(exc.message))

        serializer = InstanceSerializer(core_instance,
                                        context={"request": request},
                                        data=data)
        if serializer.is_valid():
            serializer.save()
            if boot_scripts:
                _save_scripts_to_instance(serializer.object, boot_scripts)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)


def _sort_instance_history(history_instance_list, sort_by, descending=False):
    # Using the 'sort_by' variable, sort the list:
    if not sort_by or 'end_date' in sort_by:
        return sorted(history_instance_list, key=lambda ish: