попытка сделать изоляцию
This commit is contained in:
parent
a1235cf255
commit
59f6e5bc4e
4 changed files with 27 additions and 14 deletions
21
Dockerfile
21
Dockerfile
|
|
@ -1,17 +1,18 @@
|
||||||
FROM python:3.14-slim as base
|
FROM python:3.14-slim AS base
|
||||||
|
|
||||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
PYTHONUNBUFFERED=1
|
PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN apt update && apt install make -y
|
RUN apt update && apt install make -y
|
||||||
|
|
||||||
ENV AGENT_USER="agent"
|
ENV AGENT_USER="agent"
|
||||||
RUN useradd --shell /bin/bash agent
|
|
||||||
ENV WORKSPACE_DIR="/workspace/"
|
ENV WORKSPACE_DIR="/workspace/"
|
||||||
RUN mkdir -p $WORKSPACE_DIR && chown $AGENT_USER:$AGENT_USER $WORKSPACE_DIR
|
RUN useradd --shell /bin/bash $AGENT_USER \
|
||||||
|
&& mkdir -p $WORKSPACE_DIR /home/$AGENT_USER \
|
||||||
|
&& chown -R agent:agent $WORKSPACE_DIR /home/$AGENT_USER
|
||||||
|
|
||||||
FROM base as builder
|
FROM base AS builder
|
||||||
|
|
||||||
RUN apt install git -y
|
RUN apt install git -y
|
||||||
RUN pip install uv
|
RUN pip install uv
|
||||||
|
|
@ -20,7 +21,7 @@ COPY pyproject.toml uv.lock ./
|
||||||
RUN uv sync --frozen --no-install-project --no-dev
|
RUN uv sync --frozen --no-install-project --no-dev
|
||||||
RUN uv pip install git+https://git.lambda.coredump.ru/platform/agent_api.git
|
RUN uv pip install git+https://git.lambda.coredump.ru/platform/agent_api.git
|
||||||
|
|
||||||
FROM base as production
|
FROM base AS production
|
||||||
|
|
||||||
COPY --from=builder /app/.venv /app/.venv
|
COPY --from=builder /app/.venv /app/.venv
|
||||||
ENV PATH="/app/.venv/bin:$PATH"
|
ENV PATH="/app/.venv/bin:$PATH"
|
||||||
|
|
@ -28,12 +29,15 @@ ENV PATH="/app/.venv/bin:$PATH"
|
||||||
COPY src/ /app/src/
|
COPY src/ /app/src/
|
||||||
COPY Makefile ./
|
COPY Makefile ./
|
||||||
COPY .mk/ ./.mk/
|
COPY .mk/ ./.mk/
|
||||||
|
RUN chown root:root /app && chmod 700 /app
|
||||||
|
RUN apt install sudo -y && \
|
||||||
|
echo "agent ALL=(ALL) NOPASSWD: /usr/bin/apt*" >> /etc/sudoers
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
CMD ["make", "uvicorn-prod"]
|
CMD ["make", "uvicorn-prod"]
|
||||||
|
|
||||||
FROM base as development
|
FROM base AS development
|
||||||
|
|
||||||
RUN pip install uv
|
RUN pip install uv
|
||||||
|
|
||||||
|
|
@ -47,6 +51,9 @@ ENV PATH="/app/.venv/bin:$PATH"
|
||||||
|
|
||||||
COPY Makefile ./
|
COPY Makefile ./
|
||||||
COPY .mk/ ./.mk/
|
COPY .mk/ ./.mk/
|
||||||
|
RUN chown root:root /app && chmod 700 /app
|
||||||
|
RUN apt install sudo -y && \
|
||||||
|
echo "agent ALL=(ALL) NOPASSWD: /usr/bin/apt*" >> /etc/sudoers
|
||||||
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,5 +23,9 @@ services:
|
||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
|
cap_add: # для работы bwrap
|
||||||
|
- SYS_ADMIN
|
||||||
|
security_opt: # для работы bwrap
|
||||||
|
- seccomp:unconfined
|
||||||
profiles:
|
profiles:
|
||||||
- dev
|
- dev
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import pwd
|
||||||
import subprocess
|
import subprocess
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from deepagents.backends.local_shell import LocalShellBackend, DEFAULT_EXECUTE_TIMEOUT
|
from deepagents.backends.local_shell import LocalShellBackend
|
||||||
|
|
||||||
|
|
||||||
class IsolatedShellBackend(LocalShellBackend):
|
class IsolatedShellBackend(LocalShellBackend):
|
||||||
|
|
@ -34,8 +34,9 @@ class IsolatedShellBackend(LocalShellBackend):
|
||||||
f"timeout must be positive, got {effective_timeout}"
|
f"timeout must be positive, got {effective_timeout}"
|
||||||
)
|
)
|
||||||
|
|
||||||
proc: subprocess.Popen[str] | None = None
|
proc: subprocess.Popen | None = None
|
||||||
try:
|
try:
|
||||||
|
print(f"Running shell: {command}")
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
command,
|
command,
|
||||||
shell=True,
|
shell=True,
|
||||||
|
|
@ -69,12 +70,13 @@ class IsolatedShellBackend(LocalShellBackend):
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
output = f"{output.rstrip()}\n\nExit code: {proc.returncode}"
|
output = f"{output.rstrip()}\n\nExit code: {proc.returncode}"
|
||||||
|
|
||||||
return self._make_response(output, proc.returncode, truncated)
|
result = self._make_response(output, proc.returncode, truncated)
|
||||||
|
print(result)
|
||||||
|
return result
|
||||||
|
|
||||||
except subprocess.TimeoutExpired:
|
except subprocess.TimeoutExpired:
|
||||||
if proc:
|
proc.kill()
|
||||||
proc.kill()
|
proc.communicate()
|
||||||
proc.communicate()
|
|
||||||
msg = f"Error: Command timed out after {effective_timeout} seconds."
|
msg = f"Error: Command timed out after {effective_timeout} seconds."
|
||||||
return self._make_response(msg, 124, False)
|
return self._make_response(msg, 124, False)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ def create_agent():
|
||||||
)
|
)
|
||||||
|
|
||||||
workspace_dir = os.environ["WORKSPACE_DIR"]
|
workspace_dir = os.environ["WORKSPACE_DIR"]
|
||||||
agent_user = os.environ["AGENT_USER"]
|
agent_user = os.environ.get("AGENT_USER", "agent")
|
||||||
|
|
||||||
backend = IsolatedShellBackend(
|
backend = IsolatedShellBackend(
|
||||||
user=agent_user,
|
user=agent_user,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue