Browse Source

镜像构建接口修改docker命令截取逻辑,修复bug

pull/30/head
西大锐 1 year ago
parent
commit
eef2f3e4f5
3 changed files with 36 additions and 27 deletions
  1. +18
    -18
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java
  2. +7
    -0
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java
  3. +11
    -9
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java

+ 18
- 18
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java View File

@@ -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;


+ 7
- 0
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java View File

@@ -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);


+ 11
- 9
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java View File

@@ -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;


Loading…
Cancel
Save