1515import warnings
1616
1717import click
18+ from veadk .version import VERSION
1819from veadk .config import getenv
1920from veadk .integrations .ve_code_pipeline .ve_code_pipeline import VeCodePipeline
2021from 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+ )
2128
2229warnings .filterwarnings (
2330 "ignore" , category = UserWarning , module = "pydantic._internal._fields"
2431)
2532
2633
27- def _render_volcengine_prompts () -> dict [str , str ]:
28- volcengine_access_key = click .prompt (
29- "Volcengine Access Key" , default = "" , show_default = False
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" ],
3039 )
31- if not volcengine_access_key :
32- click .echo (
33- "No Volcengine Access Key provided, will try to get it from environment variable VOLCENGINE_ACCESS_KEY."
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" ],
3450 )
35- volcengine_access_key = getenv ("VOLCENGINE_ACCESS_KEY" )
36-
37- volcengine_secret_key = click .prompt (
38- "Volcengine Secret Key" , default = "" , show_default = False
39- )
40- if not volcengine_secret_key :
41- click .echo (
42- "No Volcengine Secret Key provided, will try to get it from environment variable VOLCENGINE_SECRET_KEY."
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" ],
4360 )
44- volcengine_secret_key = getenv ("VOLCENGINE_SECRET_KEY" )
61+ except Exception as e :
62+ click .echo (f"Failed to create CR repo: { e } " )
63+ raise
4564
46- volcengine_region = click .prompt ("Volcengine Region" , default = "cn-beijing" )
47- return {
48- "volcengine_access_key" : volcengine_access_key ,
49- "volcengine_secret_key" : volcengine_secret_key ,
50- "volcengine_region" : volcengine_region ,
51- }
5265
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 = None ,
90+ help = "Volcengine access key" ,
91+ )
92+ @click .option (
93+ "--secret-key" ,
94+ default = None ,
95+ help = "Volcengine secret key" ,
96+ )
97+ @click .option (
98+ "--region" ,
99+ default = "cn-beijing" ,
100+ help = "Volcengine region" ,
101+ )
102+ @click .option (
103+ "--cr-domain" ,
104+ default = None ,
105+ help = "Container Registry domain" ,
106+ )
107+ @click .option (
108+ "--cr-namespace-name" ,
109+ default = None ,
110+ help = "Container Registry namespace name" ,
111+ )
112+ @click .option (
113+ "--cr-region" ,
114+ default = None ,
115+ help = "Container Registry region" ,
116+ )
117+ @click .option (
118+ "--cr-instance-name" ,
119+ default = None ,
120+ help = "Container Registry instance name" ,
121+ )
122+ @click .option (
123+ "--cr-repo" ,
124+ default = None ,
125+ help = "Container Registry repo" ,
126+ )
127+ @click .option (
128+ "--function-id" ,
129+ default = None ,
130+ help = "Volcengine FaaS function ID" ,
131+ )
132+ def pipeline (
133+ base_image_tag : str ,
134+ github_url : str ,
135+ github_branch : str ,
136+ github_token : str ,
137+ access_key : str ,
138+ secret_key : str ,
139+ region : str ,
140+ cr_domain : str ,
141+ cr_namespace_name : str ,
142+ cr_region : str ,
143+ cr_instance_name : str ,
144+ cr_repo : str ,
145+ function_id : str ,
146+ ) -> None :
147+ """Integrate a veadk project to volcengine pipeline for CI/CD"""
53148
54- def _render_cr_prompts () -> dict [str , str ] | None :
55- cr_domain , cr_namespace_name , cr_region , cr_instance_name , cr_repo = (
56- "" ,
57- "" ,
58- "" ,
59- "" ,
60- "" ,
149+ click .echo (
150+ "Welcome use VeADK to integrate your project to volcengine pipeline for CI/CD."
61151 )
62- cr_fields = [cr_domain , cr_namespace_name , cr_region , cr_instance_name , cr_repo ]
63- filled_fields = [field for field in cr_fields if field .strip ()]
64-
65- while len (filled_fields ) < len (cr_fields ):
66- click .echo (
67- "Please provide all the Container Registry (CR) information, "
68- "or press Enter to leave them all blank and let VeADK create the CR automatically."
69- )
70- cr_domain = click .prompt (
71- "Container Registry domain" , default = "" , show_default = False
72- )
73- cr_namespace_name = click .prompt (
74- "Container Registry namespace name" , default = "" , show_default = False
75- )
76- cr_region = click .prompt (
77- "Container Registry region" , default = "" , show_default = False
78- )
79- cr_instance_name = click .prompt (
80- "Container Registry instance name" , default = "" , show_default = False
81- )
82- cr_repo = click .prompt (
83- "Container Registry repo" , default = "" , show_default = False
84- )
85152
86- cr_fields = [cr_domain , cr_namespace_name , cr_region , cr_instance_name , cr_repo ]
87- filled_fields = [field for field in cr_fields if field .strip ()]
153+ if not access_key :
154+ access_key = getenv ("VOLCENGINE_ACCESS_KEY" )
155+ if not secret_key :
156+ secret_key = getenv ("VOLCENGINE_SECRET_KEY" )
88157
89- if len (filled_fields ) == 0 :
90- return None
158+ volcengine_settings = {
159+ "volcengine_access_key" : access_key ,
160+ "volcengine_secret_key" : secret_key ,
161+ "volcengine_region" : region ,
162+ }
91163
92- return {
164+ cr_settings = {
93165 "cr_domain" : cr_domain ,
94166 "cr_namespace_name" : cr_namespace_name ,
95167 "cr_region" : cr_region ,
96168 "cr_instance_name" : cr_instance_name ,
97169 "cr_repo" : cr_repo ,
98170 }
99171
172+ if not all (cr_settings .values ()):
173+ click .echo (
174+ "Not all Container Registry (CR) information is specified; it will be auto-completed and created."
175+ )
100176
101- @click .command ()
102- def pipeline () -> None :
103- """Integrate a veadk project to volcengine pipeline for CI/CD"""
104-
105- click .echo (
106- "Welcome use VeADK to integrate your project to volcengine pipeline for CI/CD."
107- )
108-
109- base_image_tag_options = ["preview" , "0.0.1" , "latest" ]
110- base_image_tag = click .prompt (
111- "Choose a base image tag:" , type = click .Choice (base_image_tag_options )
112- )
113-
114- github_url = click .prompt ("Github url" , default = "" , show_default = False )
115- while not github_url :
116- click .echo ("Please enter your github url." )
117- github_url = click .prompt ("Github url" , default = "" , show_default = False )
118-
119- github_branch = click .prompt ("Github branch" , default = "main" )
120-
121- github_token = click .prompt ("Github token" , default = "" , show_default = False )
122- while not github_token :
123- click .echo ("Please enter your github token." )
124- github_token = click .prompt ("Github token" , default = "" , show_default = False )
125-
126- volcengine_settings = _render_volcengine_prompts ()
127-
128- cr_settings = _render_cr_prompts ()
129-
130- if cr_settings is None :
131- click .echo ("No CR information provided, will auto-create one." )
132- # cr_settings = _auto_create_cr_config() # TODO
133-
134- # Using hardcoded values for demonstration
135- cr_settings = {
136- "cr_domain" : "test-veadk-cn-beijing.cr.volces.com" ,
137- "cr_namespace_name" : "veadk" ,
138- "cr_region" : "cn-beijing" ,
139- "cr_instance_name" : "test-veadk" ,
140- "cr_repo" : "cicd-weather-test" ,
141- }
142- click .echo ("Using the following auto-created CR configuration:" )
177+ for key , value in cr_settings .items ():
178+ if key == "cr_domain" and value is None :
179+ cr_settings [key ] = (
180+ f"{ DEFAULT_CR_INSTANCE_NAME } -cn-beijing.cr.volces.com"
181+ )
182+ elif key == "cr_namespace_name" and value is None :
183+ cr_settings [key ] = DEFAULT_CR_NAMESPACE_NAME
184+ elif key == "cr_region" and value is None :
185+ cr_settings [key ] = "cn-beijing"
186+ elif key == "cr_instance_name" and value is None :
187+ cr_settings [key ] = DEFAULT_CR_INSTANCE_NAME
188+ elif key == "cr_repo" and value is None :
189+ cr_settings [key ] = DEFAULT_CR_REPO_NAME
190+
191+ _create_cr (volcengine_settings , cr_settings )
192+
193+ click .echo ("Using the following CR configuration:" )
143194 click .echo (f"Container Registry domain: { cr_settings ['cr_domain' ]} " )
144195 click .echo (
145196 f"Container Registry namespace name: { cr_settings ['cr_namespace_name' ]} "
@@ -150,12 +201,10 @@ def pipeline() -> None:
150201 )
151202 click .echo (f"Container Registry repo: { cr_settings ['cr_repo' ]} " )
152203
153- function_id = click .prompt (
154- "Volcengine FaaS function ID" , default = "" , show_default = False
155- )
156-
157204 if not function_id :
158- click .echo ("Function ID not provided, will auto-create one." )
205+ click .echo (
206+ "No Function ID specified. The system will create one automatically. Please specify a function name:"
207+ )
159208 function_name = click .prompt (
160209 "Function name" , default = "veadk-function" , show_default = False
161210 )
@@ -167,6 +216,7 @@ def pipeline() -> None:
167216 _ , _ , function_id = vefaas_client .deploy_image (
168217 name = function_name ,
169218 image = "veadk-cn-beijing.cr.volces.com/veadk/simple-fastapi:0.1" ,
219+ registry_name = cr_settings ["cr_instance_name" ],
170220 )
171221 click .echo (f"Created function { function_name } with ID: { function_id } " )
172222
0 commit comments