Search in sources :

Example 16 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class SshInstallAgentController method installAgentSubmit.

@RequestMapping(value = "installAgentSubmit.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
@SystemPermission
public String installAgentSubmit(@ValidatorItem(value = ValidatorRule.NOT_BLANK) String id, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "节点数据") String nodeData, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "安装路径") String path) throws Exception {
    // 
    SshModel sshModel = sshService.getByKey(id, false);
    Objects.requireNonNull(sshModel, "没有找到对应ssh");
    // 判断输入的节点信息
    NodeModel nodeModel = this.getNodeModel(nodeData, sshModel);
    // 
    ServerConfigBean instance = ServerConfigBean.getInstance();
    String tempFilePath = instance.getUserTempPath().getAbsolutePath();
    JSONObject agentFile = this.getAgentFile();
    String filePath = agentFile.getString("path");
    // 
    File outFle = FileUtil.file(tempFilePath, Type.Agent.name() + "_" + IdUtil.fastSimpleUUID());
    try {
        String tag = this.unZipGetTag(filePath, outFle);
        // 
        this.readNodeAuthorize(outFle, nodeModel);
        // 查询远程是否运行
        Assert.state(!sshService.checkSshRun(sshModel, tag), "对应服务器中已经存在 Jpom 插件端,不需要再次安装啦");
        // 上传文件到服务器
        sshService.uploadDir(sshModel, path, outFle);
        // 
        String shPtah = FileUtil.normalize(path + StrUtil.SLASH + Type.Agent.name() + ".sh");
        String chmod = getParameter("chmod");
        if (StrUtil.isEmptyOrUndefined(chmod)) {
            chmod = StrUtil.EMPTY;
        } else {
            chmod = StrUtil.format("{} {} && ", chmod, shPtah);
        }
        String command = StrUtil.format("{}bash {} start upgrade", chmod, shPtah);
        String result = sshService.exec(sshModel, command);
        DefaultSystemLog.getLog().debug("ssh install agent node {} {}", command, result);
        // 休眠 5 秒, 尝试 5 次
        int waitCount = getParameterInt("waitCount", 5);
        this.loopCheck(waitCount, nodeModel, sshModel, path, result);
        // 绑定关系
        nodeModel.setSshId(sshModel.getId());
        nodeService.insert(nodeModel);
        // 
        return JsonMessage.getString(200, "操作成功:" + result);
    } finally {
        // 清理资源
        FileUtil.del(outFle);
    }
}
Also used : NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) SshModel(io.jpom.model.data.SshModel) ZipFile(java.util.zip.ZipFile) File(java.io.File) ServerConfigBean(io.jpom.system.ServerConfigBean) SystemPermission(io.jpom.permission.SystemPermission) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class NodeProjectInfoController method syncProject.

/**
 * 同步节点项目
 *
 * @return json
 */
@GetMapping(value = "sync_project", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(cls = ClassFeature.PROJECT, method = MethodFeature.DEL)
public String syncProject(String nodeId) {
    NodeModel nodeModel = nodeService.getByKey(nodeId);
    Assert.notNull(nodeModel, "对应的节点不存在");
    int count = projectInfoCacheService.delCache(nodeId, getRequest());
    String msg = projectInfoCacheService.syncExecuteNode(nodeModel);
    return JsonMessage.getString(200, "主动清除:" + count + StrUtil.SPACE + msg);
}
Also used : NodeModel(io.jpom.model.data.NodeModel) GetMapping(org.springframework.web.bind.annotation.GetMapping) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature)

Example 18 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class NodeUpdateController method confirmFastInstall.

@GetMapping(value = "confirm_fast_install.json", produces = MediaType.APPLICATION_JSON_VALUE)
public String confirmFastInstall(@ValidatorItem String id, @ValidatorItem String ip, int port) {
    JSONObject receiveCache = NodeInfoController.getReceiveCache(id);
    Assert.notNull(receiveCache, "没有对应的缓存信息");
    JSONArray jsonArray = receiveCache.getJSONArray("canUseNode");
    Assert.notEmpty(jsonArray, "没有对应的缓存信息:-1");
    Optional<NodeModel> any = jsonArray.stream().map(o -> {
        if (o instanceof NodeModel) {
            return (NodeModel) o;
        }
        JSONObject jsonObject = (JSONObject) o;
        return jsonObject.toJavaObject(NodeModel.class);
    }).filter(nodeModel -> StrUtil.equals(nodeModel.getUrl(), StrUtil.format("{}:{}", ip, port))).findAny();
    Assert.state(any.isPresent(), "ip 地址信息不正确");
    NodeModel nodeModel = any.get();
    try {
        nodeService.testNode(nodeModel);
    } catch (Exception e) {
        DefaultSystemLog.getLog().warn("测试结果:{} {}", nodeModel.getUrl(), e.getMessage());
        return JsonMessage.getString(500, "节点连接失败:" + e.getMessage());
    }
    // 插入
    boolean exists = nodeService.existsByUrl(nodeModel.getUrl(), nodeModel.getWorkspaceId(), null);
    Assert.state(!exists, "对应的节点已经存在拉:" + nodeModel.getUrl());
    nodeService.insert(nodeModel);
    // 更新结果
    receiveCache.put("type", "success");
    return JsonMessage.getString(200, "安装成功", NodeInfoController.listReceiveCache(null));
}
Also used : MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) ResourceUtil(cn.hutool.core.io.resource.ResourceUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServerConfigBean(io.jpom.system.ServerConfigBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Feature(io.jpom.permission.Feature) BaseJpomInterceptor(io.jpom.common.interceptor.BaseJpomInterceptor) NodeInfoController(io.jpom.controller.openapi.NodeInfoController) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) SystemParametersServer(io.jpom.service.system.SystemParametersServer) GetMapping(org.springframework.web.bind.annotation.GetMapping) io.jpom.common(io.jpom.common) MethodFeature(io.jpom.permission.MethodFeature) SystemPermission(io.jpom.permission.SystemPermission) IoUtil(cn.hutool.core.io.IoUtil) ClassFeature(io.jpom.permission.ClassFeature) MediaType(org.springframework.http.MediaType) Collection(java.util.Collection) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) Tuple(cn.hutool.core.lang.Tuple) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) CharsetUtil(cn.hutool.core.util.CharsetUtil) Optional(java.util.Optional) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) AgentFileModel(io.jpom.model.AgentFileModel) HttpStatus(cn.hutool.http.HttpStatus) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 19 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class EditProjectController method saveProject.

/**
 * 保存项目
 *
 * @param id id
 * @return json
 */
@RequestMapping(value = "saveProject", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String saveProject(String id) {
    // 防止和Jpom冲突
    if (StrUtil.isNotEmpty(ConfigBean.getInstance().applicationTag) && ConfigBean.getInstance().applicationTag.equalsIgnoreCase(id)) {
        return JsonMessage.getString(401, "当前项目id已经被Jpom占用");
    }
    NodeModel node = getNode();
    JsonMessage<Object> request = NodeForward.request(node, getRequest(), NodeUrl.Manage_SaveProject);
    if (request.getCode() == HttpStatus.OK.value()) {
        projectInfoCacheService.syncNode(node, id);
    }
    return request.toString();
}
Also used : NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 20 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class ProjectManageControl method deleteProject.

/**
 * 删除项目
 *
 * @param id id
 * @return json
 */
@PostMapping(value = "deleteProject", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public String deleteProject(@ValidatorItem(value = ValidatorRule.NOT_BLANK) String id, String copyId) {
    NodeModel nodeModel = getNode();
    HttpServletRequest servletRequest = getRequest();
    if (StrUtil.isEmpty(copyId)) {
        // 检查节点分发
        List<OutGivingModel> outGivingModels = outGivingServer.listByWorkspace(servletRequest);
        if (outGivingModels != null) {
            boolean match = outGivingModels.stream().anyMatch(outGivingModel -> outGivingModel.checkContains(nodeModel.getId(), id));
            Assert.state(!match, "当前项目存在节点分发,不能直接删除");
        // for (OutGivingModel outGivingModel : outGivingModels) {
        // if (outGivingModel.checkContains(nodeModel.getId(), id)) {
        // return JsonMessage.getString(405, "当前项目存在节点分发,不能直接删除");
        // }
        // }
        }
        // 
        List<MonitorModel> monitorModels = monitorService.listByWorkspace(servletRequest);
        if (monitorModels != null) {
            boolean match = monitorModels.stream().anyMatch(monitorModel -> monitorModel.checkNodeProject(nodeModel.getId(), id));
            // if (monitorService.checkProject(nodeModel.getId(), id)) {
            // return JsonMessage.getString(405, );
            // }
            Assert.state(!match, "当前项目存在监控项,不能直接删除");
        }
        boolean releaseMethod = buildService.checkReleaseMethod(nodeModel.getId() + StrUtil.COLON + id, servletRequest, BuildReleaseMethod.Project);
        Assert.state(!releaseMethod, "当前项目存在构建项,不能直接删除");
    }
    JsonMessage<Object> request = NodeForward.request(nodeModel, servletRequest, NodeUrl.Manage_DeleteProject);
    if (request.getCode() == HttpStatus.OK.value()) {
        // 
        projectInfoCacheService.syncNode(nodeModel);
    }
    return request.toString();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeModel(io.jpom.model.data.NodeModel) MonitorModel(io.jpom.model.data.MonitorModel) OutGivingModel(io.jpom.model.data.OutGivingModel) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Aggregations

NodeModel (io.jpom.model.data.NodeModel)64 JSONObject (com.alibaba.fastjson.JSONObject)27 Feature (io.jpom.permission.Feature)23 MethodFeature (io.jpom.permission.MethodFeature)23 ClassFeature (io.jpom.permission.ClassFeature)22 UserModel (io.jpom.model.data.UserModel)17 StrUtil (cn.hutool.core.util.StrUtil)12 NodeService (io.jpom.service.node.NodeService)12 Collectors (java.util.stream.Collectors)12 JsonMessage (cn.jiangzeyin.common.JsonMessage)11 List (java.util.List)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 CollUtil (cn.hutool.core.collection.CollUtil)9 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)9 NodeForward (io.jpom.common.forward.NodeForward)8 NodeUrl (io.jpom.common.forward.NodeUrl)8 Assert (org.springframework.util.Assert)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 JSONArray (com.alibaba.fastjson.JSONArray)7 BaseServerController (io.jpom.common.BaseServerController)7