feat(fix) nginx 删除操作新增还原配置

This commit is contained in:
bwcx_jzy 2022-02-12 13:46:01 +08:00
parent 83ac7fb143
commit ad9d0192dd
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
4 changed files with 332 additions and 274 deletions

View File

@ -1,5 +1,16 @@
# 🚀 版本日志
# 2.8.10
### 🐣 新增功能
1. nginx 删除操作新增还原配置
### 🐞 解决BUG、优化功能
------
# 2.8.9 (2022-02-12)
### 🐣 新增功能

View File

@ -78,13 +78,13 @@ public class NginxController extends BaseAgentController {
* @return json
*/
@RequestMapping(value = "list_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String list(String whitePath, String name) {
public String list(String whitePath, String name, String showAll) {
boolean checkNgxDirectory = whitelistDirectoryService.checkNgxDirectory(whitePath);
Assert.state(checkNgxDirectory, "文件路径错误,非白名单路径");
if (StrUtil.isEmpty(name)) {
name = StrUtil.SLASH;
}
JSONArray array = nginxService.list(whitePath, name);
List<JSONObject> array = nginxService.list(whitePath, name, showAll);
return JsonMessage.getString(200, "", array);
}
@ -122,6 +122,11 @@ public class NginxController extends BaseAgentController {
// setAttribute("data", jsonObject);
}
private void checkName(String name) {
Assert.hasText(name, "请填写文件名");
Assert.state(name.endsWith(".conf"), "文件后缀必须为\".conf\"");
}
/**
* 新增或修改配置
*
@ -132,8 +137,7 @@ public class NginxController extends BaseAgentController {
*/
@RequestMapping(value = "updateNgx", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String updateNgx(String name, String whitePath, String genre, String context) {
Assert.hasText(name, "请填写文件名");
Assert.state(name.endsWith(".conf"), "文件后缀必须为\".conf\"");
this.checkName(name);
//
boolean ngxDirectory = whitelistDirectoryService.checkNgxDirectory(whitePath);
Assert.state(ngxDirectory, "请选择正确的白名单");
@ -237,18 +241,35 @@ public class NginxController extends BaseAgentController {
* @return json
*/
@RequestMapping(value = "delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String delete(String path, String name) {
public String delete(String path, String name, String type, String from) {
if (!whitelistDirectoryService.checkNgxDirectory(path)) {
return JsonMessage.getString(400, "非法操作");
}
Assert.hasText(name, "请选择文件");
Assert.hasText(name, "请填写文件名");
if (StrUtil.equals(from, "back")) {
Assert.state(name.endsWith(".conf_back"), "不能操作此文件");
} else {
Assert.state(name.endsWith(".conf"), "文件后缀必须为\".conf\"");
}
File file = FileUtil.file(path, name);
if (StrUtil.equals(type, "real")) {
// 真删除
FileUtil.del(file);
} else {
try {
if (StrUtil.equals(from, "back")) {
// 恢复 可能出现 所以 copy Read-only file system
File back = FileUtil.file(path, StrUtil.removeSuffix(name, "_back"));
FileUtil.copy(file, back, true);
FileUtil.del(file);
} else {
// 假删除
FileUtil.rename(file, file.getName() + "_back", false, true);
}
} catch (Exception e) {
DefaultSystemLog.getLog().error("删除nginx", e);
return JsonMessage.getString(400, "删除失败:" + e.getMessage());
return JsonMessage.getString(400, "操作失败:" + e.getMessage());
}
}
String msg = this.reloadNginx();
return JsonMessage.getString(200, "删除成功" + msg);

View File

@ -23,6 +23,7 @@
package io.jpom.service.system;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
@ -33,8 +34,8 @@ import com.github.odiszapc.nginxparser.NgxBlock;
import com.github.odiszapc.nginxparser.NgxConfig;
import com.github.odiszapc.nginxparser.NgxEntry;
import com.github.odiszapc.nginxparser.NgxParam;
import io.jpom.service.BaseDataService;
import io.jpom.model.data.AgentWhitelist;
import io.jpom.service.BaseDataService;
import io.jpom.service.WhitelistDirectoryService;
import io.jpom.system.AgentConfigBean;
import io.jpom.util.JsonFileUtil;
@ -43,8 +44,11 @@ import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author Arno
@ -58,7 +62,7 @@ public class NginxService extends BaseDataService {
this.whitelistDirectoryService = whitelistDirectoryService;
}
public JSONArray list(String whitePath, String fileName) {
public List<JSONObject> list(String whitePath, String fileName, String showAll) {
AgentWhitelist agentWhitelist = whitelistDirectoryService.getWhitelist();
if (agentWhitelist == null) {
return null;
@ -72,26 +76,28 @@ public class NginxService extends BaseDataService {
if (files == null || files.length <= 0) {
return null;
}
JSONArray array = new JSONArray();
for (File file : files) {
String[] suffixes = Convert.toBool(showAll, false) ? new String[]{".conf", ".conf_back"} : new String[]{".conf"};
return Arrays.stream(files).map(file -> {
String name = file.getName();
if (file.isDirectory()) {
return null;
}
if (!StrUtil.endWithAnyIgnoreCase(name, suffixes)) {
return null;
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("path", whitePath);
long time = file.lastModified();
jsonObject.put("time", DateUtil.date(time).toString());
jsonObject.put("name", name);
jsonObject.put("relativePath", FileUtil.normalize(fileName + StrUtil.SLASH + name));
if (file.isDirectory()) {
continue;
// if (FileUtil.isEmpty(file)) {
// continue;
// }
// jsonObject.put("name", name + "【文件夹】");
// jsonObject.put("isDirectory", true);
} else {
if (!name.endsWith(".conf")) {
continue;
}
try {
NgxConfig config = NgxConfig.read(file.getPath());
List<NgxEntry> server = config.findAll(NgxBlock.class, "server");
@ -102,10 +108,9 @@ public class NginxService extends BaseDataService {
} catch (IOException e) {
DefaultSystemLog.getLog().error(e.getMessage(), e);
}
}
array.add(jsonObject);
}
return array;
return jsonObject;
}).filter(Objects::nonNull).collect(Collectors.toList());
}
/**

View File

@ -12,8 +12,13 @@
<a-table size="middle" :data-source="fileList" :loading="loading" :columns="columns" :pagination="false" bordered :rowKey="(record, index) => index">
<template slot="title">
<a-space>
<a-button size="small" type="primary" @click="handleAdd">新增配置</a-button>
<div>
查询
<a-switch v-model="listData.showAll" checked-children="显示所有" un-checked-children="显示正常" />
</div>
<a-button size="small" type="primary" @click="handleFilter">刷新</a-button>
<a-button size="small" type="primary" @click="handleAdd">新增配置</a-button>
<a-switch v-model="nginxData.status" checked-children="运行中" un-checked-children="未运行" disabled />
<a-dropdown>
<a class="ant-dropdown-link" @click="(e) => e.preventDefault()">
@ -52,9 +57,10 @@
</a-space>
</template>
<a-tooltip slot="name" slot-scope="text, record" placement="topLeft" :title="`名称:${text} server 节点数 ${record.serverCount}`">
<div @click="handleEdit(record)" style="color: blue">
<div @click="handleEdit(record)" :style="`${record.name.endsWith('.conf') ? 'color: blue' : ''}`">
<a-icon v-if="record.name.endsWith('.conf')" type="edit" />
<a-icon v-else type="eye" />
<span>{{ text }}</span>
<a-icon type="edit" />
</div>
</a-tooltip>
<!-- <a-tooltip slot="isDirectory" slot-scope="text" placement="topLeft" :title="text">
@ -71,7 +77,20 @@
</a-tooltip>
<template slot="operation" slot-scope="text, record">
<!-- <a-button type="primary" @click="handleEdit(record)">编辑</a-button> -->
<a-button size="small" type="danger" @click="handleDelete(record)">删除</a-button>
<a-popover title="删除确认" v-if="record.name.endsWith('.conf')">
<template slot="content">
<p><a-button size="small" type="danger" @click="handleDelete(record, 'temp', 'none')">临时删除</a-button></p>
<p><a-button size="small" type="danger" @click="handleDelete(record, 'real', 'none')">永久删除</a-button></p>
</template>
<a-button size="small" type="danger">删除</a-button>
</a-popover>
<a-popover title="还原" v-else>
<template slot="content">
<p><a-button size="small" type="danger" @click="handleDelete(record, 'temp', 'back')">还原配置</a-button></p>
<p><a-button size="small" type="danger" @click="handleDelete(record, 'real', 'back')">永久删除</a-button></p>
</template>
<a-button size="small" type="danger">还原</a-button>
</a-popover>
</template>
</a-table>
<!-- 编辑区 -->
@ -103,17 +122,7 @@
</a-layout>
</template>
<script>
import {
getNginxDirectoryList,
getNginxFileList,
editNginxConfig,
deleteNginxConfig,
loadNginxWhiteList,
loadNginxConfig,
loadNginxData,
doNginxCommand,
editNginxServerName,
} from "../../../../api/node-nginx";
import { getNginxDirectoryList, getNginxFileList, editNginxConfig, deleteNginxConfig, loadNginxWhiteList, loadNginxConfig, loadNginxData, doNginxCommand, editNginxServerName } from "@/api/node-nginx";
import codeEditor from "@/components/codeEditor";
@ -135,7 +144,10 @@ export default {
nginxData: {},
temp: {},
tempNode: {},
tableHeight: "80vh",
listData: {
showAll: false,
},
// tableHeight: "80vh",
editNginxVisible: false,
editNginxNameVisible: false,
defaultProps: {
@ -227,6 +239,7 @@ export default {
nodeId: this.node.id,
whitePath: this.tempNode.whitePath,
name: this.tempNode.path,
showAll: this.listData.showAll,
};
//
getNginxFileList(params).then((res) => {
@ -298,10 +311,16 @@ export default {
});
},
//
handleDelete(record) {
handleDelete(record, type, from) {
let msg;
if (from === "back") {
msg = "真的要" + (type === "real" ? "永久删除文件么?" : "还原配置文件么?");
} else {
msg = "真的要" + (type === "real" ? "永久" : "临时") + "删除文件么?";
}
this.$confirm({
title: "系统提示",
content: "真的要删除文件么?",
content: msg,
okText: "确认",
cancelText: "取消",
onOk: () => {
@ -310,6 +329,8 @@ export default {
nodeId: this.node.id,
path: record.path,
name: record.relativePath,
type: type,
from: from,
};
//
deleteNginxConfig(params).then((res) => {