#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 部署最新全部修改至线上服务器 文件清单: - addons/epay/library/v3new/Yansongda/Pay/Shortcut/Wechat/MchTransferShortcut.php - application/admin/controller/fc/Userext.php - application/admin/controller/fc/Withdraw.php - application/admin/view/fc/userext/edit.html - application/admin/view/fc/userext/detail.html - application/api/controller/Auth.php - application/api/controller/Distribution.php - application/common/library/WxTransfer.php - public/assets/js/backend/fc/userext.js """ import os import sys import io import paramiko sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace') HOST = "8.137.181.63" PORT = 22 USER = "root" PASSWORD = "liyanxia@6688" REMOTE_ROOT = "/www/wwwroot/fenleixinxi.gzyakj.com.cn" LOCAL_ROOT = r"D:\Softwares\phpstudy_pro\WWW\fenleixinxi" SYNC_FILES = [ "addons/epay/library/v3new/Yansongda/Pay/Shortcut/Wechat/MchTransferShortcut.php", "application/admin/controller/fc/Userext.php", "application/admin/controller/fc/Withdraw.php", "application/admin/view/fc/userext/edit.html", "application/admin/view/fc/userext/detail.html", "application/api/controller/Auth.php", "application/api/controller/Distribution.php", "application/common/library/WxTransfer.php", "public/assets/js/backend/fc/userext.js", ] def ssh_exec(client, cmd): stdin, stdout, stderr = client.exec_command(cmd) out = stdout.read().decode("utf-8", errors="replace").strip() err = stderr.read().decode("utf-8", errors="replace").strip() return out, err def main(): print("=== [1/3] 连接线上服务器 ===") client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(HOST, port=PORT, username=USER, password=PASSWORD, timeout=30) print(f"✓ 成功连接 {HOST}") sftp = client.open_sftp() print("✓ SFTP 通道已建立") print("\n=== [2/3] 同步最新修改的代码文件 ===") for rel_path in SYNC_FILES: local_path = os.path.join(LOCAL_ROOT, rel_path.replace("/", "\\")) remote_path = f"{REMOTE_ROOT}/{rel_path}" if not os.path.exists(local_path): print(f" ✗ 本地文件缺失: {local_path}") continue remote_dir = os.path.dirname(remote_path) ssh_exec(client, f"mkdir -p '{remote_dir}'") sftp.put(local_path, remote_path) ssh_exec(client, f"chown www:www '{remote_path}'") file_size = os.path.getsize(local_path) print(f" ✓ 已同步: {rel_path} ({file_size} 字节)") sftp.close() print("\n=== [3/3] 清理缓存与服务自检 ===") ssh_exec(client, f"rm -rf {REMOTE_ROOT}/runtime/temp/* {REMOTE_ROOT}/runtime/cache/*") print("✓ 线上 ThinkPHP 缓存清理完成") # 接口健康自检 check_urls = [ ("API 信息流接口", "https://fenleixinxi.gzyakj.com.cn/api/v1/home/feed"), ("API 分类接口", "https://fenleixinxi.gzyakj.com.cn/api/v1/home/categories"), ("后台登录页面", "https://fenleixinxi.gzyakj.com.cn/s.php/index/login"), ] for name, url in check_urls: cmd = f"curl -s -o /dev/null -w '%{{http_code}}' -k '{url}'" code, _ = ssh_exec(client, cmd) print(f" {name}: HTTP {code}") client.close() print("\n✓ 最新代码已全部部署至线上服务器!") if __name__ == "__main__": main()