|
|
|
@@ -88,6 +88,8 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
return new PageImpl<>(this.imageDao.queryAllByLimit(image, pageRequest), pageRequest, total); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 新增数据 |
|
|
|
* |
|
|
|
@@ -138,10 +140,10 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String removeById(Integer id) { |
|
|
|
public String removeById(Integer id) throws Exception { |
|
|
|
Image image = this.imageDao.queryById(id); |
|
|
|
if (image == null){ |
|
|
|
return "镜像不存在"; |
|
|
|
throw new Exception("镜像不存在"); |
|
|
|
} |
|
|
|
|
|
|
|
//判断权限,只有admin和创建者本身可以删除该数据集 |
|
|
|
@@ -150,11 +152,11 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
|
|
|
|
|
|
|
|
String createdBy = image.getCreateBy(); |
|
|
|
if (!(StringUtils.equals(username,"admin") || StringUtils.equals(username,createdBy))){ |
|
|
|
return "无权限删除该镜像"; |
|
|
|
if (!(StringUtils.equals(username,"admin") || !StringUtils.equals(username,createdBy))){ |
|
|
|
throw new Exception("无权限删除该镜像"); |
|
|
|
} |
|
|
|
if (!imageVersionService.queryByImageId(id).isEmpty()){ |
|
|
|
return "请先删除该镜像下的版本文件"; |
|
|
|
throw new Exception("请先删除该镜像下的版本文件"); |
|
|
|
} |
|
|
|
image.setState(0); |
|
|
|
return this.imageDao.update(image)>0?"删除成功":"删除失败"; |
|
|
|
@@ -167,27 +169,46 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
return new PageImpl<>(this.imageDao.queryByName(name)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 通过名字精确查询镜像 |
|
|
|
* |
|
|
|
* @param name 名字 |
|
|
|
* @return 镜像 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public Image getByName(String name) { |
|
|
|
return this.imageDao.getByName(name); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional |
|
|
|
public String insertImageAndVersion(ImageVo imageVo) throws Exception { |
|
|
|
Image image = new Image(); |
|
|
|
image.setName(imageVo.getName()); |
|
|
|
image.setDescription(imageVo.getDescription()); |
|
|
|
image.setImageType(imageVo.getImageType()); |
|
|
|
Image imageInsert = this.insert(image); |
|
|
|
if (imageInsert == null){ |
|
|
|
throw new Exception("新增镜像失败"); |
|
|
|
Image existingImage = getByName(imageVo.getName()); |
|
|
|
Image imageToUse; |
|
|
|
if(existingImage == null) { |
|
|
|
// 如果不存在相同名称的镜像,则创建新的镜像记录 |
|
|
|
Image newImage = new Image(); |
|
|
|
newImage.setName(imageVo.getName()); |
|
|
|
newImage.setDescription(imageVo.getDescription()); |
|
|
|
newImage.setImageType(imageVo.getImageType()); |
|
|
|
imageToUse = this.insert(newImage); |
|
|
|
if (imageToUse == null) { |
|
|
|
throw new Exception("新增镜像失败"); |
|
|
|
} |
|
|
|
}else{ |
|
|
|
// 如果已存在相同名称的镜像,使用已存在的镜像 |
|
|
|
imageToUse = existingImage; |
|
|
|
} |
|
|
|
ImageVersion imageVersion = new ImageVersion(); |
|
|
|
imageVersion.setImageId(imageInsert.getId()); |
|
|
|
imageVersion.setImageId(imageToUse.getId()); |
|
|
|
imageVersion.setVersion(imageVo.getVersion()); |
|
|
|
imageVersion.setUrl(imageVo.getUrl()); |
|
|
|
imageVersion.setTagName(imageVo.getTagName()); |
|
|
|
imageVersion.setFileSize(imageVo.getFileSize()); |
|
|
|
imageVersion.setStatus("building"); |
|
|
|
ImageVersion imageVersionInsert = this.imageVersionService.insert(imageVersion); |
|
|
|
if (imageVersionInsert == null) { |
|
|
|
throw new Exception("新增镜像失败"); |
|
|
|
throw new Exception("新增镜像版本失败"); |
|
|
|
} |
|
|
|
// 使用CompletableFuture异步执行不同的镜像构建逻辑 |
|
|
|
CompletableFuture.supplyAsync(() -> { |
|
|
|
@@ -207,7 +228,7 @@ public class ImageServiceImpl implements ImageService { |
|
|
|
}).thenAccept(resultMap ->{ |
|
|
|
try { |
|
|
|
String imageUrl = resultMap.get("url"); |
|
|
|
String fileSize = resultMap.get("filesize"); |
|
|
|
String fileSize = resultMap.get("fileSize"); |
|
|
|
imageVersion.setUrl(imageUrl); |
|
|
|
imageVersion.setFileSize(fileSize); |
|
|
|
imageVersion.setStatus("available"); |
|
|
|
@@ -241,18 +262,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)); |
|
|
|
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 +304,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)); |
|
|
|
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; |
|
|
|
|