Spaces:
Paused
Paused
fix: 启动验证改用 logger 输出 + 每次启动验证 CloudMail 和 CPA + 测试邮箱用随机前缀
Browse filesCo-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- src/autoteam/setup_wizard.py +70 -15
src/autoteam/setup_wizard.py
CHANGED
|
@@ -4,6 +4,7 @@ import logging
|
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
import secrets
|
|
|
|
| 7 |
|
| 8 |
from autoteam.config import PROJECT_ROOT
|
| 9 |
|
|
@@ -72,6 +73,13 @@ def check_and_setup(interactive: bool = True) -> bool:
|
|
| 72 |
missing.append((key, prompt, default, optional))
|
| 73 |
|
| 74 |
if not missing:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return True
|
| 76 |
|
| 77 |
if not interactive:
|
|
@@ -120,8 +128,13 @@ def check_and_setup(interactive: bool = True) -> bool:
|
|
| 120 |
except Exception:
|
| 121 |
pass
|
| 122 |
|
| 123 |
-
# 验证
|
| 124 |
-
_verify_cloudmail()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
return True
|
| 127 |
|
|
@@ -136,33 +149,75 @@ def _verify_cloudmail():
|
|
| 136 |
if not all([base_url, email, password, domain]):
|
| 137 |
return
|
| 138 |
|
| 139 |
-
|
| 140 |
|
| 141 |
try:
|
| 142 |
from autoteam.cloudmail import CloudMailClient
|
| 143 |
|
| 144 |
client = CloudMailClient()
|
| 145 |
client.login()
|
| 146 |
-
|
| 147 |
except Exception as e:
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
return
|
| 151 |
|
| 152 |
test_account_id = None
|
| 153 |
try:
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
return
|
| 160 |
|
| 161 |
try:
|
| 162 |
if test_account_id:
|
| 163 |
client.delete_account(test_account_id)
|
| 164 |
-
|
| 165 |
except Exception as e:
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
import secrets
|
| 7 |
+
import sys
|
| 8 |
|
| 9 |
from autoteam.config import PROJECT_ROOT
|
| 10 |
|
|
|
|
| 73 |
missing.append((key, prompt, default, optional))
|
| 74 |
|
| 75 |
if not missing:
|
| 76 |
+
# 配置齐全,每次启动验证连通性
|
| 77 |
+
if not _verify_cloudmail():
|
| 78 |
+
logger.error("[验证] CloudMail 配置有误,请修改 .env 后重新启动")
|
| 79 |
+
sys.exit(1)
|
| 80 |
+
if not _verify_cpa():
|
| 81 |
+
logger.error("[验证] CPA 配置有误,请修改 .env 后重新启动")
|
| 82 |
+
sys.exit(1)
|
| 83 |
return True
|
| 84 |
|
| 85 |
if not interactive:
|
|
|
|
| 128 |
except Exception:
|
| 129 |
pass
|
| 130 |
|
| 131 |
+
# 验证配置连通性
|
| 132 |
+
if not _verify_cloudmail():
|
| 133 |
+
logger.error("[验证] CloudMail 配置有误,请修改 .env 后重新启动")
|
| 134 |
+
sys.exit(1)
|
| 135 |
+
if not _verify_cpa():
|
| 136 |
+
logger.error("[验证] CPA 配置有误,请修改 .env 后重新启动")
|
| 137 |
+
sys.exit(1)
|
| 138 |
|
| 139 |
return True
|
| 140 |
|
|
|
|
| 149 |
if not all([base_url, email, password, domain]):
|
| 150 |
return
|
| 151 |
|
| 152 |
+
logger.info("[验证] CloudMail 配置...")
|
| 153 |
|
| 154 |
try:
|
| 155 |
from autoteam.cloudmail import CloudMailClient
|
| 156 |
|
| 157 |
client = CloudMailClient()
|
| 158 |
client.login()
|
| 159 |
+
logger.info("[验证] CloudMail 登录成功")
|
| 160 |
except Exception as e:
|
| 161 |
+
logger.error("[验证] CloudMail 登录失败: %s", e)
|
| 162 |
+
logger.error("[验证] 请检查 CLOUDMAIL_BASE_URL、CLOUDMAIL_EMAIL、CLOUDMAIL_PASSWORD")
|
| 163 |
+
return False
|
| 164 |
|
| 165 |
test_account_id = None
|
| 166 |
try:
|
| 167 |
+
import uuid as _uuid
|
| 168 |
+
|
| 169 |
+
test_account_id, test_email = client.create_temp_email(prefix=f"at-test-{_uuid.uuid4().hex[:6]}")
|
| 170 |
+
logger.info("[验证] CloudMail 创建测试邮箱成功: %s", test_email)
|
| 171 |
except Exception as e:
|
| 172 |
+
logger.error("[验证] CloudMail 创建邮箱失败: %s", e)
|
| 173 |
+
logger.error("[验证] 请检查 CLOUDMAIL_DOMAIN 是否正确")
|
| 174 |
+
return False
|
| 175 |
|
| 176 |
try:
|
| 177 |
if test_account_id:
|
| 178 |
client.delete_account(test_account_id)
|
| 179 |
+
logger.info("[验证] CloudMail 测试邮箱已清理")
|
| 180 |
except Exception as e:
|
| 181 |
+
logger.warning("[验证] CloudMail 清理测试邮箱失败: %s(不影响使用)", e)
|
| 182 |
+
|
| 183 |
+
logger.info("[验证] CloudMail 配置验证通过")
|
| 184 |
+
return True
|
| 185 |
+
|
| 186 |
|
| 187 |
+
def _verify_cpa():
|
| 188 |
+
"""验证 CPA 配置是否正确:获取认证文件列表"""
|
| 189 |
+
cpa_url = os.environ.get("CPA_URL", "")
|
| 190 |
+
cpa_key = os.environ.get("CPA_KEY", "")
|
| 191 |
+
|
| 192 |
+
if not cpa_url or not cpa_key:
|
| 193 |
+
return True # 没配就跳过
|
| 194 |
+
|
| 195 |
+
logger.info("[验证] CPA 配置...")
|
| 196 |
+
|
| 197 |
+
try:
|
| 198 |
+
import requests
|
| 199 |
+
|
| 200 |
+
resp = requests.get(
|
| 201 |
+
f"{cpa_url}/v0/management/auth-files",
|
| 202 |
+
headers={"Authorization": f"Bearer {cpa_key}"},
|
| 203 |
+
timeout=10,
|
| 204 |
+
)
|
| 205 |
+
if resp.status_code == 200:
|
| 206 |
+
data = resp.json()
|
| 207 |
+
count = len(data.get("files", []))
|
| 208 |
+
logger.info("[验证] CPA 连接成功(当前 %d 个认证文件)", count)
|
| 209 |
+
return True
|
| 210 |
+
if resp.status_code == 401:
|
| 211 |
+
logger.error("[验证] CPA 连接失败: 密钥无效 (401)")
|
| 212 |
+
logger.error("[验证] 请检查 CPA_KEY 是否正确")
|
| 213 |
+
return False
|
| 214 |
+
logger.error("[验证] CPA 连接失败: HTTP %d", resp.status_code)
|
| 215 |
+
logger.error("[验证] 请检查 CPA_URL 是否正确")
|
| 216 |
+
return False
|
| 217 |
+
except requests.exceptions.ConnectionError:
|
| 218 |
+
logger.error("[验证] CPA 连接失败: 无法连接到 %s", cpa_url)
|
| 219 |
+
logger.error("[验证] 请检查 CPA_URL 是否正确,CPA 服务是否已启动")
|
| 220 |
+
return False
|
| 221 |
+
except Exception as e:
|
| 222 |
+
logger.error("[验证] CPA 连接失败: %s", e)
|
| 223 |
+
return False
|