17
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" )
18
18
@click .option ("--port" , "-p" , type = int , default = None , help = "Port to run the server on" )
19
19
@click .option ("--detached" , "-d" , is_flag = True , default = False , help = "Starts up the server as detached background process" )
20
- def start_command (port : int | None , platform : str , detached : bool ):
20
+ @click .option ("--local" , is_flag = True , default = False , help = "If true, interacts with the mounted local repository. If false, interacts with the repository copied into the image." )
21
+ def start_command (port : int | None , platform : str , detached : bool , local : bool ):
21
22
"""Starts a local codegen server"""
22
23
codegen_version = version ("codegen" )
23
24
rich .print (f"[bold green]Codegen version:[/bold green] { codegen_version } " )
@@ -26,10 +27,12 @@ def start_command(port: int | None, platform: str, detached: bool):
26
27
port = get_free_port ()
27
28
28
29
try :
30
+ repo_path = Path .cwd ().resolve ()
31
+ repo_config = RepoConfig .from_repo_path (repo_path )
29
32
rich .print ("[bold blue]Building Docker image...[/bold blue]" )
30
- _build_docker_image (codegen_root , platform )
33
+ _build_docker_image (repo_config , codegen_root , platform , local )
31
34
rich .print ("[bold blue]Starting Docker container...[/bold blue]" )
32
- _run_docker_container (port , detached )
35
+ _run_docker_container (repo_config , port , detached , local )
33
36
rich .print (Panel (f"[green]Server started successfully![/green]\n Access the server at: [bold]http://0.0.0.0:{ port } [/bold]" , box = ROUNDED , title = "Codegen Server" ))
34
37
except subprocess .CalledProcessError as e :
35
38
rich .print (f"[bold red]Error:[/bold red] Failed to { e .cmd [0 ]} Docker container" )
@@ -39,15 +42,18 @@ def start_command(port: int | None, platform: str, detached: bool):
39
42
raise click .Abort ()
40
43
41
44
42
- def _build_docker_image (codegen_root : Path , platform : str ):
45
+ def _build_docker_image (repo_config : RepoConfig , codegen_root : Path , platform : str , local : bool ):
46
+ build_arg = [] if local else ["--build-arg" , f"LOCAL_REPO_PATH={ repo_config .repo_path } " , "--build-arg" , f"REPO_NAME={ repo_config .name } " ]
47
+ dockerfile = "Dockerfile-runner-mount" if local else "Dockerfile-runner"
43
48
build_cmd = [
44
49
"docker" ,
45
50
"buildx" ,
46
51
"build" ,
52
+ * build_arg ,
47
53
"--platform" ,
48
54
platform ,
49
55
"-f" ,
50
- str (codegen_root / "Dockerfile-runner" ),
56
+ str (codegen_root / dockerfile ),
51
57
"-t" ,
52
58
"codegen-runner" ,
53
59
"--load" ,
@@ -57,18 +63,16 @@ def _build_docker_image(codegen_root: Path, platform: str):
57
63
subprocess .run (build_cmd , check = True )
58
64
59
65
60
- def _run_docker_container (port : int , detached : bool ):
61
- repo_path = Path .cwd ().resolve ()
62
- repo_config = RepoConfig .from_repo_path (repo_path )
66
+ def _run_docker_container (repo_config : RepoConfig , port : int , detached : bool , local : bool ):
63
67
container_repo_path = f"/app/git/{ repo_config .name } "
64
68
envvars = {
65
69
"REPOSITORY_LANGUAGE" : repo_config .language .value ,
66
- "REPOSITORY_OWNER" : LocalGitRepo (repo_path ).owner ,
70
+ "REPOSITORY_OWNER" : LocalGitRepo (repo_config . repo_path ).owner ,
67
71
"REPOSITORY_PATH" : container_repo_path ,
68
72
"GITHUB_TOKEN" : SecretsConfig ().github_token ,
69
73
}
70
74
envvars_args = [arg for k , v in envvars .items () for arg in ("--env" , f"{ k } ={ v } " )]
71
- mount_args = ["-v" , f"{ repo_path } :{ container_repo_path } " ]
75
+ mount_args = ["-v" , f"{ repo_config . repo_path } :{ container_repo_path } " ] if local else [ ]
72
76
run_mode = "-d" if detached else "-it"
73
77
entry_point = f"uv run --frozen uvicorn codegen.runner.sandbox.server:app --host 0.0.0.0 --port { port } "
74
78
run_cmd = ["docker" , "run" , run_mode , "-p" , f"{ port } :{ port } " , * mount_args , * envvars_args , "codegen-runner" , entry_point ]
0 commit comments