jlshu commited on
Commit
ddfe4db
·
verified ·
1 Parent(s): fc46b78

Update Dockerfile

Browse files
Files changed (1) hide show
  1. 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 json
51
- import uuid
 
 
 
 
 
 
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
- accounts_list = []
62
-
 
63
  for idx, var_name in enumerate(account_vars, start=1):
64
- raw_val = os.environ.get(var_name, "").strip()
65
- if not raw_val:
66
  continue
 
 
 
 
 
 
67
 
68
- cookie_dict = {}
69
- # 1. 如果提供的是 JSON 数组格式(如 Cookie-Editor 导出的列表)
70
- if raw_val.startswith("[") and raw_val.endswith("]"):
71
- try:
72
- items = json.loads(raw_val)
73
- for item in items:
74
- if isinstance(item, dict) and "name" in item and "value" in item:
75
- cookie_dict[item["name"]] = item["value"]
76
- except Exception as e:
77
- print(f"[Startup Warn] Failed to parse {var_name} as JSON list: {e}")
78
-
79
- # 2. 如果提供的是 Header 格式字符串(或未成功解析为 dict)
80
- if not cookie_dict:
81
- for part in raw_val.split(";"):
82
- if "=" in part:
83
- k, v = part.strip().split("=", 1)
84
- cookie_dict[k.strip()] = v.strip()
85
-
86
- if not cookie_dict:
87
- print(f"[Startup Warn] No valid cookies found in {var_name}")
88
- continue
89
-
90
- account_id = f"acc-{uuid.uuid4().hex[:8]}"
91
- account_name = f"Account-{idx}"
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 "$@"