Browse Source

网络镜像上传1个接口

pull/14/head
fanshuai 1 year ago
parent
commit
9b54f4b673
3 changed files with 43 additions and 3 deletions
  1. +8
    -0
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java
  2. +3
    -3
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java
  3. +32
    -0
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java

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

@@ -118,6 +118,14 @@ public class ImageController {
return AjaxResult.success(this.imageService.removeById(id));
}

@PostMapping("/net")
@ApiOperation("从本地上传构建镜像")
public AjaxResult createImageFromNet(@RequestParam("name") String imageName,
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return AjaxResult.success(this.imageService.createImageFromNet(imageName,imageTag,path));
}

@PostMapping("/local")
@ApiOperation("从本地上传构建镜像")
public AjaxResult createImageFromLocal(@RequestParam("name") String imageName,


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

@@ -71,11 +71,11 @@ public interface ImageService {
* 本地上传构建镜像
* @param imageName
* @param imageTag
* @param imageDescription
* @param path
* @return
*/
String createImageFromLocal(String imageName, String imageTag, String imageDescription) throws Exception;
String createImageFromLocal(String imageName, String imageTag, String path) throws Exception;
String createImageFromNet(String imageName, String imageTag, String NetPath) throws Exception;
Map<String, String> uploadImageFiles(MultipartFile file) throws Exception;

}

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

@@ -191,6 +191,38 @@ public class ImageServiceImpl implements ImageService {
return "新增镜像成功";
}

@Override
public String createImageFromNet(String imageName, String imageTag, String netPath) throws Exception {
// 得到容器
V1Pod pod = K8sClientUtil.getNSPodList(serviceNS, deploymentName);
if (pod == null) {
throw new Exception("镜像推送服务不存在");
}
String loginCmd = "docker login -u " + harborUser +" -p "+harborpassword+" "+harborUrl;
// 执行命令 docker login -u admin -p Harbor12345 172.20.32.187
String loginlog = K8sClientUtil.executeCommand(pod,loginCmd);
// 在这个容器的/data/admin 目录下执行命令 docker load -i fileName 得到返回的镜像名字name:tag
String username = SecurityUtils.getLoginUser().getUsername();
//
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 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 s = K8sClientUtil.executeCommand(pod, cmd1);
if (StringUtils.isNotEmpty(K8sClientUtil.executeCommand(pod, cmd2))){
return imageUrl;
}else {
throw new Exception("拉取公网镜像失败,请检查网络或者镜像地址");
}
}else {
throw new Exception("拉取公网镜像失败,请检查网络或者镜像地址");
}
}

@Override
public String createImageFromLocal(String imageName, String imageTag, String path) throws Exception {
// 得到容器


Loading…
Cancel
Save