170 lines
5.9 KiB
Python
170 lines
5.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Finish dy.nomadro.live deploy: start services, nginx, certbot."""
|
|
import sys
|
|
import time
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
HOST = "108.174.50.68"
|
|
DOMAIN = "dy.nomadro.live"
|
|
PUBLIC_BASE = f"https://{DOMAIN}"
|
|
|
|
FINISH_SH = r"""#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
source /etc/douyinweb.env
|
|
APP_DIR=/opt/douyinWEB
|
|
ADMIN_PASS='Xiao4669805@'
|
|
API_PORT=${API_PORT:-8014}
|
|
WEB_PORT=${WEB_PORT:-4015}
|
|
PB_PORT=${PB_PORT:-8092}
|
|
S3_PORT=${S3_PORT:-8333}
|
|
S3_BUCKET=${S3_BUCKET:-douyin-covers}
|
|
S3_ACCESS=${S3_ACCESS:-douyin}
|
|
PB_ADMIN_EMAIL="${ADMIN_USER}@${DOMAIN}"
|
|
|
|
log() { printf '\n[%s] %s\n' "$(date '+%F %T')" "$*"; }
|
|
|
|
log "start all douyinweb units"
|
|
systemctl daemon-reload
|
|
systemctl enable --now \
|
|
douyinweb-sw-master douyinweb-sw-volume douyinweb-sw-filer douyinweb-s3 \
|
|
douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web
|
|
sleep 8
|
|
systemctl is-active douyinweb-sw-master douyinweb-sw-volume douyinweb-sw-filer douyinweb-s3 \
|
|
douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web || true
|
|
|
|
log "pb superuser + s3 bucket"
|
|
"$APP_DIR/pb/pocketbase" superuser upsert "$PB_ADMIN_EMAIL" "$ADMIN_PASS" || true
|
|
export S3_PORT S3_ACCESS S3_SECRET S3_BUCKET
|
|
"$APP_DIR/backend/.venv/bin/python" - <<PY || true
|
|
import boto3, os
|
|
from botocore.client import Config
|
|
c=boto3.client("s3", endpoint_url=f"http://127.0.0.1:{os.environ.get('S3_PORT','8333')}",
|
|
aws_access_key_id=os.environ.get("S3_ACCESS","douyin"),
|
|
aws_secret_access_key=os.environ.get("S3_SECRET",""),
|
|
region_name="us-east-1", config=Config(signature_version="s3v4", s3={"addressing_style":"path"}))
|
|
try:
|
|
c.create_bucket(Bucket=os.environ.get("S3_BUCKET","douyin-covers"))
|
|
print("bucket ok")
|
|
except Exception as e:
|
|
print("bucket", e)
|
|
PY
|
|
|
|
log "ensure frontend build"
|
|
if [ ! -f "$APP_DIR/frontend/.next/BUILD_ID" ]; then
|
|
(cd "$APP_DIR/frontend" && npm install --no-audit --no-fund && npm run build)
|
|
fi
|
|
|
|
log "sync admin"
|
|
cd "$APP_DIR/backend"
|
|
export DY_SESSION_SECRET="${SESSION_SECRET:-}"
|
|
.venv/bin/python -c "from app.session_auth import ensure_default_admin_credentials; ensure_default_admin_credentials(); from app.session_auth import get_admin_credentials; u,h=get_admin_credentials(); print('admin', u, h[:12])"
|
|
|
|
log "nginx vhost"
|
|
cat >/etc/nginx/conf.d/dy-nomadro-live.conf <<NGX
|
|
server {
|
|
listen 80;
|
|
server_name ${DOMAIN};
|
|
|
|
client_max_body_size 200m;
|
|
|
|
location ^~ /api/ {
|
|
proxy_pass http://127.0.0.1:${API_PORT};
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host \$host;
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
proxy_set_header Upgrade \$http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_read_timeout 3600s;
|
|
}
|
|
location ^~ /covers/ {
|
|
proxy_pass http://127.0.0.1:${API_PORT};
|
|
proxy_set_header Host \$host;
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
}
|
|
location ^~ /_/ {
|
|
proxy_pass http://127.0.0.1:${PB_PORT};
|
|
proxy_set_header Host \$host;
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
}
|
|
location ^~ /${S3_BUCKET}/ {
|
|
proxy_pass http://127.0.0.1:${S3_PORT};
|
|
proxy_set_header Host \$host;
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
client_max_body_size 200m;
|
|
}
|
|
location / {
|
|
proxy_pass http://127.0.0.1:${WEB_PORT};
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host \$host;
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
|
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto \$scheme;
|
|
proxy_set_header Upgrade \$http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_read_timeout 60s;
|
|
}
|
|
}
|
|
NGX
|
|
|
|
nginx -t
|
|
systemctl reload nginx
|
|
|
|
log "certbot"
|
|
if [ ! -f "/etc/letsencrypt/live/${DOMAIN}/fullchain.pem" ]; then
|
|
certbot --nginx -d "${DOMAIN}" --non-interactive --agree-tos --register-unsafely-without-email --redirect || \
|
|
certbot certonly --nginx -d "${DOMAIN}" --non-interactive --agree-tos --register-unsafely-without-email || true
|
|
nginx -t && systemctl reload nginx || true
|
|
fi
|
|
|
|
log "verify"
|
|
sleep 2
|
|
curl -fsS "http://127.0.0.1:${API_PORT}/api/health" | head -c 300; echo
|
|
curl -sS -o /dev/null -w "web:%{http_code}\n" "http://127.0.0.1:${WEB_PORT}/login" || true
|
|
curl -sS -o /dev/null -w "login:%{http_code}\n" -X POST "http://127.0.0.1:${API_PORT}/api/auth/login" \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"username":"hackrobot","password":"Xiao4669805@"}' || true
|
|
curl -sS -o /dev/null -w "pub:%{http_code}\n" --max-time 25 "https://${DOMAIN}/login" || true
|
|
curl -sS -o /dev/null -w "pub_health:%{http_code}\n" --max-time 25 "https://${DOMAIN}/api/health" || true
|
|
echo "DONE https://${DOMAIN}/"
|
|
"""
|
|
|
|
|
|
def main() -> int:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
|
|
|
|
remote = "/tmp/finish_dy.sh"
|
|
sftp = c.open_sftp()
|
|
with sftp.file(remote, "w") as f:
|
|
f.write(FINISH_SH)
|
|
sftp.chmod(remote, 0o755)
|
|
sftp.close()
|
|
|
|
chan = c.get_transport().open_session()
|
|
chan.settimeout(600)
|
|
chan.get_pty(width=200, height=50)
|
|
chan.exec_command(f"bash {remote}")
|
|
while True:
|
|
if chan.recv_ready():
|
|
print(chan.recv(65535).decode("utf-8", "replace"), end="", flush=True)
|
|
if chan.recv_stderr_ready():
|
|
print(chan.recv_stderr(65535).decode("utf-8", "replace"), end="", flush=True)
|
|
if chan.exit_status_ready() and not chan.recv_ready() and not chan.recv_stderr_ready():
|
|
break
|
|
time.sleep(0.3)
|
|
while chan.recv_ready():
|
|
print(chan.recv(65535).decode("utf-8", "replace"), end="", flush=True)
|
|
code = chan.recv_exit_status()
|
|
c.close()
|
|
print(f"\nexit {code}")
|
|
return code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|