-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment_utils.py
More file actions
71 lines (64 loc) · 2.87 KB
/
deployment_utils.py
File metadata and controls
71 lines (64 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# deployment_utils.py
import os
import json
import logging
from typing import List, Dict, Any
logger = logging.getLogger('deployment_utils')
DEPLOYMENTS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'deployments.json')
DEPLOYMENTS_BACKUP_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'deployments_backup.json')
def _ensure_deployments_file():
"""确保部署文件存在,如果不存在则创建"""
if not os.path.exists(DEPLOYMENTS_FILE):
try:
with open(DEPLOYMENTS_FILE, 'w', encoding='utf-8') as f:
json.dump([], f, ensure_ascii=False, indent=2)
logger.info("创建新的部署文件")
except Exception as e:
logger.error(f"创建部署文件时出错: {str(e)}")
raise
def _backup_deployments_file():
"""创建部署文件的备份"""
try:
if os.path.exists(DEPLOYMENTS_FILE):
with open(DEPLOYMENTS_FILE, 'r', encoding='utf-8') as src:
data = json.load(src)
with open(DEPLOYMENTS_BACKUP_FILE, 'w', encoding='utf-8') as dst:
json.dump(data, dst, ensure_ascii=False, indent=2)
logger.info("已创建部署文件备份")
except Exception as e:
logger.error(f"备份部署文件时出错: {str(e)}")
def _get_all_deployments() -> List[Dict[str, Any]]:
"""获取所有已部署的模型信息"""
_ensure_deployments_file()
try:
with open(DEPLOYMENTS_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except json.JSONDecodeError:
logger.error("部署文件格式错误,尝试从备份恢复")
try:
if os.path.exists(DEPLOYMENTS_BACKUP_FILE):
with open(DEPLOYMENTS_BACKUP_FILE, 'r', encoding='utf-8') as f:
data = json.load(f)
with open(DEPLOYMENTS_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
logger.info("已从备份恢复部署文件")
return data
except Exception as backup_error:
logger.error(f"从备份恢复失败: {str(backup_error)}")
with open(DEPLOYMENTS_FILE, 'w', encoding='utf-8') as f:
json.dump([], f, ensure_ascii=False, indent=2)
return []
except Exception as e:
logger.error(f"读取部署文件时出错: {str(e)}")
return []
def _save_deployments(deployments: List[Dict[str, Any]]):
"""保存部署信息到文件"""
_ensure_deployments_file()
_backup_deployments_file()
try:
with open(DEPLOYMENTS_FILE, 'w', encoding='utf-8') as f:
json.dump(deployments, f, ensure_ascii=False, indent=2)
logger.info(f"已保存{len(deployments)}个部署信息")
except Exception as e:
logger.error(f"保存部署信息时出错: {str(e)}")
raise