use of io.jpom.model.AfterOpt in project Jpom by dromara.
the class OutGivingRun method startRun.
/**
* 开始异步执行分发任务
*
* @param id 分发id
* @param file 文件
* @param userModel 操作的用户
* @param unzip 解压
*/
public static void startRun(String id, File file, UserModel userModel, boolean unzip) {
OutGivingServer outGivingServer = SpringUtil.getBean(OutGivingServer.class);
OutGivingModel item = outGivingServer.getByKey(id);
Objects.requireNonNull(item, "不存在分发");
AfterOpt afterOpt = ObjectUtil.defaultIfNull(EnumUtil.likeValueOf(AfterOpt.class, item.getAfterOpt()), AfterOpt.No);
//
List<OutGivingNodeProject> outGivingNodeProjects = item.outGivingNodeProjectList();
// 开启线程
if (afterOpt == AfterOpt.Order_Restart || afterOpt == AfterOpt.Order_Must_Restart) {
ThreadUtil.execute(() -> {
// 截取睡眠时间
int sleepTime = ObjectUtil.defaultIfNull(item.getIntervalTime(), 10);
//
boolean cancel = false;
for (OutGivingNodeProject outGivingNodeProject : outGivingNodeProjects) {
if (cancel) {
String userId = userModel == null ? JpomApplication.SYSTEM_ID : userModel.getId();
OutGivingItemRun.updateStatus(null, id, outGivingNodeProject, OutGivingNodeProject.Status.Cancel, "前一个节点分发失败,取消分发", userId);
} else {
OutGivingItemRun outGivingRun = new OutGivingItemRun(item, outGivingNodeProject, file, userModel, unzip);
OutGivingNodeProject.Status status = outGivingRun.call();
if (status != OutGivingNodeProject.Status.Ok) {
if (afterOpt == AfterOpt.Order_Must_Restart) {
// 完整重启,不再继续剩余的节点项目
cancel = true;
}
}
// 休眠x秒 等待之前项目正常启动
ThreadUtil.sleep(sleepTime, TimeUnit.SECONDS);
}
}
});
} else if (afterOpt == AfterOpt.Restart || afterOpt == AfterOpt.No) {
outGivingNodeProjects.forEach(outGivingNodeProject -> ThreadUtil.execAsync(new OutGivingItemRun(item, outGivingNodeProject, file, userModel, unzip)));
} else {
//
throw new IllegalArgumentException("Not implemented " + afterOpt.getDesc());
}
}
use of io.jpom.model.AfterOpt in project Jpom by dromara.
the class OutGivingProjectController method remoteDownload.
/**
* 远程下载节点分发文件
*
* @param id 分发id
* @param afterOpt 之后的操作
* @return json
*/
@RequestMapping(value = "remote_download", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.REMOTE_DOWNLOAD)
public String remoteDownload(String id, String afterOpt, String clearOld, String url, String autoUnzip) {
OutGivingModel outGivingModel = this.check(id);
AfterOpt afterOpt1 = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(afterOpt, 0));
Assert.notNull(afterOpt1, "请选择分发后的操作");
// 验证远程 地址
ServerWhitelist whitelist = outGivingWhitelistService.getServerWhitelistData(getRequest());
Set<String> allowRemoteDownloadHost = whitelist.getAllowRemoteDownloadHost();
Assert.state(CollUtil.isNotEmpty(allowRemoteDownloadHost), "还没有配置运行的远程地址");
List<String> collect = allowRemoteDownloadHost.stream().filter(s -> StrUtil.startWith(url, s)).collect(Collectors.toList());
Assert.state(CollUtil.isNotEmpty(collect), "不允许下载当前地址的文件");
try {
// outGivingModel = outGivingServer.getItem(id);
outGivingModel.setClearOld(Convert.toBool(clearOld, false));
outGivingModel.setAfterOpt(afterOpt1.getCode());
outGivingServer.update(outGivingModel);
// 下载
File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), ServerConfigBean.OUTGIVING_FILE, id);
FileUtil.mkdir(file);
File downloadFile = HttpUtil.downloadFileFromUrl(url, file);
boolean unzip = BooleanUtil.toBoolean(autoUnzip);
//
this.checkZip(downloadFile, unzip);
// 开启
OutGivingRun.startRun(outGivingModel.getId(), downloadFile, getUser(), unzip);
return JsonMessage.getString(200, "分发成功");
} catch (Exception e) {
DefaultSystemLog.getLog().error("下载远程文件异常", e);
return JsonMessage.getString(500, "下载远程文件失败:" + e.getMessage());
}
}
use of io.jpom.model.AfterOpt in project Jpom by dromara.
the class ReleaseManage method doProject.
/**
* 发布项目
*/
private void doProject() {
// AfterOpt afterOpt, boolean clearOld, boolean diffSync
AfterOpt afterOpt = BaseEnum.getEnum(AfterOpt.class, this.buildExtraModule.getAfterOpt(), AfterOpt.No);
boolean clearOld = this.buildExtraModule.isClearOld();
boolean diffSync = this.buildExtraModule.isDiffSync();
String releaseMethodDataId = this.buildExtraModule.getReleaseMethodDataId();
String[] strings = StrUtil.splitToArray(releaseMethodDataId, CharPool.COLON);
if (ArrayUtil.length(strings) != 2) {
throw new IllegalArgumentException(releaseMethodDataId + " error");
}
NodeService nodeService = SpringUtil.getBean(NodeService.class);
NodeModel nodeModel = nodeService.getByKey(strings[0]);
Objects.requireNonNull(nodeModel, "节点不存在");
String projectId = strings[1];
if (diffSync) {
this.diffSyncProject(nodeModel, projectId, afterOpt, clearOld);
return;
}
File zipFile = BuildUtil.isDirPackage(this.resultFile);
boolean unZip = true;
if (zipFile == null) {
zipFile = this.resultFile;
unZip = false;
}
JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(zipFile, null, projectId, unZip, afterOpt, nodeModel, this.userModel, clearOld);
if (jsonMessage.getCode() == HttpStatus.HTTP_OK) {
logRecorder.info("发布项目包成功:" + jsonMessage);
} else {
throw new JpomRuntimeException("发布项目包失败:" + jsonMessage);
}
}
use of io.jpom.model.AfterOpt in project Jpom by dromara.
the class ProjectFileControl method upload.
@RequestMapping(value = "upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String upload() throws Exception {
NodeProjectInfoModel pim = getProjectInfoModel();
MultipartFileBuilder multipartFileBuilder = createMultipart().addFieldName("file").setUseOriginalFilename(true);
// 压缩文件
String type = getParameter("type");
// 是否清空
String clearType = getParameter("clearType");
String levelName = getParameter("levelName");
File lib;
if (StrUtil.isEmpty(levelName)) {
lib = new File(pim.allLib());
} else {
lib = FileUtil.file(pim.allLib(), levelName);
}
// 判断是否需要清空
if ("clear".equalsIgnoreCase(clearType)) {
if (!FileUtil.clean(lib)) {
FileUtil.del(lib.toPath());
// return JsonMessage.getString(500, "清除旧lib失败");
}
}
if ("unzip".equals(type)) {
multipartFileBuilder.setFileExt(StringUtil.PACKAGE_EXT);
multipartFileBuilder.setSavePath(AgentConfigBean.getInstance().getTempPathName());
String path = multipartFileBuilder.save();
// 解压
File file = new File(path);
try {
CompressionFileUtil.unCompress(file, lib);
} finally {
if (!FileUtil.del(file)) {
DefaultSystemLog.getLog().error("删除文件失败:" + file.getPath());
}
}
} else {
multipartFileBuilder.setSavePath(FileUtil.getAbsolutePath(lib));
// 保存
multipartFileBuilder.save();
}
// 修改使用状态
pim.setUseLibDesc("upload");
projectInfoService.updateItem(pim);
//
String after = getParameter("after");
if (StrUtil.isNotEmpty(after)) {
//
List<NodeProjectInfoModel.JavaCopyItem> javaCopyItemList = pim.getJavaCopyItemList();
//
AfterOpt afterOpt = BaseEnum.getEnum(AfterOpt.class, Convert.toInt(after, AfterOpt.No.getCode()));
if ("restart".equalsIgnoreCase(after) || afterOpt == AfterOpt.Restart) {
String result = consoleService.execCommand(ConsoleCommandOp.restart, pim, null);
// 自动处理副本集
if (javaCopyItemList != null) {
ThreadUtil.execute(() -> javaCopyItemList.forEach(javaCopyItem -> {
try {
consoleService.execCommand(ConsoleCommandOp.restart, pim, javaCopyItem);
} catch (Exception e) {
DefaultSystemLog.getLog().error("重启副本集失败", e);
}
}));
}
return JsonMessage.getString(200, "上传成功并重启:" + result);
}
if (afterOpt == AfterOpt.Order_Restart || afterOpt == AfterOpt.Order_Must_Restart) {
boolean restart = this.restart(pim, null, afterOpt);
if (javaCopyItemList != null) {
ThreadUtil.execute(() -> {
// 副本
for (NodeProjectInfoModel.JavaCopyItem javaCopyItem : javaCopyItemList) {
if (!this.restart(pim, javaCopyItem, afterOpt)) {
return;
}
// 休眠30秒 等待之前项目正常启动
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException ignored) {
}
}
});
}
}
}
return JsonMessage.getString(200, "上传成功");
}
use of io.jpom.model.AfterOpt in project Jpom by dromara.
the class ReleaseManage method diffSyncProject.
/**
* 差异上传发布
*
* @param nodeModel 节点
* @param projectId 项目ID
* @param afterOpt 发布后的操作
*/
private void diffSyncProject(NodeModel nodeModel, String projectId, AfterOpt afterOpt, boolean clearOld) {
File resultFile = this.resultFile;
String resultFileParent = resultFile.isFile() ? FileUtil.getAbsolutePath(resultFile.getParent()) : FileUtil.getAbsolutePath(this.resultFile);
//
List<File> files = FileUtil.loopFiles(resultFile);
List<JSONObject> collect = files.stream().map(file -> {
//
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", StringUtil.delStartPath(file, resultFileParent, true));
jsonObject.put("sha1", SecureUtil.sha1(file));
return jsonObject;
}).collect(Collectors.toList());
//
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", projectId);
jsonObject.put("data", collect);
JsonMessage<JSONObject> requestBody = NodeForward.requestBody(nodeModel, NodeUrl.MANAGE_FILE_DIFF_FILE, this.userModel, jsonObject);
if (requestBody.getCode() != HttpStatus.HTTP_OK) {
throw new JpomRuntimeException("对比项目文件失败:" + requestBody);
}
JSONObject data = requestBody.getData();
JSONArray diff = data.getJSONArray("diff");
JSONArray del = data.getJSONArray("del");
int delSize = CollUtil.size(del);
int diffSize = CollUtil.size(diff);
if (clearOld) {
logRecorder.info(StrUtil.format("对比文件结果,产物文件 {} 个、需要上传 {} 个、需要删除 {} 个", CollUtil.size(collect), CollUtil.size(diff), delSize));
} else {
logRecorder.info(StrUtil.format("对比文件结果,产物文件 {} 个、需要上传 {} 个", CollUtil.size(collect), CollUtil.size(diff)));
}
// 清空发布才先执行删除
if (delSize > 0 && clearOld) {
jsonObject.put("data", del);
requestBody = NodeForward.requestBody(nodeModel, NodeUrl.MANAGE_FILE_BATCH_DELETE, this.userModel, jsonObject);
if (requestBody.getCode() != HttpStatus.HTTP_OK) {
throw new JpomRuntimeException("删除项目文件失败:" + requestBody);
}
}
for (int i = 0; i < diffSize; i++) {
boolean last = (i == diffSize - 1);
JSONObject diffData = (JSONObject) diff.get(i);
String name = diffData.getString("name");
File file = FileUtil.file(resultFileParent, name);
//
String startPath = StringUtil.delStartPath(file, resultFileParent, false);
//
JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(file, startPath, projectId, false, last ? afterOpt : AfterOpt.No, nodeModel, this.userModel, false);
if (jsonMessage.getCode() != HttpStatus.HTTP_OK) {
throw new JpomRuntimeException("同步项目文件失败:" + jsonMessage);
}
if (last) {
// 最后一个
logRecorder.info("发布项目包成功:" + jsonMessage);
}
}
}
Aggregations