2727)
2828
2929import veadk .config
30- from volcenginesdkvefaas .models .env_for_update_function_input import (
31- EnvForUpdateFunctionInput ,
32- )
3330from volcenginesdkvefaas .models .tag_for_update_function_input import (
3431 TagForUpdateFunctionInput ,
3532)
@@ -65,36 +62,20 @@ def __init__(self, access_key: str, secret_key: str, region: str = "cn-beijing")
6562
6663 self .template_id = "6874f3360bdbc40008ecf8c7"
6764
68- def _create_function (self , function_name : str , path : str ):
69- # 1. Read envs
70- envs = []
71- for key , value in veadk .config .veadk_environments .items ():
72- envs .append (EnvForCreateFunctionInput (key = key , value = value ))
73- logger .info (
74- f"Fetch { len (envs )} environment variables." ,
75- )
65+ def _upload_and_mount_code (self , function_id : str , path : str ):
66+ """Upload code to VeFaaS temp bucket and mount to function instance.
7667
77- # 2. Create function
78- res = self .client .create_function (
79- volcenginesdkvefaas .CreateFunctionRequest (
80- command = "./run.sh" ,
81- name = function_name ,
82- description = "Created by VeADK (Volcengine Agent Development Kit)" ,
83- tags = [TagForCreateFunctionInput (key = "provider" , value = "veadk" )],
84- runtime = "native-python3.10/v1" ,
85- request_timeout = 1800 ,
86- envs = envs ,
87- )
88- )
89- function_id = res .id
90-
91- # 3. Get a temp bucket to store code
68+ Args:
69+ function_id (str): Target function ID.
70+ path (str): Local project path.
71+ """
72+ # Get zipped code data
9273 code_zip_data , code_zip_size , error = zip_and_encode_folder (path )
9374 logger .info (
9475 f"Zipped project size: { code_zip_size / 1024 / 1024 :.2f} MB" ,
9576 )
9677
97- # 4. Upload code to VeFaaS temp bucket
78+ # Upload code to VeFaaS temp bucket
9879 req = volcenginesdkvefaas .GetCodeUploadAddressRequest (
9980 function_id = function_id , content_length = code_zip_size
10081 )
@@ -109,14 +90,42 @@ def _create_function(self, function_name: str, path: str):
10990 error_message = f"Upload failed to { upload_url } with status code { response .status_code } : { response .text } "
11091 raise ValueError (error_message )
11192
112- # 5. Mount the TOS bucket to function instance
93+ # Mount the TOS bucket to function instance
11394 res = signed_request (
11495 ak = self .ak ,
11596 sk = self .sk ,
11697 target = "CodeUploadCallback" ,
11798 body = {"FunctionId" : function_id },
11899 )
119100
101+ return res
102+
103+ def _create_function (self , function_name : str , path : str ):
104+ # Read envs
105+ envs = []
106+ for key , value in veadk .config .veadk_environments .items ():
107+ envs .append (EnvForCreateFunctionInput (key = key , value = value ))
108+ logger .info (
109+ f"Fetch { len (envs )} environment variables." ,
110+ )
111+
112+ # Create function
113+ res = self .client .create_function (
114+ volcenginesdkvefaas .CreateFunctionRequest (
115+ command = "./run.sh" ,
116+ name = function_name ,
117+ description = "Created by VeADK (Volcengine Agent Development Kit)" ,
118+ tags = [TagForCreateFunctionInput (key = "provider" , value = "veadk" )],
119+ runtime = "native-python3.10/v1" ,
120+ request_timeout = 1800 ,
121+ envs = envs ,
122+ )
123+ )
124+ function_id = res .id
125+
126+ # Upload and mount code using extracted method
127+ self ._upload_and_mount_code (function_id , path )
128+
120129 return function_name , function_id
121130
122131 def _create_application (
@@ -306,29 +315,29 @@ def deploy(
306315
307316 return url , app_id , function_id
308317
309- def update (
318+ def _update_function_code (
310319 self ,
311- name : str , # application name
320+ application_name : str , # application name
312321 path : str ,
313322 ) -> tuple [str , str , str ]:
314323 """Update existing application function code while preserving URL.
315324
316325 Args:
317- name (str): Application name to update.
326+ application_name (str): Application name to update.
318327 path (str): Local project path.
319328
320329 Returns:
321330 tuple[str, str, str]: URL, app_id, function_id
322331 """
323332 # Naming check
324- if "_" in name :
333+ if "_" in application_name :
325334 raise ValueError ("Function or Application name cannot contain '_'." )
326335
327336 # Find existing application
328- app_id = self .find_app_id_by_name (name )
337+ app_id = self .find_app_id_by_name (application_name )
329338 if not app_id :
330339 raise ValueError (
331- f"Application '{ name } ' not found. Use deploy() for new applications."
340+ f"Application '{ application_name } ' not found. Use deploy() for new applications."
332341 )
333342
334343 # Get application status and extract function info
@@ -351,75 +360,30 @@ def update(
351360 f"Start to update VeFaaS function { function_name } with path { path } ."
352361 )
353362
354- # Update function with new code
355- self ._update_function_code (function_id , path )
363+ # Upload and mount code using extracted method
364+ self ._upload_and_mount_code (function_id , path )
356365
357- logger .info (f"VeFaaS function { function_name } with ID { function_id } updated." )
358-
359- logger .info (f"Start to release VeFaaS application { app_id } ." )
360-
361- # Release the application to apply changes
362- url = self ._release_application (app_id )
363-
364- logger .info (f"VeFaaS application { name } with ID { app_id } released." )
365-
366- logger .info (f"VeFaaS application { name } with ID { app_id } updated on { url } ." )
367-
368- return url , app_id , function_id
369-
370- def _update_function_code (self , function_id : str , path : str ):
371- """Update function code by copying deploy process and using update_function.
372-
373- Args:
374- function_id (str): Target function ID to update.
375- path (str): Local project path.
376- """
377- # 1. Read envs
378- envs = []
379- for key , value in veadk .config .veadk_environments .items ():
380- envs .append (EnvForUpdateFunctionInput (key = key , value = value ))
381- logger .info (
382- f"Fetch { len (envs )} environment variables." ,
383- )
384-
385- # 2. Get a temp bucket to store code
386- code_zip_data , code_zip_size , error = zip_and_encode_folder (path )
387- logger .info (
388- f"Zipped project size: { code_zip_size / 1024 / 1024 :.2f} MB" ,
389- )
390-
391- # 3. Upload code to VeFaaS temp bucket
392- req = volcenginesdkvefaas .GetCodeUploadAddressRequest (
393- function_id = function_id , content_length = code_zip_size
394- )
395- response = self .client .get_code_upload_address (req )
396- upload_url = response .upload_address
397-
398- headers = {
399- "Content-Type" : "application/zip" ,
400- }
401- response = requests .put (url = upload_url , data = code_zip_data , headers = headers )
402- if not (200 <= response .status_code < 300 ):
403- error_message = f"Upload failed to { upload_url } with status code { response .status_code } : { response .text } "
404- raise ValueError (error_message )
405-
406- # 4. Mount the TOS bucket to function instance
407- _ = signed_request (
408- ak = self .ak ,
409- sk = self .sk ,
410- target = "CodeUploadCallback" ,
411- body = {"FunctionId" : function_id },
412- )
413-
414- # 5. Use update_function client method to apply changes
415- _ = self .client .update_function (
366+ # Use update_function client method to apply changes
367+ self .client .update_function (
416368 volcenginesdkvefaas .UpdateFunctionRequest (
417369 id = function_id ,
418370 description = "Updated by VeADK (Volcengine Agent Development Kit)" ,
419371 tags = [TagForUpdateFunctionInput (key = "provider" , value = "veadk" )],
420372 request_timeout = 1800 , # Keep same timeout as deploy
421- envs = envs ,
422373 )
423374 )
424375
425376 logger .info (f"Function updated successfully: { function_id } " )
377+
378+ logger .info (f"VeFaaS function { function_name } with ID { function_id } updated." )
379+
380+ # Release the application to apply changes
381+ url = self ._release_application (app_id )
382+
383+ logger .info (f"VeFaaS application { application_name } with ID { app_id } released." )
384+
385+ logger .info (
386+ f"VeFaaS application { application_name } with ID { app_id } updated on { url } ."
387+ )
388+
389+ return url , app_id , function_id
0 commit comments