更新deploy_to_server.py指向真实生产部署目录和服务名
This commit is contained in:
+17
-29
@@ -11,7 +11,7 @@ SERVER_IP = "159.75.81.121"
|
||||
SSH_PORT = 22
|
||||
SSH_USER = "root"
|
||||
SSH_PASS = "sp-cc123"
|
||||
REMOTE_DIR = "/opt/risk_system"
|
||||
REMOTE_DIR = "/opt/XH-202626"
|
||||
DOMAIN = "risk.aiformat.cn"
|
||||
|
||||
LOCAL_BASE = r"d:\code\XH-202626_科创企业特有风险的识别与管理"
|
||||
@@ -71,7 +71,13 @@ def deploy():
|
||||
|
||||
sftp = ssh.open_sftp()
|
||||
|
||||
# 1. 创建远程目录并清理旧的带有 emoji 的文件
|
||||
# 0. 停止并清理可能残留的冲突服务
|
||||
run_remote_cmd(ssh, "systemctl stop risk_app 2>/dev/null || true")
|
||||
run_remote_cmd(ssh, "systemctl disable risk_app 2>/dev/null || true")
|
||||
run_remote_cmd(ssh, "rm -f /etc/systemd/system/risk_app.service")
|
||||
run_remote_cmd(ssh, "rm -rf /opt/risk_system")
|
||||
|
||||
# 1. 创建远程目录并清理旧的带 emoji 文件
|
||||
run_remote_cmd(ssh, f"mkdir -p {REMOTE_DIR}")
|
||||
run_remote_cmd(ssh, f"rm -rf {REMOTE_DIR}/pages/* {REMOTE_DIR}/*系统首页*.py")
|
||||
|
||||
@@ -82,50 +88,32 @@ def deploy():
|
||||
print("[上传] 压缩包上传成功")
|
||||
sftp.close()
|
||||
|
||||
# 3. 解压并配置 Python 环境与依赖
|
||||
# 3. 解压并优先尝试 git pull 同步
|
||||
run_remote_cmd(ssh, f"cd {REMOTE_DIR} && tar -xzf deploy_package.tar.gz")
|
||||
|
||||
# 检查并安装系统依赖 (nginx, python3, python3-pip, python3-venv 等)
|
||||
setup_env_cmd = """
|
||||
if command -v apt-get &> /dev/null; then
|
||||
apt-get update -y && apt-get install -y python3 python3-pip python3-venv nginx
|
||||
elif command -v yum &> /dev/null; then
|
||||
yum install -y python3 python3-pip nginx
|
||||
fi
|
||||
"""
|
||||
run_remote_cmd(ssh, setup_env_cmd)
|
||||
run_remote_cmd(ssh, f"cd {REMOTE_DIR} && git pull origin master || true")
|
||||
|
||||
# 创建 virtualenv 并安装依赖
|
||||
venv_cmd = f"""
|
||||
cd {REMOTE_DIR}
|
||||
python3 -m venv venv || python3 -m venv venv --without-pip
|
||||
source venv/bin/activate
|
||||
pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
|
||||
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
|
||||
"""
|
||||
run_remote_cmd(ssh, venv_cmd)
|
||||
|
||||
# 4. 配置 Systemd 服务
|
||||
# 4. 配置 Systemd 服务 (使用已配置好的 miniconda risk_env 环境)
|
||||
systemd_service = f"""[Unit]
|
||||
Description=Risk Management Streamlit Web App
|
||||
Description=Risk Management Streamlit Web App (risk.aiformat.cn)
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=root
|
||||
WorkingDirectory={REMOTE_DIR}
|
||||
ExecStart={REMOTE_DIR}/venv/bin/streamlit run {REMOTE_DIR}/系统首页.py --server.port 8501 --server.address 127.0.0.1 --server.headless true --server.enableCORS false --server.enableXsrfProtection false
|
||||
ExecStart=/root/miniconda3/envs/risk_env/bin/streamlit run {REMOTE_DIR}/系统首页.py --server.port 8501 --server.address 127.0.0.1 --server.headless true --server.enableCORS false --server.enableXsrfProtection false
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
Environment=PATH=/root/miniconda3/envs/risk_env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
"""
|
||||
create_service_cmd = f"cat << 'EOF' > /etc/systemd/system/risk_app.service\n{systemd_service}\nEOF"
|
||||
create_service_cmd = f"cat << 'EOF' > /etc/systemd/system/XH-202626.service\n{systemd_service}\nEOF"
|
||||
run_remote_cmd(ssh, create_service_cmd)
|
||||
|
||||
run_remote_cmd(ssh, "systemctl daemon-reload && systemctl enable risk_app && systemctl restart risk_app")
|
||||
run_remote_cmd(ssh, "systemctl daemon-reload && systemctl enable XH-202626 && systemctl restart XH-202626")
|
||||
time.sleep(3)
|
||||
run_remote_cmd(ssh, "systemctl status risk_app --no-pager")
|
||||
run_remote_cmd(ssh, "systemctl status XH-202626 --no-pager")
|
||||
|
||||
# 5. 配置 Nginx 反向代理
|
||||
nginx_conf = f"""server {{
|
||||
|
||||
Reference in New Issue
Block a user