14
14
15
15
import os
16
16
import shutil
17
- import subprocess
18
- from typing import Optional
19
-
20
17
import click
21
-
22
- _DOCKERFILE_TEMPLATE = """
23
- FROM python:3.11-slim
24
- WORKDIR /app
25
-
26
- # Create a non-root user
27
- RUN adduser --disabled-password --gecos "" myuser
28
-
29
- # Change ownership of /app to myuser
30
- RUN chown -R myuser:myuser /app
31
-
32
- # Switch to the non-root user
33
- USER myuser
34
-
35
- # Set up environment variables - Start
36
- ENV PATH="/home/myuser/.local/bin:$PATH"
37
-
38
- ENV GOOGLE_GENAI_USE_VERTEXAI=1
39
- ENV GOOGLE_CLOUD_PROJECT={gcp_project_id}
40
- ENV GOOGLE_CLOUD_LOCATION={gcp_region}
41
-
42
- # Set up environment variables - End
43
-
44
- # Install ADK - Start
45
- RUN pip install google-adk
46
- # Install ADK - End
47
-
48
- # Copy agent - Start
49
-
50
- COPY "agents/{app_name}/" "/app/agents/{app_name}/"
51
- {install_agent_deps}
52
-
53
- # Copy agent - End
54
-
55
- EXPOSE {port}
56
-
57
- CMD adk {command} --port={port} {session_db_option} {trace_to_cloud_option} "/app/agents"
58
- """
59
-
60
-
61
- def _resolve_project (project_in_option : Optional [str ]) -> str :
62
- if project_in_option :
63
- return project_in_option
64
-
65
- result = subprocess .run (
66
- ['gcloud' , 'config' , 'get-value' , 'project' ],
67
- check = True ,
68
- capture_output = True ,
69
- text = True ,
70
- )
71
- project = result .stdout .strip ()
72
- click .echo (f'Use default project: { project } ' )
73
- return project
74
-
18
+ from typing import Optional , Tuple
19
+ from .deployers .deployer_factory import DeployerFactory
20
+ from .config .dockerfile_template import _DOCKERFILE_TEMPLATE
75
21
76
22
def to_cloud_run (
77
23
* ,
78
24
agent_folder : str ,
25
+ cloud_provider : str ,
79
26
project : Optional [str ],
80
27
region : Optional [str ],
81
28
service_name : str ,
@@ -86,6 +33,8 @@ def to_cloud_run(
86
33
with_ui : bool ,
87
34
verbosity : str ,
88
35
session_db_url : str ,
36
+ provider_args : Tuple [str ],
37
+ env : Tuple [str ],
89
38
):
90
39
"""Deploys an agent to Google Cloud Run.
91
40
@@ -104,6 +53,7 @@ def to_cloud_run(
104
53
105
54
Args:
106
55
agent_folder: The folder (absolute path) containing the agent source code.
56
+ cloud_provider: Target deployment platform (gcp, local, etc).
107
57
project: Google Cloud project id.
108
58
region: Google Cloud region.
109
59
service_name: The service name in Cloud Run.
@@ -114,10 +64,14 @@ def to_cloud_run(
114
64
with_ui: Whether to deploy with UI.
115
65
verbosity: The verbosity level of the CLI.
116
66
session_db_url: The database URL to connect the session.
67
+ provider_args: The arguments specific to cloud provider
68
+ env: The environment valriables provided
117
69
"""
118
70
app_name = app_name or os .path .basename (agent_folder )
71
+ mode = 'web' if with_ui else 'api_server'
72
+ trace_to_cloud_option = '--trace_to_cloud' if trace_to_cloud else ''
119
73
120
- click .echo (f'Start generating Cloud Run source files in { temp_folder } ' )
74
+ click .echo (f'Start generating deployment files in { temp_folder } ' )
121
75
122
76
# remove temp_folder if exists
123
77
if os .path .exists (temp_folder ):
@@ -144,12 +98,12 @@ def to_cloud_run(
144
98
gcp_region = region ,
145
99
app_name = app_name ,
146
100
port = port ,
147
- command = 'web' if with_ui else 'api_server' ,
101
+ command = mode ,
148
102
install_agent_deps = install_agent_deps ,
149
103
session_db_option = f'--session_db_url={ session_db_url } '
150
104
if session_db_url
151
105
else '' ,
152
- trace_to_cloud_option = '--trace_to_cloud' if trace_to_cloud else '' ,
106
+ trace_to_cloud_option = trace_to_cloud_option ,
153
107
)
154
108
dockerfile_path = os .path .join (temp_folder , 'Dockerfile' )
155
109
os .makedirs (temp_folder , exist_ok = True )
@@ -159,30 +113,26 @@ def to_cloud_run(
159
113
)
160
114
click .echo (f'Creating Dockerfile complete: { dockerfile_path } ' )
161
115
162
- # Deploy to Cloud Run
163
- click .echo ('Deploying to Cloud Run...' )
164
- region_options = ['--region' , region ] if region else []
165
- project = _resolve_project (project )
166
- subprocess .run (
167
- [
168
- 'gcloud' ,
169
- 'run' ,
170
- 'deploy' ,
171
- service_name ,
172
- '--source' ,
173
- temp_folder ,
174
- '--project' ,
175
- project ,
176
- * region_options ,
177
- '--port' ,
178
- str (port ),
179
- '--verbosity' ,
180
- verbosity ,
181
- '--labels' ,
182
- 'created-by=adk' ,
183
- ],
184
- check = True ,
116
+ # Deploy using the appropriate deployer
117
+ if cloud_provider is None :
118
+ cloud_provider = 'local'
119
+
120
+ click .echo (f'Deploying to { cloud_provider } ...' )
121
+ deployer = DeployerFactory .get_deployer (cloud_provider )
122
+ deployer .deploy (
123
+ agent_folder = agent_folder ,
124
+ temp_folder = temp_folder ,
125
+ service_name = service_name ,
126
+ provider_args = provider_args ,
127
+ env_vars = env ,
128
+ project = project ,
129
+ region = region ,
130
+ port = port ,
131
+ verbosity = verbosity ,
185
132
)
133
+
134
+ click .echo (f'Deployment to { cloud_provider } complete.' )
135
+
186
136
finally :
187
137
click .echo (f'Cleaning up the temp folder: { temp_folder } ' )
188
138
shutil .rmtree (temp_folder )
0 commit comments