Skip to content

Commit 4b87a5e

Browse files
committed
auto resolve platform
1 parent b828c60 commit 4b87a5e

File tree

1 file changed

+35
-11
lines changed
  • src/codegen/cli/commands/start

1 file changed

+35
-11
lines changed

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

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import platform as py_platform
12
import subprocess
23
from importlib.metadata import version
34
from pathlib import Path
@@ -18,12 +19,11 @@
1819

1920

2021
@click.command(name="start")
21-
@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")
2222
@click.option("--port", "-p", type=int, default=None, help="Port to run the server on")
2323
@click.option("--detached", "-d", is_flag=True, help="Run the server in detached mode")
2424
@click.option("--skip-build", is_flag=True, help="Skip building the Docker image")
2525
@click.option("--force", "-f", is_flag=True, help="Force start the server even if it is already running")
26-
def start_command(port: int | None, platform: str, detached: bool = False, skip_build: bool = False, force: bool = False) -> None:
26+
def start_command(port: int | None, detached: bool = False, skip_build: bool = False, force: bool = False) -> None:
2727
"""Starts a local codegen server"""
2828
repo_path = Path.cwd().resolve()
2929
repo_config = RepoConfig.from_repo_path(str(repo_path))
@@ -42,15 +42,10 @@ def start_command(port: int | None, platform: str, detached: bool = False, skip_
4242

4343
try:
4444
if not skip_build:
45-
rich.print("[bold blue]Building Docker image...[/bold blue]")
46-
_build_docker_image(codegen_root, platform)
47-
rich.print("[bold blue]Starting Docker container...[/bold blue]")
45+
_build_docker_image(codegen_root)
4846
_run_docker_container(repo_config, port, detached)
4947
rich.print(Panel(f"[green]Server started successfully![/green]\nAccess the server at: [bold]http://{_default_host}:{port}[/bold]", box=ROUNDED, title="Codegen Server"))
5048
# TODO: memory snapshot here
51-
except subprocess.CalledProcessError as e:
52-
rich.print(f"[bold red]Error:[/bold red] Failed to {e.cmd[0]} Docker container")
53-
raise click.Abort()
5449
except Exception as e:
5550
rich.print(f"[bold red]Error:[/bold red] {e!s}")
5651
raise click.Abort()
@@ -75,7 +70,8 @@ def _handle_existing_container(repo_config: RepoConfig, container: DockerContain
7570
click.Abort()
7671

7772

78-
def _build_docker_image(codegen_root: Path, platform: str) -> None:
73+
def _build_docker_image(codegen_root: Path) -> None:
74+
platform = _get_platform()
7975
build_cmd = [
8076
"docker",
8177
"buildx",
@@ -89,11 +85,31 @@ def _build_docker_image(codegen_root: Path, platform: str) -> None:
8985
"--load",
9086
str(codegen_root),
9187
]
92-
rich.print(f"build_cmd: {str.join(' ', build_cmd)}")
88+
rich.print(
89+
Panel(
90+
f"{str.join(' ', build_cmd)}",
91+
box=ROUNDED,
92+
title="Running Build Command",
93+
style="blue",
94+
padding=(1, 1),
95+
)
96+
)
9397
subprocess.run(build_cmd, check=True)
9498

9599

100+
def _get_platform() -> str:
101+
machine = py_platform.machine().lower()
102+
if machine in ("x86_64", "amd64"):
103+
return "linux/amd64"
104+
elif machine in ("arm64", "aarch64"):
105+
return "linux/arm64"
106+
else:
107+
rich.print(f"[yellow]Warning: Unknown architecture {machine}, defaulting to linux/amd64[/yellow]")
108+
return "linux/amd64"
109+
110+
96111
def _run_docker_container(repo_config: RepoConfig, port: int, detached: bool) -> None:
112+
rich.print("[bold blue]Starting Docker container...[/bold blue]")
97113
container_repo_path = f"/app/git/{repo_config.name}"
98114
name_args = ["--name", f"{repo_config.name}"]
99115
envvars = {
@@ -110,7 +126,15 @@ def _run_docker_container(repo_config: RepoConfig, port: int, detached: bool) ->
110126
detached_args = ["-d"] if detached else []
111127
run_cmd = ["docker", "run", "--rm", *detached_args, *port_args, *name_args, *mount_args, *envvars_args, CODEGEN_RUNNER_IMAGE, entry_point]
112128

113-
rich.print(f"run_cmd: {str.join(' ', run_cmd)}")
129+
rich.print(
130+
Panel(
131+
f"{str.join(' ', run_cmd)}",
132+
box=ROUNDED,
133+
title="Running Run Command",
134+
style="blue",
135+
padding=(1, 1),
136+
)
137+
)
114138
subprocess.run(run_cmd, check=True)
115139

116140
if detached:

0 commit comments

Comments
 (0)