async def test_aiohttp_error(self):
        exc = web_exceptions.HTTPNotFound()

        response = await view_test(exc)

        assert response.status == 404
        assert json.loads(response.text) == {
            "message": "HTTPNotFound", "debug_message": "404: Not Found", "code": None,
        }
Exemple #2
0
async def running_interactive_services_get(request, service_uuid):  # pylint:disable=unused-argument
    log.debug(
        "Client does running_interactive_services_get request %s with service_uuid %s",
        request, service_uuid)
    try:
        service = await producer.get_service_details(service_uuid)
        return web.json_response(data=dict(data=service), status=200)
    except exceptions.ServiceUUIDNotFoundError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))
Exemple #3
0
async def receive_redirect_to(request):
    """ Receives the OAuth redirect_to response and forwards it to a registerd
    client if a match can be found.

    """
    token = await extract_token(request)

    if token:
        return await forward_request(token, request)

    raise ex.HTTPNotFound(reason="Invalid token")
Exemple #4
0
async def get_file_bytes(request: web.Request):
    """
     Gets the file identified by the sha256sum.

     The client can decide the number of file to get using the
      "bytes" query param.

    """

    id = request.match_info['id']
    try:
        # all the following should probably go to another function
        with get_file(id) as (io, file_size, filename):

            # do we limit to N-nth byte or to the whole file?
            max_byte = request.query.get('max_byte', None)
            if max_byte:
                try:
                    max_byte = int(max_byte)
                except ValueError:
                    raise web_exceptions.HTTPBadRequest(
                        text=f'max_byte must be an integer "{max_byte}" found')
                if max_byte <= 0:
                    raise web_exceptions.HTTPBadRequest(
                        text=f'max_byte must be positive "{max_byte}" found')

            else:
                max_byte = file_size

            # We use a streaming response here
            response = web.StreamResponse(
                headers={
                    'Content-Length': str(max_byte),
                    'Content-Disposition': f'attachment;filename="{filename}"'
                })

            await response.prepare(request)

            # stream with 16 Kib chunks
            chunk_size = (2**3)**4

            # Iterate  through the file until we hit 0.
            while max_byte > 0:
                chunk_size = min(max_byte, chunk_size)
                text = io.read(chunk_size)
                await response.write(text)
                max_byte -= chunk_size

            # ok we write the eof at the end of the cycle
            await response.write_eof(b'')

    except FileNotFoundError:
        raise web_exceptions.HTTPNotFound(text=f"Resource {id} not found.")
    return response
Exemple #5
0
async def running_interactive_services_delete(request, service_uuid):  # pylint:disable=unused-argument
    log.debug(
        "Client does running_interactive_services_delete request %s with service_uuid %s",
        request, service_uuid)
    try:
        await producer.stop_service(service_uuid)
    except exceptions.ServiceUUIDNotFoundError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))

    return web.json_response(status=204)
Exemple #6
0
 async def add_card(self, request):
     """
     ---
     description: Запрос для добавления новой карты клиента
     tags:
     - Cards
     produces:
     - application/json
     - application/xml
     parameters:
     - name: card
       in: body
       description: данные новой карты
       required: true
       schema:
         type: object
         properties:
           owner_id:
             type: integer
             description: идентификатор владельца карты
             required: true
           payment_system:
             type: string
             description: платежная система
             required: true
           currency:
             type: string
             description: валюта карты
             required: true
           balance:
             type: numeric
             description: баланс карты
             required: true
     responses:
         "200":
             description: успех. Возвращает данные новой карты клиента
         "404":
             description: ошибка. Клиент не найден
         "406":
             description: ошибка клиента. Указан неверный Accept
     """
     encoder = self.__choose_encoder(request)
     data = await self.__decode_post(request)
     try:
         card = await self.__client_model.add_card(data)
         return web.HTTPCreated(content_type=encoder.content_type, body=encoder.encode(card))
     except MultipleInvalid as e:
         raise web_exceptions.HTTPBadRequest(text=str(e))
     except ItemNotFoundException as e:
         raise web_exceptions.HTTPNotFound(text=str(e))
Exemple #7
0
async def running_interactive_services_delete(
        request: web.Request, service_uuid: str) -> web.Response:
    log.debug(
        "Client does running_interactive_services_delete request %s with service_uuid %s",
        request,
        service_uuid,
    )
    try:
        await producer.stop_service(request.app, service_uuid)
    except exceptions.ServiceUUIDNotFoundError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))

    return web.json_response(status=204)
Exemple #8
0
async def services_by_key_version_get(request, service_key, service_version):  # pylint:disable=unused-argument
    log.debug(
        "Client does services_get request %s with service_key %s, service_version %s",
        request, service_key, service_version)
    try:
        services = [
            await registry_proxy.get_service_details(service_key,
                                                     service_version)
        ]
        return web.json_response(data=dict(data=services))
    except exceptions.ServiceNotAvailableError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except exceptions.RegistryConnectionError as err:
        raise web_exceptions.HTTPUnauthorized(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))
Exemple #9
0
async def _get_node_details(node_key: str, node_version: str,
                            app: web.Application) -> Dict:
    if "file-picker" in node_key:
        # create a fake file-picker schema here!!
        fake_node_details = {
            "inputs": {},
            "outputs": {
                "outFile": {
                    "label": "the output",
                    "displayOrder": 0,
                    "description": "a file",
                    "type": "data:*/*",
                }
            },
            "type": "dynamic",
        }
        return fake_node_details
    if "frontend/nodes-group" in node_key:
        return None
    if "StimulationSelectivity" in node_key:
        # create a fake file-picker schema here!!
        fake_node_details = {
            "inputs": {},
            "outputs": {
                "stimulationFactor": {
                    "label": "the output",
                    "displayOrder": 0,
                    "description": "a file",
                    "type": "data:*/*",
                    "defaultValue": 0.6,
                }
            },
            "type": "dynamic",
        }
        return fake_node_details
    node_details = await director_api.get_service_by_key_version(
        app, node_key, node_version)
    if not node_details:
        log.error(
            "Error (while getting node details) could not find service %s:%s",
            node_key,
            node_version,
        )
        raise web_exceptions.HTTPNotFound(
            reason=
            f"details of service {node_key}:{node_version} could not be found")
    return node_details
Exemple #10
0
async def get_service_labels(request: web.Request, service_key: str,
                             service_version: str) -> web.Response:
    log.debug(
        "Retrieving service labels %s with service_key %s, service_version %s",
        request,
        service_key,
        service_version,
    )
    try:
        service_labels = await registry_proxy.get_image_labels(
            request.app, service_key, service_version)
        return web.json_response(data=dict(data=service_labels))
    except exceptions.ServiceNotAvailableError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except exceptions.RegistryConnectionError as err:
        raise web_exceptions.HTTPUnauthorized(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))
Exemple #11
0
async def _get_node_extras(node_key: str, node_version: str,
                           app: web.Application) -> Dict:
    """Returns the service_extras if possible otherwise None"""
    if to_node_class(node_key) == NodeClass.FRONTEND:
        return None

    node_extras = await director_api.get_services_extras(
        app, node_key, node_version)
    if not node_extras:
        log.error(
            "Error (while getting node extras) could not find service %s:%s",
            node_key,
            node_version,
        )
        raise web_exceptions.HTTPNotFound(
            reason=
            f"extras of service {node_key}:{node_version} could not be found")
    return node_extras
async def archivate(request: web.Request):
    folder_to_download = request.app['archives_folder_path'].joinpath(
        request.match_info["archive_hash"])
    logging.debug(f"Searching for files in {folder_to_download.as_posix()}")
    #  check for unexisting folders and path injections
    if not folder_to_download.exists(
    ) or '.' in request.match_info["archive_hash"]:
        logging.debug(f"'{folder_to_download.as_posix()}' doesn't exists ")
        return web_exceptions.HTTPNotFound(text=await get_404_content(),
                                           content_type='text/html')

    logging.debug(f"{folder_to_download.as_posix()} has been found")
    response = web.StreamResponse()
    response.headers[
        'Content-Disposition'] = 'attachment; filename="archive.zip"'
    await response.prepare(request)

    cmd_to_zip = shlex.split(
        ZIP_GENERATE_CMD_TMP.format(folder_path=folder_to_download.absolute()))
    archive_stream_proc = await asyncio.create_subprocess_exec(
        *cmd_to_zip, stdout=asyncio.subprocess.PIPE)
    logging.debug(
        f"Starting download pid: {archive_stream_proc.pid} with delay {request.app['interval_secs']} secs."
    )

    try:
        while True:
            chunk = await archive_stream_proc.stdout.read(CHUNK_SIZE)
            if not chunk:
                break
            logging.debug('Sending archive chunk ...')
            await response.write(chunk)
            await asyncio.sleep(request.app['interval_secs'])
    except asyncio.CancelledError:
        logging.debug("Download was interrupted")
        logging.debug(f"Killing process {archive_stream_proc.pid}")
        archive_stream_proc.kill()
        raise
    finally:
        await archive_stream_proc.wait()
        response.force_close()

    return response
Exemple #13
0
async def _get_node_details(node_key: str, node_version: str,
                            app: web.Application) -> dict:
    if "file-picker" in node_key:
        # create a fake file-picker schema here!!
        fake_node_details = {
            "inputs": {},
            "outputs": {
                "outFile": {
                    "label": "the output",
                    "displayOrder": 0,
                    "description": "a file",
                    "type": "data:*/*"
                }
            },
            "type": "dynamic"
        }
        return fake_node_details
    if "StimulationSelectivity" in node_key:
        # create a fake file-picker schema here!!
        fake_node_details = {
            "inputs": {},
            "outputs": {
                "stimulationFactor": {
                    "label": "the output",
                    "displayOrder": 0,
                    "description": "a file",
                    "type": "data:*/*",
                    "defaultValue": 0.6
                }
            },
            "type": "dynamic"
        }
        return fake_node_details
    try:
        services_enveloped = await director_sdk.create_director_api_client(
            app).services_by_key_version_get(node_key, node_version)
        node_details = services_enveloped.data[0].to_dict()
        return node_details
    except ApiException as err:
        log.exception("Error could not find service %s:%s", node_key,
                      node_version)
        raise web_exceptions.HTTPNotFound(reason=str(err))
Exemple #14
0
async def services_by_key_version_get(request: web.Request, service_key: str,
                                      service_version: str) -> web.Response:
    log.debug(
        "Client does services_get request %s with service_key %s, service_version %s",
        request,
        service_key,
        service_version,
    )
    try:
        services = [
            await registry_proxy.get_image_details(request.app, service_key,
                                                   service_version)
        ]
        return web.json_response(data=dict(data=services))
    except exceptions.ServiceNotAvailableError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except exceptions.RegistryConnectionError as err:
        raise web_exceptions.HTTPUnauthorized(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))
Exemple #15
0
async def running_interactive_services_post(
    request: web.Request,
    user_id: str,
    project_id: str,
    service_key: str,
    service_uuid: str,
    service_tag: str,
    service_basepath: str,
) -> web.Response:
    log.debug(
        "Client does running_interactive_services_post request %s with user_id %s, project_id %s, service %s:%s, service_uuid %s, service_basepath %s",
        request,
        user_id,
        project_id,
        service_key,
        service_tag,
        service_uuid,
        service_basepath,
    )
    try:
        service = await producer.start_service(
            request.app,
            user_id,
            project_id,
            service_key,
            service_tag,
            service_uuid,
            service_basepath,
        )
        return web.json_response(data=dict(data=service), status=201)
    except exceptions.ServiceStartTimeoutError as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))
    except exceptions.ServiceNotAvailableError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except exceptions.ServiceUUIDInUseError as err:
        raise web_exceptions.HTTPConflict(reason=str(err))
    except exceptions.RegistryConnectionError as err:
        raise web_exceptions.HTTPUnauthorized(reason=str(err))
    except Exception as err:
        raise web_exceptions.HTTPInternalServerError(reason=str(err))
Exemple #16
0
async def running_interactive_services_delete(
        request: web.Request,
        service_uuid: str,
        save_state: Optional[bool] = True) -> web.Response:
    log.debug(
        "Client does running_interactive_services_delete request %s with service_uuid %s",
        request,
        service_uuid,
    )
    try:
        await producer.stop_service(request.app, service_uuid, save_state)

    except exceptions.ServiceUUIDNotFoundError as err:
        raise web_exceptions.HTTPNotFound(reason=str(err))
    except Exception as err:
        # server errors are logged (>=500)
        log.exception(
            "Failed to delete dynamic service %s (save_state=%s)",
            service_uuid,
            save_state,
        )
        raise web_exceptions.HTTPInternalServerError(reason=str(err))

    return web.json_response(status=204)