|
|
@@ -0,0 +1,105 @@
|
|
|
+package cn.reghao.bnt.web.devops.machine.service;
|
|
|
+
|
|
|
+import cn.reghao.bnt.common.docker.DockerImpl;
|
|
|
+import cn.reghao.bnt.web.devops.machine.model.vo.DockerContainer;
|
|
|
+import cn.reghao.bnt.web.devops.machine.model.vo.DockerImage;
|
|
|
+import cn.reghao.bnt.web.devops.machine.model.dto.ContainerOps;
|
|
|
+import cn.reghao.bnt.web.devops.machine.model.dto.ImageDelete;
|
|
|
+import cn.reghao.bnt.web.devops.machine.model.dto.ImageQuery;
|
|
|
+import com.github.dockerjava.api.command.InspectContainerResponse;
|
|
|
+import com.github.dockerjava.api.model.Image;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2025-12-09 11:01:17
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class DockerService {
|
|
|
+ private final DockerImpl docker = new DockerImpl();
|
|
|
+
|
|
|
+ public List<DockerImage> getDockerImages(ImageQuery imageQuery) {
|
|
|
+ String keyword = imageQuery.getKeyword();
|
|
|
+ int type = imageQuery.getType();
|
|
|
+ // imageId -> container
|
|
|
+ Map<String, List<InspectContainerResponse>> containerMap = docker.psAll().stream()
|
|
|
+ .collect(Collectors.groupingBy(InspectContainerResponse::getImageId));
|
|
|
+ return docker.images().stream()
|
|
|
+ .map(image -> {
|
|
|
+ String repoTag = image.getRepoTags().length > 0 ? image.getRepoTags()[0] : "<none>:<none>";
|
|
|
+ boolean matched = false;
|
|
|
+ if (keyword != null && !keyword.isBlank()) {
|
|
|
+ if (type == 1) {
|
|
|
+ // 关键字匹配
|
|
|
+ matched = repoTag.contains(keyword);
|
|
|
+ } else if (type == 2) {
|
|
|
+ // 前缀匹配
|
|
|
+ matched = repoTag.startsWith(keyword);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!matched) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int totalContainers = 0;
|
|
|
+ String imageId = image.getId();
|
|
|
+ List<InspectContainerResponse> list = containerMap.get(imageId);
|
|
|
+ if (list != null) {
|
|
|
+ totalContainers = list.size();
|
|
|
+ }
|
|
|
+ return new DockerImage(image, repoTag, totalContainers);
|
|
|
+ })
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void rmDockerImages(ImageDelete imageDelete) {
|
|
|
+ List<String> imageIds = imageDelete.getImageIds();
|
|
|
+ imageIds.forEach(docker::imageRm);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<DockerContainer> getDockerContainers() {
|
|
|
+ Map<String, Image> imageMap = docker.images().stream()
|
|
|
+ .collect(Collectors.toMap(Image::getId, v -> v));
|
|
|
+ return docker.psAll().stream().map(inspectContainerResponse -> {
|
|
|
+ String imageId = inspectContainerResponse.getImageId();
|
|
|
+ Image image = imageMap.get(imageId);
|
|
|
+ String repoTag = image.getRepoTags().length > 0 ? image.getRepoTags()[0] : "<none>:<none>";
|
|
|
+ return new DockerContainer(inspectContainerResponse, repoTag);
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void handleDockerContainer(ContainerOps containerOps) {
|
|
|
+ int type = containerOps.getOpsType();
|
|
|
+ String containerId = containerOps.getContainerId();
|
|
|
+ // containerId -> InspectContainerResponse
|
|
|
+ Map<String, InspectContainerResponse> containerMap = docker.psAll().stream()
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
+ InspectContainerResponse::getId,
|
|
|
+ Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0))
|
|
|
+ ));
|
|
|
+
|
|
|
+ InspectContainerResponse response = containerMap.get(containerId);
|
|
|
+ if (response == null) {
|
|
|
+ log.error("containerId {} not exist", containerId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String containerName = response.getName().replace("/", "");
|
|
|
+ if (type == 1) {
|
|
|
+ docker.start(containerName);
|
|
|
+ } else if (type == 2) {
|
|
|
+ docker.stop(containerName);
|
|
|
+ } else if (type == 3) {
|
|
|
+ docker.rm(containerId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|