1
+ import platform as py_platform
1
2
import subprocess
2
3
from importlib .metadata import version
3
4
from pathlib import Path
18
19
19
20
20
21
@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" )
22
22
@click .option ("--port" , "-p" , type = int , default = None , help = "Port to run the server on" )
23
23
@click .option ("--detached" , "-d" , is_flag = True , help = "Run the server in detached mode" )
24
24
@click .option ("--skip-build" , is_flag = True , help = "Skip building the Docker image" )
25
25
@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 :
27
27
"""Starts a local codegen server"""
28
28
repo_path = Path .cwd ().resolve ()
29
29
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_
42
42
43
43
try :
44
44
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 )
48
46
_run_docker_container (repo_config , port , detached )
49
47
rich .print (Panel (f"[green]Server started successfully![/green]\n Access the server at: [bold]http://{ _default_host } :{ port } [/bold]" , box = ROUNDED , title = "Codegen Server" ))
50
48
# 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 ()
54
49
except Exception as e :
55
50
rich .print (f"[bold red]Error:[/bold red] { e !s} " )
56
51
raise click .Abort ()
@@ -75,7 +70,8 @@ def _handle_existing_container(repo_config: RepoConfig, container: DockerContain
75
70
click .Abort ()
76
71
77
72
78
- def _build_docker_image (codegen_root : Path , platform : str ) -> None :
73
+ def _build_docker_image (codegen_root : Path ) -> None :
74
+ platform = _get_platform ()
79
75
build_cmd = [
80
76
"docker" ,
81
77
"buildx" ,
@@ -89,11 +85,31 @@ def _build_docker_image(codegen_root: Path, platform: str) -> None:
89
85
"--load" ,
90
86
str (codegen_root ),
91
87
]
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
+ )
93
97
subprocess .run (build_cmd , check = True )
94
98
95
99
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
+
96
111
def _run_docker_container (repo_config : RepoConfig , port : int , detached : bool ) -> None :
112
+ rich .print ("[bold blue]Starting Docker container...[/bold blue]" )
97
113
container_repo_path = f"/app/git/{ repo_config .name } "
98
114
name_args = ["--name" , f"{ repo_config .name } " ]
99
115
envvars = {
@@ -110,7 +126,15 @@ def _run_docker_container(repo_config: RepoConfig, port: int, detached: bool) ->
110
126
detached_args = ["-d" ] if detached else []
111
127
run_cmd = ["docker" , "run" , "--rm" , * detached_args , * port_args , * name_args , * mount_args , * envvars_args , CODEGEN_RUNNER_IMAGE , entry_point ]
112
128
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
+ )
114
138
subprocess .run (run_cmd , check = True )
115
139
116
140
if detached :
0 commit comments