Search in sources :

Example 36 with NodeModel

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

the class NodeForward method getSocketUrl.

/**
 * 获取节点socket 信息
 *
 * @param nodeModel 节点信息
 * @param nodeUrl   url
 * @return url
 */
public static String getSocketUrl(NodeModel nodeModel, NodeUrl nodeUrl, UserModel userInfo, Object... parameters) {
    String ws;
    if ("https".equalsIgnoreCase(nodeModel.getProtocol())) {
        ws = "wss";
    } else {
        ws = "ws";
    }
    if (StrUtil.isEmpty(nodeModel.getLoginPwd())) {
        NodeService nodeService = SpringUtil.getBean(NodeService.class);
        NodeModel model = nodeService.getByKey(nodeModel.getId(), false);
        nodeModel.setLoginPwd(model.getLoginPwd());
        nodeModel.setLoginName(model.getLoginName());
    }
    UrlQuery urlQuery = new UrlQuery();
    urlQuery.add(ConfigBean.JPOM_AGENT_AUTHORIZE, nodeModel.toAuthorize());
    // 
    String optUser = userInfo.getId();
    optUser = URLUtil.encode(optUser);
    urlQuery.add("optUser", optUser);
    if (ArrayUtil.isNotEmpty(parameters)) {
        for (int i = 0; i < parameters.length; i += 2) {
            Object parameter = parameters[i + 1];
            String value = Convert.toStr(parameter, StrUtil.EMPTY);
            urlQuery.add(parameters[i].toString(), URLUtil.encode(value));
        }
    }
    // 兼容旧版本-节点升级 @author jzy
    urlQuery.add("name", URLUtil.encode(nodeModel.getLoginName()));
    urlQuery.add("password", URLUtil.encode(nodeModel.getLoginPwd()));
    String format = StrUtil.format("{}://{}{}?{}", ws, nodeModel.getUrl(), nodeUrl.getUrl(), urlQuery.toString());
    DefaultSystemLog.getLog().debug("web socket url:{}", format);
    return format;
}
Also used : NodeModel(io.jpom.model.data.NodeModel) NodeService(io.jpom.service.node.NodeService) UrlQuery(cn.hutool.core.net.url.UrlQuery) JSONObject(com.alibaba.fastjson.JSONObject)

Example 37 with NodeModel

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

the class BaseNodeService method syncExecuteNode.

/**
 * 同步执行 同步节点信息
 *
 * @param nodeModel 节点信息
 * @return json
 */
public String syncExecuteNode(NodeModel nodeModel) {
    String nodeModelName = nodeModel.getName();
    if (!nodeModel.isOpenStatus()) {
        DefaultSystemLog.getLog().debug("{} 节点未启用", nodeModelName);
        return "节点未启用";
    }
    try {
        JSONArray jsonArray = this.getLitDataArray(nodeModel);
        if (CollUtil.isEmpty(jsonArray)) {
            Entity entity = Entity.create();
            entity.set("nodeId", nodeModel.getId());
            int del = super.del(entity);
            // 
            DefaultSystemLog.getLog().debug("{} 节点没有拉取到任何{}", nodeModelName, dataName);
            return "节点没有拉取到任何" + dataName;
        }
        // 查询现在存在的项目
        T where = ReflectUtil.newInstance(this.tClass);
        where.setWorkspaceId(nodeModel.getWorkspaceId());
        where.setNodeId(nodeModel.getId());
        List<T> cacheAll = super.listByBean(where);
        cacheAll = ObjectUtil.defaultIfNull(cacheAll, Collections.EMPTY_LIST);
        Set<String> cacheIds = cacheAll.stream().map(BaseNodeModel::dataId).collect(Collectors.toSet());
        // 
        List<T> projectInfoModels = jsonArray.toJavaList(this.tClass);
        List<T> models = projectInfoModels.stream().peek(item -> this.fullData(item, nodeModel)).filter(item -> {
            // 检查对应的工作空间 是否存在
            return workspaceService.exists(new WorkspaceModel(item.getWorkspaceId()));
        }).filter(projectInfoModel -> {
            // 避免重复同步
            return StrUtil.equals(nodeModel.getWorkspaceId(), projectInfoModel.getWorkspaceId());
        }).peek(item -> cacheIds.remove(item.dataId())).collect(Collectors.toList());
        // 设置 临时缓存,便于放行检查
        BaseServerController.resetInfo(UserModel.EMPTY);
        // 
        models.forEach(BaseNodeService.super::upsert);
        // 删除项目
        Set<String> strings = cacheIds.stream().map(s -> BaseNodeModel.fullId(nodeModel.getWorkspaceId(), nodeModel.getId(), s)).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(strings)) {
            super.delByKey(strings, null);
        }
        String format = StrUtil.format("{} 节点拉取到 {} 个{},已经缓存 {} 个{},更新 {} 个{},删除 {} 个缓存", nodeModelName, CollUtil.size(jsonArray), dataName, CollUtil.size(cacheAll), dataName, CollUtil.size(models), dataName, CollUtil.size(strings));
        DefaultSystemLog.getLog().debug(format);
        return format;
    } catch (Exception e) {
        return this.checkException(e, nodeModelName);
    } finally {
        BaseServerController.removeEmpty();
    }
}
Also used : PageResultDto(io.jpom.model.PageResultDto) ObjectUtil(cn.hutool.core.util.ObjectUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServletUtil(cn.hutool.extra.servlet.ServletUtil) JSONArray(com.alibaba.fastjson.JSONArray) HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeService(io.jpom.service.node.NodeService) Map(java.util.Map) BaseNodeModel(io.jpom.model.BaseNodeModel) WorkspaceModel(io.jpom.model.data.WorkspaceModel) WorkspaceService(io.jpom.service.system.WorkspaceService) NodeModel(io.jpom.model.data.NodeModel) Set(java.util.Set) SpringUtil(cn.jiangzeyin.common.spring.SpringUtil) Collectors(java.util.stream.Collectors) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) AuthorizeException(io.jpom.system.AuthorizeException) List(java.util.List) Const(io.jpom.common.Const) ReflectUtil(cn.hutool.core.util.ReflectUtil) JSONObject(com.alibaba.fastjson.JSONObject) Entity(cn.hutool.db.Entity) ThreadUtil(cn.hutool.core.thread.ThreadUtil) UserModel(io.jpom.model.data.UserModel) AgentException(io.jpom.system.AgentException) Collections(java.util.Collections) BaseServerController(io.jpom.common.BaseServerController) Assert(org.springframework.util.Assert) Entity(cn.hutool.db.Entity) WorkspaceModel(io.jpom.model.data.WorkspaceModel) JSONArray(com.alibaba.fastjson.JSONArray) AuthorizeException(io.jpom.system.AuthorizeException) AgentException(io.jpom.system.AgentException)

Example 38 with NodeModel

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

the class BaseNodeService method syncAllNode.

/**
 * 同步所有节点的项目
 */
public void syncAllNode() {
    ThreadUtil.execute(() -> {
        List<NodeModel> list = nodeService.list();
        if (CollUtil.isEmpty(list)) {
            DefaultSystemLog.getLog().debug("没有任何节点");
            return;
        }
        // 排序 避免项目被个节点绑定
        list.sort((o1, o2) -> {
            if (StrUtil.equals(o1.getWorkspaceId(), Const.WORKSPACE_DEFAULT_ID)) {
                return 1;
            }
            if (StrUtil.equals(o2.getWorkspaceId(), Const.WORKSPACE_DEFAULT_ID)) {
                return 1;
            }
            return 0;
        });
        for (NodeModel nodeModel : list) {
            this.syncNode(nodeModel);
        }
    });
}
Also used : BaseNodeModel(io.jpom.model.BaseNodeModel) NodeModel(io.jpom.model.data.NodeModel)

Example 39 with NodeModel

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

the class WorkspaceEnvVarController method syncNodeEnvVar.

private void syncNodeEnvVar(WorkspaceEnvVarModel workspaceEnvVarModel, String oldNode) {
    String workspaceId = workspaceEnvVarModel.getWorkspaceId();
    List<String> newNodeIds = StrUtil.splitTrim(workspaceEnvVarModel.getNodeIds(), StrUtil.COMMA);
    List<String> oldNodeIds = StrUtil.splitTrim(oldNode, StrUtil.COMMA);
    Collection<String> delNode = CollUtil.subtract(oldNodeIds, newNodeIds);
    UserModel user = getUser();
    // 删除
    this.syncDelNodeEnvVar(workspaceEnvVarModel.getName(), user, delNode, workspaceId);
    // 更新
    for (String newNodeId : newNodeIds) {
        NodeModel byKey = nodeService.getByKey(newNodeId);
        Assert.state(StrUtil.equals(workspaceId, byKey.getWorkspaceId()), "选择节点错误");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("description", workspaceEnvVarModel.getDescription());
        jsonObject.put("name", workspaceEnvVarModel.getName());
        if (StrUtil.isNotEmpty(workspaceEnvVarModel.getValue())) {
            jsonObject.put("value", workspaceEnvVarModel.getValue());
        } else {
            // 查询
            WorkspaceEnvVarModel byKeyExits = workspaceEnvVarService.getByKey(workspaceEnvVarModel.getId());
            jsonObject.put("value", byKeyExits.getValue());
        }
        JsonMessage<String> jsonMessage = NodeForward.request(byKey, NodeUrl.Workspace_EnvVar_Update, user, jsonObject);
        Assert.state(jsonMessage.getCode() == 200, "处理 " + byKey.getName() + " 节点同步脚本失败" + jsonMessage.getMsg());
    }
}
Also used : UserModel(io.jpom.model.data.UserModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) WorkspaceEnvVarModel(io.jpom.model.data.WorkspaceEnvVarModel)

Example 40 with NodeModel

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

the class NodeService method syncToWorkspace.

/**
 * 将节点信息同步到其他工作空间
 *
 * @param ids            多给节点ID
 * @param nowWorkspaceId 当前的工作空间ID
 * @param workspaceId    同步到哪个工作空间
 */
public void syncToWorkspace(String ids, String nowWorkspaceId, String workspaceId) {
    StrUtil.splitTrim(ids, StrUtil.COMMA).forEach(id -> {
        NodeModel data = super.getByKey(id, false, entity -> entity.set("workspaceId", nowWorkspaceId));
        Assert.notNull(data, "没有对应到节点信息");
        // 
        NodeModel where = new NodeModel();
        where.setWorkspaceId(workspaceId);
        where.setUrl(data.getUrl());
        NodeModel nodeModel = NodeService.super.queryByBean(where);
        if (nodeModel == null) {
            // 不存在则添加节点
            data.setId(null);
            data.setWorkspaceId(workspaceId);
            data.setCreateTimeMillis(null);
            data.setModifyTimeMillis(null);
            data.setModifyUser(null);
            // ssh 不同步
            data.setSshId(null);
            NodeService.super.insert(data);
        } else {
            // 修改信息
            NodeModel update = new NodeModel(nodeModel.getId());
            update.setLoginName(data.getLoginName());
            update.setLoginPwd(data.getLoginPwd());
            update.setProtocol(data.getProtocol());
            update.setHttpProxy(data.getHttpProxy());
            update.setHttpProxyType(data.getHttpProxyType());
            NodeService.super.updateById(update);
        }
    });
}
Also used : NodeModel(io.jpom.model.data.NodeModel)

Aggregations

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