修改了下载站点配置的按钮,修改了增加静态文件配置的按钮
This commit is contained in:
116
nginx_tab.py
116
nginx_tab.py
@@ -258,7 +258,7 @@ class NginxSiteThread(QThread):
|
||||
self.site_config = site_config
|
||||
self.site_name = site_name
|
||||
self.password = password
|
||||
self.operation = operation # "create" 或 "enable"
|
||||
self.operation = operation # "create" 或 "enable" 或 "download"
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
@@ -326,6 +326,22 @@ class NginxSiteThread(QThread):
|
||||
self.result_ready.emit(False, f"站点配置启用失败: {error}")
|
||||
logger.error(f"站点配置启用失败: {error}")
|
||||
|
||||
elif self.operation == "download":
|
||||
# 下载站点配置文件
|
||||
site_path = f"/etc/nginx/sites-enabled/{self.site_name}"
|
||||
download_cmd = f"bash -c 'echo \"{self.password}\" | sudo -S cat {site_path}'"
|
||||
stdin, stdout, stderr = self.ssh_client.exec_command(download_cmd)
|
||||
exit_status = stdout.channel.recv_exit_status()
|
||||
|
||||
if exit_status == 0:
|
||||
config_content = stdout.read().decode()
|
||||
self.result_ready.emit(True, config_content)
|
||||
logger.info(f"站点配置文件下载成功: {site_path}")
|
||||
else:
|
||||
error = stderr.read().decode()
|
||||
self.result_ready.emit(False, f"站点配置文件下载失败: {error}")
|
||||
logger.error(f"站点配置文件下载失败: {error}")
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
self.result_ready.emit(False, error_msg)
|
||||
@@ -400,6 +416,11 @@ class NginxTab(QWidget):
|
||||
self.enable_site_config_btn.clicked.connect(self.enable_site_config)
|
||||
site_config_btn_layout.addWidget(self.enable_site_config_btn)
|
||||
|
||||
# 下载站点配置按钮
|
||||
self.download_site_config_btn = QPushButton("下载站点配置")
|
||||
self.download_site_config_btn.clicked.connect(self.download_site_config)
|
||||
site_config_btn_layout.addWidget(self.download_site_config_btn)
|
||||
|
||||
site_config_layout.addLayout(site_config_btn_layout)
|
||||
layout.addLayout(site_config_layout)
|
||||
|
||||
@@ -783,6 +804,99 @@ http {
|
||||
logger.error(f"Nginx站点配置启用后语法检查失败: {message}")
|
||||
QMessageBox.warning(self, "错误", f"Nginx配置文件语法检查失败: {message}")
|
||||
|
||||
def download_site_config(self):
|
||||
"""下载站点配置文件"""
|
||||
if not self.ssh_client:
|
||||
self.append_output("错误: 未连接到服务器")
|
||||
return
|
||||
|
||||
if not self.project_name:
|
||||
self.append_output("错误: 未设置项目名")
|
||||
return
|
||||
|
||||
# 请求用户输入sudo密码
|
||||
dialog = PasswordDialog(self)
|
||||
if dialog.exec_() == QDialog.Accepted:
|
||||
password = dialog.get_password()
|
||||
self.append_output(f"正在下载站点配置文件: {self.project_name}...")
|
||||
|
||||
# 创建并启动站点配置下载线程
|
||||
self.site_thread = NginxSiteThread(self.ssh_client, "", self.project_name, password, "download")
|
||||
self.site_thread.result_ready.connect(self.on_download_site_config_result)
|
||||
self.site_thread.start()
|
||||
else:
|
||||
self.append_output("用户取消了密码输入")
|
||||
|
||||
def on_download_site_config_result(self, success, message):
|
||||
"""处理下载站点配置结果"""
|
||||
if success:
|
||||
# 将下载的配置内容显示在站点配置编辑器中
|
||||
self.site_config_editor.setPlainText(message)
|
||||
self.append_output("站点配置文件下载成功")
|
||||
logger.info("Nginx站点配置文件下载成功")
|
||||
|
||||
# 尝试添加静态文件映射配置
|
||||
self.add_static_mappings()
|
||||
else:
|
||||
self.append_output(f"下载失败: {message}")
|
||||
logger.error(f"Nginx站点配置文件下载失败: {message}")
|
||||
QMessageBox.warning(self, "错误", f"Nginx站点配置文件下载失败: {message}")
|
||||
|
||||
def add_static_mappings(self):
|
||||
"""添加静态文件映射配置"""
|
||||
# 读取config.json文件获取项目信息
|
||||
try:
|
||||
with open('config.json', 'r', encoding='utf-8') as f:
|
||||
config = json.load(f)
|
||||
|
||||
# 获取第一个服务器配置(假设只有一个服务器配置)
|
||||
server_config = next(iter(config.values()))
|
||||
username = server_config.get('username', '')
|
||||
project_name = server_config.get('project', '')
|
||||
remote_dir = server_config.get('remote_dir', '')
|
||||
|
||||
# 构建静态文件路径
|
||||
static_path = f"{remote_dir}/{project_name}/static"
|
||||
media_path = f"{remote_dir}/{project_name}/media"
|
||||
|
||||
# 获取当前配置内容
|
||||
current_config = self.site_config_editor.toPlainText()
|
||||
|
||||
# 检查是否已经包含静态文件映射
|
||||
if "location /static/" not in current_config:
|
||||
# 添加静态文件映射配置
|
||||
static_mapping = f"\n # 静态文件映射\n"
|
||||
static_mapping += f" location /static/ {{\n"
|
||||
static_mapping += f" alias {static_path}/;\n"
|
||||
static_mapping += f" expires 30d;\n"
|
||||
static_mapping += f" }}\n"
|
||||
|
||||
# 添加媒体文件映射配置
|
||||
static_mapping += f"\n # 媒体文件映射\n"
|
||||
static_mapping += f" location /media/ {{\n"
|
||||
static_mapping += f" alias {media_path}/;\n"
|
||||
static_mapping += f" expires 30d;\n"
|
||||
static_mapping += f" }}\n"
|
||||
|
||||
# 在server块的末尾添加静态文件映射(在最后一个}之前)
|
||||
last_brace_pos = current_config.rfind('}')
|
||||
if last_brace_pos != -1:
|
||||
new_config = current_config[:last_brace_pos] + static_mapping + current_config[last_brace_pos:]
|
||||
self.site_config_editor.setPlainText(new_config)
|
||||
self.append_output("已添加静态文件映射配置")
|
||||
logger.info("已添加静态文件映射配置")
|
||||
else:
|
||||
self.append_output("无法添加静态文件映射配置:未找到server块的结束位置")
|
||||
logger.warning("无法添加静态文件映射配置:未找到server块的结束位置")
|
||||
else:
|
||||
self.append_output("配置文件已包含静态文件映射")
|
||||
logger.info("配置文件已包含静态文件映射")
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
self.append_output(f"添加静态文件映射失败: {error_msg}")
|
||||
logger.error(f"添加静态文件映射失败: {error_msg}")
|
||||
|
||||
def restart_nginx(self):
|
||||
"""重启Nginx"""
|
||||
if not self.ssh_client:
|
||||
|
||||
Reference in New Issue
Block a user