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;
}
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();
}
}
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);
}
});
}
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());
}
}
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);
}
});
}
Aggregations