use of io.jpom.system.ServerConfigBean in project Jpom by dromara.
the class SshInstallAgentController method getAgentFile.
private JSONObject getAgentFile() throws IOException {
ServerConfigBean instance = ServerConfigBean.getInstance();
File agentZipPath = instance.getAgentZipPath();
Assert.state(FileUtil.exist(agentZipPath), "插件包文件不存在,需要重新上传");
String tempFilePath = instance.getUserTempPath().getAbsolutePath();
//
File tempAgent = FileUtil.file(tempFilePath, "temp_agent");
FileUtil.del(tempAgent);
try {
// 解析压缩包
File jarFile = JpomManifest.zipFileFind(FileUtil.getAbsolutePath(agentZipPath), Type.Agent, FileUtil.getAbsolutePath(tempAgent));
// 获取包内容
JsonMessage<Tuple> tupleJsonMessage = JpomManifest.checkJpomJar(FileUtil.getAbsolutePath(jarFile), Type.Agent, false);
Assert.state(tupleJsonMessage.getCode() == 200, tupleJsonMessage::getMsg);
Tuple data = tupleJsonMessage.getData();
JSONObject jsonObject = new JSONObject();
jsonObject.put("version", data.get(0));
jsonObject.put("timeStamp", data.get(1));
jsonObject.put("path", FileUtil.getAbsolutePath(agentZipPath));
return jsonObject;
} finally {
FileUtil.del(tempAgent);
}
}
use of io.jpom.system.ServerConfigBean 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);
}
}
use of io.jpom.system.ServerConfigBean in project Jpom by dromara.
the class SshInstallAgentController method uploadAgent.
@RequestMapping(value = "upload_agent.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EXECUTE)
@SystemPermission
public String uploadAgent() throws Exception {
ServerConfigBean instance = ServerConfigBean.getInstance();
String tempFilePath = instance.getUserTempPath().getAbsolutePath();
MultipartFileBuilder multipartFileBuilder = createMultipart().setFileExt("zip").addFieldName("file").setSavePath(tempFilePath);
String filePath = multipartFileBuilder.save();
File tempAgent = FileUtil.file(tempFilePath, "temp_agent");
FileUtil.del(tempAgent);
// 解析压缩包
File jarFile = JpomManifest.zipFileFind(filePath, Type.Agent, FileUtil.getAbsolutePath(tempAgent));
// 验证文件是否正确
JsonMessage<Tuple> tupleJsonMessage = JpomManifest.checkJpomJar(FileUtil.getAbsolutePath(jarFile), Type.Agent, false);
Assert.state(tupleJsonMessage.getCode() == 200, tupleJsonMessage::getMsg);
//
File outFle = FileUtil.file(tempFilePath, Type.Agent.name() + "_" + IdUtil.fastSimpleUUID());
try {
this.unZipGetTag(filePath, outFle);
// 保存插件包
File agentZipPath = instance.getAgentZipPath();
FileUtil.copy(FileUtil.file(filePath), agentZipPath, true);
return JsonMessage.getString(200, "上传成功");
} finally {
FileUtil.del(filePath);
FileUtil.del(jarFile);
}
}
Aggregations