Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dynamic tasks #8

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
43d0c23
Update requirements
Rubikoid Jul 10, 2024
e4d88a6
Update and fix tests.
Rubikoid Jul 10, 2024
03832ea
Small refactoring in a few places
Rubikoid Jul 10, 2024
d3a606f
Add FastUI PoC
Rubikoid Jul 10, 2024
2346502
Drop old db useless code
Rubikoid Jul 10, 2024
141df5a
A little refactor root of app
Rubikoid Jul 10, 2024
f09de6f
Init dynamic_tasks_app - microservice for dynamic task manage
Rubikoid Jul 14, 2024
bcf4b36
Update readme.md
Rubikoid Jul 16, 2024
8ed3b68
Add pydantic-yaml parser
Rubikoid Jul 22, 2024
a4f807e
Add extremly simple docker-compose.yaml files parser
Rubikoid Jul 22, 2024
1901acf
Heavy work on implementing service runner
Rubikoid Jul 22, 2024
290a949
Add & update some of examples for testing system
Rubikoid Jul 22, 2024
a4dd8ad
Add static tasks builder manager
Rubikoid Jul 22, 2024
64f4876
Update dynamic tasks req.txt
Rubikoid Jul 22, 2024
321f2cc
WIP: Move from NodePort
Rubikoid Jul 25, 2024
19aa2cc
Don't rebuild already builded images
Rubikoid Aug 4, 2024
f0b7a3f
Dev dynamic tasks app
Rubikoid Aug 7, 2024
3e707fe
A little refactor api_tasks
Rubikoid Aug 7, 2024
855955c
Prepare api_dynamic_tasks
Rubikoid Aug 7, 2024
3ad9c23
Some fixes in dynamic tasks svc
Rubikoid Aug 11, 2024
27e81c8
Make PoC for using dynamic tasks from frontend
Rubikoid Aug 11, 2024
84cbd81
Pass flags to tasks
Rubikoid Aug 11, 2024
8a3e9ed
Some fixes
Rubikoid Aug 11, 2024
0ae65cc
Make (dynamic) flags check better.
Rubikoid Aug 11, 2024
3bad917
CLI fixes
Rubikoid Aug 11, 2024
5599f56
Drop requests lib (httpx we friends now)
Rubikoid Aug 11, 2024
808eceb
Some fixes
Rubikoid Aug 11, 2024
ccc6f2e
made changes for some event
anfinogenov Aug 15, 2024
2beb993
Migrate from tonns of req.txt to one pyproject.toml
Rubikoid Aug 15, 2024
0889d56
Make service source hashing stable
Rubikoid Aug 15, 2024
d50c076
Add lock to BaseConnector
Rubikoid Aug 16, 2024
764a355
Protect expiration controller with lock
Rubikoid Aug 16, 2024
78ac9b0
Make stop method work
Rubikoid Aug 16, 2024
6062a6f
Support ipv6 links
Rubikoid Aug 16, 2024
24c598e
Implement _stop in base connector
Rubikoid Aug 16, 2024
cae0e8f
Add ipFamilyPolicy="PreferDualStack" to k8s service
Rubikoid Aug 16, 2024
1e58300
Remove flag_sign_key
Rubikoid Aug 16, 2024
f647c88
upd release notes
Rubikoid Aug 16, 2024
caf9f2b
Merge pull request #10 from kksctf/feature/front-usability-fixes
Rubikoid Aug 17, 2024
2bde268
Fix authors without @
Rubikoid Aug 16, 2024
3d51e1e
Add pdm lock
Rubikoid Aug 18, 2024
a6d443b
Update test
Rubikoid Aug 18, 2024
6e9310d
Many fixes
Rubikoid Aug 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ celerybeat.pid
# Environments
.env
.venv
.linvenv
env/
venv/
ENV/
Expand Down Expand Up @@ -147,5 +148,10 @@ cython_debug/

logs/

yatb_prod

# env
yatb.env
k3s.yaml
dynamic_tasks_app/wtf
yatb_state.json
35 changes: 18 additions & 17 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import logging
from contextlib import asynccontextmanager
from pathlib import Path

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

from . import utils
from . import api, main, utils, view
from .api.api_dynamic_tasks import __client
from .config import settings
from .db import beanie


@asynccontextmanager
async def lifespan(app: FastAPI):
await beanie.db.init()
async with __client:
try:
yield
finally:
await beanie.db.close()


app = FastAPI(
docs_url=settings.FASTAPI_DOCS_URL,
redoc_url=settings.FASTAPI_REDOC_URL,
openapi_url=settings.FASTAPI_OPENAPI_URL,
lifespan=lifespan,
)

_base_path = Path(__file__).resolve().parent
Expand All @@ -25,12 +40,10 @@
# for i in loggers:
# print(f"LOGGER: {i}")

from . import api # noqa
from . import main # noqa
from . import view # noqa

main.setup_utils(app)
app.include_router(api.router)
app.include_router(view.router)
app.include_router(view.admin.api_rotuer)

# prometheus
from prometheus_fastapi_instrumentator import Instrumentator # noqa
Expand All @@ -43,15 +56,3 @@
env_var_name="ENABLE_METRICS",
)
instrumentator.instrument(app).expose(app, endpoint=expose_url)
# utils.metrics.bad_solves_per_user

"""
@app.on_event("startup")
def startup_event():
metrics.load_all_metrics()


@app.on_event("shutdown")
def shutdown_event():
metrics.save_all_metrics()
"""
2 changes: 2 additions & 0 deletions app/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
from . import api_auth # noqa
from . import api_tasks # noqa
from . import api_users # noqa
from . import api_dynamic_tasks # noqa

api_users.router.include_router(api_auth.router)
router.include_router(api_users.router)
router.include_router(api_tasks.router)
router.include_router(api_dynamic_tasks.router)
router.include_router(admin.router)
8 changes: 4 additions & 4 deletions app/api/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ async def admin_checker(
user: auth.CURR_USER_SAFE,
token_header: str | None = Header(None, alias="X-Token"),
token_query: str | None = Query(None, alias="token"),
) -> schema.User:
) -> UserDB:
if user and user.is_admin:
return user

if token_header and token_header == settings.API_TOKEN:
return _fake_admin_user
return _fake_admin_user # type: ignore
if token_query and token_query == settings.API_TOKEN:
return _fake_admin_user
return _fake_admin_user # type: ignore

raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="No.",
)


CURR_ADMIN = Annotated[schema.User, Depends(admin_checker)]
CURR_ADMIN = Annotated[UserDB, Depends(admin_checker)]

logger = get_logger("api.admin")
router = APIRouter(
Expand Down
3 changes: 2 additions & 1 deletion app/api/admin/admin_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid
from typing import Annotated, Mapping
from collections.abc import Mapping
from typing import Annotated

from beanie import BulkWriter
from beanie.operators import Set
Expand Down
36 changes: 18 additions & 18 deletions app/api/admin/admin_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def api_admin_users_internal() -> Mapping[uuid.UUID, schema.User]:
return all_users


async def api_admin_user_get_internal(user_id: uuid.UUID) -> UserDB:
async def get_user(user_id: uuid.UUID) -> UserDB:
user = await UserDB.find_by_user_uuid(user_id)
if not user:
raise HTTPException(
Expand All @@ -27,29 +27,33 @@ async def api_admin_user_get_internal(user_id: uuid.UUID) -> UserDB:
return user


CURR_USER = Annotated[UserDB, Depends(get_user)]


class PasswordChangeForm(BaseModel):
new_password: str


@router.get("/user/{user_id}")
async def api_admin_user(user_id: uuid.UUID, user: CURR_ADMIN) -> schema.User.admin_model:
ret_user = await api_admin_user_get_internal(user_id)
return ret_user
async def api_admin_user(admin: CURR_ADMIN, user: CURR_USER) -> schema.User.admin_model:
return user


@router.post("/user/{user_id}")
async def api_admin_user_edit(new_user: schema.User, user_id: uuid.UUID, user: CURR_ADMIN) -> schema.User.admin_model:
new_user = await db.update_user_admin(user_id, new_user)
async def api_admin_user_edit(new_user: schema.User, user_id: uuid.UUID, admin: CURR_ADMIN) -> schema.User.admin_model:
# new_user = await db.update_user_admin(user_id, new_user)
raise Exception

return new_user


@router.get("/users/me")
async def api_admin_users_me(user: CURR_ADMIN) -> schema.User.admin_model:
return user
async def api_admin_users_me(admin: CURR_ADMIN) -> schema.User.admin_model:
return admin


@router.get("/users")
async def api_admin_users(user: CURR_ADMIN) -> Mapping[uuid.UUID, schema.User.admin_model]:
async def api_admin_users(admin: CURR_ADMIN) -> Mapping[uuid.UUID, schema.User.admin_model]:
all_users = await api_admin_users_internal()
return all_users

Expand All @@ -58,7 +62,7 @@ async def api_admin_users(user: CURR_ADMIN) -> Mapping[uuid.UUID, schema.User.ad
async def api_admin_user_edit_password(
new_password: PasswordChangeForm,
admin: CURR_ADMIN,
user: schema.User = Depends(api_admin_user_get_internal),
user: CURR_USER,
) -> schema.User.admin_model:
au = user.auth_source
if not isinstance(au, schema.auth.SimpleAuth.AuthModel):
Expand All @@ -73,7 +77,7 @@ async def api_admin_user_edit_password(
@router.get("/user/{user_id}/score")
async def api_admin_user_recalc_score(
admin: CURR_ADMIN,
user: UserDB = Depends(api_admin_user_get_internal),
user: CURR_USER,
) -> schema.User.admin_model:
await user.recalc_score_one()
return user
Expand All @@ -82,18 +86,14 @@ async def api_admin_user_recalc_score(
@router.delete("/user/{user_id}")
async def api_admin_user_delete(
admin: CURR_ADMIN,
user: schema.User = Depends(api_admin_user_get_internal),
user: CURR_USER,
) -> str:
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="user not exist",
)
raise Exception

if len(user.solved_tasks) > 0:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="user have solved tasks",
)
await db.delete_user(user)
# await db.delete_user(user)
return "deleted"
Loading