|
| 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 warnings |
| 16 | + |
| 17 | +import click |
| 18 | +from veadk.version import VERSION |
| 19 | +from veadk.config import getenv |
| 20 | +from veadk.integrations.ve_code_pipeline.ve_code_pipeline import VeCodePipeline |
| 21 | +from veadk.integrations.ve_faas.ve_faas import VeFaaS |
| 22 | +from veadk.integrations.ve_cr.ve_cr import VeCR |
| 23 | +from veadk.consts import ( |
| 24 | + DEFAULT_CR_INSTANCE_NAME, |
| 25 | + DEFAULT_CR_NAMESPACE_NAME, |
| 26 | + DEFAULT_CR_REPO_NAME, |
| 27 | +) |
| 28 | + |
| 29 | +warnings.filterwarnings( |
| 30 | + "ignore", category=UserWarning, module="pydantic._internal._fields" |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def _create_cr(volcengine_settings: dict[str, str], cr_settings: dict[str, str]): |
| 35 | + vecr = VeCR( |
| 36 | + access_key=volcengine_settings["volcengine_access_key"], |
| 37 | + secret_key=volcengine_settings["volcengine_secret_key"], |
| 38 | + region=volcengine_settings["volcengine_region"], |
| 39 | + ) |
| 40 | + try: |
| 41 | + vecr._create_instance(cr_settings["cr_instance_name"]) |
| 42 | + except Exception as e: |
| 43 | + click.echo(f"Failed to create CR instance: {e}") |
| 44 | + raise |
| 45 | + |
| 46 | + try: |
| 47 | + vecr._create_namespace( |
| 48 | + instance_name=cr_settings["cr_instance_name"], |
| 49 | + namespace_name=cr_settings["cr_namespace_name"], |
| 50 | + ) |
| 51 | + except Exception as e: |
| 52 | + click.echo(f"Failed to create CR namespace: {e}") |
| 53 | + raise |
| 54 | + |
| 55 | + try: |
| 56 | + vecr._create_repo( |
| 57 | + instance_name=cr_settings["cr_instance_name"], |
| 58 | + namespace_name=cr_settings["cr_namespace_name"], |
| 59 | + repo_name=cr_settings["cr_repo_name"], |
| 60 | + ) |
| 61 | + except Exception as e: |
| 62 | + click.echo(f"Failed to create CR repo: {e}") |
| 63 | + raise |
| 64 | + |
| 65 | + |
| 66 | +@click.command() |
| 67 | +@click.option( |
| 68 | + "--base-image-tag", |
| 69 | + required=True, |
| 70 | + help=f"Base VeADK image tag can be 'preview', 'latest', or a VeADK version (e.g., {VERSION})", |
| 71 | +) |
| 72 | +@click.option( |
| 73 | + "--github-url", |
| 74 | + required=True, |
| 75 | + help="The github url of your project", |
| 76 | +) |
| 77 | +@click.option( |
| 78 | + "--github-branch", |
| 79 | + default="main", |
| 80 | + help="The github branch of your project, default is main", |
| 81 | +) |
| 82 | +@click.option( |
| 83 | + "--github-token", |
| 84 | + required=True, |
| 85 | + help="The github token to manage your project", |
| 86 | +) |
| 87 | +@click.option( |
| 88 | + "--access-key", |
| 89 | + default=getenv("VOLCENGINE_ACCESS_KEY"), |
| 90 | + help="Volcengine access key, if not set, will use the value of environment variable VOLCENGINE_ACCESS_KEY", |
| 91 | +) |
| 92 | +@click.option( |
| 93 | + "--secret-key", |
| 94 | + default=getenv("VOLCENGINE_SECRET_KEY"), |
| 95 | + help="Volcengine secret key, if not set, will use the value of environment variable VOLCENGINE_SECRET_KEY", |
| 96 | +) |
| 97 | +@click.option( |
| 98 | + "--region", |
| 99 | + default="cn-beijing", |
| 100 | + help="Volcengine region, default is cn-beijing", |
| 101 | +) |
| 102 | +@click.option( |
| 103 | + "--cr-instance-name", |
| 104 | + default=DEFAULT_CR_INSTANCE_NAME, |
| 105 | + help="Container Registry instance name, default is veadk-user-instance", |
| 106 | +) |
| 107 | +@click.option( |
| 108 | + "--cr-namespace-name", |
| 109 | + default=DEFAULT_CR_NAMESPACE_NAME, |
| 110 | + help="Container Registry namespace name, default is veadk-user-namespace", |
| 111 | +) |
| 112 | +@click.option( |
| 113 | + "--cr-repo-name", |
| 114 | + default=DEFAULT_CR_REPO_NAME, |
| 115 | + help="Container Registry repo name, default is veadk-user-repo", |
| 116 | +) |
| 117 | +@click.option( |
| 118 | + "--cr-region", |
| 119 | + default="cn-beijing", |
| 120 | + help="Container Registry region, default is cn-beijing", |
| 121 | +) |
| 122 | +@click.option( |
| 123 | + "--function-id", |
| 124 | + default=None, |
| 125 | + help="Volcengine FaaS function ID, if not set, a new function will be created automatically", |
| 126 | +) |
| 127 | +def pipeline( |
| 128 | + base_image_tag: str, |
| 129 | + github_url: str, |
| 130 | + github_branch: str, |
| 131 | + github_token: str, |
| 132 | + access_key: str, |
| 133 | + secret_key: str, |
| 134 | + region: str, |
| 135 | + cr_instance_name: str, |
| 136 | + cr_namespace_name: str, |
| 137 | + cr_repo_name: str, |
| 138 | + cr_region: str, |
| 139 | + function_id: str, |
| 140 | +) -> None: |
| 141 | + """Integrate a veadk project to volcengine pipeline for CI/CD""" |
| 142 | + |
| 143 | + click.echo( |
| 144 | + "Welcome use VeADK to integrate your project to volcengine pipeline for CI/CD." |
| 145 | + ) |
| 146 | + |
| 147 | + volcengine_settings = { |
| 148 | + "volcengine_access_key": access_key, |
| 149 | + "volcengine_secret_key": secret_key, |
| 150 | + "volcengine_region": region, |
| 151 | + } |
| 152 | + |
| 153 | + cr_settings = { |
| 154 | + "cr_domain": f"{cr_instance_name}-{cr_region}.cr.volces.com", |
| 155 | + "cr_instance_name": cr_instance_name, |
| 156 | + "cr_namespace_name": cr_namespace_name, |
| 157 | + "cr_repo_name": cr_repo_name, |
| 158 | + "cr_region": cr_region, |
| 159 | + } |
| 160 | + |
| 161 | + _create_cr(volcengine_settings, cr_settings) |
| 162 | + |
| 163 | + click.echo("Using the following CR configuration:") |
| 164 | + click.echo(f"Container Registry domain: {cr_settings['cr_domain']}") |
| 165 | + click.echo(f"Container Registry namespace name: {cr_settings['cr_namespace_name']}") |
| 166 | + click.echo(f"Container Registry region: {cr_settings['cr_region']}") |
| 167 | + click.echo(f"Container Registry instance name: {cr_settings['cr_instance_name']}") |
| 168 | + click.echo(f"Container Registry repo name: {cr_settings['cr_repo_name']}") |
| 169 | + |
| 170 | + if not function_id: |
| 171 | + click.echo( |
| 172 | + "No Function ID specified. The system will create one automatically. Please specify a function name:" |
| 173 | + ) |
| 174 | + function_name = click.prompt( |
| 175 | + "Function name", default="veadk-function", show_default=False |
| 176 | + ) |
| 177 | + vefaas_client = VeFaaS( |
| 178 | + access_key=volcengine_settings["volcengine_access_key"], |
| 179 | + secret_key=volcengine_settings["volcengine_secret_key"], |
| 180 | + region=volcengine_settings["volcengine_region"], |
| 181 | + ) |
| 182 | + _, _, function_id = vefaas_client.deploy_image( |
| 183 | + name=function_name, |
| 184 | + image="veadk-cn-beijing.cr.volces.com/veadk/simple-fastapi:0.1", |
| 185 | + registry_name=cr_settings["cr_instance_name"], |
| 186 | + ) |
| 187 | + click.echo(f"Created function {function_name} with ID: {function_id}") |
| 188 | + |
| 189 | + client = VeCodePipeline( |
| 190 | + volcengine_access_key=volcengine_settings["volcengine_access_key"], |
| 191 | + volcengine_secret_key=volcengine_settings["volcengine_secret_key"], |
| 192 | + region=volcengine_settings["volcengine_region"], |
| 193 | + ) |
| 194 | + client.deploy( |
| 195 | + base_image_tag=base_image_tag, |
| 196 | + github_url=github_url, |
| 197 | + github_branch=github_branch, |
| 198 | + github_token=github_token, |
| 199 | + cr_domain=cr_settings["cr_domain"], |
| 200 | + cr_namespace_name=cr_settings["cr_namespace_name"], |
| 201 | + cr_region=cr_settings["cr_region"], |
| 202 | + cr_instance_name=cr_settings["cr_instance_name"], |
| 203 | + cr_repo_name=cr_settings["cr_repo_name"], |
| 204 | + function_id=function_id, |
| 205 | + ) |
| 206 | + |
| 207 | + click.echo("Pipeline has been created successfully.") |
0 commit comments