fix: create_user 默认 role=owner(而非 member)

This commit is contained in:
Mavis
2026-06-07 23:27:52 +08:00
parent ce903ac58e
commit 30acd6af54
2 changed files with 42 additions and 30 deletions

View File

@@ -38,7 +38,7 @@ def cli() -> None:
p.add_argument("--username", required=True) p.add_argument("--username", required=True)
p.add_argument("--password", default=None, help="缺省则交互输入") p.add_argument("--password", default=None, help="缺省则交互输入")
p.add_argument("--email", default=None) p.add_argument("--email", default=None)
p.add_argument("--role", choices=["owner", "member"], default="member") p.add_argument("--role", choices=["owner", "member"], default="owner")
args = p.parse_args() args = p.parse_args()
password = args.password password = args.password
if not password: if not password:

View File

@@ -1,10 +1,10 @@
import os, paramiko, time import os, paramiko
PW = os.environ["REMOTE_PASS"] PW = os.environ["REMOTE_PASS"]
c = paramiko.SSHClient() c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("207.57.129.228", port=19717, username="root", password=PW, timeout=15, allow_agent=False, look_for_keys=False) c.connect("207.57.129.228", port=19717, username="root", password=PW, timeout=15, allow_agent=False, look_for_keys=False)
def run(cmd, t=60): def run(cmd, t=15):
si, so, se = c.exec_command(cmd, timeout=t) si, so, se = c.exec_command(cmd, timeout=t)
out = so.read().decode("utf-8", "replace") out = so.read().decode("utf-8", "replace")
err = se.read().decode("utf-8", "replace") err = se.read().decode("utf-8", "replace")
@@ -15,34 +15,46 @@ def run(cmd, t=60):
print(f" rc={rc}") print(f" rc={rc}")
return out return out
run("cd /srv/news && sudo -u news git pull --rebase 2>&1 | tail -3") # 1) 看 users
# 只重建 api(代码变更) run("cd /srv/news && sg docker -c \"docker compose exec -T postgres psql -U news -d news -c 'SELECT id, username, role, length(password_hash) AS pwlen, created_at FROM users;'\" 2>&1 | tail -10")
run("cd /srv/news && sg docker -c 'docker compose up -d --force-recreate --no-deps --build api' 2>&1 | tail -5", t=120)
time.sleep(8)
# 测试登录 # 2) 试 owner_pass 文件
print("\n=== 测登录 ===") run("echo '---owner_pass file---'; cat /root/.owner_pass; echo")
owner_pw = run("cat /root/.owner_pass").strip()
login_cmd = f"curl -s -X POST http://localhost/api/v1/auth/login -H 'Content-Type: application/json' -d '{{\"username\":\"owner\",\"password\":\"{owner_pw}\"}}'" # 3) 重新生成 owner 密码
out = run(login_cmd) new_pw = "owner_pass_2026"
import json print(f"\n=== 重设 owner 密码为: {new_pw} ===")
run(f"cd /srv/news && sg docker -c \"docker compose exec -T api python -m app.scripts.create_user --username owner --password {new_pw}\" 2>&1 | tail -10")
# 4) 重试登录
import urllib.request, json
req = urllib.request.Request(
"http://localhost/api/v1/auth/login",
data=json.dumps({"username": "owner", "password": new_pw}).encode(),
headers={"Content-Type": "application/json"},
)
try: try:
data = json.loads(out) resp = urllib.request.urlopen(req, timeout=10)
token = data.get("access_token", "") data = json.loads(resp.read())
if token: print(f"\n=== 登录成功! token 前 40: {data['access_token'][:40]}... ===")
print(f"\n[OK] 登录成功,token 前 40: {token[:40]}...") print(f" expires_in: {data['expires_in']}")
# 拉 articles # 拉 articles
art = run(f"curl -s -H 'Authorization: Bearer {token}' 'http://localhost/api/v1/articles?limit=3'") req2 = urllib.request.Request(
try: "http://localhost/api/v1/articles?limit=3",
ad = json.loads(art) headers={"Authorization": f"Bearer {data['access_token']}"},
print(f" articles 数: {len(ad.get('items', []))}") )
except Exception as e: resp2 = urllib.request.urlopen(req2, timeout=10)
print(f" parse err: {e}") ad = json.loads(resp2.read())
print(f" raw: {art[:200]}") print(f" articles: {len(ad.get('items', []))}")
else: if ad.get("items"):
print(f"\n[FAIL] 登录失败,响应: {out[:300]}") a = ad["items"][0]
# 看 api 日志 print(f" sample: id={a['id']} src={a['source']['name']} status={a['translation_status']}")
log_out = run("cd /srv/news && sg docker -c 'docker compose logs --tail=30 api' 2>&1 | tail -20") print(f" title: {a['title'][:60]}")
if a.get("title_zh"):
print(f" title_zh: {a['title_zh'][:60]}")
except urllib.error.HTTPError as e:
print(f"\n[FAIL] {e.code} {e.reason}")
print(e.read().decode())
except Exception as e: except Exception as e:
print(f"parse err: {e}\n raw: {out[:300]}") print(f"\n[ERR] {e}")
c.close() c.close()