Esempio n. 1
0
def delete_openstack_gnocchi_metrics(self, project_id):
    del self  # unused

    project = Project.with_admin_session(project_id)
    api_session = project.api_session
    GnocchiMetrics(api_session=api_session).delete_resources(
        project_id=project.project_id)
Esempio n. 2
0
def delete_openstack_network(self,
                             network_id,
                             region,
                             project_id,
                             user_id: Optional[int] = None):
    del self  # unused

    db_network = OpenstackNetwork.objects.filter(id=network_id).first()
    user = AppUser.objects.filter(id=user_id)

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    Network(network_id=network_id,
            api_session=project.api_session).delete(region=region)

    if user and db_network:
        if user.is_staff:
            staff_signals.staff_delete_network.send(
                sender=__name__,
                user=user,
                user_id=user.id,
                network_name=db_network.name,
                network_id=db_network.id,
                username=user.username)
        else:
            user_signals.user_delete_network.send(sender=__name__,
                                                  user=user,
                                                  user_id=user.id,
                                                  network_name=db_network.name,
                                                  network_id=db_network.id,
                                                  username=user.username)
Esempio n. 3
0
def delete_openstack_project_security_groups(self, instances_list, project_id):
    del self  # unused

    project = Project.with_admin_session(project_id)
    api_session = project.api_session
    delete_project_security_groups(api_session=api_session,
                                   project_id=project.project_id,
                                   instances_list=instances_list)
Esempio n. 4
0
def delete_openstack_project(self, project_id, mark_deleted=False):
    del self  # unused
    # we never delete the project in our db when terminating service, mark_deleted indicates that
    if mark_deleted:
        db_project = OpenstackProject.objects.filter(
            project_id=project_id).first()
        if db_project:
            db_project.deleted = True
            db_project.save()
    project = Project.with_admin_session(project_id)
    project.delete()
Esempio n. 5
0
 def perform_update(self, serializer):
     db_project = Project.objects.get(id=serializer.validated_data['id'])
     openstack_project = OpenstackProject.with_admin_session(
         db_project.project_id)
     try:
         openstack_project.update(
             name=serializer.validated_data['name'],
             description=serializer.validated_data['description'],
             enabled=not serializer.validated_data['disabled'])
     except NotFound as e:
         raise ObjectNotFound(detail=e)
Esempio n. 6
0
    def delete_project(self, request, pk):
        del pk  # unused

        if staff_active_features.is_enabled('demo'):
            raise ForbiddenException(
                detail=_('Operation not allowed in demo mode'))

        delete_all_resources = request.data.get('delete_all_resources', False)
        instance = self.get_object()
        if delete_all_resources:
            tasks.delete_client_project_resources.delay(
                project_id=instance.project_id, mark_project_as_deleted=False)
            return Response(status=200,
                            data={'details': _('Project delete scheduled')})
        else:
            project = OpenstackProject.with_admin_session(instance.project_id)
            project.delete()
            return Response(status=200, data={'details': _('Project deleted')})
Esempio n. 7
0
def suspend_project(self, project_id, **kwargs):
    del self  # unused

    reason = kwargs.get('reason')
    try:
        project = Project.with_admin_session(project_id)
    except ConfigException:
        LOG.info(
            'Cannot suspend project as openstack settings are missing/incorrect'
        )
        return
    project.disable(reason=reason)

    task_list = list()

    for instance in project.instances.filter(terminated_at__isnull=True):
        task_list.append(stop_instance.s(instance.pk, True))

    celery.group(task_list).apply_async()
Esempio n. 8
0
def resume_project(self, project_id, **kwargs):
    del self, kwargs  # unused

    try:
        project = Project.with_admin_session(project_id)
    except ConfigException:
        LOG.info(
            'Cannot resume project as openstack settings are missing/incorrect'
        )
        return
    project.enable()

    task_list = list()

    for instance in project.instances.filter(terminated_at__isnull=True,
                                             stopped_by_fleio=True):
        # start only those instances stopped by fleio, if client stopped instance, do not start it
        task_list.append(start_instance.s(instance.pk, True))

    celery.group(task_list).apply_async()
Esempio n. 9
0
def delete_openstack_instance(self,
                              instance_id,
                              region,
                              project_id,
                              user_id: Optional[int] = None):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    nc = nova_client(api_session=project.api_session,
                     region_name=region,
                     extensions=True)
    try:
        user = AppUser.objects.filter(id=user_id).first()
        db_instance = ModelInstance.objects.filter(id=instance_id).first()
        nc.servers.delete(server=instance_id)
        if user and db_instance:
            if user.is_staff:
                staff_signals.staff_delete_instance.send(
                    sender=__name__,
                    user=user,
                    user_id=user.id,
                    instance_name=db_instance.name,
                    instance_id=instance_id,
                    username=user.username)
            else:
                user_signals.user_delete_instance.send(
                    sender=__name__,
                    user=user,
                    user_id=user.id,
                    instance_name=db_instance.name,
                    instance_id=instance_id,
                    username=user.username)
    except nova_exceptions.NotFound:
        ModelInstance.objects.filter(id=instance_id).delete()
    else:
        Operation.objects.create(operation_type=InstanceDeletion.name,
                                 primary_object_id=instance_id,
                                 params=json.dumps({
                                     'region': region,
                                 }))
Esempio n. 10
0
    def serialize(self, data, region, timestamp):
        try:
            openstack_project_id = data['resource_info']
            openstack_project = OpenstackProject.with_admin_session(project_id=openstack_project_id).api_project
            db_project = Project.objects.filter(project_id=openstack_project_id).first()
            project_id = RandomId('openstack.Project')() if db_project is None else db_project.id
            service_id = None if db_project is None or db_project.service is None else db_project.service.id
            project_data = {
                'id': project_id,
                'service': service_id,
                'project_id': openstack_project_id,
                'project_domain_id': openstack_project.domain_id,
                'disabled': not openstack_project.enabled,
                'name': openstack_project.name if openstack_project.name else None,
                'description': openstack_project.description if openstack_project.description else None,
                'is_domain': openstack_project.is_domain,
                self.version_field: self.get_version(timestamp)
            }

            return project_data
        except Exception as e:
            LOG.debug('{} exception when attempting to serialize project'.format(e))
Esempio n. 11
0
def delete_openstack_image(self,
                           image_id,
                           region,
                           project_id,
                           user_id: Optional[int] = None):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    gc = glance_client(api_session=project.api_session, region_name=region)
    try:
        db_image = ModelImage.objects.filter(id=image_id).first()
        if db_image.protected:
            gc.images.update(image_id=image_id, protected=False)
        gc.images.delete(image_id=image_id)

        user = AppUser.objects.filter(id=user_id).first()

        if user and db_image:
            if user.is_staff:
                staff_signals.staff_delete_image.send(
                    sender=__name__,
                    user=user,
                    user_id=user.id,
                    image_name=db_image.name,
                    image_id=db_image.id,
                    username=user.username,
                )
            else:
                user_signals.user_delete_image.send(
                    sender=__name__,
                    user=user,
                    user_id=user.id,
                    image_name=db_image.name,
                    image_id=db_image.id,
                    username=user.username,
                )

    except glance_exceptions.NotFound:
        ModelImage.objects.filter(id=image_id).delete()
Esempio n. 12
0
    def openstack_services(self, request, pk):
        del request, pk  # unused
        client = self.get_object()
        services = client.services.select_related('openstack_project').filter(
            product__product_type=ProductType.openstack,
            openstack_project__isnull=False)

        projects = []

        for service in services:
            try:
                project = service.openstack_project
            except ObjectDoesNotExist:
                project = None

            if project is not None:
                try:
                    if OpenstackProject.with_admin_session(
                            project.project_id) is None:
                        project = None
                except Exception as e:
                    LOG.exception(e)
                    project = None

            if project is not None:
                projects.append(project)

        serialized_services = ServiceBriefSerializer(
            many=True).to_representation(services)
        serialized_projects = ProjectSerializer(
            many=True).to_representation(projects)

        return Response({
            'services': serialized_services,
            'projects':
            {project['service']: project
             for project in serialized_projects}
        })
Esempio n. 13
0
def delete_client_project_resources(self,
                                    project_id,
                                    user_id: Optional[int] = None,
                                    mark_project_as_deleted=True):
    """Delete project and all it's resources"""
    project = Project.with_admin_session(project_id)
    seconds_to_wait = 2**self.request.retries
    try:
        project.disable(reason='terminating')
        parallel_tasks = [
            delete_openstack_images.si(project_id=project_id, user_id=user_id),
            celery.chain(
                delete_openstack_instances.s(project_id=project_id,
                                             user_id=user_id),
                delete_openstack_project_security_groups.s(
                    project_id=project_id),
            ),
            delete_openstack_volumes.si(project_id=project_id,
                                        user_id=user_id),
            delete_openstack_network_resources.si(project_id=project_id,
                                                  user_id=user_id),
            delete_openstack_apiusers.si(project_id=project_id)
        ]

        # run resource deletion in parallel, but BEFORE project deletion
        parallel_group = celery.group(*parallel_tasks)
        fin_chord = parallel_group | celery.chain([
            delete_openstack_gnocchi_metrics.si(project_id=project_id),
            delete_openstack_project.si(project_id=project_id,
                                        mark_deleted=mark_project_as_deleted)
        ])
        celery.chain(fin_chord).apply_async()

    except Exception as e:
        LOG.error(e)
        raise self.retry(exc=e, countdown=seconds_to_wait)
Esempio n. 14
0
def delete_openstack_volume(self,
                            volume_id,
                            region,
                            project_id,
                            user_id: Optional[int] = None):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    cc = cinder_client(api_session=project.api_session, region_name=region)
    try:
        db_volume = OpenstackVolume.objects.filter(id=volume_id).first()
        user = AppUser.objects.filter(id=user_id).first()
        cc.volumes.delete(volume=volume_id)

        if user and db_volume:
            if user.is_staff:
                staff_signals.staff_delete_volume.send(
                    sender=__name__,
                    user=user,
                    user_id=user.id,
                    volume_name=db_volume.name,
                    volume_id=db_volume.id,
                    username=user.username,
                )
            else:
                user_signals.user_delete_volume.send(
                    sender=__name__,
                    user=user,
                    user_id=user.id,
                    volume_name=db_volume.name,
                    volume_id=db_volume.id,
                    username=user.username,
                )

    except cinder_exceptions.NotFound:
        OpenstackVolume.objects.filter(id=volume_id).delete()
Esempio n. 15
0
def delete_openstack_router(self, router_id, region, project_id):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    Router(router_id=router_id,
           api_session=project.api_session).delete(region=region)
Esempio n. 16
0
def delete_openstack_floating_ip(self, floating_ip_id, region, project_id):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    FloatingIp(floating_ip_id=floating_ip_id,
               api_session=project.api_session).delete(region=region)
Esempio n. 17
0
def delete_openstack_subnetpool(self, subnet_pool_id, region, project_id):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    SubnetPool(subnet_pool_id=subnet_pool_id,
               api_session=project.api_session).delete(region=region)
Esempio n. 18
0
def delete_openstack_port(self, port_id, region, project_id):
    del self  # unused

    project = Project.with_admin_session(project_id, cache=AUTH_CACHE)
    Port(port=None, api_session=project.api_session).delete(region=region,
                                                            port_id=port_id)