docker 镜像查询

This commit is contained in:
even 2025-06-24 19:07:43 +08:00
parent 8bc10fa999
commit 41bfb30296
6 changed files with 152 additions and 2 deletions

View File

@ -0,0 +1,26 @@
package cd.casic.ci.api;
import cd.casic.ci.process.process.service.docker.DockerService;
import cd.casic.framework.commons.pojo.CommonResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/docker")
public class DockerController {
@Resource
private DockerService dockerService;
@GetMapping("/imageNameListByResourceDetailId/{resourceDetailId}")
public CommonResult<List<String>> imageList(@PathVariable String resourceDetailId){
return CommonResult.success(dockerService.imageNameList(resourceDetailId));
}
@GetMapping("/imageListByResourceId/{resourceId}")
public CommonResult<List<String>> imageListByResourceId(@PathVariable String resourceId){
return CommonResult.success(dockerService.imageListByResourceId(resourceId));
}
}

View File

@ -2,5 +2,6 @@ package cd.casic.ci.process.engine.constant;
public class AFLSlotCompileConstant { public class AFLSlotCompileConstant {
public static final String MANAGER_ID = "managerId"; public static final String MANAGER_ID = "managerId";
public static final String IMAGE_NAME = "imageName";
public static final String COMMAND_SCRIPT ="buildScript"; public static final String COMMAND_SCRIPT ="buildScript";
} }

View File

@ -41,11 +41,12 @@ public class AFLSlotCompileWorker extends DockerWorker {
globalVariables.put(PipelineGlobalVariableConstant.AFL_DOCKER_WORK_DIR_KEY,workDir); globalVariables.put(PipelineGlobalVariableConstant.AFL_DOCKER_WORK_DIR_KEY,workDir);
} }
Map<String, Object> taskProperties = task.getTaskProperties(); Map<String, Object> taskProperties = task.getTaskProperties();
String managerId = taskProperties.get(AFLSlotCompileConstant.MANAGER_ID) instanceof String ? ((String) taskProperties.get("managerId")) : null; String managerId = taskProperties.get(AFLSlotCompileConstant.MANAGER_ID) instanceof String ? ((String) taskProperties.get(AFLSlotCompileConstant.MANAGER_ID)) : null;
// ssh 上传目标文件 // ssh 上传目标文件
ResourceFindResp resourceById = resourceManagerService.findResourceById(managerId); ResourceFindResp resourceById = resourceManagerService.findResourceById(managerId);
String machineId = resourceById.getMachineId(); String machineId = resourceById.getMachineId();
String dockerId = resourceById.getDockerId(); String dockerId = resourceById.getDockerId();
String imageName = taskProperties.get(AFLSlotCompileConstant.IMAGE_NAME) instanceof String ? ((String) taskProperties.get(AFLSlotCompileConstant.IMAGE_NAME)) : null;
if (StringUtils.isEmpty(machineId)||StringUtils.isEmpty(dockerId)) { if (StringUtils.isEmpty(machineId)||StringUtils.isEmpty(dockerId)) {
append(context,"该资源不支持docker或者ssh"); append(context,"该资源不支持docker或者ssh");
} }
@ -81,7 +82,7 @@ public class AFLSlotCompileWorker extends DockerWorker {
toBadEnding(); toBadEnding();
} }
// 执行预设命令 ,进入目录 // 执行预设命令 ,进入目录
String allCommand = "docker run -v "+ PipelineGlobalVariableConstant.AFL_VOLUME_WORK_DIR_PREFIX +":"+PipelineGlobalVariableConstant.AFL_DOCKER_BASE_DIR+" -it aflplusplus bash\n" + String allCommand = "docker run -v "+ PipelineGlobalVariableConstant.AFL_VOLUME_WORK_DIR_PREFIX +":"+PipelineGlobalVariableConstant.AFL_DOCKER_BASE_DIR+" -it "+imageName+" bash\n" +
"cd "+PipelineGlobalVariableConstant.AFL_DOCKER_BASE_DIR+"\n"+// 进入到容器中的卷挂载目录 "cd "+PipelineGlobalVariableConstant.AFL_DOCKER_BASE_DIR+"\n"+// 进入到容器中的卷挂载目录
"mkdir -p "+workDir+"\n"+ "mkdir -p "+workDir+"\n"+
"cd "+workDir+"\n"; "cd "+workDir+"\n";

View File

@ -0,0 +1,10 @@
package cd.casic.ci.process.process.service.docker;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
public interface DockerService {
List<String> imageNameList(String resourceDetailId);
List<String> imageListByResourceId( String resourceId);
}

View File

@ -0,0 +1,86 @@
package cd.casic.ci.process.process.service.docker.impl;
import cd.casic.ci.process.dto.req.resource.ResourceQueryReq;
import cd.casic.ci.process.dto.resp.resource.ResourceFindResp;
import cd.casic.ci.process.dto.resp.taskResource.TaskResourceFindResp;
import cd.casic.ci.process.process.service.docker.DockerService;
import cd.casic.ci.process.process.service.resource.ResourceManagerService;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import cd.casic.module.execute.docker.dataobject.dto.DockerEndpointDo;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import jakarta.annotation.Resource;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import static java.lang.String.format;
@Service
public class DockerServiceImpl implements DockerService {
@Resource
private ResourceManagerService resourceManagerService;
@Override
public List<String> imageNameList(String resourceDetailId) {
ResourceQueryReq req = new ResourceQueryReq();
req.setType("docker");
req.setId(resourceDetailId);
TaskResourceFindResp resourceListByType = resourceManagerService.findResourceListByType(req);
List<DockerEndpointDo> dockerEndpointList = resourceListByType.getDockerEndpointList();
if (CollectionUtils.isEmpty(dockerEndpointList)) {
return new ArrayList<>();
}
DockerEndpointDo dockerInfo = dockerEndpointList.get(0);
URI uri = null;
try {
uri = new URI(format("tcp://%s:%s", dockerInfo.getHost(), dockerInfo.getPort()));
} catch (URISyntaxException e) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
}
ApacheDockerHttpClient httpClient = new ApacheDockerHttpClient.Builder().dockerHost(uri).build();
DockerClient build = DockerClientBuilder.getInstance().withDockerHttpClient(httpClient).build();
List<Image> exec = build.listImagesCmd().exec();
List<String> res = new LinkedList<>();
for (Image image : exec) {
String[] repoTags = image.getRepoTags();
if (image.getRepoTags() != null && repoTags.length > 0) {
res.add(repoTags[0]);
}
}
return res;
}
@Override
public List<String> imageListByResourceId(String resourceId) {
ResourceFindResp resourceById = resourceManagerService.findResourceById(resourceId);
DockerEndpointDo dockerInfo = resourceById.getDockerEndpoint();
URI uri = null;
try {
uri = new URI(format("tcp://%s:%s", dockerInfo.getHost(), dockerInfo.getPort()));
} catch (URISyntaxException e) {
throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
}
ApacheDockerHttpClient httpClient = new ApacheDockerHttpClient.Builder().dockerHost(uri).build();
DockerClient build = DockerClientBuilder.getInstance().withDockerHttpClient(httpClient).build();
List<Image> exec = build.listImagesCmd().exec();
List<String> res = new LinkedList<>();
for (Image image : exec) {
String[] repoTags = image.getRepoTags();
if (image.getRepoTags() != null && repoTags.length > 0) {
res.add(repoTags[0]);
}
}
return res;
}
}

View File

@ -5,6 +5,7 @@ import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.model.Bind; import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Frame; import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DockerClientBuilder; import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -15,7 +16,11 @@ import org.springframework.test.context.ActiveProfiles;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static java.lang.String.format;
@SpringBootTest(classes = {OpsServerApplication.class}) @SpringBootTest(classes = {OpsServerApplication.class})
@ActiveProfiles("local") @ActiveProfiles("local")
public class DockerTest { public class DockerTest {
@ -192,5 +197,26 @@ public class DockerTest {
System.err.println("---------------------------"); System.err.println("---------------------------");
} }
} }
@Test
public void imageList() throws URISyntaxException {
URI uri = new URI(format("tcp://%s:%s", "175.6.27.228", "22375"));
ApacheDockerHttpClient httpClient = new ApacheDockerHttpClient.Builder().dockerHost(uri).build();
DockerClient build = DockerClientBuilder.getInstance().withDockerHttpClient(httpClient).build();
List<Image> exec = build.listImagesCmd().exec();
for (Image image : exec) {
String[] repoTags = image.getRepoTags();
if (repoTags ==null) {
System.out.println("虚悬镜像");
} else if (repoTags.length>1){
System.out.println("多标签1镜像");
} else {
System.out.println("");
}
}
System.out.println(exec);
}
} }