diff --git a/veadk/cli/cli_init.py b/veadk/cli/cli_init.py index e320badb..74c12d50 100644 --- a/veadk/cli/cli_init.py +++ b/veadk/cli/cli_init.py @@ -64,7 +64,12 @@ def _render_prompts() -> dict[str, Any]: @click.command() -def init() -> None: +@click.option( + "--vefaas-template-type", default="template", help="Expected template type" +) +def init( + vefaas_template_type: str, +) -> None: """Init a veadk project that can be deployed to Volcengine VeFaaS.""" import shutil from pathlib import Path @@ -73,9 +78,14 @@ def init() -> None: import veadk.integrations.ve_faas as vefaas - click.echo( - "Welcome use VeADK to create your project. We will generate a `weather-reporter` application for you." - ) + if vefaas_template_type == "web_template": + click.echo( + "Welcome use VeADK to create your project. We will generate a `simple-blog` web application for you." + ) + else: + click.echo( + "Welcome use VeADK to create your project. We will generate a `weather-reporter` application for you." + ) cwd = Path.cwd() local_dir_name = click.prompt("Local directory name", default="veadk-cloud-proj") @@ -91,7 +101,10 @@ def init() -> None: settings = _render_prompts() settings["local_dir_name"] = local_dir_name - template_dir_path = Path(vefaas.__file__).parent / "template" + if not vefaas_template_type: + vefaas_template_type = "template" + + template_dir_path = Path(vefaas.__file__).parent / vefaas_template_type cookiecutter( template=str(template_dir_path), diff --git a/veadk/integrations/ve_faas/template/web/run.sh b/veadk/integrations/ve_faas/template/web/run.sh deleted file mode 100755 index 3f312ba1..00000000 --- a/veadk/integrations/ve_faas/template/web/run.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# 设置环境变量 -export FLASK_APP=app.py -export FLASK_ENV=production -# 初始化数据库 -python init_db.py - -echo "Starting Flask application..." -# 启动应用,使用生产服务器配置 -exec gunicorn -w 4 -b 0.0.0.0:5000 app:app \ No newline at end of file diff --git a/veadk/integrations/ve_faas/web_template/cookiecutter.json b/veadk/integrations/ve_faas/web_template/cookiecutter.json new file mode 100644 index 00000000..ef5e8f3a --- /dev/null +++ b/veadk/integrations/ve_faas/web_template/cookiecutter.json @@ -0,0 +1,17 @@ +{ + "local_dir_name": "veadk_vefaas_web_proj", + "app_name": "simple-blog", + "vefaas_application_name": "simple-blog", + "veapig_instance_name": "", + "veapig_service_name": "", + "veapig_upstream_name": "", + "use_adk_web": false, + "veadk_version": "", + "_copy_without_render": [ + "*.html", + "*.css", + "*.js", + "static/**/*", + "templates/**/*" + ] +} \ No newline at end of file diff --git a/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/__init__.py b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/__init__.py new file mode 100644 index 00000000..7f463206 --- /dev/null +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/__init__.py @@ -0,0 +1,13 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/clean.py b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/clean.py new file mode 100644 index 00000000..ceba3f39 --- /dev/null +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/clean.py @@ -0,0 +1,23 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from veadk.cloud.cloud_app import CloudApp + +def main() -> None: + cloud_app = CloudApp(vefaas_application_name="{{cookiecutter.vefaas_application_name}}") + cloud_app.delete_self() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/config.yaml.example b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/config.yaml.example new file mode 100644 index 00000000..2911de20 --- /dev/null +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/config.yaml.example @@ -0,0 +1,2 @@ +VOLCENGINE_ACCESS_KEY: +VOLCENGINE_SECRET_KEY: \ No newline at end of file diff --git a/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/deploy.py b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/deploy.py new file mode 100644 index 00000000..5855cd71 --- /dev/null +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/deploy.py @@ -0,0 +1,41 @@ +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio +from pathlib import Path + +from veadk.cloud.cloud_agent_engine import CloudAgentEngine + +async def main(): + engine = CloudAgentEngine() + + cloud_app = engine.deploy( + path=str(Path(__file__).parent / "src"), + application_name="{{cookiecutter.vefaas_application_name}}", + gateway_name="{{cookiecutter.veapig_instance_name}}", + gateway_service_name="{{cookiecutter.veapig_service_name}}", + gateway_upstream_name="{{cookiecutter.veapig_upstream_name}}", + use_adk_web={{cookiecutter.use_adk_web}}, + local_test=False, # Set to True for local testing before deploy to VeFaaS + ) + print(f"VeFaaS application ID: {cloud_app.vefaas_application_id}") + + if {{cookiecutter.use_adk_web}}: + print(f"Web is running at: {cloud_app.vefaas_endpoint}") + else: + print(f"Web template does not support use_adk_web=False") + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/veadk/integrations/ve_faas/template/web/.dockerignore b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/.dockerignore similarity index 100% rename from veadk/integrations/ve_faas/template/web/.dockerignore rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/.dockerignore diff --git a/veadk/integrations/ve_faas/template/web/.gitignore b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/.gitignore similarity index 100% rename from veadk/integrations/ve_faas/template/web/.gitignore rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/.gitignore diff --git a/veadk/integrations/ve_faas/template/web/Dockerfile b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/Dockerfile similarity index 97% rename from veadk/integrations/ve_faas/template/web/Dockerfile rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/Dockerfile index d3e6bf73..21e0e190 100644 --- a/veadk/integrations/ve_faas/template/web/Dockerfile +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/Dockerfile @@ -17,7 +17,7 @@ ENV FLASK_ENV=production ENV PYTHONUNBUFFERED=1 # 暴露端口 -EXPOSE 5000 +EXPOSE 8000 # 启动命令 CMD ["bash", "run.sh"] diff --git a/veadk/integrations/ve_faas/template/web/app.py b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/app.py similarity index 97% rename from veadk/integrations/ve_faas/template/web/app.py rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/app.py index b0b7c3f9..45e4d865 100644 --- a/veadk/integrations/ve_faas/template/web/app.py +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/app.py @@ -21,6 +21,8 @@ app.config['SECRET_KEY'] = 'your-secret-key-here' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.instance_path = os.path.join("/tmp", "flask_instance") +os.makedirs(app.instance_path, exist_ok=True) db.init_app(app) diff --git a/veadk/integrations/ve_faas/template/web/init_db.py b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/init_db.py similarity index 100% rename from veadk/integrations/ve_faas/template/web/init_db.py rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/init_db.py diff --git a/veadk/integrations/ve_faas/template/web/models.py b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/models.py similarity index 100% rename from veadk/integrations/ve_faas/template/web/models.py rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/models.py diff --git a/veadk/integrations/ve_faas/template/web/requirements.txt b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/requirements.txt similarity index 100% rename from veadk/integrations/ve_faas/template/web/requirements.txt rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/requirements.txt diff --git a/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/run.sh b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/run.sh new file mode 100755 index 00000000..9c056834 --- /dev/null +++ b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/run.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# 兼容源码部署到faas & 镜像部署到faas +pip install -r requirements.txt + +HOST="0.0.0.0" +PORT="${_FAAS_RUNTIME_PORT:-8000}" + +export SERVER_HOST=$HOST +export SERVER_PORT=$PORT + +# 设置环境变量 +export FLASK_APP=app.py +export FLASK_ENV=production + +# 初始化数据库 +python init_db.py + +echo "Starting Web application..." +# 启动应用,使用生产服务器配置 +exec python -m gunicorn -w 4 -b $SERVER_HOST:$SERVER_PORT app:app diff --git a/veadk/integrations/ve_faas/template/web/static/css/style.css b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/css/style.css similarity index 100% rename from veadk/integrations/ve_faas/template/web/static/css/style.css rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/css/style.css diff --git a/veadk/integrations/ve_faas/template/web/static/js/admin.js b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/js/admin.js similarity index 100% rename from veadk/integrations/ve_faas/template/web/static/js/admin.js rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/js/admin.js diff --git a/veadk/integrations/ve_faas/template/web/templates/admin/dashboard.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/dashboard.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/admin/dashboard.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/dashboard.html diff --git a/veadk/integrations/ve_faas/template/web/templates/admin/edit_post.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/edit_post.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/admin/edit_post.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/edit_post.html diff --git a/veadk/integrations/ve_faas/template/web/templates/admin/login.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/login.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/admin/login.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/login.html diff --git a/veadk/integrations/ve_faas/template/web/templates/admin/posts.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/posts.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/admin/posts.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/posts.html diff --git a/veadk/integrations/ve_faas/template/web/templates/base.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/base.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/base.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/base.html diff --git a/veadk/integrations/ve_faas/template/web/templates/index.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/index.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/index.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/index.html diff --git a/veadk/integrations/ve_faas/template/web/templates/post.html b/veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/post.html similarity index 100% rename from veadk/integrations/ve_faas/template/web/templates/post.html rename to veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/post.html