|
27 | 27 | ) |
28 | 28 |
|
29 | 29 | import veadk.config |
30 | | -from volcenginesdkvefaas.models.tag_for_update_function_input import ( |
31 | | - TagForUpdateFunctionInput, |
32 | | -) |
33 | 30 | from veadk.cli.services.veapig.apig import APIGateway |
34 | 31 | from veadk.utils.logger import get_logger |
35 | 32 | from veadk.utils.misc import formatted_timestamp |
@@ -217,6 +214,77 @@ def _list_application(self): |
217 | 214 | ) |
218 | 215 | return response["Result"]["Items"] |
219 | 216 |
|
| 217 | + def _update_function_code( |
| 218 | + self, |
| 219 | + application_name: str, # application name |
| 220 | + path: str, |
| 221 | + ) -> tuple[str, str, str]: |
| 222 | + """Update existing application function code while preserving URL. |
| 223 | +
|
| 224 | + Args: |
| 225 | + application_name (str): Application name to update. |
| 226 | + path (str): Local project path. |
| 227 | +
|
| 228 | + Returns: |
| 229 | + tuple[str, str, str]: URL, app_id, function_id |
| 230 | + """ |
| 231 | + # Naming check |
| 232 | + if "_" in application_name: |
| 233 | + raise ValueError("Function or Application name cannot contain '_'.") |
| 234 | + |
| 235 | + # Find existing application |
| 236 | + app_id = self.find_app_id_by_name(application_name) |
| 237 | + if not app_id: |
| 238 | + raise ValueError( |
| 239 | + f"Application '{application_name}' not found. Use deploy() for new applications." |
| 240 | + ) |
| 241 | + |
| 242 | + # Get application status and extract function info |
| 243 | + status, full_response = self._get_application_status(app_id) |
| 244 | + if status == "deploy_fail": |
| 245 | + raise ValueError( |
| 246 | + f"Cannot update failed application. Current status: {status}" |
| 247 | + ) |
| 248 | + |
| 249 | + # Extract function name from application config |
| 250 | + cloud_resource = full_response["Result"]["CloudResource"] |
| 251 | + cloud_resource = json.loads(cloud_resource) |
| 252 | + function_name = cloud_resource["framework"]["function"]["Name"] |
| 253 | + # existing_url = cloud_resource["framework"]["url"]["system_url"] |
| 254 | + function_id = cloud_resource["framework"]["function"]["Id"] |
| 255 | + if not function_id: |
| 256 | + raise ValueError(f"Function '{function_name}' not found for update") |
| 257 | + |
| 258 | + logger.info( |
| 259 | + f"Start to update VeFaaS function {function_name} with path {path}." |
| 260 | + ) |
| 261 | + |
| 262 | + # Upload and mount code using extracted method |
| 263 | + self._upload_and_mount_code(function_id, path) |
| 264 | + |
| 265 | + # Use update_function client method to apply changes |
| 266 | + self.client.update_function( |
| 267 | + volcenginesdkvefaas.UpdateFunctionRequest( |
| 268 | + id=function_id, |
| 269 | + request_timeout=1800, # Keep same timeout as deploy |
| 270 | + ) |
| 271 | + ) |
| 272 | + |
| 273 | + logger.info(f"Function updated successfully: {function_id}") |
| 274 | + |
| 275 | + logger.info(f"VeFaaS function {function_name} with ID {function_id} updated.") |
| 276 | + |
| 277 | + # Release the application to apply changes |
| 278 | + url = self._release_application(app_id) |
| 279 | + |
| 280 | + logger.info(f"VeFaaS application {application_name} with ID {app_id} released.") |
| 281 | + |
| 282 | + logger.info( |
| 283 | + f"VeFaaS application {application_name} with ID {app_id} updated on {url}." |
| 284 | + ) |
| 285 | + |
| 286 | + return url, app_id, function_id |
| 287 | + |
220 | 288 | def find_app_id_by_name(self, name: str): |
221 | 289 | apps = self._list_application() |
222 | 290 | for app in apps: |
@@ -314,76 +382,3 @@ def deploy( |
314 | 382 | logger.info(f"VeFaaS application {name} with ID {app_id} deployed on {url}.") |
315 | 383 |
|
316 | 384 | return url, app_id, function_id |
317 | | - |
318 | | - def _update_function_code( |
319 | | - self, |
320 | | - application_name: str, # application name |
321 | | - path: str, |
322 | | - ) -> tuple[str, str, str]: |
323 | | - """Update existing application function code while preserving URL. |
324 | | -
|
325 | | - Args: |
326 | | - application_name (str): Application name to update. |
327 | | - path (str): Local project path. |
328 | | -
|
329 | | - Returns: |
330 | | - tuple[str, str, str]: URL, app_id, function_id |
331 | | - """ |
332 | | - # Naming check |
333 | | - if "_" in application_name: |
334 | | - raise ValueError("Function or Application name cannot contain '_'.") |
335 | | - |
336 | | - # Find existing application |
337 | | - app_id = self.find_app_id_by_name(application_name) |
338 | | - if not app_id: |
339 | | - raise ValueError( |
340 | | - f"Application '{application_name}' not found. Use deploy() for new applications." |
341 | | - ) |
342 | | - |
343 | | - # Get application status and extract function info |
344 | | - status, full_response = self._get_application_status(app_id) |
345 | | - if status == "deploy_fail": |
346 | | - raise ValueError( |
347 | | - f"Cannot update failed application. Current status: {status}" |
348 | | - ) |
349 | | - |
350 | | - # Extract function name from application config |
351 | | - cloud_resource = full_response["Result"]["CloudResource"] |
352 | | - cloud_resource = json.loads(cloud_resource) |
353 | | - function_name = cloud_resource["framework"]["function"]["Name"] |
354 | | - # existing_url = cloud_resource["framework"]["url"]["system_url"] |
355 | | - function_id = cloud_resource["framework"]["function"]["Id"] |
356 | | - if not function_id: |
357 | | - raise ValueError(f"Function '{function_name}' not found for update") |
358 | | - |
359 | | - logger.info( |
360 | | - f"Start to update VeFaaS function {function_name} with path {path}." |
361 | | - ) |
362 | | - |
363 | | - # Upload and mount code using extracted method |
364 | | - self._upload_and_mount_code(function_id, path) |
365 | | - |
366 | | - # Use update_function client method to apply changes |
367 | | - self.client.update_function( |
368 | | - volcenginesdkvefaas.UpdateFunctionRequest( |
369 | | - id=function_id, |
370 | | - description="Updated by VeADK (Volcengine Agent Development Kit)", |
371 | | - tags=[TagForUpdateFunctionInput(key="provider", value="veadk")], |
372 | | - request_timeout=1800, # Keep same timeout as deploy |
373 | | - ) |
374 | | - ) |
375 | | - |
376 | | - 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