qodalis-cli-server-plugin-docker 1.0.0b39__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qodalis_cli_docker/__init__.py +44 -0
- qodalis_cli_docker/builder.py +196 -0
- qodalis_cli_docker/commands.py +1434 -0
- qodalis_cli_docker/compose.py +319 -0
- qodalis_cli_docker/console/extension.css +32 -0
- qodalis_cli_docker/console/extension.js +89 -0
- qodalis_cli_docker/console_page.py +28 -0
- qodalis_cli_docker/controller.py +309 -0
- qodalis_cli_docker/engine.py +482 -0
- qodalis_cli_docker/exec_socket.py +246 -0
- qodalis_cli_docker/mcp_tools.py +149 -0
- qodalis_cli_docker/plugin.py +36 -0
- qodalis_cli_docker/service.py +2007 -0
- qodalis_cli_docker/settings.py +172 -0
- qodalis_cli_docker/shapes.py +317 -0
- qodalis_cli_server_plugin_docker-1.0.0b39.dist-info/METADATA +300 -0
- qodalis_cli_server_plugin_docker-1.0.0b39.dist-info/RECORD +20 -0
- qodalis_cli_server_plugin_docker-1.0.0b39.dist-info/WHEEL +4 -0
- qodalis_cli_server_plugin_docker-1.0.0b39.dist-info/entry_points.txt +2 -0
- qodalis_cli_server_plugin_docker-1.0.0b39.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Qodalis CLI Docker plugin: containers, images, compose stacks, volumes and networks through the Docker Engine API, in the
|
|
2
|
+
Qodalis Console, the terminal, over MCP and at ``/api/v1/qcli/docker``."""
|
|
3
|
+
|
|
4
|
+
#: The package's icon on the Qodalis Console's plugins page (``CliInstalledPlugin.icon``).
|
|
5
|
+
__plugin_icon__ = "🐳"
|
|
6
|
+
|
|
7
|
+
from .builder import DOCKER_PERMISSION_ENTRIES, CliDockerBuilder, CliDockerPlugin
|
|
8
|
+
from .commands import DockerModule
|
|
9
|
+
from .console_page import DOCKER_CONSOLE_PAGE, DOCKER_CONSOLE_PREFIX
|
|
10
|
+
from .controller import DOCKER_ADMIN, DOCKER_EXEC, DOCKER_MANAGE, DOCKER_PREFIX, DOCKER_READ, create_docker_router
|
|
11
|
+
from .engine import DockerError, DockerHostConfig, EngineClient, parse_endpoint
|
|
12
|
+
from .exec_socket import EXEC_SOCKET_PATH, create_exec_handler
|
|
13
|
+
from .service import UNPROTECTED_TEXT, DockerCaller, DockerService, read_compose_file
|
|
14
|
+
from .shapes import container_name
|
|
15
|
+
from .settings import DockerSettings, docker_settings, read_docker_settings
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"DOCKER_ADMIN",
|
|
19
|
+
"DOCKER_CONSOLE_PAGE",
|
|
20
|
+
"DOCKER_CONSOLE_PREFIX",
|
|
21
|
+
"DOCKER_EXEC",
|
|
22
|
+
"DOCKER_MANAGE",
|
|
23
|
+
"DOCKER_PERMISSION_ENTRIES",
|
|
24
|
+
"DOCKER_PREFIX",
|
|
25
|
+
"DOCKER_READ",
|
|
26
|
+
"UNPROTECTED_TEXT",
|
|
27
|
+
"EXEC_SOCKET_PATH",
|
|
28
|
+
"CliDockerBuilder",
|
|
29
|
+
"CliDockerPlugin",
|
|
30
|
+
"DockerCaller",
|
|
31
|
+
"DockerError",
|
|
32
|
+
"DockerHostConfig",
|
|
33
|
+
"DockerModule",
|
|
34
|
+
"DockerService",
|
|
35
|
+
"DockerSettings",
|
|
36
|
+
"EngineClient",
|
|
37
|
+
"container_name",
|
|
38
|
+
"create_docker_router",
|
|
39
|
+
"create_exec_handler",
|
|
40
|
+
"docker_settings",
|
|
41
|
+
"parse_endpoint",
|
|
42
|
+
"read_compose_file",
|
|
43
|
+
"read_docker_settings",
|
|
44
|
+
]
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""Fluent builder of the Docker plugin: containers, images, compose stacks, volumes and networks through the Docker Engine
|
|
2
|
+
API, in the Console's Docker page, the terminal (``docker``), over MCP, at ``/api/v1/qcli/docker`` and with an interactive
|
|
3
|
+
exec WebSocket (``/ws/v1/qcli/docker/exec``)."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from datetime import datetime
|
|
10
|
+
from typing import Any, Awaitable, Callable, Optional
|
|
11
|
+
|
|
12
|
+
from fastapi import APIRouter
|
|
13
|
+
|
|
14
|
+
from qodalis_cli_server_abstractions import CliEventAudience, CliRoutePermission
|
|
15
|
+
|
|
16
|
+
from .commands import DockerModule
|
|
17
|
+
from .console_page import docker_page
|
|
18
|
+
from .controller import (
|
|
19
|
+
DOCKER_ADMIN, DOCKER_EXEC, DOCKER_MANAGE, DOCKER_PREFIX, DOCKER_READ, create_docker_router,
|
|
20
|
+
)
|
|
21
|
+
from .exec_socket import EXEC_SOCKET_PATH, EXEC_SOCKET_PATH_UNVERSIONED, create_exec_handler
|
|
22
|
+
from .service import DockerService
|
|
23
|
+
from .settings import DockerSettings
|
|
24
|
+
|
|
25
|
+
#: The permissions the plugin adds to the server's catalog.
|
|
26
|
+
DOCKER_PERMISSION_ENTRIES: list[dict[str, str]] = [
|
|
27
|
+
{"name": DOCKER_READ, "description": "See Docker containers, images, stacks, volumes, networks, logs and stats", "group": "Docker"},
|
|
28
|
+
{"name": DOCKER_MANAGE, "description": "Start, stop and restart containers and stacks, pull images", "group": "Docker"},
|
|
29
|
+
{"name": DOCKER_EXEC, "description": "Run commands inside containers", "group": "Docker"},
|
|
30
|
+
{"name": DOCKER_ADMIN, "description": "Remove containers and images, prune, create and remove volumes and networks", "group": "Docker"},
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class CliDockerPlugin:
|
|
36
|
+
"""The built plugin: mount it with ``CliServerResult.mount_plugin`` (its ``module`` is the commands)."""
|
|
37
|
+
|
|
38
|
+
prefix: str
|
|
39
|
+
router: APIRouter
|
|
40
|
+
#: ``docker:read`` / ``docker:manage`` on the prefix, ``docker:exec`` on ``/exec``, ``docker:admin`` on ``/admin``.
|
|
41
|
+
route_permissions: list[CliRoutePermission]
|
|
42
|
+
#: The ``docker`` terminal commands.
|
|
43
|
+
module: DockerModule
|
|
44
|
+
permissions: list[dict[str, str]]
|
|
45
|
+
#: The service the routes, commands, socket and MCP tools share.
|
|
46
|
+
service: DockerService
|
|
47
|
+
#: The Console's Docker page and Overview card (``/qcli/modules/docker``), when a build is found.
|
|
48
|
+
console_pages: list[Any] = field(default_factory=list)
|
|
49
|
+
event_bus: Any = None
|
|
50
|
+
#: Seconds between the session / ``docker:exec`` re-checks of an open exec socket (tests; default: the auth plugin's, 30).
|
|
51
|
+
exec_check_interval: Optional[float] = None
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def event_personalizers(self) -> list[Any]:
|
|
55
|
+
"""Per-socket reduction of ``docker:operation``: a socket without ``docker:admin`` gets no log and no message of a
|
|
56
|
+
``stack-up`` (compose output can quote values)."""
|
|
57
|
+
service = self.service
|
|
58
|
+
|
|
59
|
+
def personalize(message: dict[str, Any], can: Callable[[str], bool], principal: Any = None) -> Optional[dict[str, Any]]:
|
|
60
|
+
# a socket without a principal sees the log only when the server allows unprotected callers
|
|
61
|
+
if principal is None:
|
|
62
|
+
allowed = service.settings.allow_unprotected is True
|
|
63
|
+
else:
|
|
64
|
+
allowed = bool(can(DOCKER_ADMIN))
|
|
65
|
+
return message if allowed else service.operation_for_readers(message)
|
|
66
|
+
|
|
67
|
+
return [("docker:operation", personalize)]
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def mcp_providers(self) -> list[Any]:
|
|
71
|
+
"""The ``docker_*`` MCP tools, served when the server's MCP endpoint is on."""
|
|
72
|
+
try:
|
|
73
|
+
from .mcp_tools import DockerMcpToolProvider
|
|
74
|
+
except ImportError: # without the server package
|
|
75
|
+
return []
|
|
76
|
+
return [DockerMcpToolProvider(self.service)]
|
|
77
|
+
|
|
78
|
+
def dispose(self) -> None:
|
|
79
|
+
"""Called when the server stops: kills the compose processes that still run."""
|
|
80
|
+
self.service.dispose()
|
|
81
|
+
|
|
82
|
+
def set_event_bus(self, bus: Any) -> None:
|
|
83
|
+
self.event_bus = bus
|
|
84
|
+
|
|
85
|
+
def on_mount(self, server: Any) -> None:
|
|
86
|
+
"""Hook called by ``CliServerResult.mount_plugin``: operations publish ``docker:operation`` on the server's event bus,
|
|
87
|
+
and the exec WebSocket joins the server's own sockets (the versioned and the unversioned path)."""
|
|
88
|
+
if self.event_bus is None:
|
|
89
|
+
self.event_bus = getattr(server, "event_bus", None)
|
|
90
|
+
app = getattr(server, "app", None)
|
|
91
|
+
options = getattr(server, "options", None)
|
|
92
|
+
if app is None or getattr(options, "cli_routes", True) is False:
|
|
93
|
+
return
|
|
94
|
+
def users() -> Any:
|
|
95
|
+
services = getattr(getattr(server, "builder", None), "plugin_services", None)
|
|
96
|
+
return services.get("users") if services is not None else None
|
|
97
|
+
|
|
98
|
+
handler = create_exec_handler(self.service, users, self.exec_check_interval)
|
|
99
|
+
routes = app.router.routes
|
|
100
|
+
before = len(routes)
|
|
101
|
+
for path in (EXEC_SOCKET_PATH, EXEC_SOCKET_PATH_UNVERSIONED):
|
|
102
|
+
app.add_api_websocket_route(path, handler)
|
|
103
|
+
added = routes[before:]
|
|
104
|
+
# ahead of anything a host added before (a catch-all)
|
|
105
|
+
routes[:] = added + routes[:before]
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class CliDockerBuilder:
|
|
109
|
+
"""Fluent builder for the Docker plugin.
|
|
110
|
+
|
|
111
|
+
Example::
|
|
112
|
+
|
|
113
|
+
result.mount_plugin(CliDockerBuilder().set_settings(lambda: DockerSettings(endpoint="unix:///var/run/docker.sock")).build())
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
def __init__(self) -> None:
|
|
117
|
+
self._settings: Callable[[], DockerSettings] = lambda: DockerSettings()
|
|
118
|
+
self._audit: Optional[Callable[[dict[str, Any]], Awaitable[None]]] = None
|
|
119
|
+
self._console_dir: str | bool | None = None
|
|
120
|
+
self._stats_gap: Optional[float] = None
|
|
121
|
+
self._now: Optional[Callable[[], datetime]] = None
|
|
122
|
+
self._new_id: Optional[Callable[[], str]] = None
|
|
123
|
+
self._pull_silence: Optional[float] = None
|
|
124
|
+
self._compose_timeout: Optional[float] = None
|
|
125
|
+
self._compose_down_timeout: Optional[float] = None
|
|
126
|
+
self._exec_check_interval: Optional[float] = None
|
|
127
|
+
self._logger: Optional[logging.Logger] = None
|
|
128
|
+
self._event_bus: Any = None
|
|
129
|
+
|
|
130
|
+
def set_settings(self, settings: "DockerSettings | Callable[[], DockerSettings]") -> "CliDockerBuilder":
|
|
131
|
+
"""The settings, read on every call (a function reading the plugin's config provider)."""
|
|
132
|
+
self._settings = settings if callable(settings) else (lambda: settings) # type: ignore[assignment,return-value]
|
|
133
|
+
return self
|
|
134
|
+
|
|
135
|
+
def set_audit(self, audit: Callable[[dict[str, Any]], Awaitable[None]]) -> "CliDockerBuilder":
|
|
136
|
+
"""Where the changes are audited (``context.audit`` of the plugin entry); none by default."""
|
|
137
|
+
self._audit = audit
|
|
138
|
+
return self
|
|
139
|
+
|
|
140
|
+
def set_console_dir(self, directory: "str | bool") -> "CliDockerBuilder":
|
|
141
|
+
"""Where the Console's Docker page is (``@qodalis/console-module-docker``'s build); by default the copy in this package
|
|
142
|
+
(``console/``, ``scripts/copy_console.py``). ``False`` leaves the page out."""
|
|
143
|
+
self._console_dir = directory
|
|
144
|
+
return self
|
|
145
|
+
|
|
146
|
+
def set_logger(self, logger: logging.Logger) -> "CliDockerBuilder":
|
|
147
|
+
self._logger = logger
|
|
148
|
+
return self
|
|
149
|
+
|
|
150
|
+
def set_event_bus(self, bus: Any) -> "CliDockerBuilder":
|
|
151
|
+
self._event_bus = bus
|
|
152
|
+
return self
|
|
153
|
+
|
|
154
|
+
def set_testing(
|
|
155
|
+
self, *, stats_gap_seconds: Optional[float] = None, now: Optional[Callable[[], datetime]] = None,
|
|
156
|
+
new_id: Optional[Callable[[], str]] = None, pull_silence_seconds: Optional[float] = None,
|
|
157
|
+
compose_timeout_seconds: Optional[float] = None, compose_down_timeout_seconds: Optional[float] = None,
|
|
158
|
+
exec_check_interval_seconds: Optional[float] = None,
|
|
159
|
+
) -> "CliDockerBuilder":
|
|
160
|
+
"""Tests: the pause between the two stats samples, the clock, the operation ids and how a compose file is read."""
|
|
161
|
+
self._stats_gap, self._now, self._new_id, self._pull_silence = stats_gap_seconds, now, new_id, pull_silence_seconds
|
|
162
|
+
self._compose_timeout, self._compose_down_timeout = compose_timeout_seconds, compose_down_timeout_seconds
|
|
163
|
+
self._exec_check_interval = exec_check_interval_seconds
|
|
164
|
+
return self
|
|
165
|
+
|
|
166
|
+
def build(self) -> CliDockerPlugin:
|
|
167
|
+
holder: dict[str, CliDockerPlugin] = {}
|
|
168
|
+
|
|
169
|
+
async def publish(event_type: str, data: dict[str, Any], permission: str) -> None:
|
|
170
|
+
bus = holder["plugin"].event_bus if "plugin" in holder else self._event_bus
|
|
171
|
+
if bus is not None:
|
|
172
|
+
await bus.publish(event_type, data, CliEventAudience(permission=permission))
|
|
173
|
+
|
|
174
|
+
service = DockerService(
|
|
175
|
+
self._settings, audit=self._audit, publish=publish, log=self._logger, now=self._now, new_id=self._new_id,
|
|
176
|
+
stats_gap_seconds=self._stats_gap, pull_silence_seconds=self._pull_silence,
|
|
177
|
+
compose_timeout_seconds=self._compose_timeout, compose_down_timeout_seconds=self._compose_down_timeout,
|
|
178
|
+
)
|
|
179
|
+
page = docker_page(self._console_dir)
|
|
180
|
+
plugin = CliDockerPlugin(
|
|
181
|
+
prefix=DOCKER_PREFIX,
|
|
182
|
+
router=create_docker_router(service),
|
|
183
|
+
route_permissions=[
|
|
184
|
+
CliRoutePermission(DOCKER_PREFIX, DOCKER_READ, DOCKER_MANAGE),
|
|
185
|
+
CliRoutePermission(f"{DOCKER_PREFIX}/exec", DOCKER_EXEC, DOCKER_EXEC),
|
|
186
|
+
CliRoutePermission(f"{DOCKER_PREFIX}/admin", DOCKER_ADMIN, DOCKER_ADMIN),
|
|
187
|
+
],
|
|
188
|
+
module=DockerModule(service),
|
|
189
|
+
permissions=[dict(p) for p in DOCKER_PERMISSION_ENTRIES],
|
|
190
|
+
service=service,
|
|
191
|
+
console_pages=[page] if page else [],
|
|
192
|
+
event_bus=self._event_bus,
|
|
193
|
+
exec_check_interval=self._exec_check_interval,
|
|
194
|
+
)
|
|
195
|
+
holder["plugin"] = plugin
|
|
196
|
+
return plugin
|