初版:抖音自动续火花(从线上实例导出)
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
"""一键将整站源码与环境全自动部署到云服务器 (Zero-Touch Remote Deployer)
|
||||
|
||||
极速优化版:
|
||||
1. 自动过滤开发依赖与临时文件;
|
||||
2. 采用内存级 tar.gz 压缩包单文件毫秒级极速上传(从 5 分钟加速至 2 秒!);
|
||||
3. 远程一键解压并执行 deploy/deploy.sh 启动后台守护服务!
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
import tarfile
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
# 确保控制台 UTF-8
|
||||
if hasattr(sys.stdout, "reconfigure"):
|
||||
try:
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent
|
||||
|
||||
# 仅跳过通用编译缓存与本地虚拟环境
|
||||
IGNORE_NAMES = {".git", "__pycache__", ".venv", "venv", ".pytest_cache", ".idea", ".vscode"}
|
||||
|
||||
|
||||
def print_banner():
|
||||
print("=" * 65)
|
||||
print(" 🚀 抖音云端自动续火花 · 云服务器一键自动化极速部署工具")
|
||||
print("=" * 65)
|
||||
|
||||
|
||||
def create_deploy_archive() -> Path:
|
||||
"""通用全量打包:自动打包当前项目所有文件与目录"""
|
||||
temp_tar = Path(tempfile.gettempdir()) / "douyin_cloud_streak_deploy.tar.gz"
|
||||
|
||||
print("[*] 正在全量打包项目文件 (包含所有模块、配置、前端与数据)...")
|
||||
with tarfile.open(temp_tar, "w:gz") as tar:
|
||||
for item in BASE_DIR.iterdir():
|
||||
if item.name in IGNORE_NAMES:
|
||||
continue
|
||||
|
||||
def _filter(tarinfo):
|
||||
for ign in IGNORE_NAMES:
|
||||
if f"/{ign}/" in f"/{tarinfo.name}/" or tarinfo.name == ign:
|
||||
return None
|
||||
if tarinfo.name.endswith(".pyc") or tarinfo.name.endswith(".pid"):
|
||||
return None
|
||||
return tarinfo
|
||||
|
||||
tar.add(str(item), arcname=item.name, filter=_filter)
|
||||
|
||||
size_kb = temp_tar.stat().st_size / 1024
|
||||
print(f"[✓] 全量打包完成!压缩包大小: {size_kb:.1f} KB\n")
|
||||
return temp_tar
|
||||
|
||||
|
||||
def main():
|
||||
print_banner()
|
||||
|
||||
server_ip = input("👉 请输入云服务器公网 IP 地址 (例如 123.45.67.89): ").strip()
|
||||
if not server_ip:
|
||||
print("[❌ 错误] 服务器 IP 不能为空!")
|
||||
input("\n按回车键退出...")
|
||||
sys.exit(1)
|
||||
|
||||
server_user = input("👉 请输入 SSH 用户名 [直接回车默认 root]: ").strip() or "root"
|
||||
port_input = input("👉 请输入 SSH 端口号 [直接回车默认 22]: ").strip()
|
||||
server_port = int(port_input) if port_input.isdigit() else 22
|
||||
remote_dir = input("👉 请输入部署路径 [直接回车默认 /opt/douyin-cloud-streak]: ").strip() or "/opt/douyin-cloud-streak"
|
||||
|
||||
print("-" * 65)
|
||||
password = input("🔑 请输入服务器密码 (明文可见,支持右键直接粘贴): ").strip()
|
||||
print("-" * 65)
|
||||
|
||||
if not password:
|
||||
print("[❌ 错误] 密码不能为空!")
|
||||
input("\n按回车键退出...")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"\n[*] 正在连接到服务器 {server_user}@{server_ip}:{server_port} ...")
|
||||
|
||||
try:
|
||||
import paramiko
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(
|
||||
hostname=server_ip,
|
||||
port=server_port,
|
||||
username=server_user,
|
||||
password=password,
|
||||
timeout=15,
|
||||
banner_timeout=15,
|
||||
auth_timeout=15
|
||||
)
|
||||
print("[✓] SSH 连接成功!\n")
|
||||
|
||||
# 1. 打包并极速上传
|
||||
archive_path = create_deploy_archive()
|
||||
remote_tar = f"/tmp/douyin_cloud_streak_deploy.tar.gz"
|
||||
|
||||
print(f"[*] 正在将打包文件秒级上传至服务器 /tmp ...")
|
||||
sftp = ssh.open_sftp()
|
||||
sftp.put(str(archive_path), remote_tar)
|
||||
sftp.close()
|
||||
archive_path.unlink(missing_ok=True)
|
||||
print("[✓] 上传完成!\n")
|
||||
|
||||
# 2. 远程解压并部署 (强制覆盖旧版本文件)
|
||||
print(f"[*] 正在远程强制覆盖解压至 {remote_dir} 并启动部署...")
|
||||
extract_cmd = f"mkdir -p '{remote_dir}' && tar --overwrite -xzf '{remote_tar}' -C '{remote_dir}' && rm -f '{remote_tar}'"
|
||||
stdin, stdout, stderr = ssh.exec_command(extract_cmd)
|
||||
stdout.channel.recv_exit_status()
|
||||
print("[✓] 远程代码覆盖解压就绪!\n")
|
||||
|
||||
# 3. 执行 deploy/deploy.sh
|
||||
print("=" * 65)
|
||||
print(" [*] 正在云端服务器自动安装依赖环境并启动守护进程...")
|
||||
print(" [*] 这通常需要 1~2 分钟(配置 Python 虚拟环境与 Playwright 内核)...")
|
||||
print("=" * 65)
|
||||
|
||||
cmd = f"cd '{remote_dir}' && sed -i 's/\\r$//' deploy/deploy.sh deploy/*.service 2>/dev/null || true; chmod +x deploy/deploy.sh && bash deploy/deploy.sh"
|
||||
stdin, stdout, stderr = ssh.exec_command(cmd, get_pty=True)
|
||||
|
||||
for line in iter(stdout.readline, ""):
|
||||
print(line, end="")
|
||||
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
|
||||
if exit_status == 0:
|
||||
# 读取 .env 里的 AUTH_TOKEN
|
||||
token_cmd = f"grep '^AUTH_TOKEN=' '{remote_dir}/.env' | cut -d= -f2- | tr -d '\\r\\n'"
|
||||
_, token_out, _ = ssh.exec_command(token_cmd)
|
||||
token = token_out.read().decode('utf-8', 'ignore').strip()
|
||||
|
||||
print("\n" + "=" * 65)
|
||||
print(" 🎉🎉🎉 恭喜!云服务器已全部部署完成并成功启动!")
|
||||
print("=" * 65)
|
||||
print(f"🌐 Web 管理后台地址: http://{server_ip}:8000")
|
||||
print(f"🔑 访问安全令牌 (Token): {token or 'spark_secret_token_change_me'}")
|
||||
print("=" * 65)
|
||||
print("【接下来只需】:")
|
||||
print(f"1. 浏览器打开: http://{server_ip}:8000")
|
||||
print("2. 在「好友与消息」页面点击「一键勾选火花好友」并保存;")
|
||||
print("3. 云服务器将在每天设定的时间 24 小时全自动为您维持火花!")
|
||||
print("=" * 65)
|
||||
else:
|
||||
print(f"\n[⚠️ 部署脚本返回状态码 {exit_status}] 请查看上方输出信息。")
|
||||
|
||||
ssh.close()
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n[❌ 部署发生异常] {e}")
|
||||
|
||||
input("\n按回车键退出...")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user