Skip to content

Commit 12c1bcd

Browse files
committed
fix(vefaas): fix _update_function_code() without envs; create _upload_and_mount_code()
1 parent 34c70b5 commit 12c1bcd

File tree

2 files changed

+66
-130
lines changed

2 files changed

+66
-130
lines changed

veadk/cli/services/vefaas/vefaas.py

Lines changed: 60 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
)
2828

2929
import veadk.config
30-
from volcenginesdkvefaas.models.env_for_update_function_input import (
31-
EnvForUpdateFunctionInput,
32-
)
3330
from 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

veadk/cloud/cloud_agent_engine.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -168,59 +168,31 @@ def remove(self, app_name: str):
168168
app_id = self._vefaas_service.find_app_id_by_name(app_name)
169169
self._vefaas_service.delete(app_id)
170170

171-
def update(
171+
def update_function_code(
172172
self,
173173
application_name: str,
174174
path: str,
175-
use_studio: bool = False,
176-
use_adk_web: bool = False,
177175
) -> CloudApp:
178176
"""Update existing agent project code while keeping the same URL.
179177
180178
Args:
181179
application_name (str): Existing application name to update.
182180
path (str): Local agent project path.
183-
use_studio (bool): Whether to use studio mode.
184-
use_adk_web (bool): Whether to use ADK web mode.
185181
186182
Returns:
187183
CloudApp: Updated cloud app with same endpoint.
188184
"""
189-
assert not (use_studio and use_adk_web), (
190-
"use_studio and use_adk_web can not be True at the same time."
191-
)
192-
193-
# prevent deepeval writing operations
194-
import veadk.config
195-
196-
veadk.config.veadk_environments["DEEPEVAL_TELEMETRY_OPT_OUT"] = "YES"
197-
198-
if use_studio:
199-
veadk.config.veadk_environments["USE_STUDIO"] = "True"
200-
else:
201-
import veadk.config
202-
203-
veadk.config.veadk_environments["USE_STUDIO"] = "False"
204-
205-
if use_adk_web:
206-
import veadk.config
207-
208-
veadk.config.veadk_environments["USE_ADK_WEB"] = "True"
209-
else:
210-
import veadk.config
211-
212-
veadk.config.veadk_environments["USE_ADK_WEB"] = "False"
213-
214185
# convert `path` to absolute path
215186
path = str(Path(path).resolve())
216187
self._prepare(path, application_name)
217188

218189
try:
219-
vefaas_application_url, app_id, function_id = self._vefaas_service.update(
220-
name=application_name,
221-
path=path,
190+
vefaas_application_url, app_id, function_id = (
191+
self._vefaas_service._update_function_code(
192+
application_name=application_name,
193+
path=path,
194+
)
222195
)
223-
_ = function_id # for future use
224196

225197
return CloudApp(
226198
vefaas_application_name=application_name,

0 commit comments

Comments
 (0)