Update Dockerfile
Browse files- Dockerfile +45 -52
Dockerfile
CHANGED
|
@@ -37,7 +37,7 @@ RUN mkdir -p /app/data /tmp
|
|
| 37 |
# Set permissions
|
| 38 |
RUN chmod +x /app/main.py
|
| 39 |
|
| 40 |
-
# 创建启动初始化脚本:
|
| 41 |
RUN cat <<'EOF' > /app/entrypoint.sh
|
| 42 |
#!/bin/bash
|
| 43 |
set -e
|
|
@@ -47,8 +47,14 @@ echo "[Startup] Checking for ACCOUNT* secrets..."
|
|
| 47 |
python3 - <<'PYEOF'
|
| 48 |
import os
|
| 49 |
import re
|
| 50 |
-
import
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
acc_pattern = re.compile(r'^ACCOUNT\d+$', re.IGNORECASE)
|
| 54 |
account_vars = [k for k in os.environ.keys() if acc_pattern.match(k)]
|
|
@@ -58,58 +64,45 @@ if not account_vars:
|
|
| 58 |
print("[Startup] No ACCOUNT* secrets found.")
|
| 59 |
else:
|
| 60 |
print(f"[Startup] Found {len(account_vars)} secret(s): {', '.join(account_vars)}")
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
for idx, var_name in enumerate(account_vars, start=1):
|
| 64 |
-
|
| 65 |
-
if not
|
| 66 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# 组装符合系统 AccountModel 约定的字典结构
|
| 94 |
-
account_obj = {
|
| 95 |
-
"id": account_id,
|
| 96 |
-
"name": account_name,
|
| 97 |
-
"cookies": cookie_dict,
|
| 98 |
-
"is_active": True,
|
| 99 |
-
"created_at": 1789320000.0,
|
| 100 |
-
"updated_at": 1789320000.0
|
| 101 |
-
}
|
| 102 |
-
accounts_list.append(account_obj)
|
| 103 |
-
print(f"[Startup] Prepared {var_name} as '{account_name}' ({len(cookie_dict)} cookie entries)")
|
| 104 |
-
|
| 105 |
-
if accounts_list:
|
| 106 |
-
os.makedirs("/app/data", exist_ok=True)
|
| 107 |
-
target_file = "/app/data/accounts.json"
|
| 108 |
-
|
| 109 |
-
# 写入 accounts.json
|
| 110 |
-
with open(target_file, "w", encoding="utf-8") as f:
|
| 111 |
-
json.dump(accounts_list, f, ensure_ascii=False, indent=2)
|
| 112 |
-
print(f"[Startup] Successfully wrote {len(accounts_list)} account(s) to {target_file}")
|
| 113 |
PYEOF
|
| 114 |
|
| 115 |
exec "$@"
|
|
|
|
| 37 |
# Set permissions
|
| 38 |
RUN chmod +x /app/main.py
|
| 39 |
|
| 40 |
+
# 创建启动初始化脚本:使用原生的 parse_cookie 与 AccountStore 落盘
|
| 41 |
RUN cat <<'EOF' > /app/entrypoint.sh
|
| 42 |
#!/bin/bash
|
| 43 |
set -e
|
|
|
|
| 47 |
python3 - <<'PYEOF'
|
| 48 |
import os
|
| 49 |
import re
|
| 50 |
+
import sys
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
from aistudio_api.infrastructure.account.account_store import AccountStore
|
| 54 |
+
from aistudio_api.infrastructure.account.cookie_parser import parse_cookie
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"[Startup Error] Import failed: {e}")
|
| 57 |
+
sys.exit(0)
|
| 58 |
|
| 59 |
acc_pattern = re.compile(r'^ACCOUNT\d+$', re.IGNORECASE)
|
| 60 |
account_vars = [k for k in os.environ.keys() if acc_pattern.match(k)]
|
|
|
|
| 64 |
print("[Startup] No ACCOUNT* secrets found.")
|
| 65 |
else:
|
| 66 |
print(f"[Startup] Found {len(account_vars)} secret(s): {', '.join(account_vars)}")
|
| 67 |
+
store = AccountStore()
|
| 68 |
+
first_account = None
|
| 69 |
+
|
| 70 |
for idx, var_name in enumerate(account_vars, start=1):
|
| 71 |
+
raw_cookie = os.environ.get(var_name, "").strip()
|
| 72 |
+
if not raw_cookie:
|
| 73 |
continue
|
| 74 |
+
try:
|
| 75 |
+
print(f"[Startup] Parsing {var_name}...")
|
| 76 |
+
# 使用项目原生的解析函数提取 Google 安全字段
|
| 77 |
+
account = parse_cookie(raw_cookie)
|
| 78 |
+
if hasattr(account, "name"):
|
| 79 |
+
account.name = f"Account-{idx}"
|
| 80 |
|
| 81 |
+
# 使用项目原生的 store 方法持久化账号
|
| 82 |
+
store.save(account)
|
| 83 |
+
print(f"[Startup] Successfully saved {var_name} as '{account.name}' (ID: {account.id})")
|
| 84 |
+
|
| 85 |
+
if first_account is None:
|
| 86 |
+
first_account = account
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"[Startup Error] Failed to parse/save {var_name}: {e}")
|
| 89 |
+
|
| 90 |
+
# 将第一个账号设置为活跃账号,避免 /accounts/active 返回 404
|
| 91 |
+
if first_account is not None:
|
| 92 |
+
try:
|
| 93 |
+
if hasattr(store, "set_active"):
|
| 94 |
+
store.set_active(first_account.id)
|
| 95 |
+
elif hasattr(store, "set_active_id"):
|
| 96 |
+
store.set_active_id(first_account.id)
|
| 97 |
+
print(f"[Startup] Set active account to: {first_account.id}")
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"[Startup Note] Setting active account: {e}")
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
loaded = store.list_all() if hasattr(store, "list_all") else store.list()
|
| 103 |
+
print(f"[Startup] Confirmed loaded accounts in store: {len(loaded)}")
|
| 104 |
+
except Exception:
|
| 105 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
PYEOF
|
| 107 |
|
| 108 |
exec "$@"
|