From 7ae8c9acb7dda2a4e27604e9efc6e3565ab5ccee Mon Sep 17 00:00:00 2001 From: Ali Kamali Date: Thu, 5 Jun 2025 13:01:35 +0330 Subject: [PATCH] feat: add container logs --- bot/urls.py | 10 +++++++++- bot/views.py | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bot/urls.py b/bot/urls.py index 81b0f95..6a958e6 100644 --- a/bot/urls.py +++ b/bot/urls.py @@ -1,6 +1,13 @@ from django.urls import path -from bot.views import CreateBotView, DeleteUpdateBot, Deploy, GenerateCodeView, MyBots +from bot.views import ( + CreateBotView, + DeleteUpdateBot, + Deploy, + GenerateCodeView, + Log, + MyBots, +) urlpatterns = [ path("create-bot/", CreateBotView.as_view(), name="create-bot"), @@ -8,4 +15,5 @@ path("my-bots//", DeleteUpdateBot.as_view(), name="delete-bot"), path("/generate-code/", GenerateCodeView.as_view(), name="generate-code"), path("/deploy/", Deploy.as_view(), name="deploy"), + path("/log/", Log.as_view(), name="log"), ] diff --git a/bot/views.py b/bot/views.py index b12ad78..91f2226 100644 --- a/bot/views.py +++ b/bot/views.py @@ -124,3 +124,23 @@ def get(self, request: Request, bot: int) -> Response: }, status=status.HTTP_202_ACCEPTED, ) + + +class Log(APIView): + def get(self, request: Request, bot: int) -> Response: + try: + bot_instance = Bot.objects.get(id=bot, user=request.iam_user) + except Bot.DoesNotExist: + raise ValidationError( + "Bot not found or you don't have permission to access it", + ) + + client = docker.from_env() + container_name = f"bot-container-{bot}" + container = client.containers.get(container_name) + logs = container.logs().decode("utf-8") + + return Response( + {"logs": logs}, + status=status.HTTP_200_OK, + )