2626 TagForCreateFunctionInput ,
2727)
2828
29+ import veadk .config
2930from veadk .cli .services .veapig .apig import APIGateway
3031from veadk .utils .logger import get_logger
3132from veadk .utils .misc import formatted_timestamp
@@ -58,27 +59,16 @@ def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing")
5859
5960 self .template_id = "6874f3360bdbc40008ecf8c7"
6061
61- def _create_function (self , name : str , path : str ):
62- function_name = f"{ name } -fn-{ formatted_timestamp ()} "
63-
64- # 1. Create a function instance in cloud
65- typer .echo (
66- typer .style ("Runtime: native-python3.10/v1" , fg = typer .colors .BRIGHT_BLACK )
67- )
68-
62+ def _create_function (self , function_name : str , path : str ):
63+ # 1. Read envs
6964 envs = []
70-
71- import veadk .config
72-
7365 for key , value in veadk .config .veadk_environments .items ():
7466 envs .append (EnvForCreateFunctionInput (key = key , value = value ))
75- typer .echo (
76- typer .style (
77- f"Fetch { len (envs )} environment variables." ,
78- fg = typer .colors .BRIGHT_BLACK ,
79- )
67+ logger .info (
68+ f"Fetch { len (envs )} environment variables." ,
8069 )
8170
71+ # 2. Create function
8272 res = self .client .create_function (
8373 volcenginesdkvefaas .CreateFunctionRequest (
8474 command = "./run.sh" ,
@@ -88,21 +78,17 @@ def _create_function(self, name: str, path: str):
8878 runtime = "native-python3.10/v1" ,
8979 request_timeout = 1800 ,
9080 envs = envs ,
91- # tls_config=TlsConfigForCreateFunctionInput(enable_log=True),
9281 )
9382 )
9483 function_id = res .id
9584
96- # 2. Get a temp bucket to store code
97- # proj_path = get_project_path()
85+ # 3. Get a temp bucket to store code
9886 code_zip_data , code_zip_size , error = zip_and_encode_folder (path )
99- typer .echo (
100- typer .style (
101- f"Zipped project size: { code_zip_size / 1024 / 1024 :.2f} MB" ,
102- fg = typer .colors .BRIGHT_BLACK ,
103- )
87+ logger .info (
88+ f"Zipped project size: { code_zip_size / 1024 / 1024 :.2f} MB" ,
10489 )
10590
91+ # 4. Upload code to VeFaaS temp bucket
10692 req = volcenginesdkvefaas .GetCodeUploadAddressRequest (
10793 function_id = function_id , content_length = code_zip_size
10894 )
@@ -113,28 +99,18 @@ def _create_function(self, name: str, path: str):
11399 "Content-Type" : "application/zip" ,
114100 }
115101 response = requests .put (url = upload_url , data = code_zip_data , headers = headers )
116- if 200 <= response .status_code < 300 :
117- # print(f"Upload successful! Size: {code_zip_size / 1024 / 1024:.2f} MB")
118- pass
119- else :
102+ if not (200 <= response .status_code < 300 ):
120103 error_message = f"Upload failed to { upload_url } with status code { response .status_code } : { response .text } "
121104 raise ValueError (error_message )
122105
123- # 3 . Mount the TOS bucket to function instance
106+ # 5 . Mount the TOS bucket to function instance
124107 res = signed_request (
125108 ak = self .ak ,
126109 sk = self .sk ,
127110 target = "CodeUploadCallback" ,
128111 body = {"FunctionId" : function_id },
129112 )
130113
131- typer .echo (
132- typer .style (
133- f"Function ID on VeFaaS service: { function_id } " ,
134- fg = typer .colors .BRIGHT_BLACK ,
135- )
136- )
137-
138114 return function_name , function_id
139115
140116 def _create_application (
@@ -183,23 +159,21 @@ def _release_application(self, app_id: str):
183159 host = "open.volcengineapi.com" ,
184160 )
185161
162+ logger .info (f"Start to release VeFaaS application { app_id } ." )
186163 status , full_response = self ._get_application_status (app_id )
187164 while status not in ["deploy_success" , "deploy_fail" ]:
188165 time .sleep (10 )
189- typer .echo (
190- typer .style (
191- f"Current status: { status } " ,
192- fg = typer .colors .BRIGHT_BLACK ,
193- )
194- )
195166 status , full_response = self ._get_application_status (app_id )
196167
197168 assert status == "deploy_success" , (
198169 f"Release application failed. Response: { full_response } "
199170 )
171+
200172 cloud_resource = full_response ["Result" ]["CloudResource" ]
201173 cloud_resource = json .loads (cloud_resource )
174+
202175 url = cloud_resource ["framework" ]["url" ]["system_url" ]
176+
203177 return url
204178
205179 def _get_application_status (self , app_id : str ):
@@ -257,15 +231,29 @@ def delete(self, app_id: str):
257231
258232 def deploy (
259233 self ,
260- name : str , # application name
234+ name : str ,
261235 path : str ,
262236 gateway_name : str = "" ,
263237 gateway_service_name : str = "" ,
264238 gateway_upstream_name : str = "" ,
265239 ) -> tuple [str , str , str ]:
240+ """Deploy an agent project to VeFaaS service.
241+
242+ Args:
243+ name (str): Application name (warning: not function name).
244+ path (str): Project path.
245+ gateway_name (str, optional): Gateway name. Defaults to "".
246+ gateway_service_name (str, optional): Gateway service name. Defaults to "".
247+ gateway_upstream_name (str, optional): Gateway upstream name. Defaults to "".
248+
249+ Returns:
250+ tuple[str, str, str]: (url, app_id, function_id)
251+ """
252+ # Naming check
266253 if "_" in name :
267254 raise ValueError ("Function or Application name cannot contain '_'." )
268255
256+ # Give default names
269257 if not gateway_name :
270258 gateway_name = f"{ name } -gw-{ formatted_timestamp ()} "
271259
@@ -286,14 +274,15 @@ def deploy(
286274 if not gateway_upstream_name :
287275 gateway_upstream_name = f"{ name } -gw-us-{ formatted_timestamp ()} "
288276
289- typer .echo (
290- typer .style ("[1/3] " , fg = typer .colors .GREEN )
291- + "Create VeFaaS service on cloud."
277+ function_name = f"{ name } -fn"
278+
279+ logger .info (
280+ f"Start to create VeFaaS function { function_name } with path { path } . Gateway: { gateway_name } , Gateway Service: { gateway_service_name } , Gateway Upstream: { gateway_upstream_name } ."
292281 )
293- typer . echo ( typer . style ( f"Project path: { path } " , fg = typer . colors . BRIGHT_BLACK ) )
294- function_name , function_id = self . _create_function ( name , path )
282+ function_name , function_id = self . _create_function ( function_name , path )
283+ logger . info ( f"VeFaaS function { function_name } with ID { function_id } created." )
295284
296- typer . echo ( typer . style ( "[2/3] " , fg = typer . colors . GREEN ) + "Create application." )
285+ logger . info ( f"Start to create VeFaaS application { name } ." )
297286 app_id = self ._create_application (
298287 name ,
299288 function_name ,
@@ -302,13 +291,11 @@ def deploy(
302291 gateway_service_name ,
303292 )
304293
305- typer .echo (
306- typer .style ("[3/3] " , fg = typer .colors .GREEN ) + "Release application."
307- )
294+ logger .info (f"VeFaaS application { name } with ID { app_id } created." )
295+ logger .info (f"Start to release VeFaaS application { app_id } ." )
308296 url = self ._release_application (app_id )
297+ logger .info (f"VeFaaS application { name } with ID { app_id } released." )
309298
310- typer .echo (
311- typer .style (f"\n Successfully deployed on:\n \n { url } " , fg = typer .colors .BLUE )
312- )
299+ logger .info (f"VeFaaS application { name } with ID { app_id } deployed on { url } ." )
313300
314301 return url , app_id , function_id
0 commit comments