运行状态

This commit is contained in:
jiangzeyin 2018-09-29 15:48:14 +08:00
parent b56e5fa55b
commit ebc5f261e3
7 changed files with 164 additions and 116 deletions

View File

@ -1,12 +1,17 @@
package cn.jiangzeyin.controller.manage;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.controller.BaseController;
import cn.jiangzeyin.model.ProjectInfoModel;
import cn.jiangzeyin.oss.OssManagerService;
import cn.jiangzeyin.service.manage.ManageService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import java.io.IOException;
/**
* Created by jiangzeyin on 2018/9/29.
@ -18,9 +23,16 @@ public class BuildController extends BaseController {
@Resource
private OssManagerService ossManagerService;
@RequestMapping(value = "build", method = RequestMethod.GET)
public String console(String id) {
@Resource
private ManageService manageService;
@RequestMapping(value = "build", method = RequestMethod.GET)
public String console(String id) throws IOException {
ProjectInfoModel projectInfoModel = manageService.getProjectInfo(id);
if (projectInfoModel != null && StrUtil.isNotEmpty(projectInfoModel.getBuildTag())) {
JSONArray jsonArray = ossManagerService.list(projectInfoModel.getBuildTag());
setAttribute("array", jsonArray);
}
return "manage/build";
}
}

View File

@ -1,14 +1,11 @@
package cn.jiangzeyin.controller.manage;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.common.PageUtil;
import cn.jiangzeyin.common.interceptor.LoginInterceptor;
import cn.jiangzeyin.controller.base.AbstractBaseControl;
import cn.jiangzeyin.model.ProjectInfoModel;
import cn.jiangzeyin.service.manage.CommandService;
import cn.jiangzeyin.service.manage.ManageService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@ -27,6 +24,8 @@ public class ManageControl extends AbstractBaseControl {
@Resource
private ManageService manageService;
@Resource
private CommandService commandService;
/**
@ -54,7 +53,12 @@ public class ManageControl extends AbstractBaseControl {
JSONArray array = new JSONArray();
Set<String> setKey = json.keySet();
for (String asetKey : setKey) {
array.add(json.get(asetKey));
ProjectInfoModel projectInfoModel = manageService.getProjectInfo(asetKey);
String result = commandService.execCommand(CommandService.CommandOp.status, projectInfoModel, null);
JSONObject jsonObject = json.getJSONObject(asetKey);
boolean status = result.contains(CommandService.RUNING_TAG);
jsonObject.put("status", status);
array.add(jsonObject);
}
return PageUtil.getPaginate(200, "查询成功!", array);
} catch (IOException e) {
@ -81,7 +85,4 @@ public class ManageControl extends AbstractBaseControl {
return JsonMessage.getString(500, e.getMessage());
}
}
}

View File

@ -1,10 +1,12 @@
package cn.jiangzeyin.oss;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.jiangzeyin.common.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
@ -22,7 +24,7 @@ import java.util.List;
@Service
public class OssManagerService {
public List<OSSObjectSummary> list(String name) {
public JSONArray list(String name) {
OSSClient ossClient;
String prefix = String.format("%s%s", getKeyPrefix(), name);
ListObjectsRequest request;
@ -37,17 +39,24 @@ public class OssManagerService {
request.setMarker(nextMarker);
ObjectListing objectListing = ossClient.listObjects(request);
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
sums.forEach(ossObjectSummary -> {
String newKey = ossObjectSummary.getKey().substring(prefix.length());
ossObjectSummary.setKey(newKey);
});
//
summaryList.addAll(sums);
nextMarker = objectListing.getNextMarker();
// 关闭OSSClient
ossClient.shutdown();
} while (nextMarker != null);
summaryList.sort((o1, o2) -> o2.getLastModified().compareTo(o1.getLastModified()));
return summaryList;
JSONArray jsonArray = new JSONArray();
summaryList.forEach(ossObjectSummary -> {
JSONObject jsonObject = new JSONObject();
String newKey = ossObjectSummary.getKey().substring(prefix.length());
jsonObject.put("shortKey", newKey);
jsonObject.put("key", ossObjectSummary.getKey());
jsonObject.put("time", DateUtil.date(ossObjectSummary.getLastModified()).toString());
jsonObject.put("size", FileUtil.readableFileSize(ossObjectSummary.getSize()));
jsonArray.add(jsonObject);
});
return jsonArray;
}
private OSSClient getOSSClient() {

View File

@ -1,16 +1,36 @@
package cn.jiangzeyin.service.manage;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.spring.SpringUtil;
import cn.jiangzeyin.model.ProjectInfoModel;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by jiangzeyin on 2018/9/28.
*/
@Service
public class CommandService {
public static final String RUNING_TAG = "running";
public static final String STOP_TAG = "stopped";
public enum CommandOp {
/**
* 启动
*/
start,
stop,
restart,
status,
showlog
}
public File getCommandFile() {
File file = new File(getCommandPath());
@ -20,11 +40,66 @@ public class CommandService {
return file;
}
public String getCommandPath() {
private String getCommandPath() {
String command = SpringUtil.getEnvironment().getProperty("boot-online.command");
if (StrUtil.isEmpty(command)) {
throw new RuntimeException("请配置命令文件");
}
return command;
}
/**
* 执行shell命令
*
* @param commandOp 执行的操作
* @param projectInfoModel 项目信息
*/
public String execCommand(CommandOp commandOp, ProjectInfoModel projectInfoModel, Evt evt) {
String result = "error";
if (commandOp == CommandOp.showlog) {
return result;
}
InputStream is;
CommandService commandService = SpringUtil.getBean(CommandService.class);
String commandPath = commandService.getCommandPath();
// 项目启动信息
String tag = projectInfoModel.getTag();
String mainClass = projectInfoModel.getMainClass();
String lib = projectInfoModel.getLib();
String log = projectInfoModel.getLog();
String token = projectInfoModel.getToken();
String jvm = projectInfoModel.getJvm();
String args = projectInfoModel.getArgs();
try {
// 执行命令
String command = String.format("%s %s %s %s %s %s %s [%s][%s]", commandPath, commandOp.toString(), tag, mainClass, lib, log, token, jvm, args);
DefaultSystemLog.LOG().info(command);
Process process = Runtime.getRuntime().exec(command);
int wait = process.waitFor();
if (wait == 0) {
is = process.getInputStream();
} else {
is = process.getErrorStream();
}
result = IoUtil.read(is, CharsetUtil.CHARSET_UTF_8);
is.close();
process.destroy();
} catch (IOException | InterruptedException e) {
DefaultSystemLog.ERROR().error("执行命令异常", e);
if (evt != null) {
evt.error(e);
}
}
return result;
}
public interface Evt {
/**
* 执行异常
*
* @param e e
*/
void error(Exception e);
}
}

View File

@ -29,15 +29,15 @@ import java.util.concurrent.ExecutorService;
*/
@ServerEndpoint(value = "/console/{userInfo}")
@Component
public class LogWebSocketHandle implements TailLogThread.Evn {
public class LogWebSocketHandle implements TailLogThread.Evn, CommandService.Evt {
private static final String RUNING_TAG = "running";
private static final String STOP_TAG = "stopped";
private static ExecutorService EXECUTOR_SERVICE = null;
private Process process;
private InputStream inputStream;
private TailLogThread thread;
private CommandService commandService;
private Session session;
/**
* 新的WebSocket请求开启
@ -47,6 +47,7 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
if (EXECUTOR_SERVICE == null) {
EXECUTOR_SERVICE = ThreadPoolService.newCachedThreadPool(LogWebSocketHandle.class);
}
commandService = SpringUtil.getBean(CommandService.class);
// 通过用户名和密码的Md5值判断是否是登录的
try {
UserService userService = SpringUtil.getBean(UserService.class);
@ -54,6 +55,7 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
sendMsg(session, JsonMessage.getString(500, "用户名或密码错误!"));
session.close();
}
this.session = session;
} catch (Exception e) {
DefaultSystemLog.ERROR().error(e.getMessage());
try {
@ -85,33 +87,25 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
return;
}
String op = json.getString("op");
CommandService.CommandOp commandOp = CommandService.CommandOp.valueOf(op);
JSONObject resultData = null;
String strResult;
// 执行相应命令
switch (op) {
case "start":
// 启动项目
strResult = execCommand(session, "start", projectInfoModel);
if (strResult.contains(RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "启动成功", json);
switch (commandOp) {
case start:
case restart:
strResult = commandService.execCommand(commandOp, projectInfoModel, this);
if (strResult.contains(CommandService.RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "操作成功", json);
} else {
resultData = JsonMessage.toJson(400, strResult, json);
}
break;
case "restart":
// 重启项目
strResult = execCommand(session, "restart", projectInfoModel);
if (strResult.contains(RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "重启成功", json);
} else {
resultData = JsonMessage.toJson(400, strResult, json);
}
break;
case "stop":
case stop:
// 停止项目
strResult = execCommand(session, "stop", projectInfoModel);
if (strResult.contains(STOP_TAG)) {
resultData = JsonMessage.toJson(200, "已停止", json);
strResult = commandService.execCommand(commandOp, projectInfoModel, this);
if (strResult.contains(CommandService.STOP_TAG)) {
resultData = JsonMessage.toJson(200, "操作成功", json);
if (thread != null) {
thread.stop();
}
@ -119,17 +113,17 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
resultData = JsonMessage.toJson(500, strResult, json);
}
break;
case "status":
case status:
// 获取项目状态
strResult = execCommand(session, "status", projectInfoModel);
strResult = commandService.execCommand(commandOp, projectInfoModel, this);
json.put("result", strResult);
if (strResult.contains(RUNING_TAG)) {
if (strResult.contains(CommandService.RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "运行中", json);
} else {
resultData = JsonMessage.toJson(404, "未运行", json);
}
break;
case "showlog":
case showlog:
showLog(session, projectInfoModel);
break;
default:
@ -167,48 +161,6 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
}
/**
* 执行shell命令
*
* @param session 用于输出的websocket会话
* @param op 执行的操作
* @param projectInfoModel 项目信息
*/
private String execCommand(Session session, String op, ProjectInfoModel projectInfoModel) {
InputStream is;
String result = "error";
CommandService commandService = SpringUtil.getBean(CommandService.class);
String commandPath = commandService.getCommandPath();
// 项目启动信息
String tag = projectInfoModel.getTag();
String mainClass = projectInfoModel.getMainClass();
String lib = projectInfoModel.getLib();
String log = projectInfoModel.getLog();
String token = projectInfoModel.getToken();
String jvm = projectInfoModel.getJvm();
String args = projectInfoModel.getArgs();
try {
// 执行命令
String command = String.format("%s %s %s %s %s %s %s [%s][%s]", commandPath, op, tag, mainClass, lib, log, token, jvm, args);
DefaultSystemLog.LOG().info(command);
Process process = Runtime.getRuntime().exec(command);
int wait = process.waitFor();
if (wait == 0) {
is = process.getInputStream();
} else {
is = process.getErrorStream();
}
result = IoUtil.read(is, CharsetUtil.CHARSET_UTF_8);
is.close();
process.destroy();
} catch (IOException | InterruptedException e) {
DefaultSystemLog.ERROR().error("执行命令异常", e);
sendMsg(session, "执行命令异常:" + ExceptionUtil.stacktraceToString(e));
}
return result;
}
/**
* WebSocket请求关闭
*/
@ -258,6 +210,14 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
onClose();
}
@Override
public void error(Exception e) {
if (session == null) {
return;
}
sendMsg(session, "执行命令异常:" + ExceptionUtil.stacktraceToString(e));
}
public static void main(String[] args) {
try {
String command = "/boot-line/command/run_boot.sh status Testteststesttst cn.jiangzeyin.BootOnLineApplication /boot-line/boot/online/lib /boot-line/boot/online/run.log no";
@ -277,4 +237,6 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
}
}
}

View File

@ -4,6 +4,11 @@
<head>
#parse("./common/head.vm")
<title>项目管理系统</title>
<style>
body {
padding: 20px;
}
</style>
</head>
<body>
@ -18,36 +23,16 @@
</tr>
</thead>
<tbody>
<tr>
<td>贤心</td>
<td>汉族</td>
<td>1989-10-14</td>
<td>人生似修行</td>
</tr>
<tr>
<td>张爱玲</td>
<td>汉族</td>
<td>1920-09-30</td>
<td>于千万人之中遇见你所遇见的人,于千万年之中,时间的无涯的荒野里…</td>
</tr>
<tr>
<td>Helen Keller</td>
<td>拉丁美裔</td>
<td>1880-06-27</td>
<td> Life is either a daring adventure or nothing.</td>
</tr>
<tr>
<td>岳飞</td>
<td>汉族</td>
<td>1103-北宋崇宁二年</td>
<td>教科书再滥改,也抹不去“民族英雄”的事实</td>
</tr>
<tr>
<td>孟子</td>
<td>华夏族(汉族)</td>
<td>公元前-372年</td>
<td>猿强,则国强。国强,则猿更强!</td>
</tr>
#foreach($item in $array)
<tr>
<td>$item.shortKey</td>
<td>$item.time</td>
<td>$item.size</td>
<td>
<button class="layui-btn layui-btn-normal layui-btn-sm">安装</button>
</td>
</tr>
#end
</tbody>
</table>
</div>

View File

@ -30,6 +30,10 @@
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn" lay-event="build">构建</a>
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
</script>
<script type="text/html" id="status_templ">
<input type="checkbox" disabled name="status" {{# if(d.status){ }} checked {{# } }} lay-skin="switch"
lay-text="运行中|未运行">
</script>
<script type="text/javascript">
var table;
layui.use(['layer', 'element', 'table', 'form'], function () {
@ -46,7 +50,7 @@
even: true,
cols: [[
{field: 'id', title: '项目名称'},
{field: 'modifyTime', title: '运行状态', width: '10%'},
{title: '运行状态', width: '10%', templet: "#status_templ"},
{field: 'createTime', title: '创建时间', width: '15%'},
{field: 'modifyTime', title: '修改时间', width: '15%'},
{field: 'op', title: '操作', toolbar: '#bar_projects'}