Skip to content

Commit e36f1d9

Browse files
refine deploy cli and cloud app (#30)
* refine deploy cli and cloud app * remove cloud template
1 parent 34f37f4 commit e36f1d9

File tree

8 files changed

+203
-280
lines changed

8 files changed

+203
-280
lines changed

veadk/cli/main.py

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@
3232
app = typer.Typer(name="vego")
3333

3434

35+
def set_variable_in_file(file_path: str, setting_values: dict):
36+
import ast
37+
38+
with open(file_path, "r", encoding="utf-8") as f:
39+
source_code = f.read()
40+
41+
tree = ast.parse(source_code)
42+
43+
class VariableTransformer(ast.NodeTransformer):
44+
def visit_Assign(self, node: ast.Assign):
45+
for target in node.targets:
46+
if isinstance(target, ast.Name) and target.id in setting_values:
47+
node.value = ast.Constant(value=setting_values[target.id])
48+
return node
49+
50+
transformer = VariableTransformer()
51+
new_tree = transformer.visit(tree)
52+
ast.fix_missing_locations(new_tree)
53+
new_source_code = ast.unparse(new_tree)
54+
55+
with open(file_path, "w", encoding="utf-8") as f:
56+
f.write(new_source_code)
57+
58+
print("Your project has beed created.")
59+
60+
3561
@app.command()
3662
def init():
3763
"""Init a veadk project that can be deployed to Volcengine VeFaaS."""
@@ -40,9 +66,9 @@ def init():
4066
cwd = Path.cwd()
4167
template_dir = Path(__file__).parent.resolve() / "services" / "vefaas" / "template"
4268

43-
name = Prompt.ask("Project name", default="veadk-cloud-agent")
69+
local_dir_name = Prompt.ask("Local directory name", default="veadk-cloud-proj")
4470

45-
target_dir = cwd / name
71+
target_dir = cwd / local_dir_name
4672

4773
if target_dir.exists():
4874
response = Confirm.ask(
@@ -52,11 +78,57 @@ def init():
5278
print("Operation cancelled.")
5379
return
5480
else:
55-
shutil.rmtree(target_dir) # 删除旧目录
81+
shutil.rmtree(target_dir)
5682
print(f"Deleted existing directory: {target_dir}")
5783

84+
vefaas_application_name = Prompt.ask(
85+
"Volcengine FaaS application name", default="veadk-cloud-agent"
86+
)
87+
88+
gateway_name = Prompt.ask(
89+
"Volcengine gateway instance name", default="", show_default=True
90+
)
91+
92+
gateway_service_name = Prompt.ask(
93+
"Volcengine gateway service name", default="", show_default=True
94+
)
95+
96+
gateway_upstream_name = Prompt.ask(
97+
"Volcengine gateway upstream name", default="", show_default=True
98+
)
99+
100+
deploy_mode_options = {
101+
"1": "A2A Server",
102+
"2": "VeADK Studio",
103+
"3": "VeADK Web / Google ADK Web",
104+
}
105+
106+
deploy_mode = Prompt.ask(
107+
"""Choose your deploy mode:
108+
1. A2A Server
109+
2. VeADK Studio
110+
3. VeADK Web / Google ADK Web
111+
""",
112+
default="1",
113+
)
114+
115+
if deploy_mode in deploy_mode_options:
116+
deploy_mode = deploy_mode_options[deploy_mode]
117+
else:
118+
print("Invalid deploy mode, set default to A2A Server")
119+
deploy_mode = deploy_mode_options["1"]
120+
121+
setting_values = {
122+
"VEFAAS_APPLICATION_NAME": vefaas_application_name,
123+
"GATEWAY_NAME": gateway_name,
124+
"GATEWAY_SERVICE_NAME": gateway_service_name,
125+
"GATEWAY_UPSTREAM_NAME": gateway_upstream_name,
126+
"USE_STUDIO": deploy_mode == deploy_mode_options["2"],
127+
"USE_ADK_WEB": deploy_mode == deploy_mode_options["3"],
128+
}
129+
58130
shutil.copytree(template_dir, target_dir)
59-
print(f"Created new project: {name}")
131+
set_variable_in_file(target_dir / "deploy.py", setting_values)
60132

61133

62134
# @app.command()

veadk/cli/services/vefaas/template/deploy.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,38 @@
2020
SESSION_ID = "cloud_app_test_session"
2121
USER_ID = "cloud_app_test_user"
2222

23+
VEFAAS_APPLICATION_NAME = "weather-reporter"
24+
GATEWAY_NAME = ""
25+
GATEWAY_SERVICE_NAME = ""
26+
GATEWAY_UPSTREAMNAME = ""
2327
USE_STUDIO = False
2428
USE_ADK_WEB = False
2529

2630

2731
async def main():
2832
engine = CloudAgentEngine()
33+
2934
cloud_app = engine.deploy(
3035
path=str(Path(__file__).parent / "src"),
31-
name="weather-reporter", # <--- set your application name
36+
application_name=VEFAAS_APPLICATION_NAME,
37+
gateway_name=GATEWAY_NAME,
38+
gateway_service_name=GATEWAY_SERVICE_NAME,
39+
gateway_upstream_name=GATEWAY_UPSTREAMNAME,
3240
use_studio=USE_STUDIO,
3341
use_adk_web=USE_ADK_WEB,
34-
# gateway_name="", # <--- set your gateway instance name if you have one
3542
)
3643

3744
if not USE_STUDIO and not USE_ADK_WEB:
3845
response_message = await cloud_app.message_send(
3946
"How is the weather like in Beijing?", SESSION_ID, USER_ID
4047
)
41-
48+
print(f"VeFaaS application ID: {cloud_app.vefaas_application_id}")
4249
print(f"Message ID: {response_message.messageId}")
43-
4450
print(
45-
f"Response from {cloud_app.endpoint}: {response_message.parts[0].root.text}"
51+
f"Response from {cloud_app.vefaas_endpoint}: {response_message.parts[0].root.text}"
4652
)
47-
48-
print(f"App ID: {cloud_app.app_id}")
4953
else:
50-
print(f"VeADK Studio URL: {cloud_app.endpoint}")
54+
print(f"Web is running at: {cloud_app.vefaas_endpoint}")
5155

5256

5357
if __name__ == "__main__":

veadk/cloud/cloud_agent_engine.py

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -40,71 +40,54 @@ def model_post_init(self, context: Any, /) -> None:
4040
)
4141

4242
def _prepare(self, path: str, name: str):
43-
# VeFaaS path check
44-
if "_" in name:
45-
raise ValueError(
46-
f"Invalid Volcengine FaaS function name `{name}`, please use lowercase letters and numbers, or replace it with a `-` char."
47-
)
48-
49-
# project path check
43+
# basic check
5044
assert os.path.exists(path), f"Local agent project path `{path}` not exists."
5145
assert os.path.isdir(path), (
5246
f"Local agent project path `{path}` is not a directory."
5347
)
5448

55-
assert os.path.exists(os.path.join(path, "agent.py")), (
56-
f"Local agent project path `{path}` does not contain `agent.py` file. Please prepare it according to veadk-python/cloud/template/agent.py.example"
57-
)
58-
59-
if os.path.exists(os.path.join(path, "app.py")):
60-
logger.warning(
61-
f"Local agent project path `{path}` contains an `app.py` file. Use your own `app.py` file may cause unexpected behavior."
62-
)
63-
else:
64-
logger.info(
65-
f"No `app.py` detected in local agent project path `{path}`. Prepare it."
66-
)
67-
template_app_py = (
68-
f"{Path(__file__).resolve().parent.resolve()}/template/app.py"
69-
)
70-
import shutil
71-
72-
shutil.copy(template_app_py, os.path.join(path, "app.py"))
73-
74-
if os.path.exists(os.path.join(path, "studio_app.py")):
75-
logger.warning(
76-
f"Local agent project path `{path}` contains an `studio_app.py` file. Use your own `studio_app.py` file may cause unexpected behavior."
77-
)
78-
else:
79-
logger.info(
80-
f"No `studio_app.py` detected in local agent project path `{path}`. Prepare it."
81-
)
82-
template_studio_app_py = (
83-
f"{Path(__file__).resolve().parent.resolve()}/template/studio_app.py"
49+
# VeFaaS application/function name check
50+
if "_" in name:
51+
raise ValueError(
52+
f"Invalid Volcengine FaaS function name `{name}`, please use lowercase letters and numbers, or replace it with a `-` char."
8453
)
85-
import shutil
8654

87-
shutil.copy(template_studio_app_py, os.path.join(path, "studio_app.py"))
55+
# project structure check
56+
assert os.path.exists(os.path.join(path, "agent.py")), (
57+
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"
58+
)
8859

89-
if os.path.exists(os.path.join(path, "run.sh")):
60+
if not os.path.exists(os.path.join(path, "config.yaml")):
9061
logger.warning(
91-
f"Local agent project path `{path}` contains a `run.sh` file. Use your own `run.sh` file may cause unexpected behavior."
92-
)
93-
else:
94-
logger.info(
95-
f"No `run.sh` detected in local agent project path `{path}`. Prepare it."
96-
)
97-
template_run_sh = (
98-
f"{Path(__file__).resolve().parent.resolve()}/template/run.sh"
62+
f"Local agent project path `{path}` does not contain `config.yaml` file. Some important config items may not be set."
9963
)
100-
import shutil
10164

102-
shutil.copy(template_run_sh, os.path.join(path, "run.sh"))
65+
# prepare template files if not have
66+
template_files = [
67+
"app.py",
68+
"studio_app.py",
69+
"run.sh",
70+
"requirements.txt",
71+
"__init__.py",
72+
]
73+
for template_file in template_files:
74+
if os.path.exists(os.path.join(path, template_file)):
75+
logger.warning(
76+
f"Local agent project path `{path}` contains a `{template_file}` file. Use your own `{template_file}` file may cause unexpected behavior."
77+
)
78+
else:
79+
logger.info(
80+
f"No `{template_file}` detected in local agent project path `{path}`. Prepare it."
81+
)
82+
template_file_path = f"{Path(__file__).resolve().parent.resolve().parent.resolve()}/cli/services/vefaas/template/src/{template_file}"
83+
import shutil
84+
85+
shutil.copy(template_file_path, os.path.join(path, template_file))
10386

10487
def deploy(
10588
self,
89+
application_name: str,
10690
path: str,
107-
name: str,
10891
gateway_name: str = "",
10992
gateway_service_name: str = "",
11093
gateway_upstream_name: str = "",
@@ -123,14 +106,13 @@ def deploy(
123106
assert not (use_studio and use_adk_web), (
124107
"use_studio and use_adk_web can not be True at the same time."
125108
)
109+
126110
# prevent deepeval writing operations
127111
import veadk.config
128112

129113
veadk.config.veadk_environments["DEEPEVAL_TELEMETRY_OPT_OUT"] = "YES"
130114

131115
if use_studio:
132-
import veadk.config
133-
134116
veadk.config.veadk_environments["USE_STUDIO"] = "True"
135117
else:
136118
import veadk.config
@@ -148,29 +130,29 @@ def deploy(
148130

149131
# convert `path` to absolute path
150132
path = str(Path(path).resolve())
151-
self._prepare(path, name)
133+
self._prepare(path, application_name)
152134

153135
if not gateway_name:
154-
gateway_name = f"{name}-gw-{formatted_timestamp()}"
136+
gateway_name = f"{application_name}-gw-{formatted_timestamp()}"
155137
if not gateway_service_name:
156-
gateway_service_name = f"{name}-gw-svr-{formatted_timestamp()}"
138+
gateway_service_name = f"{application_name}-gw-svr-{formatted_timestamp()}"
157139
if not gateway_upstream_name:
158-
gateway_upstream_name = f"{name}-gw-us-{formatted_timestamp()}"
140+
gateway_upstream_name = f"{application_name}-gw-us-{formatted_timestamp()}"
159141

160142
try:
161143
vefaas_application_url, app_id, function_id = self._vefaas_service.deploy(
162144
path=path,
163-
name=name,
145+
name=application_name,
164146
gateway_name=gateway_name,
165147
gateway_service_name=gateway_service_name,
166148
gateway_upstream_name=gateway_upstream_name,
167149
)
168150
_ = function_id # for future use
169151

170152
return CloudApp(
171-
name=name,
172-
endpoint=vefaas_application_url,
173-
app_id=app_id,
153+
vefaas_application_name=application_name,
154+
vefaas_endpoint=vefaas_application_url,
155+
vefaas_application_id=app_id,
174156
)
175157
except Exception as e:
176158
raise ValueError(

0 commit comments

Comments
 (0)