1414
1515import json
1616import time
17-
17+ import shutil
18+ import tempfile
19+ from pathlib import Path
20+ from cookiecutter .main import cookiecutter
21+ import veadk .integrations .ve_faas as vefaas
22+ from veadk .version import VERSION
1823import requests
1924import volcenginesdkcore
2025import volcenginesdkvefaas
@@ -305,12 +310,10 @@ def _update_function_code(
305310
306311 # Get application status and extract function info
307312 status , full_response = self ._get_application_status (app_id )
308-
309313 # Extract function name from application config
310314 cloud_resource = full_response ["Result" ]["CloudResource" ]
311315 cloud_resource = json .loads (cloud_resource )
312316 function_name = cloud_resource ["framework" ]["function" ]["Name" ]
313- # existing_url = cloud_resource["framework"]["url"]["system_url"]
314317 function_id = cloud_resource ["framework" ]["function" ]["Id" ]
315318 if not function_id :
316319 raise ValueError (f"Function '{ function_name } ' not found for update" )
@@ -319,31 +322,62 @@ def _update_function_code(
319322 f"Start to update VeFaaS function { function_name } with path { path } ."
320323 )
321324
322- # Upload and mount code using extracted method
323- self ._upload_and_mount_code (function_id , path )
325+ user_proj_path = Path (path ).resolve ()
326+ template_dir = Path (vefaas .__file__ ).parent / "template"
327+ tmp_dir_name = f"{ user_proj_path .name } _update_{ formatted_timestamp ()} "
324328
325- # Use update_function client method to apply changes
326- self .client .update_function (
327- volcenginesdkvefaas .UpdateFunctionRequest (
328- id = function_id ,
329- request_timeout = 1800 , # Keep same timeout as deploy
330- )
331- )
329+ settings = {
330+ "local_dir_name" : tmp_dir_name .replace ("-" , "_" ),
331+ "app_name" : user_proj_path .name .replace ("-" , "_" ),
332+ "veadk_version" : VERSION ,
333+ }
332334
333- logger . info ( f"Function updated successfully: { function_id } " )
335+ temp_base = Path ( tempfile . gettempdir () )
334336
335- logger .info (f"VeFaaS function { function_name } with ID { function_id } updated." )
337+ cookiecutter (
338+ template = str (template_dir ),
339+ output_dir = str (temp_base ),
340+ no_input = True ,
341+ extra_context = settings ,
342+ )
336343
337- # Release the application to apply changes
338- url = self ._release_application (app_id )
344+ tmp_path = temp_base / tmp_dir_name
339345
340- logger .info (f"VeFaaS application { application_name } with ID { app_id } released." )
346+ try :
347+ agent_dir = tmp_path / "src" / user_proj_path .name .replace ("-" , "_" )
348+ if agent_dir .exists ():
349+ shutil .rmtree (agent_dir )
350+ agent_dir .mkdir (parents = True , exist_ok = True )
351+ shutil .copytree (user_proj_path , agent_dir , dirs_exist_ok = True )
352+ user_requirements = user_proj_path / "requirements.txt"
353+
354+ if user_requirements .exists ():
355+ logger .debug ("Using user-provided requirements.txt" )
356+ shutil .copy (user_requirements , tmp_path / "src" / "requirements.txt" )
357+ else :
358+ logger .warning ("No requirements.txt found, using template default" )
341359
342- logger .info (
343- f"VeFaaS application { application_name } with ID { app_id } updated on { url } ."
344- )
360+ self ._upload_and_mount_code (function_id , str (tmp_path / "src" ))
361+ self .client .update_function (
362+ volcenginesdkvefaas .UpdateFunctionRequest (
363+ id = function_id ,
364+ request_timeout = 1800 , # Keep same timeout as deploy
365+ )
366+ )
367+ logger .info (
368+ f"VeFaaS function { function_name } with ID { function_id } updated."
369+ )
370+ url = self ._release_application (app_id )
371+ logger .info (
372+ f"VeFaaS application { application_name } with ID { app_id } released."
373+ )
374+ logger .info (f"VeFaaS application { application_name } updated on { url } ." )
375+ return url , app_id , function_id
345376
346- return url , app_id , function_id
377+ finally :
378+ if tmp_path .exists ():
379+ shutil .rmtree (tmp_path )
380+ logger .debug (f"Cleaned up temporary directory: { tmp_path } " )
347381
348382 def get_application_details (self , app_id : str = None , app_name : str = None ):
349383 if not app_id and not app_name :
0 commit comments