Skip to content

Commit

Permalink
fix 容器构建产物为文件时保存路径层级错误 #71
Browse files Browse the repository at this point in the history
  • Loading branch information
bwcx-jzy committed Feb 29, 2024
1 parent 53d5d27 commit d11f287
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-BETA.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
2. 【server】优化 支持配置前端所有参数编码来规避部分安全规则检查(感谢[@zhaozxc2010](https://gitee.com/zhaozxc2010) [Gitee issues I8Z1VJ](https://gitee.com/dromara/Jpom/issues/I8Z1VJ)
3. 【server】优化 上传文件空文件提示文件路径(感谢[@SchuckBate](https://gitee.com/skBate) [Gitee issues I93FI6](https://gitee.com/dromara/Jpom/issues/I93FI6)
4. 【server】优化 监听日志文件消息发送失败后自动移除会话(感谢[@singlethread](https://gitee.com/zengwei_joni) [Gitee issues I93ZFX](https://gitee.com/dromara/Jpom/issues/I93ZFX)
5. 【server】优化 容器构建产物为文件时保存路径层级错误(感谢[@vfhky](https://github.com/vfhky)[Github Pr 71](https://github.com/dromara/Jpom/pull/71)

------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.dromara.jpom;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
Expand Down Expand Up @@ -89,29 +90,50 @@ public void onNext(Frame object) {
*/
public static void copyArchiveFromContainerCmd(DockerClient dockerClient, String containerId, LogRecorder logRecorder, String resultFile, String resultFileOut) {
logRecorder.system("download file from : {}", resultFile);
try (InputStream stream = dockerClient.copyArchiveFromContainerCmd(containerId, resultFile).exec();
TarArchiveInputStream tarStream = new TarArchiveInputStream(stream)) {
TarArchiveEntry tarArchiveEntry;
while ((tarArchiveEntry = tarStream.getNextTarEntry()) != null) {
if (!tarStream.canReadEntryData(tarArchiveEntry)) {
logRecorder.systemWarning("不能读取tarArchiveEntry {}", tarArchiveEntry.getName());
}
if (tarArchiveEntry.isDirectory()) {
continue;
File tmpDir = FileUtil.getTmpDir();
File fileArchive = FileUtil.file(tmpDir, "jpom", "docker-temp-archive", containerId);
try {
try (InputStream stream = dockerClient.copyArchiveFromContainerCmd(containerId, resultFile).exec();
TarArchiveInputStream tarStream = new TarArchiveInputStream(stream)) {
TarArchiveEntry tarArchiveEntry;
while ((tarArchiveEntry = tarStream.getNextEntry()) != null) {
if (!tarStream.canReadEntryData(tarArchiveEntry)) {
logRecorder.systemWarning("不能读取tarArchiveEntry {}", tarArchiveEntry.getName());
}
if (tarArchiveEntry.isDirectory()) {
continue;
}
String archiveEntryName = tarArchiveEntry.getName();
// 截取第一级目录
archiveEntryName = StrUtil.subAfter(archiveEntryName, StrUtil.SLASH, false);
// 可能中包含文件 使用原名称
archiveEntryName = StrUtil.emptyToDefault(archiveEntryName, tarArchiveEntry.getName());
File currentFile = FileUtil.file(fileArchive, archiveEntryName);
FileUtil.mkParentDirs(currentFile);
FileUtil.writeFromStream(tarStream, currentFile, false);
}
String archiveEntryName = tarArchiveEntry.getName();
// 截取第一级目录
archiveEntryName = StrUtil.subAfter(archiveEntryName, StrUtil.SLASH, false);
// 可能中包含文件 使用原名称
archiveEntryName = StrUtil.emptyToDefault(archiveEntryName, tarArchiveEntry.getName());
File currentFile = FileUtil.file(resultFileOut, archiveEntryName);
FileUtil.mkParentDirs(currentFile);
FileUtil.writeFromStream(tarStream, currentFile, false);
} catch (NotFoundException notFoundException) {
logRecorder.systemWarning("容器中没有找到执行结果文件: {}", notFoundException.getMessage());
} catch (Exception e) {
logRecorder.error("无法获取容器执行结果文件", e);
}
// github pr 71
// https://github.com/dromara/Jpom/pull/71
File[] files = fileArchive.listFiles();
if (files == null) {
logRecorder.systemWarning("临时结果文件不存在: {}", fileArchive.getAbsolutePath());
return;
}
File resultFileOutFile = FileUtil.file(resultFileOut);
if (ArrayUtil.length(files) == 1) {
FileUtil.mkParentDirs(resultFileOutFile);
FileUtil.move(files[0], resultFileOutFile, true);
} else {
FileUtil.mkdir(resultFileOutFile);
FileUtil.moveContent(fileArchive, resultFileOutFile, true);
}
} catch (NotFoundException notFoundException) {
logRecorder.systemWarning("容器中没有找到执行结果文件: {}", notFoundException.getMessage());
} catch (Exception e) {
logRecorder.error("无法获取容器执行结果文件", e);
} finally {
FileUtil.del(fileArchive);
}
}

Expand Down

0 comments on commit d11f287

Please sign in to comment.