判断仓库是否存在,db server 调整

This commit is contained in:
bwcx_jzy 2021-08-13 13:05:57 +08:00
parent 1675b565ee
commit c67284ff85
13 changed files with 220 additions and 193 deletions

View File

@ -1,6 +1,7 @@
package io.jpom.common;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.extra.servlet.ServletUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
@ -96,13 +97,13 @@ public class GlobalDefaultExceptionHandler {
}
/**
* 声明要捕获的异常 (参数或者状态异常)
* 声明要捕获的异常 (参数状态验证异常)
*
* @param request 请求
* @param response 响应
* @param e 异常
*/
@ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class})
@ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class, ValidateException.class})
public void paramExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
DefaultSystemLog.getLog().error("controller " + request.getRequestURI(), e);
ServletUtil.write(response, JsonMessage.getString(405, e.getMessage()), MediaType.APPLICATION_JSON_VALUE);

View File

@ -1,7 +1,6 @@
package io.jpom.controller.build;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
@ -17,6 +16,7 @@ import io.jpom.plugin.ClassFeature;
import io.jpom.plugin.Feature;
import io.jpom.plugin.MethodFeature;
import io.jpom.service.dblog.RepositoryService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@ -68,21 +68,37 @@ public class RepositoryController {
@PostMapping(value = "/build/repository/edit")
@Feature(method = MethodFeature.EDIT)
public Object editRepository(RepositoryModel repositoryModelReq) {
this.checkInfo(repositoryModelReq);
if (null == repositoryModelReq.getId()) {
// insert data
if (null == repositoryModelReq.getModifyTime()) {
repositoryModelReq.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss"));
}
repositoryModelReq.setId(IdUtil.fastSimpleUUID());
repositoryService.insert(repositoryModelReq);
} else {
// update data
repositoryModelReq.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss"));
repositoryService.updateById(repositoryModelReq);
}
return JsonMessage.toJson(200, "操作成功");
}
/**
* 检查信息
*
* @param repositoryModelReq 仓库信息
*/
private void checkInfo(RepositoryModel repositoryModelReq) {
Assert.hasText(repositoryModelReq.getName(), "请填写仓库名称");
Integer repoType = repositoryModelReq.getRepoType();
Assert.state(repoType != null && (repoType == 1 || repoType == 0), "请选择仓库类型");
Assert.hasText(repositoryModelReq.getGitUrl(), "请填写仓库地址");
// 判断仓库是否重复
Entity entity = Entity.create();
if (repositoryModelReq.getId() != null) {
Validator.validateGeneral(repositoryModelReq.getId(), "错误的ID");
entity.set("id", "<> " + repositoryModelReq.getId());
}
entity.set("gitUrl", repositoryModelReq.getGitUrl());
Assert.state(!repositoryService.exists(entity), "已经存在对应的仓库信息啦");
}
/**
* delete
*
@ -92,7 +108,7 @@ public class RepositoryController {
@PostMapping(value = "/build/repository/delete")
@Feature(method = MethodFeature.DEL)
public Object delRepository(String id) {
repositoryService.deleteById(id);
repositoryService.delByKey(id);
return JsonMessage.getString(200, "删除成功");
}
}

View File

@ -2,8 +2,9 @@ package io.jpom.model.log;
import cn.hutool.core.util.StrUtil;
import io.jpom.JpomApplication;
import io.jpom.model.BaseJsonModel;
import io.jpom.model.BaseDbModel;
import io.jpom.model.data.UserModel;
import io.jpom.service.h2db.TableName;
/**
* ssh 终端执行日志
@ -11,16 +12,8 @@ import io.jpom.model.data.UserModel;
* @author jiangzeyin
* @date 2021/08/04
*/
public class SshTerminalExecuteLog extends BaseJsonModel {
/**
* 表名
*/
public static final String TABLE_NAME = SshTerminalExecuteLog.class.getSimpleName().toUpperCase();
/**
* 数据id
*/
private String id;
@TableName("SSHTERMINALEXECUTELOG")
public class SshTerminalExecuteLog extends BaseDbModel {
/**
* 操作ip
*/
@ -123,13 +116,6 @@ public class SshTerminalExecuteLog extends BaseJsonModel {
this.commands = commands;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSshName() {
return sshName;

View File

@ -38,8 +38,7 @@ public class DbBuildHistoryLogService extends BaseDbCommonService<BuildHistoryLo
private BuildService buildService;
public DbBuildHistoryLogService() {
super(BuildHistoryLog.TABLE_NAME, BuildHistoryLog.class);
setKey("id");
super(BuildHistoryLog.TABLE_NAME, "id", BuildHistoryLog.class);
}
/**

View File

@ -15,35 +15,34 @@ import org.springframework.stereotype.Service;
@Service
public class DbMonitorNotifyLogService extends BaseDbCommonService<MonitorNotifyLog> {
public DbMonitorNotifyLogService() {
super(MonitorNotifyLog.TABLE_NAME, MonitorNotifyLog.class);
setKey("logId");
}
public DbMonitorNotifyLogService() {
super(MonitorNotifyLog.TABLE_NAME, "logId", MonitorNotifyLog.class);
}
@Override
public void insert(MonitorNotifyLog monitorNotifyLog) {
super.insert(monitorNotifyLog);
//
DbConfig.autoClear(getTableName(), "createTime");
}
@Override
public void insert(MonitorNotifyLog monitorNotifyLog) {
super.insert(monitorNotifyLog);
//
DbConfig.autoClear(getTableName(), "createTime");
}
/**
* 修改执行结果
*
* @param logId 通知id
* @param status 状态
* @param errorMsg 错误消息
*/
public void updateStatus(String logId, boolean status, String errorMsg) {
Entity entity = new Entity();
entity.set("notifyStatus", status);
if (errorMsg != null) {
entity.set("notifyError", errorMsg);
}
//
Entity where = new Entity();
where.set("logId", logId);
super.update(entity, where);
}
/**
* 修改执行结果
*
* @param logId 通知id
* @param status 状态
* @param errorMsg 错误消息
*/
public void updateStatus(String logId, boolean status, String errorMsg) {
Entity entity = new Entity();
entity.set("notifyStatus", status);
if (errorMsg != null) {
entity.set("notifyError", errorMsg);
}
//
Entity where = new Entity();
where.set("logId", logId);
super.update(entity, where);
}
}

View File

@ -15,30 +15,29 @@ import org.springframework.stereotype.Service;
@Service
public class DbOutGivingLogService extends BaseDbCommonService<OutGivingLog> {
public DbOutGivingLogService() {
super(OutGivingLog.TABLE_NAME, OutGivingLog.class);
setKey("id");
}
public DbOutGivingLogService() {
super(OutGivingLog.TABLE_NAME, "id", OutGivingLog.class);
}
@Override
public void insert(OutGivingLog outGivingLog) {
outGivingLog.setStartTime(System.currentTimeMillis());
if (outGivingLog.getStatus() == OutGivingNodeProject.Status.Cancel.getCode()) {
outGivingLog.setEndTime(System.currentTimeMillis());
}
super.insert(outGivingLog);
}
@Override
public void insert(OutGivingLog outGivingLog) {
outGivingLog.setStartTime(System.currentTimeMillis());
if (outGivingLog.getStatus() == OutGivingNodeProject.Status.Cancel.getCode()) {
outGivingLog.setEndTime(System.currentTimeMillis());
}
super.insert(outGivingLog);
}
@Override
public int update(OutGivingLog outGivingLog) {
Entity entity = new Entity();
entity.set("status", outGivingLog.getStatus());
// 结束
entity.set("endTime", System.currentTimeMillis());
entity.set("result", outGivingLog.getResult());
//
Entity where = new Entity();
where.set("id", outGivingLog.getId());
return super.update(entity, where);
}
@Override
public int update(OutGivingLog outGivingLog) {
Entity entity = new Entity();
entity.set("status", outGivingLog.getStatus());
// 结束
entity.set("endTime", System.currentTimeMillis());
entity.set("result", outGivingLog.getResult());
//
Entity where = new Entity();
where.set("id", outGivingLog.getId());
return super.update(entity, where);
}
}

View File

@ -13,15 +13,14 @@ import org.springframework.stereotype.Service;
@Service
public class DbSystemMonitorLogService extends BaseDbCommonService<SystemMonitorLog> {
public DbSystemMonitorLogService() {
super(SystemMonitorLog.TABLE_NAME, SystemMonitorLog.class);
setKey("id");
}
public DbSystemMonitorLogService() {
super(SystemMonitorLog.TABLE_NAME, "id", SystemMonitorLog.class);
}
public PageResult<SystemMonitorLog> getMonitorData(long startTime, long endTime) {
Entity entity = new Entity(SystemMonitorLog.TABLE_NAME);
entity.set(" MONITORTIME", ">= " + startTime);
entity.set("MONITORTIME", "<= " + endTime);
return listPage(entity, null);
}
public PageResult<SystemMonitorLog> getMonitorData(long startTime, long endTime) {
Entity entity = new Entity(SystemMonitorLog.TABLE_NAME);
entity.set(" MONITORTIME", ">= " + startTime);
entity.set("MONITORTIME", "<= " + endTime);
return listPage(entity, null);
}
}

View File

@ -28,79 +28,78 @@ import java.util.List;
@Service
public class DbUserOperateLogService extends BaseDbCommonService<UserOperateLogV1> {
private final MonitorUserOptService monitorUserOptService;
private final UserService userService;
private final BuildService buildService;
private final MonitorUserOptService monitorUserOptService;
private final UserService userService;
private final BuildService buildService;
public DbUserOperateLogService(MonitorUserOptService monitorUserOptService,
UserService userService,
BuildService buildService) {
super(UserOperateLogV1.TABLE_NAME, UserOperateLogV1.class);
this.monitorUserOptService = monitorUserOptService;
this.userService = userService;
this.buildService = buildService;
setKey("reqId");
}
public DbUserOperateLogService(MonitorUserOptService monitorUserOptService,
UserService userService,
BuildService buildService) {
super(UserOperateLogV1.TABLE_NAME, "reqId", UserOperateLogV1.class);
this.monitorUserOptService = monitorUserOptService;
this.userService = userService;
this.buildService = buildService;
}
@Override
public void insert(UserOperateLogV1 userOperateLogV1) {
super.insert(userOperateLogV1);
DbConfig.autoClear(getTableName(), "optTime");
ThreadUtil.execute(() -> {
UserOperateLogV1.OptType optType = BaseEnum.getEnum(UserOperateLogV1.OptType.class, userOperateLogV1.getOptType());
if (optType == null) {
return;
}
UserModel optUserItem = userService.getItem(userOperateLogV1.getUserId());
if (optUserItem == null) {
return;
}
String otherMsg = "";
if (optType == UserOperateLogV1.OptType.StartBuild || optType == UserOperateLogV1.OptType.EditBuild) {
BuildModel item = buildService.getItem(userOperateLogV1.getDataId());
if (item != null) {
otherMsg = StrUtil.format("操作的构建名称:{}\n", item.getName());
}
}
List<MonitorUserOptModel> monitorUserOptModels = monitorUserOptService.listByType(optType, userOperateLogV1.getUserId());
if (CollUtil.isEmpty(monitorUserOptModels)) {
return;
}
for (MonitorUserOptModel monitorUserOptModel : monitorUserOptModels) {
List<String> notifyUser = monitorUserOptModel.getNotifyUser();
if (CollUtil.isEmpty(notifyUser)) {
continue;
}
for (String userId : notifyUser) {
UserModel item = userService.getItem(userId);
if (item == null) {
continue;
}
//
String context = StrUtil.format("操作用户:{}\n操作状态{}\n操作类型{}\n操作节点{}\n 操作数据id: {}\n操作IP: {}\n{}",
optUserItem.getName(), userOperateLogV1.getOptStatusMsg(), userOperateLogV1.getOptTypeMsg(),
userOperateLogV1.getNodeId(), userOperateLogV1.getDataId(), userOperateLogV1.getIp(), otherMsg);
// 邮箱
String email = item.getEmail();
if (StrUtil.isNotEmpty(email)) {
MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.mail, email);
ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context));
@Override
public void insert(UserOperateLogV1 userOperateLogV1) {
super.insert(userOperateLogV1);
DbConfig.autoClear(getTableName(), "optTime");
ThreadUtil.execute(() -> {
UserOperateLogV1.OptType optType = BaseEnum.getEnum(UserOperateLogV1.OptType.class, userOperateLogV1.getOptType());
if (optType == null) {
return;
}
UserModel optUserItem = userService.getItem(userOperateLogV1.getUserId());
if (optUserItem == null) {
return;
}
String otherMsg = "";
if (optType == UserOperateLogV1.OptType.StartBuild || optType == UserOperateLogV1.OptType.EditBuild) {
BuildModel item = buildService.getItem(userOperateLogV1.getDataId());
if (item != null) {
otherMsg = StrUtil.format("操作的构建名称:{}\n", item.getName());
}
}
List<MonitorUserOptModel> monitorUserOptModels = monitorUserOptService.listByType(optType, userOperateLogV1.getUserId());
if (CollUtil.isEmpty(monitorUserOptModels)) {
return;
}
for (MonitorUserOptModel monitorUserOptModel : monitorUserOptModels) {
List<String> notifyUser = monitorUserOptModel.getNotifyUser();
if (CollUtil.isEmpty(notifyUser)) {
continue;
}
for (String userId : notifyUser) {
UserModel item = userService.getItem(userId);
if (item == null) {
continue;
}
//
String context = StrUtil.format("操作用户:{}\n操作状态{}\n操作类型{}\n操作节点{}\n 操作数据id: {}\n操作IP: {}\n{}",
optUserItem.getName(), userOperateLogV1.getOptStatusMsg(), userOperateLogV1.getOptTypeMsg(),
userOperateLogV1.getNodeId(), userOperateLogV1.getDataId(), userOperateLogV1.getIp(), otherMsg);
// 邮箱
String email = item.getEmail();
if (StrUtil.isNotEmpty(email)) {
MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.mail, email);
ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context));
}
// dingding
String dingDing = item.getDingDing();
if (StrUtil.isNotEmpty(dingDing)) {
MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.dingding, dingDing);
ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context));
}
// 企业微信
String workWx = item.getWorkWx();
if (StrUtil.isNotEmpty(workWx)) {
MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.workWx, workWx);
ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context));
}
}
}
});
}
}
// dingding
String dingDing = item.getDingDing();
if (StrUtil.isNotEmpty(dingDing)) {
MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.dingding, dingDing);
ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context));
}
// 企业微信
String workWx = item.getWorkWx();
if (StrUtil.isNotEmpty(workWx)) {
MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.workWx, workWx);
ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context));
}
}
}
});
}
}

View File

@ -1,5 +1,6 @@
package io.jpom.service.dblog;
import cn.hutool.core.date.LocalDateTimeUtil;
import io.jpom.model.data.RepositoryModel;
import io.jpom.service.h2db.BaseDbService;
import org.springframework.stereotype.Service;
@ -11,5 +12,17 @@ import org.springframework.stereotype.Service;
@Service
public class RepositoryService extends BaseDbService<RepositoryModel> {
@Override
public void insert(RepositoryModel repositoryModelReq) {
if (null == repositoryModelReq.getModifyTime()) {
repositoryModelReq.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss"));
}
super.insert(repositoryModelReq);
}
@Override
public void updateById(RepositoryModel info) {
info.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss"));
super.updateById(info);
}
}

View File

@ -1,12 +1,11 @@
package io.jpom.service.dblog;
import cn.hutool.core.date.SystemClock;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import io.jpom.model.data.SshModel;
import io.jpom.model.data.UserModel;
import io.jpom.model.log.SshTerminalExecuteLog;
import io.jpom.service.h2db.BaseDbCommonService;
import io.jpom.service.h2db.BaseDbService;
import org.springframework.stereotype.Service;
import java.util.List;
@ -20,12 +19,7 @@ import java.util.stream.Collectors;
* @date 2021/08/04
*/
@Service
public class SshTerminalExecuteLogService extends BaseDbCommonService<SshTerminalExecuteLog> {
public SshTerminalExecuteLogService() {
super(SshTerminalExecuteLog.TABLE_NAME, SshTerminalExecuteLog.class);
setKey("id");
}
public class SshTerminalExecuteLogService extends BaseDbService<SshTerminalExecuteLog> {
/**
* 批量记录日志
@ -41,7 +35,7 @@ public class SshTerminalExecuteLogService extends BaseDbCommonService<SshTermina
long optTime = SystemClock.now();
List<SshTerminalExecuteLog> executeLogs = commands.stream().filter(StrUtil::isNotEmpty).map(s -> {
SshTerminalExecuteLog sshTerminalExecuteLog = new SshTerminalExecuteLog();
sshTerminalExecuteLog.setId(IdUtil.fastSimpleUUID());
//sshTerminalExecuteLog.setId(IdUtil.fastSimpleUUID());
if (sshItem != null) {
sshTerminalExecuteLog.setSshId(sshItem.getId());
sshTerminalExecuteLog.setSshName(sshItem.getName());

View File

@ -38,17 +38,19 @@ public abstract class BaseDbCommonService<T> {
/**
* 主键
*/
private String key;
private final String key;
public BaseDbCommonService(String tableName, Class<T> tClass) {
public BaseDbCommonService(String tableName, String key, Class<T> tClass) {
this.tableName = this.covetTableName(tableName, tClass);
this.tClass = tClass;
this.key = key;
}
@SuppressWarnings("unchecked")
public BaseDbCommonService(String tableName) {
public BaseDbCommonService(String tableName, String key) {
this.tClass = (Class<T>) TypeUtil.getTypeArgument(this.getClass());
this.tableName = this.covetTableName(tableName, this.tClass);
this.key = key;
}
@ -60,8 +62,8 @@ public abstract class BaseDbCommonService<T> {
return tableName;
}
protected void setKey(String key) {
this.key = key;
protected String getKey() {
return key;
}
/**
@ -215,6 +217,29 @@ public abstract class BaseDbCommonService<T> {
}
}
/**
* 判断是否存在
*
* @param where 条件
* @return true 存在
*/
public boolean exists(Entity where) {
if (!DbConfig.getInstance().isInit()) {
// ignore
return false;
}
where.setTableName(getTableName());
Db db = Db.use();
db.setWrapper((Character) null);
long count;
try {
count = db.count(where);
} catch (SQLException e) {
throw new JpomRuntimeException("数据库异常", e);
}
return count > 0;
}
/**
* 分页查询
*

View File

@ -1,6 +1,7 @@
package io.jpom.service.h2db;
import cn.hutool.core.date.SystemClock;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.Entity;
import io.jpom.common.Const;
@ -18,8 +19,7 @@ import java.util.Collection;
public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonService<T> {
public BaseDbService() {
super(null);
setKey(Const.ID_STR);
super(null, Const.ID_STR);
}
@Override
@ -33,6 +33,7 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
public void insert(T t) {
// def create time
t.setCreateTimeMillis(ObjectUtil.defaultIfNull(t.getCreateTimeMillis(), SystemClock.now()));
t.setId(ObjectUtil.defaultIfNull(t.getId(), IdUtil.fastSimpleUUID()));
super.insert(t);
}
@ -40,7 +41,10 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
@Override
public void insert(Collection<T> t) {
// def create time
t.forEach(t1 -> t1.setCreateTimeMillis(ObjectUtil.defaultIfNull(t1.getCreateTimeMillis(), SystemClock.now())));
t.forEach(t1 -> {
t1.setCreateTimeMillis(ObjectUtil.defaultIfNull(t1.getCreateTimeMillis(), SystemClock.now()));
t1.setId(ObjectUtil.defaultIfNull(t1.getId(), IdUtil.fastSimpleUUID()));
});
super.insert(t);
}
@ -59,16 +63,4 @@ public abstract class BaseDbService<T extends BaseDbModel> extends BaseDbCommonS
where.set(Const.ID_STR, info.getId());
super.update(entity, where);
}
/**
* delete by id
*
* @param id 主键
*/
public void deleteById(String id) {
Entity where = new Entity(getTableName());
where.set(Const.ID_STR, id);
del(where);
}
}

View File

@ -122,3 +122,8 @@ CREATE TABLE IF NOT EXISTS PUBLIC.SSHTERMINALEXECUTELOG
CONSTRAINT SSHTERMINALEXECUTELOG_PK PRIMARY KEY (ID)
);
COMMENT ON TABLE SSHTERMINALEXECUTELOG is 'ssh 终端操作记录表';
ALTER TABLE SSHTERMINALEXECUTELOG
ADD IF NOT EXISTS CREATETIMEMILLIS BIGINT COMMENT '数据创建时间';
ALTER TABLE SSHTERMINALEXECUTELOG
ADD IF NOT EXISTS MODIFYTIMEMILLIS BIGINT COMMENT '数据修改时间';