From 3ab1921ffeb4add5acc4fcd58ec6ad5ee64b26fa Mon Sep 17 00:00:00 2001 From: Carol Jung Date: Thu, 27 Feb 2025 11:39:10 -0800 Subject: [PATCH 1/2] fix: Dockerfile support for both published and dev version --- .../start/{Dockerfile-runner => Dockerfile} | 27 +++++++------ src/codegen/cli/commands/start/main.py | 39 ++++++++++++------- 2 files changed, 42 insertions(+), 24 deletions(-) rename src/codegen/cli/commands/start/{Dockerfile-runner => Dockerfile} (73%) diff --git a/src/codegen/cli/commands/start/Dockerfile-runner b/src/codegen/cli/commands/start/Dockerfile similarity index 73% rename from src/codegen/cli/commands/start/Dockerfile-runner rename to src/codegen/cli/commands/start/Dockerfile index 4934d83cc..cb9968eb6 100644 --- a/src/codegen/cli/commands/start/Dockerfile-runner +++ b/src/codegen/cli/commands/start/Dockerfile @@ -21,10 +21,6 @@ RUN apt-get update && apt-get install -y \ # Cleanup apt cache to reduce image size && rm -rf /var/lib/apt/lists/* -# Set git config -RUN git config --global user.email "team+codegenbot@codegen.sh" \ - && git config --global user.name "codegen-bot" - # Install nvm and Node.js SHELL ["/bin/bash", "-c"] RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash \ @@ -46,14 +42,23 @@ RUN node --version \ && pnpm --version \ && python --version -# Build codegen -WORKDIR /codegen-sdk -COPY . . +# Add build argument for codegen version and build type +ARG CODEGEN_VERSION +ARG BUILD_TYPE="release" # Can be "release" or "dev" + +# Install codegen based on BUILD_TYPE RUN --mount=type=cache,target=/root/.cache/uv \ - uv venv && source .venv/bin/activate \ - && uv sync --frozen --no-dev --all-extras \ - && uv pip install --system -e . --no-deps \ - && uv pip install --system . + --mount=type=bind,source=.,target=/codegen-sdk,rw \ + if [ "$BUILD_TYPE" = "release" ]; then \ + uv pip install --system codegen==${CODEGEN_VERSION}; \ + else \ + cd /codegen-sdk \ + && uv venv && source .venv/bin/activate \ + && uv sync --frozen --no-dev --all-extras \ + && uv pip install --system -e . --no-deps \ + && uv pip install --system .; \ + fi + RUN codegen --version # Create a non-root user for local development + debugging diff --git a/src/codegen/cli/commands/start/main.py b/src/codegen/cli/commands/start/main.py index 740a8ed96..bba3f842c 100644 --- a/src/codegen/cli/commands/start/main.py +++ b/src/codegen/cli/commands/start/main.py @@ -33,16 +33,15 @@ def start_command(port: int | None, detached: bool = False, skip_build: bool = F container.remove() else: return _handle_existing_container(repo_config, container, force) - - codegen_version = version("codegen") - rich.print(f"[bold green]Codegen version:[/bold green] {codegen_version}") - codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent + if port is None: port = get_free_port() try: if not skip_build: - _build_docker_image(codegen_root) + codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent + codegen_version = version("codegen") + _build_docker_image(codegen_root=codegen_root, codegen_version=codegen_version) _run_docker_container(repo_config, port, detached) rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://{_default_host}:{port}[/bold]", box=ROUNDED, title="Codegen Server")) # TODO: memory snapshot here @@ -51,7 +50,7 @@ def start_command(port: int | None, detached: bool = False, skip_build: bool = F raise click.Abort() -def _handle_existing_container(repo_config: RepoConfig, container: DockerContainer, force: bool) -> None: +def _handle_existing_container(repo_config: RepoConfig, container: DockerContainer) -> None: if container.is_running(): rich.print( Panel( @@ -70,27 +69,36 @@ def _handle_existing_container(repo_config: RepoConfig, container: DockerContain click.Abort() -def _build_docker_image(codegen_root: Path) -> None: - platform = _get_platform() - dockerfile_path = Path(__file__).parent / "Dockerfile-runner" +def _build_docker_image(codegen_root: Path, codegen_version: str) -> None: + build_type = _get_build_type(codegen_version) build_cmd = [ "docker", "buildx", "build", "--platform", - platform, + _get_platform(), "-f", - str(dockerfile_path), + str(Path(__file__).parent / "Dockerfile"), "-t", "codegen-runner", + "--build-arg", + f"CODEGEN_VERSION={codegen_version}", + "--build-arg", + f"BUILD_TYPE={build_type}", "--load", - str(codegen_root), ] + + # Only add the context path if we're doing a local build + if build_type == "dev": + build_cmd.append(str(codegen_root)) + else: + build_cmd.append(".") # Minimal context when installing from PyPI + rich.print( Panel( f"{str.join(' ', build_cmd)}", box=ROUNDED, - title="Running Build Command", + title=f"Running Build Command ({build_type})", style="blue", padding=(1, 1), ) @@ -98,6 +106,11 @@ def _build_docker_image(codegen_root: Path) -> None: subprocess.run(build_cmd, check=True) +def _get_build_type(version: str) -> str: + """Get the build type based on the version string.""" + return "dev" if "dev" in version or "+" in version else "release" + + def _get_platform() -> str: machine = py_platform.machine().lower() if machine in ("x86_64", "amd64"): From e4cae48d1e434658a502da332bab171399d83a9a Mon Sep 17 00:00:00 2001 From: caroljung-cg <165736129+caroljung-cg@users.noreply.github.com> Date: Thu, 27 Feb 2025 19:41:00 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- src/codegen/cli/commands/start/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/cli/commands/start/main.py b/src/codegen/cli/commands/start/main.py index bba3f842c..34c5f8957 100644 --- a/src/codegen/cli/commands/start/main.py +++ b/src/codegen/cli/commands/start/main.py @@ -33,7 +33,7 @@ def start_command(port: int | None, detached: bool = False, skip_build: bool = F container.remove() else: return _handle_existing_container(repo_config, container, force) - + if port is None: port = get_free_port()