|
8 | 8 | from rich.panel import Panel |
9 | 9 |
|
10 | 10 | from codegen.configs.models.secrets import SecretsConfig |
| 11 | +from codegen.git.repo_operator.local_git_repo import LocalGitRepo |
11 | 12 | from codegen.git.schemas.repo_config import RepoConfig |
| 13 | +from codegen.shared.network.port import get_free_port |
12 | 14 |
|
13 | 15 |
|
14 | 16 | @click.command(name="start") |
15 | 17 | @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") |
16 | | -@click.option("--port", "-p", type=int, default=8000) |
| 18 | +@click.option("--port", "-p", type=int, default=None, help="Port to run the server on") |
17 | 19 | @click.option("--detached", "-d", is_flag=True, default=False, help="Starts up the server as detached background process") |
18 | | -def start_command(port: int, platform: str, detached: bool): |
| 20 | +def start_command(port: int | None, platform: str, detached: bool): |
19 | 21 | """Starts a local codegen server""" |
20 | 22 | codegen_version = version("codegen") |
21 | 23 | rich.print(f"[bold green]Codegen version:[/bold green] {codegen_version}") |
22 | 24 | codegen_root = Path(__file__).parent.parent.parent.parent.parent.parent |
| 25 | + if port is None: |
| 26 | + port = get_free_port() |
23 | 27 |
|
24 | 28 | try: |
25 | 29 | rich.print("[bold blue]Building Docker image...[/bold blue]") |
26 | 30 | _build_docker_image(codegen_root, platform) |
27 | 31 | rich.print("[bold blue]Starting Docker container...[/bold blue]") |
28 | | - _run_docker_container(port, platform, detached) |
| 32 | + _run_docker_container(port, detached) |
29 | 33 | 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 | 34 | except subprocess.CalledProcessError as e: |
31 | 35 | rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container") |
@@ -59,16 +63,15 @@ def _run_docker_container(port: int, detached: bool): |
59 | 63 | container_repo_path = f"/app/git/{repo_config.name}" |
60 | 64 | envvars = { |
61 | 65 | "REPOSITORY_LANGUAGE": repo_config.language.value, |
62 | | - "REPOSITORY_OWNER": repo_config.organization_name, |
| 66 | + "REPOSITORY_OWNER": LocalGitRepo(repo_path).owner, |
63 | 67 | "REPOSITORY_PATH": container_repo_path, |
64 | 68 | "GITHUB_TOKEN": SecretsConfig().github_token, |
65 | 69 | } |
66 | 70 | envvars_args = [arg for k, v in envvars.items() for arg in ("--env", f"{k}={v}")] |
67 | | - |
68 | 71 | mount_args = ["-v", f"{repo_path}:{container_repo_path}"] |
69 | 72 | run_mode = "-d" if detached else "-it" |
70 | 73 | 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] |
| 74 | + run_cmd = ["docker", "run", run_mode, "-p", f"{port}:{port}", *mount_args, *envvars_args, "codegen-runner", entry_point] |
72 | 75 |
|
73 | 76 | rich.print(f"run_cmd: {str.join(' ', run_cmd)}") |
74 | 77 | subprocess.run(run_cmd, check=True) |
0 commit comments