Files
django.remote/main.py
2025-08-31 20:35:59 +08:00

194 lines
10 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import sys
import os
from PySide6.QtWidgets import QApplication, QMainWindow, QTabWidget, QStatusBar
from PySide6.QtCore import QSize
from loguru import logger
from server_connection_tab import ServerConnectionTab
from remote_commands_tab import RemoteCommandsTab
from django_tab import DjangoTab
from gunicorn_tab import GunicornTab
from nginx_tab import NginxTab
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
logger.info("初始化主窗口")
self.setWindowTitle("服务器管理工具")
self.setMinimumSize(QSize(800, 600))
# 创建标签页组件
self.tabs = QTabWidget()
self.setCentralWidget(self.tabs)
# 创建状态栏
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
# 显示当前工作目录路径
current_dir = os.getcwd()
self.status_bar.showMessage(f"当前目录: {current_dir}")
logger.info(f"设置状态栏显示当前目录: {current_dir}")
# 添加服务器连接标签页
self.server_connection_tab = ServerConnectionTab()
self.tabs.addTab(self.server_connection_tab, "服务器连接")
# 添加远程命令标签页
self.remote_commands_tab = RemoteCommandsTab()
self.tabs.addTab(self.remote_commands_tab, "远程命令")
# 添加Django管理标签页
self.django_tab = DjangoTab()
self.tabs.addTab(self.django_tab, "Django")
# 添加Gunicorn管理标签页
self.gunicorn_tab = GunicornTab()
self.tabs.addTab(self.gunicorn_tab, "Gunicorn")
# 添加Nginx管理标签页
self.nginx_tab = NginxTab()
self.tabs.addTab(self.nginx_tab, "Nginx")
# 连接标签页切换信号
self.tabs.currentChanged.connect(self.on_tab_changed)
logger.info("主窗口初始化完成")
def on_tab_changed(self, index):
logger.info(f"标签页切换到: {index}")
# 更新状态栏显示当前目录信息
if index == 0: # 服务器连接标签页
current_dir = os.getcwd()
self.status_bar.showMessage(f"当前目录: {current_dir}")
logger.info(f"状态栏更新为本地目录: {current_dir}")
# 当切换到远程命令标签页时传递SSH客户端和服务器配置
elif index == 1: # 远程命令标签页
ssh_client = self.server_connection_tab.get_ssh_client()
self.remote_commands_tab.set_ssh_client(ssh_client)
# 获取当前选中的服务器配置
current_alias = self.server_connection_tab.alias_combo.currentText()
if current_alias and current_alias in self.server_connection_tab.config_data:
server_config = self.server_connection_tab.config_data[current_alias]
git_url = server_config.get("git_url", "")
remote_dir = server_config.get("remote_dir", "")
self.remote_commands_tab.set_server_config(git_url, remote_dir)
# 调用set_server_info方法更新服务器信息
self.remote_commands_tab.set_server_info(server_config)
# 尝试获取远程服务器当前目录并更新状态栏
try:
if hasattr(self.remote_commands_tab, 'current_dir_display') and self.remote_commands_tab.current_dir_display.text():
current_dir = self.remote_commands_tab.current_dir_display.text()
self.status_bar.showMessage(f"远程服务器 {current_alias}: {current_dir}")
logger.info(f"状态栏更新为远程服务器目录: {current_alias}: {current_dir}")
else:
# 更新状态栏显示远程服务器信息
self.status_bar.showMessage(f"远程服务器: {current_alias} | 远程目录: {remote_dir}")
logger.info(f"状态栏更新为远程服务器: {current_alias}, 目录: {remote_dir}")
except Exception as e:
logger.error(f"获取远程目录信息失败: {str(e)}")
# 更新状态栏显示远程服务器信息
self.status_bar.showMessage(f"远程服务器: {current_alias} | 远程目录: {remote_dir}")
logger.info(f"状态栏更新为远程服务器: {current_alias}, 目录: {remote_dir}")
else:
# 如果没有配置远程目录,初始化为默认目录
self.remote_commands_tab.current_dir_display.setText("~")
self.remote_commands_tab.refresh_directory()
# 更新状态栏显示远程服务器信息
self.status_bar.showMessage(f"远程服务器: {current_alias} | 远程目录: ~")
logger.info(f"状态栏更新为远程服务器: {current_alias}, 目录: ~")
# 当切换到Django标签页时传递SSH客户端和用户名
elif index == 2: # Django标签页
ssh_client = self.server_connection_tab.get_ssh_client()
self.django_tab.set_ssh_client(ssh_client)
# 获取当前选中的服务器配置中的用户名
current_alias = self.server_connection_tab.alias_combo.currentText()
if current_alias and current_alias in self.server_connection_tab.config_data:
server_config = self.server_connection_tab.config_data[current_alias]
username = server_config.get("username", "")
self.django_tab.set_username(username)
# 更新状态栏显示Django项目信息
project_name = server_config.get("project", "")
remote_dir = server_config.get("remote_dir", "")
self.status_bar.showMessage(f"远程服务器: {current_alias} | Django项目: {project_name} | 项目目录: {remote_dir}")
logger.info(f"状态栏更新为Django项目: {project_name}, 目录: {remote_dir}")
# 当切换到Gunicorn标签页时传递SSH客户端、用户名和项目信息
elif index == 3: # Gunicorn标签页
ssh_client = self.server_connection_tab.get_ssh_client()
self.gunicorn_tab.set_ssh_client(ssh_client)
# 获取当前选中的服务器配置中的用户名和项目信息
current_alias = self.server_connection_tab.alias_combo.currentText()
if current_alias and current_alias in self.server_connection_tab.config_data:
server_config = self.server_connection_tab.config_data[current_alias]
username = server_config.get("username", "")
project_name = server_config.get("project", "")
# 获取Django路径根据用户说明路径应该是/home/[user]/[project_path]/[project_name]
# 其中user是config.json中的usernameproject_path是git_url中的webstatusproject_name是project的值
remote_dir = server_config.get("remote_dir", "")
git_url = server_config.get("git_url", "")
project_name = server_config.get("project", "")
# 从git_url中提取project_pathwebstatus
project_path = ""
if git_url:
# git_url格式为http://192.168.3.241:3000/xiaji/webstatus.git
# 提取最后一个/和.git之间的部分作为project_path
import os
git_name = os.path.basename(git_url) # 获取webstatus.git
if git_name.endswith(".git"):
project_path = git_name[:-4] # 去掉.git后缀得到webstatus
# 构建Django路径/home/[user]/[project_path]/ (statuspage的父目录)
if remote_dir and project_path:
django_path = f"{remote_dir}/{project_path}/"
else:
django_path = f"{remote_dir}/" if remote_dir else ""
logger.info(f"构建的Django路径: {django_path}, 项目名: {project_name}")
self.gunicorn_tab.set_username(username)
self.gunicorn_tab.set_project_info(project_name, django_path)
# 更新状态栏显示Gunicorn服务信息
self.status_bar.showMessage(f"远程服务器: {current_alias} | Gunicorn服务: gunicorn_{project_name} | 服务目录: {django_path}")
logger.info(f"状态栏更新为Gunicorn服务: gunicorn_{project_name}, 目录: {django_path}")
# 当切换到Nginx标签页时传递SSH客户端、用户名和项目信息
elif index == 4: # Nginx标签页
ssh_client = self.server_connection_tab.get_ssh_client()
self.nginx_tab.set_ssh_client(ssh_client)
# 获取当前选中的服务器配置中的用户名和项目信息
current_alias = self.server_connection_tab.alias_combo.currentText()
if current_alias and current_alias in self.server_connection_tab.config_data:
server_config = self.server_connection_tab.config_data[current_alias]
username = server_config.get("username", "")
project_name = server_config.get("project", "")
server_ip = server_config.get("ip", "")
self.nginx_tab.set_username(username)
self.nginx_tab.set_project_info(project_name, server_ip)
# 更新状态栏显示Nginx服务信息
self.status_bar.showMessage(f"远程服务器: {current_alias} | Nginx服务: nginx | 项目: {project_name}")
logger.info(f"状态栏更新为Nginx服务: nginx, 项目: {project_name}")
if __name__ == "__main__":
logger.add("app.log", rotation="10 MB")
logger.info("启动应用程序")
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())