|
|
|
@@ -8,6 +8,7 @@ import com.ruoyi.platform.mapper.ImageVersionDao; |
|
|
|
import com.ruoyi.platform.service.ImageService; |
|
|
|
import com.ruoyi.platform.service.ImageVersionService; |
|
|
|
import com.ruoyi.platform.service.MinioService; |
|
|
|
import com.ruoyi.platform.utils.FileUtil; |
|
|
|
import com.ruoyi.platform.utils.K8sClientUtil; |
|
|
|
import com.ruoyi.platform.vo.ImageVo; |
|
|
|
import com.ruoyi.system.api.model.LoginUser; |
|
|
|
@@ -23,7 +24,9 @@ import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
|
|
|
/** |
|
|
|
* (Image)表服务实现类 |
|
|
|
@@ -171,7 +174,6 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
image.setName(imageVo.getName()); |
|
|
|
image.setDescription(imageVo.getDescription()); |
|
|
|
image.setImageType(imageVo.getImageType()); |
|
|
|
|
|
|
|
Image imageInsert = this.insert(image); |
|
|
|
if (imageInsert == null){ |
|
|
|
throw new Exception("新增镜像失败"); |
|
|
|
@@ -181,17 +183,50 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
imageVersion.setVersion(imageVo.getVersion()); |
|
|
|
imageVersion.setUrl(imageVo.getUrl()); |
|
|
|
imageVersion.setTagName(imageVo.getTagName()); |
|
|
|
//imageVersion.setFileSize(datasetVo.getFileSize()); |
|
|
|
|
|
|
|
imageVersion.setFileSize(imageVo.getFileSize()); |
|
|
|
imageVersion.setStatus("building"); |
|
|
|
ImageVersion imageVersionInsert = this.imageVersionService.insert(imageVersion); |
|
|
|
if (imageVersionInsert == null) { |
|
|
|
throw new Exception("新增镜像失败"); |
|
|
|
} |
|
|
|
return "新增镜像成功"; |
|
|
|
// 使用CompletableFuture异步执行不同的镜像构建逻辑 |
|
|
|
CompletableFuture.supplyAsync(() -> { |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
try { |
|
|
|
if(imageVo.getUploadType()==0){ |
|
|
|
resultMap = createImageFromNet(imageVo.getName(), imageVo.getTagName(), imageVo.getPath()); |
|
|
|
}else{ |
|
|
|
resultMap = createImageFromLocal(imageVo.getName(), imageVo.getTagName(), imageVo.getPath()); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
imageVersion.setStatus("failed"); |
|
|
|
imageVersionService.update(imageVersion); |
|
|
|
throw new RuntimeException("镜像构建失败: " + e.getMessage(), e); |
|
|
|
} |
|
|
|
return resultMap; |
|
|
|
}).thenAccept(resultMap ->{ |
|
|
|
try { |
|
|
|
String imageUrl = resultMap.get("url"); |
|
|
|
String fileSize = resultMap.get("filesize"); |
|
|
|
imageVersion.setUrl(imageUrl); |
|
|
|
imageVersion.setFileSize(fileSize); |
|
|
|
imageVersion.setStatus("available"); |
|
|
|
imageVersionService.update(imageVersion); |
|
|
|
} catch (Exception e) { |
|
|
|
System.err.println("更新数据库失败: " + e.getMessage()); |
|
|
|
} |
|
|
|
}).exceptionally(ex -> { |
|
|
|
System.err.println("异步操作发生错误: " + ex.getMessage()); |
|
|
|
return null; |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return "新增镜像成功,镜像构建中..."; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String createImageFromNet(String imageName, String imageTag, String netPath) throws Exception { |
|
|
|
public Map<String, String> createImageFromNet(String imageName, String imageTag, String netPath) throws Exception { |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
// 得到容器 |
|
|
|
V1Pod pod = k8sClientUtil.getNSPodList(serviceNS, deploymentName); |
|
|
|
if (pod == null) { |
|
|
|
@@ -203,7 +238,7 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
// 在这个容器的/data/admin 目录下执行命令 docker load -i fileName 得到返回的镜像名字name:tag |
|
|
|
String username = SecurityUtils.getLoginUser().getUsername(); |
|
|
|
// |
|
|
|
String logs2 = k8sClientUtil.executeCommand(pod,"docker pull "+netPath); |
|
|
|
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()); |
|
|
|
@@ -211,19 +246,28 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
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))){ |
|
|
|
return imageUrl; |
|
|
|
resultMap.put("url", imageUrl); |
|
|
|
//得到镜像文件大小 |
|
|
|
long sizeInBytes = Long.parseLong(k8sClientUtil.executeCommand(pod, cmd3)); |
|
|
|
String formattedImageSize = FileUtil.formatFileSize(sizeInBytes); // 格式化镜像文件大小 |
|
|
|
resultMap.put("fileSize", formattedImageSize); |
|
|
|
return resultMap; |
|
|
|
|
|
|
|
}else { |
|
|
|
throw new Exception("拉取公网镜像失败,请检查网络或者镜像地址"); |
|
|
|
throw new Exception("拉取公网镜像失败,请检查网络或者镜像地址"); |
|
|
|
} |
|
|
|
}else { |
|
|
|
throw new Exception("拉取公网镜像失败,请检查网络或者镜像地址"); |
|
|
|
throw new Exception("拉取公网镜像失败,请检查网络或者镜像地址"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String createImageFromLocal(String imageName, String imageTag, String path) throws Exception { |
|
|
|
public Map<String, String> createImageFromLocal(String imageName, String imageTag, String path) throws Exception { |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
// 得到容器 |
|
|
|
V1Pod pod = k8sClientUtil.getNSPodList(serviceNS, deploymentName); |
|
|
|
if (pod == null) { |
|
|
|
@@ -244,11 +288,17 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
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))){ |
|
|
|
return imageUrl; |
|
|
|
resultMap.put("url", imageUrl); |
|
|
|
//得到镜像文件大小 |
|
|
|
long sizeInBytes = Long.parseLong(k8sClientUtil.executeCommand(pod, cmd3)); |
|
|
|
String formattedImageSize = FileUtil.formatFileSize(sizeInBytes); // 格式化镜像文件大小 |
|
|
|
resultMap.put("fileSize", formattedImageSize); |
|
|
|
return resultMap; |
|
|
|
}else { |
|
|
|
throw new Exception("解析镜像压缩包失败,请检查镜像文件"); |
|
|
|
throw new Exception("解析镜像压缩包失败,请检查镜像文件"); |
|
|
|
} |
|
|
|
}else { |
|
|
|
throw new Exception("解析镜像压缩包失败,请检查镜像文件"); |
|
|
|
@@ -259,7 +309,6 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
public Map<String, String> uploadImageFiles(MultipartFile file) throws Exception { |
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
String path = loginUser.getUsername()+"/"+file.getOriginalFilename(); |
|
|
|
Map<String, String> stringMap = minioService.uploadFile(bucketName, path, file); |
|
|
|
return stringMap; |
|
|
|
return minioService.uploadFile(bucketName, path, file); |
|
|
|
} |
|
|
|
} |