mirror of
https://gitee.com/dromara/Jpom.git
synced 2024-11-30 19:08:18 +08:00
nginx启动管理
This commit is contained in:
parent
9967612844
commit
a06b9982ee
@ -1,8 +1,10 @@
|
||||
package cn.keepbx.jpom.controller.system;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.text.StrSpliter;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import cn.jiangzeyin.common.DefaultSystemLog;
|
||||
import cn.jiangzeyin.common.JsonMessage;
|
||||
import cn.keepbx.jpom.common.BaseAgentController;
|
||||
@ -216,4 +218,174 @@ public class NginxController extends BaseAgentController {
|
||||
String msg = this.reloadNginx();
|
||||
return JsonMessage.getString(200, "删除成功" + msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取nginx状态
|
||||
*/
|
||||
@RequestMapping(value = "status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String status() {
|
||||
JSONObject ngxConf = nginxService.getNgxConf();
|
||||
if (ngxConf.size() <= 0) {
|
||||
ngxConf.put("name", "nginx");
|
||||
ngxConf.put("status", "open");
|
||||
nginxService.save(ngxConf);
|
||||
}
|
||||
return JsonMessage.getString(200, "", getStatus(ngxConf.getString("name")));
|
||||
}
|
||||
|
||||
private JSONObject getStatus(String name) {
|
||||
JSONObject object = new JSONObject();
|
||||
object.put("status", "close");
|
||||
if (SystemUtil.getOsInfo().isWindows()) {
|
||||
String result = CommandUtil.execSystemCommand("sc query " + name);
|
||||
List<String> strings = StrSpliter.splitTrim(result, "\n", true);
|
||||
for (String string : strings) {
|
||||
string = string.toUpperCase();
|
||||
if (string.contains("STATE")) {
|
||||
if (string.contains("RUNNING")) {
|
||||
object.put("status", "open");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (SystemUtil.getOsInfo().isLinux()) {
|
||||
String format = StrUtil.format("service {} status", name);
|
||||
String result = CommandUtil.execCommand(format);
|
||||
List<String> strings = StrSpliter.splitTrim(result, "\n", true);
|
||||
for (String string : strings) {
|
||||
string = string.toUpperCase();
|
||||
if (string.contains("ACTIVE")) {
|
||||
if (string.contains("RUNNING")) {
|
||||
object.put("status", "open");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改nginx配置
|
||||
*/
|
||||
@RequestMapping(value = "updateConf", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String updateConf() {
|
||||
String name = getParameter("name");
|
||||
String status = getParameter("status");
|
||||
JSONObject ngxConf = nginxService.getNgxConf();
|
||||
boolean update = !status.equals(ngxConf.getString("status"));
|
||||
String name1 = ngxConf.getString("name");
|
||||
if (!name.equals(name1)) {
|
||||
update = true;
|
||||
boolean b = nginxService.updateServiceName(name, name1);
|
||||
if (!b) {
|
||||
return JsonMessage.getString(400, "修改服务名称失败");
|
||||
}
|
||||
}
|
||||
JSONObject object = getStatus(name1);
|
||||
if (!status.equals(object.getString("status"))) {
|
||||
if ("open".equals(status)) {
|
||||
open();
|
||||
} else if ("close".equals(status)) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
if (update) {
|
||||
ngxConf.put("name", name);
|
||||
ngxConf.put("status", status);
|
||||
nginxService.save(ngxConf);
|
||||
}
|
||||
return JsonMessage.getString(200, "修改成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*/
|
||||
@RequestMapping(value = "config", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String config() {
|
||||
JSONObject ngxConf = nginxService.getNgxConf();
|
||||
return JsonMessage.getString(200, "", ngxConf);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动nginx
|
||||
*/
|
||||
@RequestMapping(value = "open", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String open() {
|
||||
JSONObject ngxConf = nginxService.getNgxConf();
|
||||
String name = ngxConf.getString("name");
|
||||
String result = null;
|
||||
if (SystemUtil.getOsInfo().isWindows()) {
|
||||
String format = StrUtil.format("net start {}", name);
|
||||
result = CommandUtil.execSystemCommand(format);
|
||||
} else if (SystemUtil.getOsInfo().isLinux()) {
|
||||
String format = StrUtil.format("service {} start", name);
|
||||
result = CommandUtil.execSystemCommand(format);
|
||||
}
|
||||
return JsonMessage.getString(200, "nginx服务已启动:" + result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭nginx
|
||||
*/
|
||||
@RequestMapping(value = "close", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String close() {
|
||||
JSONObject ngxConf = nginxService.getNgxConf();
|
||||
String name = ngxConf.getString("name");
|
||||
String result = null;
|
||||
if (SystemUtil.getOsInfo().isWindows()) {
|
||||
String format = StrUtil.format("net stop {}", name);
|
||||
result = CommandUtil.execSystemCommand(format);
|
||||
} else if (SystemUtil.getOsInfo().isLinux()) {
|
||||
String format = StrUtil.format("service {} stop", name);
|
||||
result = CommandUtil.execSystemCommand(format);
|
||||
}
|
||||
return JsonMessage.getString(200, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载
|
||||
*/
|
||||
@RequestMapping(value = "reload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String reload() {
|
||||
if (SystemUtil.getOsInfo().isLinux()) {
|
||||
String result = CommandUtil.execSystemCommand("nginx -t");
|
||||
List<String> strings = StrSpliter.splitTrim(result, "\n", true);
|
||||
for (String str : strings) {
|
||||
if (!str.endsWith("successful") || !str.endsWith("ok")) {
|
||||
return JsonMessage.getString(400, result);
|
||||
}
|
||||
}
|
||||
CommandUtil.execSystemCommand("nginx -s reload");
|
||||
} else if (SystemUtil.getOsInfo().isWindows()) {
|
||||
JSONObject ngxConf = nginxService.getNgxConf();
|
||||
String name = ngxConf.getString("name");
|
||||
String result = CommandUtil.execSystemCommand("sc qc " + name);
|
||||
List<String> strings = StrSpliter.splitTrim(result, "\n", true);
|
||||
//服务路径
|
||||
String path = "";
|
||||
for (String str : strings) {
|
||||
str = str.toUpperCase().trim();
|
||||
if (str.startsWith("BINARY_PATH_NAME")) {
|
||||
path = str.substring(str.indexOf(":") + 1).replace("\"", "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (StrUtil.isEmpty(path)) {
|
||||
return JsonMessage.getString(400, "未找到nginx路径");
|
||||
}
|
||||
File file = FileUtil.file(path).getParentFile();
|
||||
result = CommandUtil.execSystemCommand("nginx -t", file);
|
||||
if (StrUtil.isNotEmpty(result)) {
|
||||
return JsonMessage.getString(400, result);
|
||||
}
|
||||
}
|
||||
return JsonMessage.getString(200, "重新加载成功");
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,14 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.system.SystemUtil;
|
||||
import cn.jiangzeyin.common.DefaultSystemLog;
|
||||
import cn.keepbx.jpom.common.BaseOperService;
|
||||
import cn.keepbx.jpom.model.data.AgentWhitelist;
|
||||
import cn.keepbx.jpom.service.WhitelistDirectoryService;
|
||||
import cn.keepbx.jpom.system.AgentConfigBean;
|
||||
import cn.keepbx.util.CommandUtil;
|
||||
import cn.keepbx.util.JsonFileUtil;
|
||||
import cn.keepbx.util.StringUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@ -224,4 +228,36 @@ public class NginxService extends BaseOperService {
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取nginx配置
|
||||
* name 修改后的服务名
|
||||
* status 状态:开启 open/ 关闭close
|
||||
*/
|
||||
public JSONObject getNgxConf() {
|
||||
JSONObject object = getJSONObject(AgentConfigBean.NGINX_CONF);
|
||||
return object == null ? new JSONObject() : object;
|
||||
}
|
||||
|
||||
public void save(JSONObject object) {
|
||||
String dataFilePath = getDataFilePath(AgentConfigBean.NGINX_CONF);
|
||||
JsonFileUtil.saveJson(dataFilePath, object);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改服务名称
|
||||
*
|
||||
* @param newName 新名称
|
||||
* @param oldName 旧名称
|
||||
*/
|
||||
public boolean updateServiceName(String newName, String oldName) {
|
||||
if (SystemUtil.getOsInfo().isWindows()) {
|
||||
String format = StrUtil.format("sc \\\\\\{} description {} {}", oldName, newName, newName);
|
||||
String result = CommandUtil.execSystemCommand(format);
|
||||
return !result.contains("失败");
|
||||
} else if (SystemUtil.getOsInfo().isLinux()) {
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -48,6 +48,11 @@ public class AgentConfigBean {
|
||||
*/
|
||||
public static final String SERVER_ID = "SERVER.json";
|
||||
|
||||
/**
|
||||
* nginx配置信息
|
||||
*/
|
||||
public static final String NGINX_CONF = "nginx_conf.json";
|
||||
|
||||
private static AgentConfigBean agentConfigBean;
|
||||
|
||||
/**
|
||||
|
@ -93,6 +93,13 @@ public enum NodeUrl {
|
||||
|
||||
System_Nginx_delete("/system/nginx/delete"),
|
||||
|
||||
System_Nginx_status("/system/nginx/status"),
|
||||
System_Nginx_config("/system/nginx/config"),
|
||||
System_Nginx_open("/system/nginx/open"),
|
||||
System_Nginx_close("/system/nginx/close"),
|
||||
System_Nginx_updateConf("/system/nginx/updateConf"),
|
||||
System_Nginx_reload("/system/nginx/reload"),
|
||||
|
||||
System_Certificate_saveCertificate("/system/certificate/saveCertificate"),
|
||||
System_Certificate_getCertList("/system/certificate/getCertList"),
|
||||
System_Certificate_delete("/system/certificate/delete"),
|
||||
|
@ -75,4 +75,56 @@ public class NginxController extends BaseServerController {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_delete).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取nginx状态
|
||||
*/
|
||||
@RequestMapping(value = "status", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String status() {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_status).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取nginx配置状态
|
||||
*/
|
||||
@RequestMapping(value = "config", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String config() {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_config).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动nginx
|
||||
*/
|
||||
@RequestMapping(value = "open", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String open() {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_open).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭nginx
|
||||
*/
|
||||
@RequestMapping(value = "close", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String close() {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_close).toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改nginx
|
||||
*/
|
||||
@RequestMapping(value = "updateConf", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String updateConf() {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_updateConf).toString();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "reload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
|
||||
@ResponseBody
|
||||
public String reload() {
|
||||
return NodeForward.request(getNode(), getRequest(), NodeUrl.System_Nginx_reload).toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,10 +9,41 @@
|
||||
<button id="addNgx" class="layui-btn layui-btn-sm">新增nginx配置</button>
|
||||
<button onclick="reload();" class="layui-btn layui-btn-sm">刷新表格</button>
|
||||
|
||||
<button id="reload" class="layui-btn layui-btn-sm layui-btn-danger">重启nginx</button>
|
||||
<button id="config" class="layui-btn layui-btn-sm">nginx配置</button>
|
||||
<button id="open" class="layui-btn layui-btn-sm layui-bg-blue" style="display: none">启动nginx</button>
|
||||
<button id="reload" class="layui-btn layui-btn-sm layui-bg-blue" style="display: none">重新加载nginx</button>
|
||||
<button id="close" class="layui-btn layui-btn-sm layui-bg-orange" style="display: none">停止nginx</button>
|
||||
</div>
|
||||
<table class="layui-table" id="tab_ngx" lay-filter="tab_ngx" style="margin: 0;"></table>
|
||||
|
||||
<div style="display: none;padding: 20px;" id="div_conf">
|
||||
<form action="./updateConf" class="layui-form" id="form_conf">
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label">服务名称</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" id="name" name="name" placeholder="服务名称" class="layui-input" required
|
||||
lay-verify="required">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-inline">
|
||||
<label class="layui-form-label">状态</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="radio" name="status" value="open" title="启动">
|
||||
<input type="radio" name="status" value="close" title="停止">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-input-block">
|
||||
<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
<script type="text/html" id="bar_ngx">
|
||||
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-warm" lay-event="update">编辑</a>
|
||||
@ -21,9 +52,9 @@
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
var index = 0;
|
||||
|
||||
function loadSuccess() {
|
||||
getStatus();
|
||||
|
||||
// 表格工具条事件
|
||||
table.on('tool(tab_ngx)', function (obj) {
|
||||
var data = obj.data;
|
||||
@ -97,6 +128,110 @@
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//nginx配置
|
||||
$('#config').on('click', function () {
|
||||
loadingAjax({
|
||||
url: './config',
|
||||
success: function (ret) {
|
||||
if (ret.code !== 200 || !ret.data) {
|
||||
layer.msg(ret.msg);
|
||||
return;
|
||||
}
|
||||
var data = ret.data;
|
||||
$('#name').val(data.name);
|
||||
$("input[value='" + data.status + "']").attr("checked", true);
|
||||
form.render();
|
||||
layer.open({
|
||||
type: 1,
|
||||
title: '编辑',
|
||||
content: $('#div_conf'),
|
||||
area: ['60%', '80%']
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//开启nginx
|
||||
$('#open').on('click', function () {
|
||||
loadingAjax({
|
||||
url: './open',
|
||||
success: function (ret) {
|
||||
if (ret.msg) {
|
||||
layer.msg(ret.msg);
|
||||
}
|
||||
getStatus();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//重新加载nginx
|
||||
$('#reload').on('click', function () {
|
||||
loadingAjax({
|
||||
url: './reload',
|
||||
success: function (ret) {
|
||||
if (ret.msg) {
|
||||
layer.msg(ret.msg);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//关闭nginx
|
||||
$('#close').on('click', function () {
|
||||
layer.confirm('确定停止nginx服务?', {
|
||||
title: '系统提示'
|
||||
}, function (index) {
|
||||
layer.close(index);
|
||||
loadingAjax({
|
||||
url: './close',
|
||||
success: function (ret) {
|
||||
if (ret.msg) {
|
||||
layer.msg(ret.msg);
|
||||
}
|
||||
getStatus();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//获取状态
|
||||
function getStatus() {
|
||||
loadingAjax({
|
||||
url: './status',
|
||||
success: function (ret) {
|
||||
if (ret.code !== 200) {
|
||||
layer.msg(ret.msg);
|
||||
return;
|
||||
}
|
||||
var data = ret.data;
|
||||
var isOpen = "open" === data.status;
|
||||
if (isOpen) {
|
||||
$('#close').css("display", "inline");
|
||||
$('#reload').css("display", "inline");
|
||||
$('#open').css("display", "none");
|
||||
} else {
|
||||
$('#open').css("display", "inline");
|
||||
$('#close').css("display", "none");
|
||||
$('#reload').css("display", "none");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//监听提交
|
||||
form.on('submit(formDemo)', function (data) {
|
||||
loadingAjax({
|
||||
url: './updateConf',
|
||||
data: data.field,
|
||||
success: function (ret) {
|
||||
getStatus();
|
||||
layer.msg(ret.msg);
|
||||
layer.closeAll('page');
|
||||
}
|
||||
});
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
function reload() {
|
||||
|
Loading…
Reference in New Issue
Block a user