|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Deterministic Dockerfile generation for Studio environments.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from dataclasses import dataclass |
| 20 | + |
| 21 | +from .models import EnvironmentInput |
| 22 | + |
| 23 | + |
| 24 | +@dataclass(frozen=True) |
| 25 | +class _Package: |
| 26 | + label: str |
| 27 | + description: str |
| 28 | + installer: str |
| 29 | + package_name: str |
| 30 | + |
| 31 | + |
| 32 | +_OPERATING_SYSTEMS = { |
| 33 | + "ubuntu-22.04": ("Ubuntu 22.04", "ubuntu:22.04"), |
| 34 | + "ubuntu-24.04": ("Ubuntu 24.04", "ubuntu:24.04"), |
| 35 | +} |
| 36 | + |
| 37 | +_PYTHON_PATCH_VERSIONS = { |
| 38 | + "3.10": "3.10.18", |
| 39 | + "3.12": "3.12.11", |
| 40 | +} |
| 41 | + |
| 42 | +_VEADK_RUNTIME_REQUIREMENT = ( |
| 43 | + '"veadk-python[a2ui,database,eval,extensions,harness,harness-sidecar,pdf,speech]' |
| 44 | + '>=1.1.1"' |
| 45 | +) |
| 46 | + |
| 47 | +_PACKAGES = { |
| 48 | + "lark-cli": _Package("lark-cli", "飞书开放平台命令行工具", "pip", "lark-cli"), |
| 49 | + "pandoc": _Package("pandoc", "文档格式转换工具", "apt", "pandoc"), |
| 50 | + "opencli": _Package( |
| 51 | + "opencli", |
| 52 | + "将网站与桌面应用转换为命令行工具", |
| 53 | + "npm", |
| 54 | + "@jackwener/opencli@1.8.7", |
| 55 | + ), |
| 56 | + "uv": _Package("uv", "快速 Python 包与项目管理器", "pip", "uv"), |
| 57 | + "ripgrep": _Package("ripgrep", "高性能文本检索工具", "apt", "ripgrep"), |
| 58 | + "jq": _Package("jq", "JSON 查询与转换工具", "apt", "jq"), |
| 59 | + "github-cli": _Package("GitHub CLI", "在终端中管理 GitHub 工作流", "apt", "gh"), |
| 60 | + "playwright": _Package( |
| 61 | + "Playwright", "浏览器自动化与端到端测试", "pip", "playwright" |
| 62 | + ), |
| 63 | + "chromium": _Package("Chromium", "无头浏览器运行时", "apt", "chromium"), |
| 64 | + "git": _Package("Git", "代码版本管理", "apt", "git"), |
| 65 | + "curl": _Package("curl", "网络请求与文件下载", "apt", "curl"), |
| 66 | + "ffmpeg": _Package("FFmpeg", "音视频转码与处理", "apt", "ffmpeg"), |
| 67 | + "imagemagick": _Package("ImageMagick", "图片转换与批处理", "apt", "imagemagick"), |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +def build_dockerfile(config: EnvironmentInput) -> str: |
| 72 | + """Build the canonical Dockerfile when the user did not provide one.""" |
| 73 | + if config.dockerfile: |
| 74 | + return validate_dockerfile(config.dockerfile) |
| 75 | + |
| 76 | + os_label, base_image = _OPERATING_SYSTEMS[config.operating_system] |
| 77 | + python_version = config.language.removeprefix("python-") |
| 78 | + python_patch_version = _PYTHON_PATCH_VERSIONS[python_version] |
| 79 | + selected_options = set(config.option_ids) |
| 80 | + uses_ubuntu_python = ( |
| 81 | + config.operating_system == "ubuntu-22.04" and python_version == "3.10" |
| 82 | + ) or (config.operating_system == "ubuntu-24.04" and python_version == "3.12") |
| 83 | + lines = [ |
| 84 | + f"# Operating system: {os_label}", |
| 85 | + f"FROM {base_image}", |
| 86 | + "", |
| 87 | + "ARG DEBIAN_FRONTEND=noninteractive", |
| 88 | + "ARG PIP_INDEX_URL=https://pypi.org/simple", |
| 89 | + "ARG PYTHON_SOURCE_BASE_URL=https://www.python.org/ftp/python", |
| 90 | + "ARG PLAYWRIGHT_DOWNLOAD_HOST=https://cdn.playwright.dev", |
| 91 | + "ARG PIP_DEFAULT_TIMEOUT=300", |
| 92 | + "ARG PIP_RETRIES=10", |
| 93 | + "", |
| 94 | + "# Ubuntu repositories: keep the official hosts, use HTTPS, and tolerate transient builder egress failures.", |
| 95 | + 'RUN printf \'Acquire::Retries "5";\\nAcquire::ForceIPv4 "true";\\nAcquire::http::Timeout "60";\\nAcquire::https::Timeout "60";\\n\' > /etc/apt/apt.conf.d/80-veadk-network \\', |
| 96 | + " && sed -i 's|http://archive.ubuntu.com|https://archive.ubuntu.com|g; s|http://security.ubuntu.com|https://security.ubuntu.com|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.sources 2>/dev/null || true", |
| 97 | + "", |
| 98 | + "ENV PYTHONDONTWRITEBYTECODE=1 \\", |
| 99 | + " PYTHONUNBUFFERED=1 \\", |
| 100 | + " PIP_NO_CACHE_DIR=1", |
| 101 | + "", |
| 102 | + f"# Python {python_version}", |
| 103 | + "RUN apt-get update \\", |
| 104 | + ] |
| 105 | + if uses_ubuntu_python: |
| 106 | + lines.extend( |
| 107 | + ( |
| 108 | + f" && apt-get install -y --no-install-recommends ca-certificates python{python_version} python{python_version}-venv \\", |
| 109 | + f" && python{python_version} -m venv /opt/venv \\", |
| 110 | + " && rm -rf /var/lib/apt/lists/*", |
| 111 | + ) |
| 112 | + ) |
| 113 | + else: |
| 114 | + lines.extend( |
| 115 | + ( |
| 116 | + " && apt-get install -y --no-install-recommends build-essential ca-certificates curl libbz2-dev libffi-dev libgdbm-dev liblzma-dev libncursesw5-dev libreadline-dev libsqlite3-dev libssl-dev tk-dev uuid-dev zlib1g-dev \\", |
| 117 | + f' && curl --retry 5 --retry-all-errors --connect-timeout 30 -fsSL "${{PYTHON_SOURCE_BASE_URL}}/{python_patch_version}/Python-{python_patch_version}.tgz" -o /tmp/python.tgz \\', |
| 118 | + " && mkdir -p /tmp/python-source \\", |
| 119 | + " && tar -xzf /tmp/python.tgz --strip-components=1 -C /tmp/python-source \\", |
| 120 | + " && cd /tmp/python-source \\", |
| 121 | + " && ./configure --prefix=/opt/python --with-ensurepip=install \\", |
| 122 | + ' && make -j"$(nproc)" \\', |
| 123 | + " && make install \\", |
| 124 | + f" && /opt/python/bin/python{python_version} -m venv /opt/venv \\", |
| 125 | + " && rm -rf /tmp/python-source /tmp/python.tgz /var/lib/apt/lists/*", |
| 126 | + ) |
| 127 | + ) |
| 128 | + lines.extend(("", 'ENV PATH="/opt/venv/bin:$PATH"')) |
| 129 | + if {"playwright", "chromium"} & selected_options: |
| 130 | + lines.extend( |
| 131 | + ( |
| 132 | + "", |
| 133 | + "ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright \\", |
| 134 | + " PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT=300000", |
| 135 | + ) |
| 136 | + ) |
| 137 | + lines.extend( |
| 138 | + ( |
| 139 | + "", |
| 140 | + "WORKDIR /workspace", |
| 141 | + "", |
| 142 | + "# VeADK: preinstall the complete Agent runtime dependency profile.", |
| 143 | + "# lxml-html-clean: VeADK Studio webpage content runtime dependency.", |
| 144 | + "# Agent images based on this environment only need to copy user code.", |
| 145 | + "RUN python -m pip install --upgrade \\", |
| 146 | + f" {_VEADK_RUNTIME_REQUIREMENT} \\", |
| 147 | + ' "agentkit-sdk-python==0.8.4" \\', |
| 148 | + ' "starlette<1.0.0" \\', |
| 149 | + " lxml-html-clean", |
| 150 | + 'ENV VEADK_ENVIRONMENT_IMAGE="1"', |
| 151 | + ) |
| 152 | + ) |
| 153 | + for option_id in config.option_ids: |
| 154 | + package = _PACKAGES[option_id] |
| 155 | + lines.extend(("", f"# {package.label}: {package.description}")) |
| 156 | + if option_id == "github-cli": |
| 157 | + lines.extend( |
| 158 | + ( |
| 159 | + "RUN apt-get update \\", |
| 160 | + " && apt-get install -y --no-install-recommends wget \\", |
| 161 | + " && mkdir -p -m 755 /etc/apt/keyrings \\", |
| 162 | + " && wget -qO /etc/apt/keyrings/githubcli-archive-keyring.gpg https://cli.github.com/packages/githubcli-archive-keyring.gpg \\", |
| 163 | + " && chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \\", |
| 164 | + ' && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" > /etc/apt/sources.list.d/github-cli.list \\', |
| 165 | + " && apt-get update \\", |
| 166 | + " && apt-get install -y --no-install-recommends gh \\", |
| 167 | + " && rm -rf /var/lib/apt/lists/*", |
| 168 | + ) |
| 169 | + ) |
| 170 | + elif option_id == "opencli": |
| 171 | + lines.extend( |
| 172 | + ( |
| 173 | + "RUN apt-get update \\", |
| 174 | + " && apt-get install -y --no-install-recommends ca-certificates curl xz-utils \\", |
| 175 | + ' && node_arch="$(dpkg --print-architecture)" \\', |
| 176 | + ' && case "$node_arch" in amd64) node_arch=x64 ;; arm64) node_arch=arm64 ;; *) echo "Unsupported architecture: $node_arch" >&2; exit 1 ;; esac \\', |
| 177 | + ' && curl --retry 5 --connect-timeout 30 -fsSL "https://nodejs.org/dist/v22.18.0/node-v22.18.0-linux-${node_arch}.tar.xz" -o /tmp/node.tar.xz \\', |
| 178 | + " && tar -xJf /tmp/node.tar.xz -C /usr/local --strip-components=1 \\", |
| 179 | + f" && npm install --global {package.package_name} \\", |
| 180 | + " && npm cache clean --force \\", |
| 181 | + " && rm -f /tmp/node.tar.xz \\", |
| 182 | + " && rm -rf /var/lib/apt/lists/*", |
| 183 | + ) |
| 184 | + ) |
| 185 | + elif option_id == "playwright": |
| 186 | + lines.append("RUN python -m pip install --upgrade playwright") |
| 187 | + if "chromium" not in selected_options: |
| 188 | + lines.append("RUN python -m playwright install --with-deps chromium") |
| 189 | + elif option_id == "chromium": |
| 190 | + if "playwright" not in selected_options: |
| 191 | + lines.append("RUN python -m pip install --upgrade playwright") |
| 192 | + lines.append("RUN python -m playwright install --with-deps chromium") |
| 193 | + elif package.installer == "apt": |
| 194 | + lines.extend( |
| 195 | + ( |
| 196 | + "RUN apt-get update \\", |
| 197 | + f" && apt-get install -y --no-install-recommends {package.package_name} \\", |
| 198 | + " && rm -rf /var/lib/apt/lists/*", |
| 199 | + ) |
| 200 | + ) |
| 201 | + else: |
| 202 | + lines.append(f"RUN python -m pip install --upgrade {package.package_name}") |
| 203 | + lines.extend(("", 'CMD ["/bin/bash"]')) |
| 204 | + return "\n".join(lines) |
| 205 | + |
| 206 | + |
| 207 | +def validate_dockerfile(value: str) -> str: |
| 208 | + dockerfile = value.strip() |
| 209 | + if "\x00" in dockerfile: |
| 210 | + raise ValueError("Dockerfile 不能包含空字符。") |
| 211 | + if not any( |
| 212 | + line.lstrip().upper().startswith("FROM ") |
| 213 | + for line in dockerfile.splitlines() |
| 214 | + if line.strip() and not line.lstrip().startswith("#") |
| 215 | + ): |
| 216 | + raise ValueError("Dockerfile 必须包含 FROM 指令。") |
| 217 | + return dockerfile |
| 218 | + |
| 219 | + |
| 220 | +__all__ = ["build_dockerfile", "validate_dockerfile"] |
0 commit comments