|
|
|
@@ -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,26 +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.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(() -> { |
|
|
|
@@ -206,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"); |
|
|
|
@@ -240,7 +262,7 @@ 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.lastIndexOf(harborUrl), logs2.length()); |
|
|
|
String substring = logs2.substring(logs2.lastIndexOf(harborUrl)); |
|
|
|
String cleanedString = substring.replaceAll("(\\r|\\n)", ""); |
|
|
|
String tagCmd = "docker tag " + cleanedString + " " + harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; |
|
|
|
String imageUrl = harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; |
|
|
|
@@ -282,7 +304,7 @@ 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.lastIndexOf(harborUrl), logs2.length()); |
|
|
|
String substring = logs2.substring(logs2.lastIndexOf(harborUrl)); |
|
|
|
String cleanedString = substring.replaceAll("(\\r|\\n)", ""); |
|
|
|
String tagCmd = "docker tag " + cleanedString + " " + harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; |
|
|
|
String imageUrl = harborUrl + "/" + repository + "/" + username + "/" + imageName + ":" + imageTag; |
|
|
|
|