async def filtred_log_get(request):
    try:
        # TODO: сделать фильтрацию
        # запросы в базу с 'where'
        return web.Response(text='Ok')
    except Exception as e:
        return web.HTTPServerError(text=f'error: {e}')
Пример #2
0
async def register(request):
    username = await authorized_userid(request)
    if username:
        return web.HTTPOk()

    data = await request.json()
    username = data["username"]
    password = data["password"]
    email = data.get("email", None)
    if User.by_name(username):
        return web.HTTPConflict(reason="Username already taken")
    elif not username:
        return web.HTTPBadRequest(reason="Please provide a username")
    elif not password:
        return web.HTTPBadRequest(reason="Please provide a password")
    else:
        try:
            with db.atomic():
                u = User(name=username)
                u.set_password(password)
                if email:
                    u.email = email
                u.save()
        except:
            return web.HTTPServerError(
                reason=
                "An unexpected error occured on the server during account creation.  Operation reverted."
            )
        response = web.HTTPOk()
        await remember(request, response, username)
        return response
Пример #3
0
async def http_error_handler(req: Request, handler: Callable) -> Response:
    """Middleware for handling exceptions received from the API methods.

    :param req: A request instance
    :param handler: A request handler
    :raises: Reformatted HTTP Exceptions
    :returns: Successful requests unaffected
    """
    try:
        response = await handler(req)
        return response
    except web.HTTPError as error:
        details = _json_exception(error.status, error, req.url)
        LOG.error(details)
        c_type = "application/problem+json"
        if error.status == 400:
            raise web.HTTPBadRequest(text=details, content_type=c_type)
        elif error.status == 401:
            raise web.HTTPUnauthorized(
                headers={"WWW-Authenticate": 'OAuth realm="/", charset="UTF-8"'}, text=details, content_type=c_type
            )
        elif error.status == 403:
            raise web.HTTPForbidden(text=details, content_type=c_type)
        elif error.status == 404:
            raise web.HTTPNotFound(text=details, content_type=c_type)
        elif error.status == 415:
            raise web.HTTPUnsupportedMediaType(text=details, content_type=c_type)
        elif error.status == 422:
            raise web.HTTPUnprocessableEntity(text=details, content_type=c_type)
        else:
            raise web.HTTPServerError()
Пример #4
0
 def critical(self,
              request: web.Request = None,
              exception: Exception = None,
              traceback=None,
              state: int = 500,
              headers: dict = {},
              **kwargs) -> web.Response:
     # TODO: process the exception object
     if not request:
         request = self.request
     response_obj = {
         "status": "Failed",
         "reason": str(exception),
         "stacktrace": traceback,
     }
     args = {
         "text": json.dumps(response_obj),
         "reason": "Server Error",
         "content_type": "application/json",
         **kwargs,
     }
     if state == 500:  # bad request
         obj = web.HTTPInternalServerError(**args)
     else:
         obj = web.HTTPServerError(**args)
     for header, value in headers.items():
         obj.headers[header] = value
     return obj
Пример #5
0
async def get_metric_list(project_id: str,
                          cloud_token: str,
                          *,
                          mn2dim_target=False):
    mn2dim = dict()
    url = EYE_METRIC_LIST_URL.format(project_id=project_id)
    headers = {"X-Auth-Token": cloud_token}

    async with aiohttp.ClientSession() as session:
        async with session.get(url, headers=headers) as resp:
            if resp.status != 200:  # todo: Понять почему 403
                logging.error(
                    f"{await resp.json() = } {resp.status = } {headers = }")
                raise web.HTTPServerError()
            resp = await resp.json()

    def inner():
        for metric in resp["metrics"]:
            yield {
                "id": metric["namespace"],
                "metric_name": metric["metric_name"],
                "name": verbose_namespace(metric["namespace"]),
            }

    if mn2dim_target:
        for metric in resp["metrics"]:
            mn2dim[metric["metric_name"]] = ','.join([
                metric["dimensions"][0]["name"],
                metric["dimensions"][0]["value"]
            ])  # todo: ignore kafka with 2 dimensions?

        return mn2dim

    return {"metrics": uniques(inner())}
Пример #6
0
    async def code(self, request: web.Request):
        if 'code' not in request.query:
            raise web.HTTPFound("/register?" +
                                urlencode({"redirect": request.url}))

        code = request.query["code"]
        data = {
            "code": code,
            "grant_type": "authorization_code",
            "redirect_uri": "http://api.typheus.me/hub",
            "client_id": self.client_id,
            "client_secret": self.client_secret,
            "scope": 'identify guilds'
        }
        response = await self.session.post(
            f"https://discordapp.com/api/oauth2/token",
            data=urlencode(data),
            headers={'Content-Type': "application/x-www-form-urlencoded"})
        js = await response.json()
        if 'error' in js:
            raise web.HTTPServerError(
                reason=f"Invalid code or redirect {js['error']}")
        token = js['access_token']
        logging.info("Received Discord OAuth2 code, grabbing token")
        raise web.HTTPFound(f"/hub?token={token}")
Пример #7
0
async def populate(request):
  N = request.query.get('num_users', 1)
  errors = await asyncio.gather(*[insert(request.app) 
      for _ in range(N)], return_exceptions=True)

  if any(errors):
    msg = " ".joint([e.reason for e in errors if e])
    raise web.HTTPServerError(text=msg)
  raise web.HTTPOk()
 async def get_detail(self, request):
     async with request.app['db'].acquire() as conn:
         try:
             procedure = await get_procedure(conn, request)
         except RecordNotFound as e:
             raise web.HTTPNotFound(text=str(e))
         except TypeError:
             return web.HTTPServerError(text=str(DataTypeError))
         return web.json_response(json_handler(procedure))
Пример #9
0
 async def is_mobile(self, tn):
     data, status, _ = await \
         self.client.request('GET', phone_data_url.format(tn=tn),
                             headers={'Accept': 'application/json'})
     if status != 200:
         raise web.HTTPServerError()
     data = json.loads(data)
     if data['carrier'].get('type') == 'mobile':
         return True
     return False
Пример #10
0
async def get_image_png(request):
    id = request.match_info.get('id', None)
    frame = _get_image(id)
    success, buffer = cv2.imencode(".png", frame)

    if not success:
        raise web.HTTPServerError()

    io_buf = BytesIO(buffer)

    return web.Response(body=io_buf.getvalue(), content_type='image/png')
Пример #11
0
    async def get(self):
        if not self.riki_path:
            raise web.HTTPSeeOther(f'{APP_PATH}page/index')

        page_fs_path = os.path.join(self.data_dir, self.riki_path)
        pelms = page_fs_path.rsplit('.', 1)
        page_suff = None if len(pelms) < 2 else pelms[-1]

        if self.dir_metadata.directory_type == 'gallery':
            raise web.HTTPSeeOther(f'{APP_PATH}gallery/{self.riki_path}/index')
        elif files.page_is_dir(page_fs_path):
            if self.dir_metadata.directory_type == 'page':
                raise web.HTTPSeeOther(
                    f'{APP_PATH}page/{self.riki_path}/index')
            else:
                raise web.HTTPServerError('Unknown page type')
        elif page_suff and page_suff in appconf.RAW_FILES:
            with open(page_fs_path, 'rb') as fr:
                web.header('Content-Type', appconf.RAW_FILES[page_suff])
                return fr.read()
        else:
            page_fs_path = f'{page_fs_path}.md'
            curr_dir = os.path.dirname(self.riki_path)
            page_name = os.path.basename(self.riki_path)

        # setup the directory information
        if curr_dir:
            path_elms = path_dir_elms(curr_dir)
            curr_dir_fs = os.path.join(self.data_dir, curr_dir)
        else:
            curr_dir = ''
            path_elms = []
            curr_dir_fs = self.data_dir

        # transform the page
        if files.page_exists(page_fs_path):
            page_info = files.get_version_info(
                self.data_dir,
                page_fs_path,
                info_encoding=conf.hg_info_encoding)
            inner_html = load_markdown(page_fs_path)
            page_template = 'page.html'
        else:
            inner_html = ''
            page_info = files.RevisionInfo()
            page_template = 'dummy_page.html'

        data = dict(html=inner_html,
                    page_list=self.generate_page_list(curr_dir_fs),
                    path_elms=path_elms,
                    page_info=page_info,
                    page_name=page_name,
                    curr_dir_name=self.get_current_dirname(curr_dir))
        return self.response_html(page_template, data)
Пример #12
0
async def precache(app, old_id, new_id):
    with ExitStack() as es:
        old_dir = es.enter_context(TemporaryDirectory())
        new_dir = es.enter_context(TemporaryDirectory())

        await asyncio.gather(
            app.artifact_manager.retrieve_artifacts(
                old_id, old_dir, filter_fn=is_binary, timeout=PRECACHE_RETRIEVE_TIMEOUT
            ),
            app.artifact_manager.retrieve_artifacts(
                new_id, new_dir, filter_fn=is_binary, timeout=PRECACHE_RETRIEVE_TIMEOUT
            ),
        )

        old_binaries = find_binaries(old_dir)
        if not old_binaries:
            raise ArtifactsMissing(old_id)

        new_binaries = find_binaries(new_dir)
        if not new_binaries:
            raise ArtifactsMissing(new_id)

        debdiff_cache_path = app.debdiff_cache_path(old_id, new_id)

        if debdiff_cache_path and not os.path.exists(debdiff_cache_path):
            with open(debdiff_cache_path, "wb") as f:
                f.write(
                    await run_debdiff(
                        [p for (n, p) in old_binaries], [p for (n, p) in new_binaries]
                    )
                )
            logging.info("Precached debdiff result for %s/%s", old_id, new_id)

        diffoscope_cache_path = app.diffoscope_cache_path(old_id, new_id)
        if diffoscope_cache_path and not os.path.exists(diffoscope_cache_path):
            try:
                diffoscope_diff = await asyncio.wait_for(
                    run_diffoscope(
                        old_binaries, new_binaries,
                        lambda: _set_limits(app.task_memory_limit)), app.task_timeout
                )
            except MemoryError:
                raise web.HTTPServiceUnavailable(text="diffoscope used too much memory")
            except asyncio.TimeoutError:
                raise web.HTTPGatewayTimeout(text="diffoscope timed out")

            try:
                with open(diffoscope_cache_path, "w") as f:
                    json.dump(diffoscope_diff, f)
            except json.JSONDecodeError as e:
                raise web.HTTPServerError(text=str(e))
            logging.info("Precached diffoscope result for %s/%s", old_id, new_id)
Пример #13
0
async def login_by_vk(request: 'Request'):
    if VK_CLIENT_ID is None:
        logger.error('VK_CLIENT_ID not set')
        raise web.HTTPServerError()

    opts = {
        'client_id': VK_CLIENT_ID,
        'redirect_uri': request.app['config']['app']['domain'] + "/oauth/_vk",
        'display': 'page',
        'scope': (1 << 16) + (1 << 22),
        'response_type': 'code',
    }
    raise web.HTTPFound('https://oauth.vk.com/authorize?' + urlencode(opts))
Пример #14
0
 def _get_user(cls, token):
     db = DatabaseManager.DatabaseManager.get_session()
     try:
         row = db.query(cls).filter(cls.token == token).one_or_none()
         if not row:
             return None
         else:
             return row.object_user
     except Exception as ex:
         traceback.print_exc()
         raise web.HTTPServerError(reason=str(ex))
     finally:
         db.close()
Пример #15
0
 def _generate_token(cls, user_id):
     db = DatabaseManager.DatabaseManager.get_session()
     try:
         user = cls._get_user_byid(user_id)
         if not user:
             raise web.HTTPNotFound(reason="User not found.")
         else:
             token = ChatUserTokens.ChatUserTokens(user.get_id())
             db.add(token)
             db.commit()
             return token.token
     except Exception as ex:
         raise web.HTTPServerError(reason=str(ex))
     finally:
         db.close()
Пример #16
0
    async def post(self):
        pack = await self.request.content.read()
        s = pack.split(b"\r")[0][2:]
        p = mp.MultipartParser(BytesIO(mp.tob(pack)), s)
        blob = p.parts()
        """
            0  :  chunkNumber
            1  :  chunkSize
            2  :  currentChunkSize
            3  :  totalSize
            4  :  identifier
            5  :  filename
            6  :  relativePath
            7  :  totalChunks
            8  :  file
        """
        curr_chunkNumber = int(blob[0].value)
        totalChunks = blob[7].value
        filename = blob[5].value
        file = blob[8].value

        ret = None
        try:
            if filename not in files:
                f = File(filename, totalChunks)
                files[filename] = f
                ret = await handle_chunks(filename, curr_chunkNumber, file)
            else:
                ret = await handle_chunks(filename, curr_chunkNumber, file)
            if ret == False:
                return web.HTTPExpectationFailed()
            elif ret:
                analyz = Analyzes(ret)
                analyzes[filename] = analyz
                threading.Thread(target=analyz.start_analysis).start()

        except Exception as e:
            print(e)
            return web.HTTPServerError()
        return web.HTTPOk()
Пример #17
0
async def handle_location(req):
    dbredis = req.app[DB_REDIS]
    settings = req.app[SETTINGS]
    session = req.app[HTTP_SESSION]
    device_id = req.match_info['device_id']
    location_id = req.match_info['location_id']
    event_name = req.match_info['event_name']
    event_marker_id = '{}/{}/{}'.format(device_id, location_id, event_name)
    logger.info('Received event marker: %s', event_marker_id)
    data = await req.json()
    if data['secret'] != IFTTT_APPLET_SECRET:
        raise web.HTTPForbidden(reason='bad secret')
    try:
        if data['action'].lower() == 'entered':
            occured_at = data.get('occuredAt', None)
            if occured_at:
                occured_at = parsedate_to_datetime(occured_at)
                occured_at = occured_at.timestamp()
            await store_event_marker(dbredis, event_marker_id, occured_at)
        if data['action'].lower() == 'exited':
            started_at = await get_event_marker(dbredis, event_marker_id)
            if started_at is None:
                logger.warning('Got exit but never entered for location: %s from device: %s', location_id, device_id)
                return web.HTTPOk()
            now = int(time.time())
            delta = (now - int(started_at)) / 60.  # minutes
            if delta < 15:
                logger.warning('Too little time spent in location: %s from device: %s', location_id, device_id)
                return web.HTTPOk()
            payload = {
                'value1': float("{0:.2f}".format(delta))
            }
            await trigger_maker_event(session, event_name, payload, settings.get(IFTTT_MAKER_KEY))
            await remove_event_marker(dbredis, event_marker_id)
        return web.HTTPOk()
    except Exception:
        logger.exception('Cannot handle data: %s', data)
        raise web.HTTPServerError()
Пример #18
0
async def create(request: web.Request) -> web.Response:
    data = await request.json()
    message = data.get("message", None)
    if message:
        try:
            notification = Notification.create(uuid=uuid.uuid4(),
                                               message=message)
            notification.save()
            await sio.emit(
                "Notification.Show",
                {
                    "uuid": str(notification.uuid),
                    "message": notification.message
                },
                namespace=GAME_NS,
            )
        except:
            return web.HTTPServerError(
                reason="Failed to create new notification.")
        return web.HTTPOk(
            text=f"Created new notification with id {notification.uuid}")
    else:
        return web.HTTPBadRequest(reason="Missing mandatory field 'message'")
Пример #19
0
 async def login(self, request: Request):
     #print("Line 10 in Users.py\n", await request.json())
     try:
         body = await request.json()
         user_name = body.get('username')
         user_pass = body.get('password')
         if not user_name:
             return web.HTTPBadRequest(reason="You are missing the username field for the login user.")
         if not user_pass:
             return web.HTTPBadRequest(reason="You are missing the password field. Please send hashed password.")
         else:
             user: ChatUser.ChatUser = await ChatUser.ChatUser.get_user_byname(user_name)
             if not user:
                 return web.HTTPBadRequest(reason="This user is not registered with the system.")
             if await self.run_executor(user.test_password, user_pass):
                 data = {'id': user.get_id(), 'token': await ChatUser.ChatUser.generate_token(user.get_id())}
                 await ChatUser.ChatUser.set_status(user.get_name(), "online")
                 return web.json_response(data)
             else:
                 return web.HTTPForbidden(reason="Bad password.")
     except Exception as ex:
         traceback.print_exc()
         return web.HTTPServerError(text="{}".format(ex))
async def consumers_statuses(request):
    """
    Function to submit a query.
    :param request: used to get the params to connect to the db
    :return: list of json object

    ---
    tags:
    - Dashboard
    summary: Shows the status of a consumer
    parameters: []
    responses:
      200:
        description: Ok
      404:
        description: Not Found
    """
    try:
        consumers = await get_consumers_statuses(request.app['engine'])
    except DatabaseConnectionError as e:
        raise web.HTTPServerError() from e

    return web.json_response(consumers)
Пример #21
0
    async def get(self):
        gallery_fs_dir = os.path.join(self.data_dir, self.riki_path)
        if files.page_is_dir(gallery_fs_dir):
            if self.dir_metadata.directory_type == 'page':
                raise web.HTTPSeeOther(
                    f'{APP_PATH}page/{self.riki_path}/index')
            elif self.dir_metadata.directory_type == 'gallery':
                raise web.HTTPSeeOther(
                    f'{APP_PATH}gallery/{self.riki_path}/index')
            else:
                raise web.HTTPServerError('Unknown page type')
        elif os.path.isfile(gallery_fs_dir):
            raise web.HTTPInternalServerError('Gallery directory malformed')
        elif os.path.basename(gallery_fs_dir) == 'index':
            gallery_fs_dir = os.path.dirname(gallery_fs_dir)
        else:
            raise web.HTTPNotFound()

        try:
            images = files.list_files(gallery_fs_dir,
                                      files.file_is_image,
                                      recursive=False)
        except FileNotFoundError:
            raise web.HTTPNotFound()
        extended: List[files.FileInfo] = []

        for img in images:
            info = files.get_file_info(img, path_prefix=self.data_dir)
            info.metadata = pictures.get_metadata(img)
            extended.append(info)
        values = dict(files=extended,
                      page_list=[],
                      path_elms=path_dir_elms(self.riki_path),
                      curr_dir_name=self.get_current_dirname(self.riki_path),
                      num_files=await self.get_num_files(gallery_fs_dir),
                      description=self.dir_metadata.description)
        return self.response_html('gallery.html', values)
Пример #22
0
async def websocket_handler(request):
    ws = web.WebSocketResponse()
    await ws.prepare(request)

    request.app['websockets'].add(ws)

    try:
        p = psutil.Process(int(request.match_info["pid"]))
    except BaseException:
        return web.HTTPServerError(reason="Process not found.")

    try:
        while True:
            await ws.send_json({
                "name": p.name(),
                "cpu": p.cpu_percent() / psutil.cpu_count(),
                "memory": p.memory_info()[1] * 0.000001,
                "memory_percent": p.memory_percent(),
            })
            await asyncio.sleep(0.5)
    finally:
        request.app['websockets'].discard(ws)

    return ws
async def get_posts(request: web.Request) -> web.Response:
    """
    Getting posts from db with applying query params
    Args:
        request: aiohttp request

    Returns:
        List with posts
    """
    query_params = request.rel_url.query
    limit, offset, order_key, order_type = _validate_init_params(query_params)

    try:
        posts = await Post.get_list(
            limit=limit,
            offset=offset,
            order_key=order_key,
            order_type=order_type
        )
        result = views.json_posts(posts)

        return web.json_response(result)
    except peewee.DatabaseError:
        return web.HTTPServerError(text='Something went wrong')
Пример #24
0
    async def _handle_vuespa_ws_http(self, req):
        ws_id = req.match_info['id']
        fn_id = req.match_info['fn']
        ws_client = self._websocket_clients.get(ws_id)
        if ws_client is None:
            raise web.HTTPNotFound()

        # Synchronization note: if threaded, this would need a lock. However,
        # asyncio doesn't need a lock for code which does not include "await".
        web_id = ws_client._socket_http_next
        ws_client._socket_http_next += 1
        ws_client._socket_http_reqs[web_id] = socket_req = {
            'event': asyncio.Event(),
            'result': None,
            'err': None,
        }
        try:
            args = dict(req.query)
            if req.has_body:
                args.update(await req.json())
            await ws_client._socket.send_str(
                json.dumps({
                    'type': 'http',
                    'fn': fn_id,
                    'id': web_id,
                    'args': args,
                }))
            await socket_req['event'].wait()
        finally:
            del ws_client._socket_http_reqs[web_id]

        if socket_req['err'] is not None:
            raise web.HTTPServerError(body=socket_req['err'])

        return web.Response(body=socket_req['result'],
                            content_type='application/json')
Пример #25
0
    async def handle_item_add(self, req):
        if req.has_body and req.content_type == 'multipart/form-data':
            reader = await req.multipart()
            data = {}
            file_name, temp_path = None, None

            while True:
                field = await reader.next()
                if field is None:
                    break
                name = field.name
                if name in self.JSON_FIELDS:
                    try:
                        data[name] = json.loads(await field.text())
                    except json.JSONDecodeError:
                        if temp_path is not None:
                            os.unlink(temp_path)
                        return web.HTTPBadRequest(text='Invalid field: ' +
                                                  name)
                elif name in self.TEXT_FIELDS:
                    data[name] = await field.text()
                    if name == 'FileName':
                        file_name = data[name]
                elif data[
                        'ObjectType'] in self.FILE_TYPES and name == 'FileContent':
                    # Read the file content, if file name is not yet known,
                    # we take it from the request
                    if file_name is None:
                        file_name = field.filename
                    if file_name is None:
                        return web.HTTPBadRequest(text='File name unknown')
                    # https://docs.aiohttp.org/en/stable/web_quickstart.html#file-uploads
                    try:
                        temp_fd, temp_path = tempfile.mkstemp()
                        temp_fp = os.fdopen(temp_fd, mode='wb')
                        while True:
                            chunk = await field.read_chunk(
                            )  # 8192 bytes by default.
                            if not chunk:
                                break
                            temp_fp.write(chunk)
                        temp_fp.close()
                    except:
                        if temp_path is None:
                            logging.exception(
                                'Failed to create a temporary file')
                        else:
                            os.unlink(temp_path)
                            logging.exception(
                                f'Failed to write file content to {temp_path}')
                        return web.HTTPServerError()
                else:
                    if temp_path is not None:
                        os.unlink(temp_path)
                    return web.HTTPBadRequest(text='Invalid field: ' + name)

            data = self._storage.add_object(data, temp_path)
            if data is not None:
                print(data)
                await self._ws_server.broadcast_item_added(data)
                return web.HTTPNoContent()
            else:
                if temp_path is not None:
                    os.unlink(temp_path)
                return web.HTTPBadRequest(text='Invalid object data')
        else:
            return web.HTTPBadRequest(text='Form data required')
Пример #26
0
async def archive_get_routing(request: web.Request) -> web.StreamResponse:
    if (GIT_ARCHIVER_ACCESS_KEY and request.headers.get('Authorization', '') !=
            'Bearer %s' % GIT_ARCHIVER_ACCESS_KEY):
        return web.HTTPUnauthorized()

    try:
        repo = request.query['repo'].strip()
        clone_options = request.query.get('clone_options', '').strip()
        disk_quota = int(
            request.query.get('disk_quota', GIT_ARCHIVER_MAX_DISK_QUOTA))
    except Exception:
        return web.HTTPBadRequest(
            text='Failed to parse all required parameters')

    if not (0 < disk_quota <= GIT_ARCHIVER_MAX_DISK_QUOTA):
        return web.HTTPBadRequest(text='Invalid `disk_quota` parameter')

    loop = asyncio.get_event_loop()
    with tempfile.NamedTemporaryFile('w',
                                     dir=GIT_ARCHIVER_ARCHIVE_VOLUME_PATH,
                                     encoding='utf-8') as run_sh_file:
        archive_name = '%s.zip' % uuid.uuid4().hex

        run_sh_file.write(
            WORKER_CMD_TEMPLATE.format(repo=repo,
                                       clone_options=clone_options,
                                       archive_filename='/output_mnt/%s' %
                                       archive_name))

        run_sh_file.flush()

        archive_host_filename = os.path.join(GIT_ARCHIVER_ARCHIVE_VOLUME_PATH,
                                             archive_name)

        worker_container = None
        try:
            async with concurrent_job_semaphore:
                worker_container = await loop.run_in_executor(
                    thread_executor,
                    functools.partial(
                        docker_client.containers.create,
                        GIT_ARCHIVER_DOCKER_WORKER_IMAGE,
                        ['bash', '/run.sh'],
                        mounts=[
                            docker.types.Mount('/run.sh',
                                               run_sh_file.name,
                                               type='bind',
                                               read_only=True),
                            docker.types.Mount(
                                '/output_mnt',
                                GIT_ARCHIVER_ARCHIVE_VOLUME_PATH,
                                type='bind'),
                            docker.types.Mount('/tmpfs_mnt',
                                               '',
                                               type='tmpfs',
                                               tmpfs_size=disk_quota)
                        ],
                        user=os.getuid()  # type: ignore
                    ))

                await loop.run_in_executor(thread_executor,
                                           worker_container.start)

                worker_result = await loop.run_in_executor(
                    thread_executor,
                    functools.partial(worker_container.wait,
                                      timeout=GIT_ARCHIVER_TIMEOUT))

                worker_logs = await loop.run_in_executor(
                    thread_executor, worker_container.logs)
                worker_logs = worker_logs.decode('utf-8')

            if worker_result['StatusCode'] != 0:
                return web.HTTPBadRequest(
                    text=
                    'Worker is terminated with non-zero status code\n===\n%s' %
                    worker_logs)

            if not os.path.exists(archive_host_filename):
                return web.HTTPBadRequest(
                    text='Archive file is not created\n===\n%s' % worker_logs)

            resp = web.FileResponse(archive_host_filename,
                                    headers={
                                        'Content-Disposition':
                                        'attachment; filename="%s"' %
                                        archive_name
                                    })
            await resp.prepare(request)

            # TODO: dirty hack to prevent double calling of `FileResponse.prepare()`
            async def _dummy_prepare(request):
                return None

            resp.prepare = _dummy_prepare  # type: ignore

            # From here, `archive_host_filename` doesn't need to be accessbiel anymore

            return resp

        except asyncio.CancelledError:
            logger.debug('The request is cancelled')
            raise

        except Exception:
            logger.exception('Failed to archive git repo')
            return web.HTTPServerError(text='Unexpected error is occured')

        finally:
            if worker_container is not None:
                await loop.run_in_executor(
                    thread_executor,
                    functools.partial(worker_container.remove, force=True))

            if os.path.exists(archive_host_filename):
                os.unlink(archive_host_filename)
Пример #27
0
                                 status=400)

    async with request.app.db.acquire() as conn:  # type: aiopg.sa.SAConnection
        user = await inner_auth(conn, request)

        data = await conn.execute(
            r"""
            DELETE FROM app_templates T
            WHERE (T.project_id = %s) and (T.user_id = %s) and (T.id = %s)
            RETURNING T.id
        """, (project_id, user["id"], template_id))
        data = await data.fetchall()

    if len(data) > 1:
        logging.error(f"{data = }. query delete few template once")
        return web.HTTPServerError()
    elif len(data) == 1:
        return web.HTTPNoContent()
    else:
        return web.HTTPNotFound()


def main():
    app = Application()
    app.add_routes([
        web.route('POST', '/v1/login', login_handler),
        web.route('POST', '/v1/projects', projects_handler),
        web.route('POST', '/v1/eye/metric_list', eye_metric_list_handler),
        web.route('POST', '/v1/eye/query', eye_query_handler),
        web.route('POST', '/v1/cts/overview', cts_handler),
        web.route('POST', '/v1/cts/detail', cts_detail_handler),
Пример #28
0
async def get_my_profile(request: web.Request):
    # NOTE: ONLY login required to see its profile. E.g. anonymous can never see its profile

    @retry(**PostgresRetryPolicyUponOperation(logger).kwargs)
    async def _query_db(uid: str, engine: Engine) -> List[RowProxy]:
        async with engine.acquire() as conn:
            query = (sa.select(
                [
                    users.c.email,
                    users.c.role,
                    users.c.name,
                    users.c.primary_gid,
                    groups.c.gid,
                    groups.c.name,
                    groups.c.description,
                    groups.c.type,
                ],
                use_labels=True,
            ).select_from(
                users.join(
                    user_to_groups.join(groups,
                                        user_to_groups.c.gid == groups.c.gid),
                    users.c.id == user_to_groups.c.uid,
                )).where(users.c.id == uid).order_by(sa.asc(groups.c.name)))
            result = await conn.execute(query)
            return await result.fetchall()

    # here we get all user_group combinations but only the group changes
    user_groups: List[RowProxy] = await _query_db(
        uid=request[RQT_USERID_KEY], engine=request.app[APP_DB_ENGINE_KEY])

    if not user_groups:
        raise web.HTTPServerError(reason="could not find profile!")

    # get the primary group and the all group
    user_primary_group = all_group = {}
    other_groups = []
    for user_group in user_groups:
        if user_group["users_primary_gid"] == user_group["groups_gid"]:
            user_primary_group = user_group
        elif user_group["groups_type"] == GroupType.EVERYONE:
            all_group = user_group
        else:
            other_groups.append(user_group)

    parts = user_primary_group["users_name"].split(".") + [""]
    return {
        "login": user_primary_group["users_email"],
        "first_name": parts[0],
        "last_name": parts[1],
        "role": user_primary_group["users_role"].name.capitalize(),
        "gravatar_id": gravatar_hash(user_primary_group["users_email"]),
        "groups": {
            "me": {
                "gid": user_primary_group["groups_gid"],
                "label": user_primary_group["groups_name"],
                "description": user_primary_group["groups_description"],
            },
            "organizations": [{
                "gid": group["groups_gid"],
                "label": group["groups_name"],
                "description": group["groups_description"],
            } for group in other_groups],
            "all": {
                "gid": all_group["groups_gid"],
                "label": all_group["groups_name"],
                "description": all_group["groups_description"],
            },
        },
    }
Пример #29
0
async def processor_start(request: web.Request):
    """
    Start a processor from post request.

    Post params:

    - id:* id of processor instance
      max 20 chars, first char must be alphabetic or underscore, other chars must be alphabetic, digit or underscore
    - start_params: the processor specific start parameters as a json string
    - input_ref: list of reference values to the inputs to be used
    - output_ref: list of reference values to the outputs to be used
    - measurement_ref: list of reference values to the measurement inputs to be used for the inputs.
      Must be in the same order as input_ref.
    - measurement_proportion: list of scales to be used on measurement values before inputting them.
      Must be in the same order as input_ref.
    """

    post = await request.post()
    processor_id = try_get_validate(post, 'id')
    if processor_id not in request.app['processors']:
        raise web.HTTPBadRequest(
            reason=f'processor with id {processor_id} does not exist')
    processor_instance: Processor = request.app['processors'][processor_id]
    input_refs = await try_get_all(post, 'input_ref', int)
    if post.get('output_ref', '') == 'all':
        output_refs = 'all'
    else:
        output_refs = await try_get_all(post, 'output_ref', int)
    measurement_refs = await try_get_all(post, 'measurement_ref', int)
    measurement_proportions = await try_get_all(post, 'measurement_proportion',
                                                float)
    try:
        start_params = json.loads(post.get('start_params', '{}'))
    except JSONDecodeError:
        raise web.HTTPUnprocessableEntity(
            reason='Could not process start_params as json')
    if processor_instance.started:
        raise web.HTTPUnprocessableEntity(
            reason='Processor has already been started')
    try:
        processor_instance.start(
            input_refs=input_refs,
            measurement_refs=measurement_refs,
            measurement_proportions=measurement_proportions,
            output_refs=output_refs,
            start_params=start_params)
    except:
        raise web.HTTPUnprocessableEntity(
            reason=
            f'The selected Processor instance is not startable (probably crashed)'
        )
    start_results = await retrieve_processor_status(request.app,
                                                    processor_instance)
    path = os.path.join(request.app['settings'].PROCESSOR_DIR,
                        processor_id + '.json')
    with open(path, 'w') as f:
        f.write(dumps(processor_instance))
    if 'error' in start_results:
        raise web.HTTPServerError(body=dumps(start_results),
                                  content_type='application/json')
    raise web.HTTPAccepted(body=dumps(start_results),
                           content_type='application/json')