Browse Source

1、优化保存镜像方法

pull/127/head
chenzhihang 1 year ago
parent
commit
86511836d7
3 changed files with 25 additions and 19 deletions
  1. +12
    -13
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java
  2. +1
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java
  3. +12
    -5
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java

+ 12
- 13
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java View File

@@ -45,7 +45,7 @@ public class ImageController extends BaseController {
@RequestParam("size") int size, @RequestParam("size") int size,
@RequestParam(value = "image_type") int imageType) { @RequestParam(value = "image_type") int imageType) {
image.setImageType(imageType); image.setImageType(imageType);
PageRequest pageRequest = PageRequest.of(page,size);
PageRequest pageRequest = PageRequest.of(page, size);
return genericsSuccess(this.imageService.queryByPage(image, pageRequest)); return genericsSuccess(this.imageService.queryByPage(image, pageRequest));
} }


@@ -72,6 +72,7 @@ public class ImageController extends BaseController {
public GenericsAjaxResult<Page<Image>> queryByName(@PathVariable("name") String name) { public GenericsAjaxResult<Page<Image>> queryByName(@PathVariable("name") String name) {
return genericsSuccess(this.imageService.queryByName(name)); return genericsSuccess(this.imageService.queryByName(name));
} }

/** /**
* 新增数据 * 新增数据
* *
@@ -84,9 +85,8 @@ public class ImageController extends BaseController {
} }


/** /**
*
* @param imageVo 实体 * @param imageVo 实体
* 新增镜像和版本 @PostMapping
* 新增镜像和版本 @PostMapping
* @return 新增结果 * @return 新增结果
*/ */
@PostMapping("/addImageAndVersion") @PostMapping("/addImageAndVersion")
@@ -122,21 +122,20 @@ public class ImageController extends BaseController {
@PostMapping("/net") @PostMapping("/net")
@ApiOperation("从网络上传构建镜像") @ApiOperation("从网络上传构建镜像")
public GenericsAjaxResult<Map<String, String>> createImageFromNet(@RequestParam("name") String imageName, public GenericsAjaxResult<Map<String, String>> createImageFromNet(@RequestParam("name") String imageName,
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromNet(imageName,imageTag,path));
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromNet(imageName, imageTag, path));
} }


@PostMapping("/local") @PostMapping("/local")
@ApiOperation("从本地上传构建镜像") @ApiOperation("从本地上传构建镜像")
public GenericsAjaxResult<Map<String, String>> createImageFromLocal(@RequestParam("name") String imageName, public GenericsAjaxResult<Map<String, String>> createImageFromLocal(@RequestParam("name") String imageName,
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromLocal(imageName,imageTag,path));
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromLocal(imageName, imageTag, path));
} }





/** /**
* 镜像上传 * 镜像上传
* *
@@ -145,14 +144,14 @@ public class ImageController extends BaseController {
@PostMapping("/upload") @PostMapping("/upload")
@ApiOperation(value = "上传镜像文件", notes = "上传镜像tar包,返回存储路径") @ApiOperation(value = "上传镜像文件", notes = "上传镜像tar包,返回存储路径")
public GenericsAjaxResult<Map<String, String>> uploadImageFiles(@RequestParam("file") MultipartFile file) throws Exception { public GenericsAjaxResult<Map<String, String>> uploadImageFiles(@RequestParam("file") MultipartFile file) throws Exception {
return genericsSuccess(this.imageService.uploadImageFiles(file));
return genericsSuccess(this.imageService.uploadImageFiles(file));
} }




@PostMapping("/saveImage") @PostMapping("/saveImage")
@ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存,并推送到horbor") @ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存,并推送到horbor")
public GenericsAjaxResult<String> saveImage(@RequestBody ImageVo imageVo){
return genericsSuccess(this.imageService.saveImage(imageVo));
public void saveImage(@RequestBody ImageVo imageVo) {
this.imageService.saveImage(imageVo);
} }
} }



+ 1
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java View File

@@ -93,5 +93,5 @@ public interface ImageService {
Map<String, String> createImageFromNet(String imageName, String imageTag, String NetPath) throws Exception; Map<String, String> createImageFromNet(String imageName, String imageTag, String NetPath) throws Exception;
Map<String, String> uploadImageFiles(MultipartFile file) throws Exception; Map<String, String> uploadImageFiles(MultipartFile file) throws Exception;


String saveImage(ImageVo imageVo);
void saveImage(ImageVo imageVo);
} }

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

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -367,10 +368,18 @@ public class ImageServiceImpl implements ImageService {


@Override @Override
@Transactional @Transactional
public String saveImage(ImageVo imageVo) {
if (imageDao.getByName(imageVo.getName()) != null) {
throw new IllegalStateException("镜像名称已存在");
@Async
public void saveImage(ImageVo imageVo) {
Image oldImage = imageDao.getByName(imageVo.getName());
if (oldImage != null) {
List<ImageVersion> oldImageVersions = imageVersionDao.queryByImageId(oldImage.getId());
for (ImageVersion oldImageVersion : oldImageVersions) {
if(oldImageVersion.getTagName().equals(imageVo.getTagName())){
throw new IllegalStateException("镜像tag不能重复");
}
}
} }

LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser loginUser = SecurityUtils.getLoginUser();
String username = loginUser.getUsername().toLowerCase(); String username = loginUser.getUsername().toLowerCase();
String podName = username + "-editor-pod" + "-" + imageVo.getDevEnvironmentId().toString(); String podName = username + "-editor-pod" + "-" + imageVo.getDevEnvironmentId().toString();
@@ -412,8 +421,6 @@ public class ImageServiceImpl implements ImageService {
devEnvironment.setId(imageVo.getDevEnvironmentId()); devEnvironment.setId(imageVo.getDevEnvironmentId());
devEnvironment.setImage(resultMap.get("imageName")); devEnvironment.setImage(resultMap.get("imageName"));
devEnvironmentDao.update(devEnvironment); devEnvironmentDao.update(devEnvironment);

return "保存镜像成功";
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("保存镜像失败:" + e); throw new RuntimeException("保存镜像失败:" + e);
} }


Loading…
Cancel
Save