Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion bot/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
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"),
path("my-bots/", MyBots.as_view(), name="my-bots"),
path("my-bots/<int:pk>/", DeleteUpdateBot.as_view(), name="delete-bot"),
path("<int:bot>/generate-code/", GenerateCodeView.as_view(), name="generate-code"),
path("<int:bot>/deploy/", Deploy.as_view(), name="deploy"),
path("<int:bot>/log/", Log.as_view(), name="log"),
]
20 changes: 20 additions & 0 deletions bot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)