nomadweb/scripts/deploy_wechat_login.py
eric 79dda36dd3 Ship Taro WeChat mini-app with H5-aligned UI and WeChat login.
Document wemini credentials and native weapp workflow in README; add jscode2session auth endpoint.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-27 22:05:56 -05:00

93 lines
2.2 KiB
Python

"""Upload WeChat login backend + patch server env, restart API."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "107.173.30.245"
USER = "root"
PASS = "Xiao4669805"
REMOTE = "/opt/nomadweb"
ROOT = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/services/wechat.py",
"backend/app/services/auth.py",
"backend/app/routers/auth.py",
"backend/app/schemas.py",
"deploy/nomadro-api.env.example",
]
ENV_LINES = [
"WECHAT_APPID=wx11f6afc44ea1532f",
"WECHAT_SECRET=d1814f60f900cfdee1813ccc6e00bc2d",
"WECHAT_ALLOW_DEVTOOLS=true",
]
def ensure_dir(sftp: paramiko.SFTPClient, path: str) -> None:
parts = path.strip("/").split("/")
cur = ""
for p in parts:
cur += "/" + p
try:
sftp.stat(cur)
except OSError:
sftp.mkdir(cur)
def main() -> int:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(HOST, username=USER, password=PASS, timeout=30)
sftp = client.open_sftp()
for rel in FILES:
local = ROOT / rel
remote = f"{REMOTE}/{rel.replace(chr(92), '/')}"
ensure_dir(sftp, "/".join(remote.split("/")[:-1]))
print("upload", rel)
sftp.put(str(local), remote)
sftp.close()
patch = "\n".join(ENV_LINES)
cmd = f"""
set -e
ENV={REMOTE}/deploy/nomadro-api.env
touch "$ENV"
for key in WECHAT_APPID WECHAT_SECRET WECHAT_ALLOW_DEVTOOLS; do
grep -v "^${{key}}=" "$ENV" > "$ENV.tmp" || true
mv "$ENV.tmp" "$ENV"
done
cat >> "$ENV" <<'EOF'
{patch}
EOF
systemctl restart nomadro-api
sleep 2
curl -s -o /dev/null -w "%{{http_code}}" http://127.0.0.1:8055/api/v1/health
echo
# smoke: mock code should work with ALLOW_DEVTOOLS
curl -s -X POST http://127.0.0.1:8055/api/v1/auth/wechat \\
-H 'Content-Type: application/json' \\
-d '{{"code":"dev"}}' | head -c 400
echo
grep '^WECHAT_' "$ENV"
"""
_, stdout, stderr = client.exec_command(cmd, timeout=120)
out = stdout.read().decode("utf-8", errors="replace")
err = stderr.read().decode("utf-8", errors="replace")
print(out)
if err.strip():
print("STDERR:", err)
client.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())