|
| 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 | +import click |
| 16 | +import os |
| 17 | + |
| 18 | +from veadk.cloud.cloud_agent_engine import CloudAgentEngine |
| 19 | +from veadk.utils.logger import get_logger |
| 20 | + |
| 21 | +logger = get_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +@click.command() |
| 25 | +@click.option( |
| 26 | + "--volcengine-access-key", |
| 27 | + default=None, |
| 28 | + help="Volcengine access key for authentication. Defaults to VOLCENGINE_ACCESS_KEY environment variable.", |
| 29 | +) |
| 30 | +@click.option( |
| 31 | + "--volcengine-secret-key", |
| 32 | + default=None, |
| 33 | + help="Volcengine secret key for authentication. Defaults to VOLCENGINE_SECRET_KEY environment variable.", |
| 34 | +) |
| 35 | +@click.option( |
| 36 | + "--application-name", |
| 37 | + required=True, |
| 38 | + help="Name of the cloud application to update.", |
| 39 | +) |
| 40 | +@click.option( |
| 41 | + "--path", |
| 42 | + default=".", |
| 43 | + help="Local path containing the updated code. Defaults to current directory.", |
| 44 | +) |
| 45 | +def update( |
| 46 | + volcengine_access_key: str, |
| 47 | + volcengine_secret_key: str, |
| 48 | + application_name: str, |
| 49 | + path: str, |
| 50 | +) -> None: |
| 51 | + """Update function code of a deployed cloud application on Volcengine FaaS. |
| 52 | +
|
| 53 | + This command updates the code of an existing cloud application without changing |
| 54 | + the endpoint or other resources. It uploads the local project code to replace |
| 55 | + the existing function implementation. |
| 56 | +
|
| 57 | + The update process: |
| 58 | + 1. Authenticates with Volcengine using provided credentials |
| 59 | + 2. Validates the local project path and application name |
| 60 | + 3. Uploads the updated code to the existing application |
| 61 | + 4. Preserves the existing endpoint and gateway configuration |
| 62 | +
|
| 63 | + Args: |
| 64 | + volcengine_access_key: Volcengine platform access key for authentication. |
| 65 | + If not provided, uses VOLCENGINE_ACCESS_KEY environment variable. |
| 66 | + volcengine_secret_key: Volcengine platform secret key for authentication. |
| 67 | + If not provided, uses VOLCENGINE_SECRET_KEY environment variable. |
| 68 | + application_name: Name of the existing cloud application to update. |
| 69 | + path: Local directory path containing the updated agent project. |
| 70 | + Defaults to current directory if not specified. |
| 71 | +
|
| 72 | + Note: |
| 73 | + - Application must already exist on Volcengine FaaS |
| 74 | + - Only function code is updated, endpoint remains unchanged |
| 75 | + - Uses default region 'cn-beijing' for Volcengine services |
| 76 | +
|
| 77 | + Raises: |
| 78 | + ValueError: If authentication fails or application not found. |
| 79 | + FileNotFoundError: If local path does not exist. |
| 80 | + """ |
| 81 | + # Set environment variables if provided |
| 82 | + if volcengine_access_key and "VOLCENGINE_ACCESS_KEY" not in os.environ: |
| 83 | + os.environ["VOLCENGINE_ACCESS_KEY"] = volcengine_access_key |
| 84 | + if volcengine_secret_key and "VOLCENGINE_SECRET_KEY" not in os.environ: |
| 85 | + os.environ["VOLCENGINE_SECRET_KEY"] = volcengine_secret_key |
| 86 | + |
| 87 | + # Initialize cloud agent engine |
| 88 | + engine = CloudAgentEngine() |
| 89 | + |
| 90 | + try: |
| 91 | + # Update function code |
| 92 | + updated_app = engine.update_function_code( |
| 93 | + application_name=application_name, |
| 94 | + path=path, |
| 95 | + ) |
| 96 | + |
| 97 | + logger.info(f"Successfully updated cloud application '{application_name}'") |
| 98 | + logger.info(f"Endpoint: {updated_app.vefaas_endpoint}") |
| 99 | + logger.info(f"Application ID: {updated_app.vefaas_application_id}") |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + logger.error(f"Failed to update cloud application: {e}") |
| 103 | + raise |
0 commit comments