diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java index 831c242f..b9a3d990 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java @@ -181,7 +181,6 @@ public class ImageServiceImpl implements ImageService { ImageVersion imageVersion = new ImageVersion(); imageVersion.setImageId(imageInsert.getId()); imageVersion.setVersion(imageVo.getVersion()); - imageVersion.setUrl(imageVo.getUrl()); imageVersion.setTagName(imageVo.getTagName()); imageVersion.setFileSize(imageVo.getFileSize()); imageVersion.setStatus("building"); @@ -241,18 +240,18 @@ public class ImageServiceImpl implements ImageService { String logs2 = k8sClientUtil.executeCommand(pod,"docker pull "+ netPath); // 在容器里执行 docker tag name:tag nexus3.kube-system.svc:8083/imageName:imageTag if (StringUtils.isNoneBlank(logs2)){ - String substring = logs2.substring(logs2.indexOf(harborUrl), logs2.length()); + String substring = logs2.substring(logs2.lastIndexOf(harborUrl), logs2.length()); String cleanedString = substring.replaceAll("(\\r|\\n)", ""); - String cmd1 = "docker tag " + cleanedString+ " " + harborUrl+"/"+repository+"/"+username+"/" + imageName + imageTag; - String imageUrl = harborUrl+"/"+repository+"/"+username+"/" + imageName + imageTag; - String cmd2 = "docker push " + imageUrl; - String cmd3 = "docker inspect --format='{{.Size}}' " + imageUrl; - - String s = k8sClientUtil.executeCommand(pod, cmd1); - if (StringUtils.isNotEmpty(k8sClientUtil.executeCommand(pod, cmd2))){ + String tagCmd = "docker tag " + cleanedString + " " + harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; + String imageUrl = harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; + String pushCmd = "docker push " + imageUrl; + String sizeCmd = "docker inspect --format='{{.Size}}' " + imageUrl; + String s = k8sClientUtil.executeCommand(pod, tagCmd); + if (StringUtils.isNotEmpty(k8sClientUtil.executeCommand(pod, pushCmd))){ resultMap.put("url", imageUrl); //得到镜像文件大小 - long sizeInBytes = Long.parseLong(k8sClientUtil.executeCommand(pod, cmd3)); + String imageSizeStr = k8sClientUtil.executeCommand(pod, sizeCmd); + long sizeInBytes = Long.parseLong(imageSizeStr.trim()); String formattedImageSize = FileUtil.formatFileSize(sizeInBytes); // 格式化镜像文件大小 resultMap.put("fileSize", formattedImageSize); return resultMap; @@ -283,17 +282,18 @@ public class ImageServiceImpl implements ImageService { String logs2 = k8sClientUtil.executeCommand(pod,"docker load -i "+filePath); // 在容器里执行 docker tag name:tag nexus3.kube-system.svc:8083/imageName:imageTag if (StringUtils.isNoneBlank(logs2)){ - String substring = logs2.substring(logs2.indexOf(harborUrl), logs2.length()); + String substring = logs2.substring(logs2.lastIndexOf(harborUrl), logs2.length()); String cleanedString = substring.replaceAll("(\\r|\\n)", ""); - String cmd1 = "docker tag " + cleanedString+ " " + harborUrl+"/"+repository+"/"+username+"/" + imageName + imageTag; - String imageUrl = harborUrl+"/"+repository+"/"+username+"/" + imageName + imageTag; - String cmd2 = "docker push " + imageUrl; - String cmd3 = "docker inspect --format='{{.Size}}' " + imageUrl; - String s = k8sClientUtil.executeCommand(pod, cmd1); - if (StringUtils.isNotEmpty(k8sClientUtil.executeCommand(pod, cmd2))){ + String tagCmd = "docker tag " + cleanedString + " " + harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; + String imageUrl = harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; + String pushCmd = "docker push " + imageUrl; + String sizeCmd = "docker inspect --format='{{.Size}}' " + imageUrl; + String s = k8sClientUtil.executeCommand(pod, tagCmd); + if (StringUtils.isNotEmpty(k8sClientUtil.executeCommand(pod, pushCmd))){ resultMap.put("url", imageUrl); //得到镜像文件大小 - long sizeInBytes = Long.parseLong(k8sClientUtil.executeCommand(pod, cmd3)); + String imageSizeStr = k8sClientUtil.executeCommand(pod, sizeCmd); + long sizeInBytes = Long.parseLong(imageSizeStr.trim()); String formattedImageSize = FileUtil.formatFileSize(sizeInBytes); // 格式化镜像文件大小 resultMap.put("fileSize", formattedImageSize); return resultMap; diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java index 58bdabfe..9815d25c 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java @@ -406,6 +406,13 @@ public class K8sClientUtil { builder.append(line); builder.append(System.getProperty("line.separator")); } + // 等待进程结束,并获取退出值 + int exitValue = proc.waitFor(); + if (exitValue != 0) { + // 如果进程的退出值不为0,表示命令执行失败 + throw new RuntimeException("命令执行失败,退出值:" + exitValue); + } + return builder.toString(); } catch (Exception e) { log.error("执行命令异常", e); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java index 1f6319cf..c4b313c2 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java @@ -33,11 +33,13 @@ public class ImageVo implements Serializable { */ @ApiModelProperty(name = "version") private String version; + /** * 镜像推送地址 */ - @ApiModelProperty(name = "url") - private String url; +// @ApiModelProperty(name = "url") +// private String url; + /** * 镜像tag名称 */ @@ -102,13 +104,13 @@ public class ImageVo implements Serializable { this.version = version; } - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } +// public String getUrl() { +// return url; +// } +// +// public void setUrl(String url) { +// this.url = url; +// } public String getTagName() { return tagName;