org.dromara.jpom.controller.ssh.SshController Maven / Gradle / Ivy
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Code Technology Studio
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.dromara.jpom.controller.ssh;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.keepbx.jpom.IJsonMessage;
import cn.keepbx.jpom.model.JsonMessage;
import lombok.extern.slf4j.Slf4j;
import org.dromara.jpom.common.BaseServerController;
import org.dromara.jpom.common.validator.ValidatorItem;
import org.dromara.jpom.common.validator.ValidatorRule;
import org.dromara.jpom.func.assets.server.MachineSshServer;
import org.dromara.jpom.model.PageResultDto;
import org.dromara.jpom.model.data.NodeModel;
import org.dromara.jpom.model.data.SshModel;
import org.dromara.jpom.model.enums.BuildReleaseMethod;
import org.dromara.jpom.model.log.SshTerminalExecuteLog;
import org.dromara.jpom.permission.ClassFeature;
import org.dromara.jpom.permission.Feature;
import org.dromara.jpom.permission.MethodFeature;
import org.dromara.jpom.permission.SystemPermission;
import org.dromara.jpom.service.dblog.BuildInfoService;
import org.dromara.jpom.service.dblog.SshTerminalExecuteLogService;
import org.dromara.jpom.service.node.ssh.SshService;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author bwcx_jzy
* @since 2019/8/9
*/
@RestController
@RequestMapping(value = "node/ssh")
@Feature(cls = ClassFeature.SSH)
@Slf4j
public class SshController extends BaseServerController {
private final SshService sshService;
private final SshTerminalExecuteLogService sshTerminalExecuteLogService;
private final BuildInfoService buildInfoService;
private final MachineSshServer machineSshServer;
public SshController(SshService sshService,
SshTerminalExecuteLogService sshTerminalExecuteLogService,
BuildInfoService buildInfoService,
MachineSshServer machineSshServer) {
this.sshService = sshService;
this.sshTerminalExecuteLogService = sshTerminalExecuteLogService;
this.buildInfoService = buildInfoService;
this.machineSshServer = machineSshServer;
}
@PostMapping(value = "list_data.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listData(HttpServletRequest request) {
PageResultDto pageResultDto = sshService.listPage(request);
pageResultDto.each(sshModel -> {
sshModel.setMachineSsh(machineSshServer.getByKey(sshModel.getMachineSshId()));
List nodeBySshId = nodeService.getNodeBySshId(sshModel.getId());
sshModel.setLinkNode(CollUtil.getFirst(nodeBySshId));
});
return new JsonMessage<>(200, "", pageResultDto);
}
@GetMapping(value = "list_data_all.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listDataAll(HttpServletRequest request) {
List list = sshService.listByWorkspace(request);
return new JsonMessage<>(200, "", list);
}
@GetMapping(value = "list-tree", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listTree(HttpServletRequest request) {
List list = sshService.listByWorkspace(request);
Map> groupNode = new HashMap<>(4);
List> treeNodes = list.stream()
.map(sshModel -> {
String group = sshModel.getGroup();
String groupId = SecureUtil.sha1(StrUtil.emptyToDefault(group, StrUtil.EMPTY));
String groupId2 = StrUtil.format("g_{}", groupId);
groupNode.computeIfAbsent(groupId, s -> new TreeNode<>(groupId2, StrUtil.SLASH, group, 0));
//
TreeNode treeNode = new TreeNode<>(sshModel.getId(), groupId2, sshModel.getName(), 0);
Map extra = new HashMap<>();
extra.put("fileDirs", sshModel.getFileDirs());
extra.put("isLeaf", true);
treeNode.setExtra(extra);
return treeNode;
})
.collect(Collectors.toList());
//
treeNodes.addAll(groupNode.values());
Tree tree = TreeUtil.buildSingle(treeNodes, StrUtil.SLASH);
return new JsonMessage<>(200, "", tree);
}
@GetMapping(value = "get-item.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage getItem(@ValidatorItem String id, HttpServletRequest request) {
SshModel byKey = sshService.getByKey(id, request);
Assert.notNull(byKey, "对应的 ssh 不存在");
return new JsonMessage<>(200, "", byKey);
}
/**
* 查询所有的分组
*
* @return list
*/
@GetMapping(value = "list-group-all", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public IJsonMessage> listGroupAll(HttpServletRequest request) {
List listGroup = sshService.listGroup(request);
return JsonMessage.success("", listGroup);
}
/**
* 编辑
*
* @param name 名称
* @param group 分组名
* @param request 请求对象
* @param id ID
* @return json
*/
@PostMapping(value = "save.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public IJsonMessage save(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "ssh名称不能为空") String name,
String id,
String group,
HttpServletRequest request) {
SshModel sshModel = new SshModel();
sshModel.setName(name);
sshModel.setGroup(group);
sshModel.setId(id);
sshService.updateById(sshModel, request);
return JsonMessage.success("操作成功");
}
@PostMapping(value = "del.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public IJsonMessage
© 2015 - 2024 Weber Informatics LLC | Privacy Policy