Esempio n. 1
0
def _execute_provider_action(identity, user, instance, action_name):
    driver = get_cached_driver(identity=identity)
    try:
        if not action_name:
            logger.debug("No 'action_name' provided")
            return
        elif action_name == 'Suspend':
            suspend_instance(driver, instance, identity.provider.uuid,
                             identity.uuid, user)
        elif action_name == 'Stop':
            stop_instance(driver, instance, identity.provider.uuid,
                          identity.uuid, user)
        elif action_name == 'Shelve':
            shelve_instance(driver, instance, identity.provider.uuid,
                            identity.uuid, user)
        elif action_name == 'Shelve Offload':
            offload_instance(driver, instance, identity.provider.uuid,
                             identity.uuid, user)
        elif action_name == 'Terminate':
            destroy_instance(identity.uuid, instance)
        else:
            raise Exception("Encountered Unknown Action Named %s" % action)
    except ObjectDoesNotExist:
        # This may be unreachable when null,blank = True
        logger.debug("Provider %s - 'Do Nothing' for Over Allocation" %
                     provider)
        return
Esempio n. 2
0
def _execute_provider_action(identity, user, instance, action_name):
    driver = get_cached_driver(identity=identity)

    # NOTE: This if statement is a HACK! It will be removed when IP management is enabled in an upcoming version. -SG
    reclaim_ip = True if identity.provider.location != 'iPlant Cloud - Tucson' else False
    # ENDNOTE

    # NOTE: This metadata statement is a HACK! It should be removed when all instances matching this metadata key have been removed.
    instance_has_home_mount = instance.extra['metadata'].get('atmosphere_ephemeral_home_mount', 'false').lower()
    if instance_has_home_mount == 'true' and action_name == 'Shelve':
        logger.info("Instance %s will be suspended instead of shelved, because the ephemeral storage is in /home" % instance.id)
        action_name = 'Suspend'


    logger.info("User %s has gone over their allocation on Instance %s - Enforcement Choice: %s" % (user, instance, action_name))
    try:
        if not action_name:
            logger.debug("No 'action_name' provided")
            return
        elif action_name == 'Suspend':
            suspend_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user,
                reclaim_ip)
        elif action_name == 'Stop':
            stop_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user,
                reclaim_ip)
        elif action_name == 'Shelve':
            shelve_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user,
                reclaim_ip)
        elif action_name == 'Shelve Offload':
            offload_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user,
                reclaim_ip)
        elif action_name == 'Terminate':
            destroy_instance(user, identity.uuid, instance.id)
        else:
            raise Exception("Encountered Unknown Action Named %s" % action_name)
    except ObjectDoesNotExist:
        # This may be unreachable when null,blank = True
        logger.debug("Provider %s - 'Do Nothing' for Over Allocation" % identity.provider)
        return
Esempio n. 3
0
 def perform_destroy(self, instance):
     user = self.request.user
     identity_uuid = instance.created_by_identity.uuid
     identity = Identity.objects.get(uuid=identity_uuid)
     try:
         # Test that there is not an attached volume BEFORE we destroy
         #NOTE: Although this is a task we are calling and waiting for response..
         core_instance = destroy_instance(user, identity_uuid,
                                          instance.provider_alias)
         serialized_instance = InstanceSerializer(
             core_instance,
             context={'request': self.request},
             data={},
             partial=True)
         if not serialized_instance.is_valid():
             return Response(serialized_instance.data,
                             status=status.HTTP_400_BAD_REQUEST)
         return Response(status=status.HTTP_204_NO_CONTENT)
     except VolumeAttachConflict as exc:
         message = exc.message
         return failure_response(status.HTTP_409_CONFLICT, message)
     except (socket_error, ConnectionFailure):
         return connection_failure(identity)
     except LibcloudInvalidCredsError:
         return invalid_creds(identity)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT, str(exc.message))
Esempio n. 4
0
 def perform_destroy(self, instance):
     user = self.request.user
     identity_uuid = instance.created_by_identity.uuid
     identity = Identity.objects.get(id=identity_uuid)
     try:
         # Test that there is not an attached volume BEFORE we destroy
         #NOTE: Although this is a task we are calling and waiting for response..
         core_instance = destroy_instance(
             user,
             identity_uuid,
             instance.provider_alias)
         serialized_instance = InstanceSerializer(
             core_instance, context={
                 'request': self.request},
             data={}, partial=True)
         if not serialized_instance.is_valid():
             return Response(serialized_instance.data,
                             status=status.HTTP_400_BAD_REQUEST)
         return Response(status=status.HTTP_204_NO_CONTENT)
     except VolumeAttachConflict as exc:
         message = exc.message
         return failure_response(status.HTTP_409_CONFLICT, message)
     except (socket_error, ConnectionFailure):
         return connection_failure(identity)
     except InvalidCredsError:
         return invalid_creds(identity)
     except Exception as exc:
         logger.exception("Encountered a generic exception. "
                          "Returning 409-CONFLICT")
         return failure_response(status.HTTP_409_CONFLICT,
                                 str(exc.message))
Esempio n. 5
0
def _execute_provider_action(identity, user, instance, action_name):
    driver = get_cached_driver(identity=identity)

    # NOTE: This if statement is a HACK! It will be removed when IP management is enabled in an upcoming version. -SG
    reclaim_ip = True if identity.provider.location != 'iPlant Cloud - Tucson' else False
    # ENDNOTE

    # NOTE: This metadata statement is a HACK! It should be removed when all instances matching this metadata key have been removed.
    instance_has_home_mount = instance.extra['metadata'].get(
        'atmosphere_ephemeral_home_mount', 'false').lower()
    if instance_has_home_mount == 'true' and action_name == 'Shelve':
        logger.info(
            "Instance %s will be suspended instead of shelved, because the ephemeral storage is in /home"
            % instance.id)
        action_name = 'Suspend'

    logger.info(
        "User %s has gone over their allocation on Instance %s - Enforcement Choice: %s"
        % (user, instance, action_name))
    try:
        if not action_name:
            logger.debug("No 'action_name' provided")
            return
        elif action_name == 'Suspend':
            suspend_instance(driver, instance, identity.provider.uuid,
                             identity.uuid, user, reclaim_ip)
        elif action_name == 'Stop':
            stop_instance(driver, instance, identity.provider.uuid,
                          identity.uuid, user, reclaim_ip)
        elif action_name == 'Shelve':
            shelve_instance(driver, instance, identity.provider.uuid,
                            identity.uuid, user, reclaim_ip)
        elif action_name == 'Shelve Offload':
            offload_instance(driver, instance, identity.provider.uuid,
                             identity.uuid, user, reclaim_ip)
        elif action_name == 'Terminate':
            destroy_instance(user, identity.uuid, instance.id)
        else:
            raise Exception("Encountered Unknown Action Named %s" %
                            action_name)
    except ObjectDoesNotExist:
        # This may be unreachable when null,blank = True
        logger.debug("Provider %s - 'Do Nothing' for Over Allocation" %
                     identity.provider)
        return
Esempio n. 6
0
def _execute_provider_action(identity, user, instance, action_name):
    driver = get_cached_driver(identity=identity)
    try:
        if not action_name:
            logger.debug("No 'action_name' provided")
            return
        elif action_name == 'Suspend':
            suspend_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user)
        elif action_name == 'Stop':
            stop_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user)
        elif action_name == 'Shelve':
            shelve_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user)
        elif action_name == 'Shelve Offload':
            offload_instance(
                driver,
                instance,
                identity.provider.uuid,
                identity.uuid,
                user)
        elif action_name == 'Terminate':
            destroy_instance(user, identity.uuid, instance)
        else:
            raise Exception("Encountered Unknown Action Named %s" % action)
    except ObjectDoesNotExist:
        # This may be unreachable when null,blank = True
        logger.debug(
            "Provider %s - 'Do Nothing' for Over Allocation" %
            provider)
        return
Esempio n. 7
0
def destroy_instance(instance_alias, core_identity_uuid):
    from service import instance as instance_service
    from rtwo.driver import OSDriver
    try:
        logger.debug("destroy_instance task started at %s." % datetime.now())
        node_destroyed = instance_service.destroy_instance(
            core_identity_uuid, instance_alias)
        core_identity = Identity.objects.get(uuid=core_identity_uuid)
        driver = get_esh_driver(core_identity)
        if isinstance(driver, OSDriver):
            #Spawn off the last two tasks
            logger.debug("OSDriver Logic -- Remove floating ips and check"
                         " for empty project")
            driverCls = driver.__class__
            provider = driver.provider
            identity = driver.identity
            instances = driver.list_instances()
            active = [driver._is_active_instance(inst) for inst in instances]
            if not active:
                logger.debug("Driver shows 0 of %s instances are active" %
                             (len(instances), ))
                #For testing ONLY.. Test cases ignore countdown..
                if app.conf.CELERY_ALWAYS_EAGER:
                    logger.debug("Eager task waiting 1 minute")
                    time.sleep(60)
                clean_task = clean_empty_ips.si(driverCls,
                                                provider,
                                                identity,
                                                immutable=True,
                                                countdown=5)
                remove_task = remove_empty_network.si(driverCls,
                                                      provider,
                                                      identity,
                                                      core_identity_uuid,
                                                      immutable=True,
                                                      countdown=60)
                clean_task.join(remove_task)
                clean_task.apply_async()
            else:
                logger.debug("Driver shows %s of %s instances are active" %
                             (len(active), len(instances)))
                #For testing ONLY.. Test cases ignore countdown..
                if app.conf.CELERY_ALWAYS_EAGER:
                    logger.debug("Eager task waiting 15 seconds")
                    time.sleep(15)
                destroy_chain = clean_empty_ips.si(driverCls,
                                                   provider,
                                                   identity,
                                                   immutable=True,
                                                   countdown=5)
                destroy_chain.apply_async()
        logger.debug("destroy_instance task finished at %s." % datetime.now())
        return node_destroyed
    except Exception as exc:
        logger.exception(exc)
        destroy_instance.retry(exc=exc)
Esempio n. 8
0
def destroy_instance(instance_alias, core_identity_id):
    from service import instance as instance_service
    from rtwo.driver import OSDriver
    from api import get_esh_driver
    try:
        logger.debug("destroy_instance task started at %s." % datetime.now())
        node_destroyed = instance_service.destroy_instance(
            core_identity_id, instance_alias)
        core_identity = Identity.objects.get(id=core_identity_id)
        driver = get_esh_driver(core_identity)
        if isinstance(driver, OSDriver):
            #Spawn off the last two tasks
            logger.debug("OSDriver Logic -- Remove floating ips and check"
                         " for empty project")
            driverCls = driver.__class__
            provider = driver.provider
            identity = driver.identity
            instances = driver.list_instances()
            active = [driver._is_active_instance(inst) for inst in instances]
            if not active:
                logger.debug("Driver shows 0 of %s instances are active"
                             % (len(instances),))
                #For testing ONLY.. Test cases ignore countdown..
                if app.conf.CELERY_ALWAYS_EAGER:
                    logger.debug("Eager task waiting 1 minute")
                    time.sleep(60)
                destroy_chain = chain(
                    clean_empty_ips.subtask(
                        (driverCls, provider, identity),
                        immutable=True, countdown=5),
                    remove_empty_network.subtask(
                        (driverCls, provider, identity, core_identity_id),
                        immutable=True, countdown=60))
                destroy_chain()
            else:
                logger.debug("Driver shows %s of %s instances are active"
                             % (len(active), len(instances)))
                #For testing ONLY.. Test cases ignore countdown..
                if app.conf.CELERY_ALWAYS_EAGER:
                    logger.debug("Eager task waiting 15 seconds")
                    time.sleep(15)
                destroy_chain = \
                    clean_empty_ips.subtask(
                        (driverCls, provider, identity),
                        immutable=True, countdown=5).apply_async()
        logger.debug("destroy_instance task finished at %s." % datetime.now())
        return node_destroyed
    except Exception as exc:
        logger.exception(exc)
        destroy_instance.retry(exc=exc)
Esempio n. 9
0
def _execute_provider_action(identity, user, instance, action_name):
    driver = get_cached_driver(identity=identity)

    # NOTE: This if statement is a HACK! It will be removed when IP management is enabled in an upcoming version. -SG
    reclaim_ip = True if identity.provider.location != 'iPlant Cloud - Tucson' else False
    # ENDNOTE

    logger.info(
        "User %s has gone over their allocation on Instance %s - Enforcement Choice: %s"
        % (user, instance, action_name))
    try:
        if not action_name:
            logger.debug("No 'action_name' provided")
            return
        elif action_name == 'Suspend':
            suspend_instance(driver, instance, identity.provider.uuid,
                             identity.uuid, user, reclaim_ip)
        elif action_name == 'Stop':
            stop_instance(driver, instance, identity.provider.uuid,
                          identity.uuid, user, reclaim_ip)
        elif action_name == 'Shelve':
            shelve_instance(driver, instance, identity.provider.uuid,
                            identity.uuid, user, reclaim_ip)
        elif action_name == 'Shelve Offload':
            offload_instance(driver, instance, identity.provider.uuid,
                             identity.uuid, user, reclaim_ip)
        elif action_name == 'Terminate':
            destroy_instance(user, identity.uuid, instance.id)
        else:
            raise Exception("Encountered Unknown Action Named %s" %
                            action_name)
    except ObjectDoesNotExist:
        # This may be unreachable when null,blank = True
        logger.debug("Provider %s - 'Do Nothing' for Over Allocation" %
                     identity.provider)
        return