Skip to content

Commit 9ae5ed8

Browse files
committed
update deploy function
1 parent 45ee9da commit 9ae5ed8

File tree

6 files changed

+105
-92
lines changed

6 files changed

+105
-92
lines changed

veadk/cli/cli_deploy.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ def deploy(
5252
path: str,
5353
) -> None:
5454
"""Deploy a user project to Volcengine FaaS application."""
55+
import asyncio
5556
import shutil
5657
from pathlib import Path
5758

5859
from cookiecutter.main import cookiecutter
5960

6061
import veadk.integrations.ve_faas as vefaas
6162
from veadk.config import getenv
62-
from veadk.utils.misc import formatted_timestamp
63+
from veadk.utils.logger import get_logger
64+
from veadk.utils.misc import formatted_timestamp, load_module_from_file
65+
66+
logger = get_logger(__name__)
6367

6468
if not access_key:
6569
access_key = getenv("VOLCENGINE_ACCESS_KEY")
@@ -84,14 +88,13 @@ def deploy(
8488
"use_adk_web": use_adk_web,
8589
}
8690

87-
print(settings)
88-
8991
cookiecutter(
9092
template=str(template_dir_path),
9193
output_dir=TEMP_PATH,
9294
no_input=True,
9395
extra_context=settings,
9496
)
97+
logger.debug(f"Create a template project at {TEMP_PATH}/{tmp_dir_name}")
9598

9699
agent_dir = (
97100
Path(TEMP_PATH)
@@ -104,20 +107,20 @@ def deploy(
104107
shutil.rmtree(agent_dir)
105108
agent_dir.mkdir(parents=True, exist_ok=True)
106109

107-
# mv
110+
# copy
108111
shutil.copytree(user_proj_abs_path, agent_dir, dirs_exist_ok=True)
112+
logger.debug(f"Remove agent module from {user_proj_abs_path} to {agent_dir}")
113+
114+
# load
115+
logger.debug(
116+
f"Load deploy module from {Path(TEMP_PATH) / tmp_dir_name / 'deploy.py'}"
117+
)
118+
deploy_module = load_module_from_file(
119+
module_name="deploy_module", file_path=str(Path(TEMP_PATH) / tmp_dir_name)
120+
)
121+
logger.info(f"Begin deploy from {Path(TEMP_PATH) / tmp_dir_name / 'src'}")
122+
asyncio.run(deploy_module.main())
109123

110-
# mv requirements.txt and config.yaml if have
111-
if (user_proj_abs_path / "requirements.txt").exists():
112-
shutil.copy(
113-
user_proj_abs_path / "requirements.txt",
114-
f"{TEMP_PATH}/{tmp_dir_name}/requirements.txt",
115-
)
116-
# remove file
117-
118-
if (user_proj_abs_path / "config.yaml").exists():
119-
shutil.copy(
120-
user_proj_abs_path / "config.yaml",
121-
f"{TEMP_PATH}/{tmp_dir_name}/config.yaml",
122-
)
123-
# remove file
124+
# remove tmp file
125+
logger.info("Deploy done. Delete temp dir.")
126+
shutil.rmtree(Path(TEMP_PATH) / tmp_dir_name)

veadk/cli/cli_init.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,90 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from typing import Any
16+
1517
import click
1618

1719

20+
def _render_prompts() -> dict[str, Any]:
21+
vefaas_application_name = click.prompt(
22+
"Volcengine FaaS application name", default="veadk-cloud-agent"
23+
)
24+
25+
veapig_instance_name = click.prompt(
26+
"Volcengine API Gateway instance name", default="", show_default=True
27+
)
28+
29+
veapig_service_name = click.prompt(
30+
"Volcengine API Gateway service name", default="", show_default=True
31+
)
32+
33+
veapig_upstream_name = click.prompt(
34+
"Volcengine API Gateway upstream name", default="", show_default=True
35+
)
36+
37+
deploy_mode_options = {
38+
"1": "A2A/MCP Server",
39+
"2": "VeADK Web / Google ADK Web",
40+
}
41+
42+
click.echo("Choose a deploy mode:")
43+
for key, value in deploy_mode_options.items():
44+
click.echo(f" {key}. {value}")
45+
46+
deploy_mode = click.prompt(
47+
"Enter your choice", type=click.Choice(deploy_mode_options.keys())
48+
)
49+
50+
return {
51+
"vefaas_application_name": vefaas_application_name,
52+
"veapig_instance_name": veapig_instance_name,
53+
"veapig_service_name": veapig_service_name,
54+
"veapig_upstream_name": veapig_upstream_name,
55+
"use_adk_web": deploy_mode == "2",
56+
}
57+
58+
1859
@click.command()
1960
def init() -> None:
2061
"""Init a veadk project that can be deployed to Volcengine VeFaaS."""
62+
import shutil
2163
from pathlib import Path
2264

2365
from cookiecutter.main import cookiecutter
2466

2567
import veadk.integrations.ve_faas as vefaas
2668

2769
cwd = Path.cwd()
70+
local_dir_name = click.prompt("Local directory name", default="veadk-cloud-proj")
71+
target_dir_path = cwd / local_dir_name
72+
73+
click.echo(
74+
"Welcome use VeADK to create your project. We will generate a `weather-reporter` application for you."
75+
)
76+
77+
if target_dir_path.exists():
78+
click.confirm(
79+
f"Directory '{target_dir_path}' already exists, do you want to overwrite it",
80+
abort=True,
81+
)
82+
shutil.rmtree(target_dir_path)
83+
84+
settings = _render_prompts()
85+
settings["local_dir_name"] = local_dir_name
2886

2987
template_dir_path = Path(vefaas.__file__).parent / "template"
3088

31-
cookiecutter(template=str(template_dir_path), output_dir=str(cwd))
89+
cookiecutter(
90+
template=str(template_dir_path),
91+
output_dir=str(cwd),
92+
extra_context=settings,
93+
no_input=True,
94+
)
95+
96+
click.echo(f"Template project has been generated at {target_dir_path}")
97+
click.echo(f"Edit {target_dir_path / 'src/'} to define your agents")
98+
click.echo(
99+
f"Edit {target_dir_path / 'deploy.py'} to define your deployment attributes"
100+
)
101+
click.echo("Run python `deploy.py` for deployment on Volcengine FaaS platform.")

veadk/cloud/cloud_agent_engine.py

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,58 +56,22 @@ def _prepare(self, path: str, name: str):
5656
f"Invalid Volcengine FaaS function name `{name}`, please use lowercase letters and numbers, or replace it with a `-` char."
5757
)
5858

59-
# project structure check
60-
assert os.path.exists(os.path.join(path, "agent.py")), (
61-
f"Local agent project path `{path}` does not contain `agent.py` file. Please prepare it according to our document https://volcengine.github.io/veadk-python/deploy.html"
62-
)
63-
64-
if not os.path.exists(os.path.join(path, "config.yaml")):
65-
logger.warning(
66-
f"Local agent project path `{path}` does not contain `config.yaml` file. Some important config items may not be set."
67-
)
68-
69-
# prepare template files if not have
70-
# template_files = [
71-
# "app.py",
72-
# "run.sh",
73-
# # "requirements.txt",
74-
# "__init__.py",
75-
# ]
76-
# for template_file in template_files:
77-
# if os.path.exists(os.path.join(path, template_file)):
78-
# logger.warning(
79-
# f"Local agent project path `{path}` contains a `{template_file}` file. Use your own `{template_file}` file may cause unexpected behavior."
80-
# )
81-
# else:
82-
# logger.info(
83-
# f"No `{template_file}` detected in local agent project path `{path}`. Prepare it."
84-
# )
85-
# import veadk.integrations.ve_faas as vefaas
86-
87-
# template_file_path = (
88-
# Path(vefaas.__file__).parent / "template" / "src" / template_file
89-
# )
90-
# import shutil
91-
92-
# shutil.copy(template_file_path, os.path.join(path, template_file))
93-
9459
# copy user's requirements.txt
95-
if os.path.exists(os.path.join(path, "requirements.txt")):
96-
logger.warning(
97-
f"Local agent project path `{path}` contains a `requirements.txt` file. Skip copy requirements."
98-
)
99-
return
100-
10160
module = load_module_from_file(
10261
module_name="agent_source", file_path=f"{path}/agent.py"
10362
)
10463

10564
requirement_file_path = module.agent_run_config.requirement_file_path
106-
shutil.copy(requirement_file_path, os.path.join(path, "requirements.txt"))
65+
if Path(requirement_file_path).exists():
66+
shutil.copy(requirement_file_path, os.path.join(path, "requirements.txt"))
10767

108-
logger.info(
109-
f"Copy requirement file: from {requirement_file_path} to {path}/requirements.txt"
110-
)
68+
logger.info(
69+
f"Copy requirement file: from {requirement_file_path} to {path}/requirements.txt"
70+
)
71+
else:
72+
logger.warning(
73+
f"Requirement file: {requirement_file_path} not found or you have no requirement file in your project. Use a default one."
74+
)
11175

11276
def _try_launch_fastapi_server(self, path: str):
11377
"""Try to launch a fastapi server for tests according to user's configuration.

veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/agent.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# ruff: noqa
16-
17-
from {{ cookiecutter.app_name|replace('-', '_') }}.agent import agent
15+
from {{ cookiecutter.app_name|replace('-', '_') }}.agent import agent # type: ignore
1816

1917
from veadk.memory.short_term_memory import ShortTermMemory
2018
from veadk.types import AgentRunConfig
2119

2220
# [required] instantiate the agent run configuration
2321
agent_run_config = AgentRunConfig(
2422
app_name="{{ cookiecutter.app_name }}",
25-
agent=agent,
23+
agent=agent, # type: ignore
2624
requirement_file_path="{{ cookiecutter.requirement_file_path }}",
27-
short_term_memory=ShortTermMemory(backend="{{ cookiecutter.short_term_memory_backend }}"),
25+
short_term_memory=ShortTermMemory(backend="{{ cookiecutter.short_term_memory_backend }}"), # type: ignore
2826
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git+https://github.com/volcengine/veadk-python.git

veadk/integrations/ve_faas/types.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)