Skip to content

Commit 2a9740c

Browse files
author
codegen-bot
committed
refined
1 parent c37c816 commit 2a9740c

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

Dockerfile-runner

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | b
3434

3535
# Add node and npm to PATH
3636
ENV PATH=$NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
37+
RUN node --version \
38+
&& corepack --version \
39+
&& npm --version \
40+
&& yarn --version \
41+
&& pnpm --version \
42+
&& python --version
3743

3844
# Install codegen from source instead of PyPI
3945
WORKDIR /codegen-sdk
40-
41-
# Add build argument for repo path
42-
ARG REPO_PATH=.
43-
# Copy from specified repo path instead of current directory
44-
COPY ${REPO_PATH} .
46+
COPY . .
4547

4648
# Install dependencies and build codegen with entry points
4749
RUN --mount=type=cache,target=/root/.cache/uv \
@@ -50,17 +52,7 @@ RUN --mount=type=cache,target=/root/.cache/uv \
5052
&& uv pip install --system -e . --no-deps \
5153
&& uv pip install --system .
5254

53-
# Change back to app directory for running
54-
WORKDIR /app
55-
56-
# Verify all installations
57-
RUN codegen --version \
58-
&& node --version \
59-
&& corepack --version \
60-
&& npm --version \
61-
&& yarn --version \
62-
&& pnpm --version \
63-
&& python --version
55+
RUN codegen --version
6456

6557
# Create a non-root user for local development + debugging
6658
RUN useradd -m -s /bin/bash user

src/codegen/cli/commands/start/main.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from rich.box import ROUNDED
88
from rich.panel import Panel
99

10+
from codegen.configs.models.secrets import SecretsConfig
11+
from codegen.git.schemas.repo_config import RepoConfig
12+
1013

1114
@click.command(name="start")
1215
@click.option("--platform", "-t", type=click.Choice(["linux/amd64", "linux/arm64", "linux/amd64,linux/arm64"]), default="linux/amd64,linux/arm64", help="Target platform(s) for the Docker image")
@@ -15,44 +18,57 @@
1518
def start_command(port: int, platform: str, detached: bool):
1619
"""Starts a local codegen server"""
1720
codegen_version = version("codegen")
18-
rich.print(codegen_version)
19-
repo_root = Path(__file__).parent.parent.parent.parent.parent.parent
20-
dockerfile_path = repo_root / "Dockerfile-runner"
21+
rich.print(f"[bold green]Codegen version:[/bold green] {codegen_version}")
22+
codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent
23+
24+
try:
25+
rich.print("[bold blue]Building Docker image...[/bold blue]")
26+
_build_docker_image(codegen_root, platform)
27+
rich.print("[bold blue]Starting Docker container...[/bold blue]")
28+
_run_docker_container(port, platform, detached)
29+
rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://0.0.0.0:{port}[/bold]", box=ROUNDED, title="Codegen Server"))
30+
except subprocess.CalledProcessError as e:
31+
rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container")
32+
raise click.Abort()
33+
except Exception as e:
34+
rich.print(f"[bold red]Error:[/bold red] {e!s}")
35+
raise click.Abort()
2136

22-
# Build the Docker image
23-
rich.print("[bold blue]Building Docker image...[/bold blue]")
37+
38+
def _build_docker_image(codegen_root: Path, platform: str):
2439
build_cmd = [
2540
"docker",
2641
"buildx",
2742
"build",
2843
"--platform",
2944
platform,
3045
"-f",
31-
str(dockerfile_path),
46+
str(codegen_root / "Dockerfile-runner"),
3247
"-t",
3348
"codegen-runner",
3449
"--load",
35-
str(repo_root),
50+
str(codegen_root),
3651
]
3752
rich.print(f"build_cmd: {str.join(' ', build_cmd)}")
53+
subprocess.run(build_cmd, check=True)
3854

39-
try:
40-
subprocess.run(build_cmd, check=True)
41-
42-
# Run the Docker container
43-
rich.print("[bold blue]Starting Docker container...[/bold blue]")
44-
run_mode = "-d" if detached else "-it"
45-
entry_point = f'"uv run --frozen uvicorn codegen.runner.sandbox.server:app --host 0.0.0.0 --port {port}"'
46-
run_cmd = ["docker", "run", run_mode, "-p", f"8000:{port}", "codegen-runner", entry_point]
4755

48-
rich.print(f"run_cmd: {str.join(' ', run_cmd)}")
49-
subprocess.run(run_cmd, check=True)
56+
def _run_docker_container(port: int, detached: bool):
57+
repo_path = Path.cwd().resolve()
58+
repo_config = RepoConfig.from_repo_path(repo_path)
59+
container_repo_path = f"/app/git/{repo_config.name}"
60+
envvars = {
61+
"REPOSITORY_LANGUAGE": repo_config.language.value,
62+
"REPOSITORY_OWNER": repo_config.organization_name,
63+
"REPOSITORY_PATH": container_repo_path,
64+
"GITHUB_TOKEN": SecretsConfig().github_token,
65+
}
66+
envvars_args = [arg for k, v in envvars.items() for arg in ("--env", f"{k}={v}")]
5067

51-
rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://0.0.0.0:{port}[/bold]", box=ROUNDED, title="Codegen Server"))
68+
mount_args = ["-v", f"{repo_path}:{container_repo_path}"]
69+
run_mode = "-d" if detached else "-it"
70+
entry_point = f"uv run --frozen uvicorn codegen.runner.sandbox.server:app --host 0.0.0.0 --port {port}"
71+
run_cmd = ["docker", "run", run_mode, "-p", f"8000:{port}", *mount_args, *envvars_args, "codegen-runner", entry_point]
5272

53-
except subprocess.CalledProcessError as e:
54-
rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container")
55-
raise click.Abort()
56-
except Exception as e:
57-
rich.print(f"[bold red]Error:[/bold red] {e!s}")
58-
raise click.Abort()
73+
rich.print(f"run_cmd: {str.join(' ', run_cmd)}")
74+
subprocess.run(run_cmd, check=True)

0 commit comments

Comments
 (0)