Search in sources :

Example 1 with ContainerImage

use of org.edgegallery.developer.model.resource.container.ContainerImage in project developer-be by EdgeGallery.

the class ContainerImageServiceImpl method createImage.

private boolean createImage(String repoTags, String inputImageId, String fileName, String projectName) {
    String[] images = repoTags.split(":");
    String imageName = images[0];
    String imageVersion = images[1];
    String uploadImgPath = devRepoEndpoint + "/" + projectName + "/" + imageName + ":" + imageVersion;
    LOGGER.warn("uploadImgPath : {}", uploadImgPath);
    ContainerImage containerImage = new ContainerImage();
    containerImage.setImageId(inputImageId);
    containerImage.setImageType("private");
    containerImage.setImageName(imageName.trim());
    containerImage.setImageVersion(imageVersion.trim());
    containerImage.setImagePath(uploadImgPath);
    containerImage.setUserId(AccessUserUtil.getUser().getUserId());
    containerImage.setUserName(AccessUserUtil.getUser().getUserName());
    containerImage.setFileName(fileName);
    ContainerImage either = createContainerImage(containerImage);
    if (either == null) {
        LOGGER.error("create harbor image db record failed!");
        return false;
    }
    return true;
}
Also used : ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage)

Example 2 with ContainerImage

use of org.edgegallery.developer.model.resource.container.ContainerImage in project developer-be by EdgeGallery.

the class ContainerImageServiceImpl method deleteContainerImage.

/**
 * delete image.
 *
 * @param imageId imageId
 * @return
 */
@Override
public Boolean deleteContainerImage(String imageId) {
    String loginUserId = AccessUserUtil.getUser().getUserId();
    ContainerImage oldImage = containerImageMapper.getContainerImage(imageId);
    int retCode;
    boolean isDel;
    if (!VMImageUtil.isAdminUser() && !loginUserId.equals(oldImage.getUserId())) {
        String errorMsg = "Cannot delete data created by others";
        LOGGER.error(errorMsg);
        throw new ForbiddenException(errorMsg, ResponseConsts.RET_REQUEST_FORBIDDEN);
    }
    isDel = ContainerImageUtil.deleteImage(oldImage.getImagePath(), oldImage.getUserName());
    retCode = containerImageMapper.deleteContainerImageById(imageId);
    LOGGER.warn("isDel {}", isDel);
    LOGGER.warn("retcode {}", retCode);
    if (!isDel) {
        String errorMsg = "delete ContainerImage image from harbor failed.";
        LOGGER.error(errorMsg);
        throw new FileOperateException(errorMsg, ResponseConsts.RET_DELETE_FILE_FAIL);
    }
    if (retCode < 1) {
        String errorMsg = "delete ContainerImage record failed.";
        LOGGER.error(errorMsg);
        throw new DataBaseException(errorMsg, ResponseConsts.RET_DELETE_DATA_FAIL);
    }
    LOGGER.info("delete ContainerImage success");
    return true;
}
Also used : ForbiddenException(org.edgegallery.developer.exception.ForbiddenException) ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage) DataBaseException(org.edgegallery.developer.exception.DataBaseException) FileOperateException(org.edgegallery.developer.exception.FileOperateException)

Example 3 with ContainerImage

use of org.edgegallery.developer.model.resource.container.ContainerImage in project developer-be by EdgeGallery.

the class ContainerImageServiceImpl method synchronizeHarborImage.

/**
 * synchronizeHarborImage.
 *
 * @return
 */
@Override
public ResponseEntity synchronizeHarborImage() {
    LOGGER.info("begin synchronize image...");
    // get imagePath list from db
    List<ContainerImage> containerImages = containerImageMapper.getAllImageByAdmin();
    List<String> list = new ArrayList<>();
    if (!CollectionUtils.isEmpty(containerImages)) {
        for (ContainerImage containerImage : containerImages) {
            String image = containerImage.getImagePath();
            if (StringUtils.isNotEmpty(image)) {
                list.add(image.substring(image.lastIndexOf("/") + 1).trim());
            }
        }
    }
    LOGGER.warn("list: {}", list);
    // get Harbor image list
    List<String> harborList = ContainerImageUtil.getHarborImageList();
    if (CollectionUtils.isEmpty(harborList)) {
        LOGGER.warn("harbor repo no images!");
        return ResponseEntity.ok("harbor repo no images!");
    }
    LOGGER.warn("harborList: {}", harborList);
    List<String> imageList = new ArrayList<>();
    for (String harbor : harborList) {
        imageList.add(harbor.substring(harbor.indexOf("/") + 1, harbor.indexOf("+")).trim());
    }
    LOGGER.warn("imageList: {}", imageList);
    LOGGER.warn("bijiao: {}", ListUtil.isEquals(list, imageList));
    LOGGER.warn("bijiao2: {}", list.containsAll(imageList));
    List<String> compareList = new ArrayList<>();
    if (ListUtil.isEquals(list, imageList) || list.containsAll(imageList)) {
        LOGGER.warn("no need synchronize!");
        return ResponseEntity.ok("already the latest image list!");
    } else {
        imageList.removeAll(list);
        for (int i = 0; i < imageList.size(); i++) {
            for (int j = 0; j < harborList.size(); j++) {
                String harbor = harborList.get(j).substring(harborList.get(j).indexOf("/") + 1, harborList.get(j).indexOf("+")).trim();
                if (imageList.get(i).equals(harbor)) {
                    compareList.add(harborList.get(j));
                }
            }
        }
    }
    LOGGER.warn("compareList: {}", compareList);
    if (!CollectionUtils.isEmpty(compareList)) {
        for (String harborImage : compareList) {
            ContainerImage containerImage = new ContainerImage();
            containerImage.setImageId(UUID.randomUUID().toString());
            String imageName = harborImage.substring(harborImage.indexOf("/") + 1, harborImage.indexOf(":"));
            containerImage.setImageName(imageName);
            containerImage.setImageVersion(harborImage.substring(harborImage.indexOf(":") + 1, harborImage.indexOf("+")));
            containerImage.setUserId(AccessUserUtil.getUser().getUserId());
            containerImage.setUserName(AccessUserUtil.getUser().getUserName());
            String pushTime = harborImage.substring(harborImage.indexOf("+") + 1);
            containerImage.setUploadTime(new Date(Instant.parse(pushTime).toEpochMilli()));
            containerImage.setCreateTime(new Date());
            containerImage.setImageType("private");
            containerImage.setImagePath(devRepoEndpoint + "/" + harborImage.substring(0, harborImage.indexOf("+")));
            containerImage.setImageStatus(EnumContainerImageStatus.UPLOAD_SUCCEED);
            containerImage.setFileName(imageName + ".tar");
            int res = containerImageMapper.createContainerImage(containerImage);
            if (res < 1) {
                LOGGER.error("create container image failed!");
                throw new DataBaseException("create container image failed", ResponseConsts.RET_CREATE_FILE_FAIL);
            }
        }
        LOGGER.info("end synchronize image...");
    }
    return ResponseEntity.ok("synchronized successfully!");
}
Also used : ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage) ArrayList(java.util.ArrayList) DataBaseException(org.edgegallery.developer.exception.DataBaseException) Date(java.util.Date)

Example 4 with ContainerImage

use of org.edgegallery.developer.model.resource.container.ContainerImage in project developer-be by EdgeGallery.

the class ContainerImageServiceImpl method updateContainerImage.

/**
 * updateContainerImage.
 *
 * @param imageId imageId
 * @param containerImage containerImage
 * @return
 */
@Override
public ContainerImage updateContainerImage(String imageId, ContainerImage containerImage) {
    String loginUserId = AccessUserUtil.getUser().getUserId();
    ContainerImage oldImage = containerImageMapper.getContainerImage(imageId);
    String oldUserId = oldImage.getUserId();
    if (StringUtils.isNotEmpty(oldUserId) && !loginUserId.equals(oldImage.getUserId())) {
        String errorMsg = "Cannot modify data created by others";
        LOGGER.error(errorMsg);
        throw new ForbiddenException(errorMsg, ResponseConsts.RET_REQUEST_FORBIDDEN);
    }
    String type = containerImage.getImageType();
    int retCode = 0;
    if (StringUtils.isNotEmpty(oldUserId) && StringUtils.isNotEmpty(type)) {
        retCode = containerImageMapper.updateContainerImageType(imageId, oldUserId, type);
    }
    if (retCode < 1) {
        String errorMsg = "update ContainerImage type failed.";
        LOGGER.error(errorMsg);
        throw new DataBaseException(errorMsg, ResponseConsts.RET_UPDATE_DATA_FAIL);
    }
    LOGGER.info("update ContainerImage type success");
    return containerImageMapper.getContainerImage(imageId);
}
Also used : ForbiddenException(org.edgegallery.developer.exception.ForbiddenException) ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage) DataBaseException(org.edgegallery.developer.exception.DataBaseException)

Example 5 with ContainerImage

use of org.edgegallery.developer.model.resource.container.ContainerImage in project developer-be by EdgeGallery.

the class ContainerImageServiceTest method testCreateContainerImageSuccess.

@Test
public void testCreateContainerImageSuccess() {
    ContainerImage containerImage = new ContainerImage();
    containerImage.setImageId(UUID.randomUUID().toString());
    containerImage.setImageName("test");
    containerImage.setImageVersion("1.0");
    containerImage.setImageType("private");
    containerImage.setImageStatus(EnumContainerImageStatus.UPLOAD_SUCCEED);
    containerImage.setUserName("admin");
    containerImage.setUserId(UUID.randomUUID().toString());
    containerImage.setCreateTime(new Date());
    containerImage.setUploadTime(new Date());
    containerImage.setImagePath("xxx/xxx/test:1.0");
    containerImage.setFileName("test.tar");
    ContainerImage response = containerImageService.createContainerImage(containerImage);
    Assert.assertNotNull(response);
}
Also used : ContainerImage(org.edgegallery.developer.model.resource.container.ContainerImage) Date(java.util.Date) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ContainerImage (org.edgegallery.developer.model.resource.container.ContainerImage)15 Test (org.junit.Test)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 DataBaseException (org.edgegallery.developer.exception.DataBaseException)5 Date (java.util.Date)4 ForbiddenException (org.edgegallery.developer.exception.ForbiddenException)3 IllegalRequestException (org.edgegallery.developer.exception.IllegalRequestException)3 ContainerImageReq (org.edgegallery.developer.model.resource.container.ContainerImageReq)3 Gson (com.google.gson.Gson)2 ArrayList (java.util.ArrayList)2 FileOperateException (org.edgegallery.developer.exception.FileOperateException)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 ResultActions (org.springframework.test.web.servlet.ResultActions)2 DockerClient (com.github.dockerjava.api.DockerClient)1 PullImageResultCallback (com.github.dockerjava.api.command.PullImageResultCallback)1 SaveImageCmd (com.github.dockerjava.api.command.SaveImageCmd)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Page (org.edgegallery.developer.model.common.Page)1