|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +import click |
| 18 | + |
| 19 | + |
| 20 | +def _set_variable_in_file(file_path: str, setting_values: dict): |
| 21 | + import ast |
| 22 | + |
| 23 | + with open(file_path, "r", encoding="utf-8") as f: |
| 24 | + source_code = f.read() |
| 25 | + |
| 26 | + tree = ast.parse(source_code) |
| 27 | + |
| 28 | + class VariableTransformer(ast.NodeTransformer): |
| 29 | + def visit_Assign(self, node: ast.Assign): |
| 30 | + for target in node.targets: |
| 31 | + if isinstance(target, ast.Name) and target.id in setting_values: |
| 32 | + node.value = ast.Constant(value=setting_values[target.id]) |
| 33 | + return node |
| 34 | + |
| 35 | + transformer = VariableTransformer() |
| 36 | + new_tree = transformer.visit(tree) |
| 37 | + ast.fix_missing_locations(new_tree) |
| 38 | + new_source_code = ast.unparse(new_tree) |
| 39 | + |
| 40 | + with open(file_path, "w", encoding="utf-8") as f: |
| 41 | + f.write(new_source_code) |
| 42 | + |
| 43 | + click.echo("Your project has beed created.") |
| 44 | + |
| 45 | + |
| 46 | +def _render_prompts() -> dict[str, Any]: |
| 47 | + vefaas_application_name = click.prompt( |
| 48 | + "Volcengine FaaS application name", default="veadk-cloud-agent" |
| 49 | + ) |
| 50 | + |
| 51 | + gateway_name = click.prompt( |
| 52 | + "Volcengine gateway instance name", default="", show_default=True |
| 53 | + ) |
| 54 | + |
| 55 | + gateway_service_name = click.prompt( |
| 56 | + "Volcengine gateway service name", default="", show_default=True |
| 57 | + ) |
| 58 | + |
| 59 | + gateway_upstream_name = click.prompt( |
| 60 | + "Volcengine gateway upstream name", default="", show_default=True |
| 61 | + ) |
| 62 | + |
| 63 | + deploy_mode_options = { |
| 64 | + "1": "A2A/MCP Server", |
| 65 | + "2": "VeADK Studio", |
| 66 | + "3": "VeADK Web / Google ADK Web", |
| 67 | + } |
| 68 | + |
| 69 | + click.echo("Choose a deploy mode:") |
| 70 | + for key, value in deploy_mode_options.items(): |
| 71 | + click.echo(f" {key}. {value}") |
| 72 | + |
| 73 | + deploy_mode = click.prompt( |
| 74 | + "Enter your choice", type=click.Choice(deploy_mode_options.keys()) |
| 75 | + ) |
| 76 | + |
| 77 | + return { |
| 78 | + "VEFAAS_APPLICATION_NAME": vefaas_application_name, |
| 79 | + "GATEWAY_NAME": gateway_name, |
| 80 | + "GATEWAY_SERVICE_NAME": gateway_service_name, |
| 81 | + "GATEWAY_UPSTREAM_NAME": gateway_upstream_name, |
| 82 | + "USE_STUDIO": deploy_mode == deploy_mode_options["2"], |
| 83 | + "USE_ADK_WEB": deploy_mode == deploy_mode_options["3"], |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +@click.command() |
| 88 | +def init() -> None: |
| 89 | + """Init a veadk project that can be deployed to Volcengine VeFaaS.""" |
| 90 | + import shutil |
| 91 | + from pathlib import Path |
| 92 | + |
| 93 | + import veadk.integrations.ve_faas as vefaas |
| 94 | + |
| 95 | + cwd = Path.cwd() |
| 96 | + local_dir_name = click.prompt("Directory name", default="veadk-cloud-proj") |
| 97 | + target_dir_path = cwd / local_dir_name |
| 98 | + |
| 99 | + if target_dir_path.exists(): |
| 100 | + click.confirm( |
| 101 | + f"Directory '{target_dir_path}' already exists, do you want to overwrite it", |
| 102 | + abort=True, |
| 103 | + ) |
| 104 | + shutil.rmtree(target_dir_path) |
| 105 | + |
| 106 | + setting_values = _render_prompts() |
| 107 | + |
| 108 | + template_dir_path = Path(vefaas.__file__).parent / "template" |
| 109 | + shutil.copytree(template_dir_path, target_dir_path) |
| 110 | + _set_variable_in_file(target_dir_path / "deploy.py", setting_values) |
0 commit comments