-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
157 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,26 @@ | ||
from threading import Thread | ||
|
||
from rich.text import Text | ||
from textual.app import ComposeResult | ||
from textual.containers import Container | ||
from textual.widgets import Label, TabPane, Button, RichLog | ||
import docker | ||
from textual.containers import Horizontal | ||
from textual.widgets import TabPane, Button, Select | ||
|
||
from models import Project | ||
import subprocess | ||
from textual.widgets import DataTable | ||
from textual import work, on | ||
from textual import on | ||
|
||
from presentation.docker.container_table import ContainerTable | ||
from presentation.docker.container_log_widget import ContainerLogWidget | ||
from presentation.docker.container_select import ContainerSelect | ||
|
||
|
||
class DockerPan(TabPane): | ||
def __init__(self, project: Project, **kwargs): | ||
self.project = project | ||
super().__init__(**kwargs, title="Docker", id="docker-pan") | ||
self.data_table = ContainerTable(title="Containers", project=self.project, classes="table_container") | ||
self.docker_logs = RichLog( | ||
id="docker_log", | ||
highlight=True, | ||
markup=True, | ||
classes="modal_container", | ||
) | ||
self.docker_logs = ContainerLogWidget() | ||
|
||
def compose(self) -> ComposeResult: | ||
with Container(id="project_docker"): | ||
with Horizontal(id="docker_container_select_container"): | ||
yield ContainerSelect() | ||
yield Button.success(" Refresh", id="docker_refresh") | ||
yield self.data_table | ||
yield self.docker_logs | ||
|
||
@on(Button.Pressed, "#docker_refresh") | ||
def action_refresh(self): | ||
self.data_table.refresh_containers() | ||
|
||
@on(DataTable.RowSelected) | ||
def on_docker_container_selected(self, event: DataTable.RowSelected) -> None: | ||
container_name = event.row_key.value | ||
self.stream_logs(container_name) | ||
|
||
@work(exclusive=True, thread=True) | ||
def stream_logs(self, container_id: str): | ||
# Create a Docker client | ||
self.docker_logs.clear() | ||
self.docker_logs.write(f"Logs for container {container_id}:\n") | ||
client = docker.from_env() | ||
container = client.containers.get(container_id) | ||
yield self.docker_logs | ||
|
||
# Stream the logs from the container | ||
for log in container.logs(stream=True, follow=True): | ||
# Convert bytes to string and update the logs widget | ||
log_message = log.decode('utf-8').strip() | ||
self.docker_logs.write(log_message) | ||
@on(Select.Changed) | ||
def select_changed(self, event: Select.Changed) -> None: | ||
self.docker_logs.stream_logs(event.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from textual.widgets import RichLog | ||
from textual import work | ||
|
||
from service_locator import Container | ||
|
||
|
||
class ContainerLogWidget(RichLog): | ||
def __init__(self, **kargs): | ||
super().__init__( | ||
id="docker_log", | ||
highlight=True, | ||
markup=True, | ||
**kargs) | ||
|
||
@work(exclusive=True, thread=True) | ||
def stream_logs(self, container_id: str): | ||
self.clear() | ||
self.border_title = f"Logs for container {container_id}" | ||
|
||
for log in Container.docker_client().get_container_logs(container_id): | ||
# Convert bytes to string and update the logs widget | ||
self.write(log.decode('utf-8').strip()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from textual.widgets import Select | ||
|
||
from service_locator import Container | ||
|
||
|
||
class ContainerSelect(Select): | ||
def __init__(self, **kargs): | ||
super().__init__( | ||
((docker_container.name, docker_container.id) for docker_container in Container.docker_client().get_running_containers()), | ||
**kargs | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from dependency_injector import containers, providers | ||
|
||
from services import DockerClient | ||
|
||
|
||
class Container(containers.DeclarativeContainer): | ||
config = providers.Configuration() | ||
docker_client = providers.Singleton(DockerClient) | ||
|
||
# api_client = providers.Singleton( | ||
# ApiClient, | ||
# api_key=config.api_key, | ||
# timeout=config.timeout, | ||
# ) | ||
# | ||
# service = providers.Factory( | ||
# Service, | ||
# api_client=api_client, | ||
# ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .docker import DockerClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import docker | ||
|
||
class DockerClient: | ||
def __init__(self): | ||
self.client = docker.from_env() | ||
|
||
def get_running_containers(self) -> list: | ||
"""Fetches a list of running containers.""" | ||
return self.client.containers.list() | ||
|
||
def get_container_logs(self, container_id): | ||
"""Fetches logs from a specific container.""" | ||
container = self.client.containers.get(container_id) | ||
return container.logs(stream=True, follow=True) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.