From c594dca0ec33f6a26043e4fc26610ecba13e4825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A5=BF=E5=A4=A7=E9=94=90?= <1070211640@qq.com> Date: Tue, 27 Aug 2024 15:55:13 +0800 Subject: [PATCH 1/6] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E9=95=9C=E5=83=8F=E5=90=AF=E5=8A=A8=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82=202=E3=80=81pod=E9=80=89=E6=8B=A9GP?= =?UTF-8?q?U=E8=8A=82=E7=82=B9=E9=83=A8=E7=BD=B2=E3=80=82=203=E3=80=81?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BF=9D=E5=AD=98=E9=95=9C=E5=83=8F=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-modules/management-platform/pom.xml | 13 ++- .../controller/image/ImageController.java | 9 +- .../ruoyi/platform/service/ImageService.java | 3 +- .../service/impl/ImageServiceImpl.java | 29 ++++++ .../service/impl/JupyterServiceImpl.java | 2 +- .../platform/utils/DockerClientUtil.java | 98 +++++++++++++++++++ .../ruoyi/platform/utils/K8sClientUtil.java | 28 +++++- .../java/com/ruoyi/platform/vo/ImageVo.java | 14 ++- ruoyi-modules/ruoyi-job/pom.xml | 10 +- 9 files changed, 196 insertions(+), 10 deletions(-) create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java diff --git a/ruoyi-modules/management-platform/pom.xml b/ruoyi-modules/management-platform/pom.xml index 37234ad0..c004f9ae 100644 --- a/ruoyi-modules/management-platform/pom.xml +++ b/ruoyi-modules/management-platform/pom.xml @@ -140,7 +140,12 @@ com.github.docker-java docker-java - 3.1.1 + 3.2.13 + + + com.github.docker-java + docker-java-transport-httpclient5 + 3.2.13 com.baomidou @@ -216,6 +221,12 @@ 3.0.8 compile + + commons-lang + commons-lang + 2.6 + compile + diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java index a0b117d8..d389bad8 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java @@ -78,16 +78,15 @@ public class ImageController extends BaseController { * @param image 实体 * @return 新增结果 */ - @PostMapping @ApiOperation("新增镜像,不包含镜像版本") public GenericsAjaxResult add(@RequestBody Image image) { return genericsSuccess(this.imageService.insert(image)); } /** - * 新增镜像和版本 * * @param imageVo 实体 + * 新增镜像和版本 @PostMapping * @return 新增结果 */ @PostMapping("/addImageAndVersion") @@ -149,5 +148,11 @@ public class ImageController extends BaseController { return genericsSuccess(this.imageService.uploadImageFiles(file)); } + + @PostMapping("/saveImage") + @ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存,并推送到horbor") + public GenericsAjaxResult saveImage(ImageVo imageVo){ + return genericsSuccess(this.imageService.saveImage(imageVo)); + } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java index 59614ac9..fb8984ed 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ImageService.java @@ -93,6 +93,5 @@ public interface ImageService { Map createImageFromNet(String imageName, String imageTag, String NetPath) throws Exception; Map uploadImageFiles(MultipartFile file) throws Exception; - - + String saveImage(ImageVo imageVo); } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java index d95609ee..17bfdd48 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java @@ -9,12 +9,14 @@ import com.ruoyi.platform.mapper.ImageVersionDao; import com.ruoyi.platform.service.ImageService; import com.ruoyi.platform.service.ImageVersionService; import com.ruoyi.platform.service.MinioService; +import com.ruoyi.platform.utils.DockerClientUtil; import com.ruoyi.platform.utils.FileUtil; import com.ruoyi.platform.utils.K8sClientUtil; import com.ruoyi.platform.vo.ImageVo; import com.ruoyi.system.api.model.LoginUser; import io.kubernetes.client.openapi.models.V1PersistentVolumeClaim; import io.kubernetes.client.openapi.models.V1Pod; +import lombok.Synchronized; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; @@ -50,6 +52,9 @@ public class ImageServiceImpl implements ImageService { private ImageVersionDao imageVersionDao; @Resource private K8sClientUtil k8sClientUtil; + @Resource + private DockerClientUtil dockerClientUtil; + @Resource private MinioService minioService; @Value("${harbor.bucketName}") @@ -75,6 +80,8 @@ public class ImageServiceImpl implements ImageService { private String proxyUrl; @Value("${minio.pvcName}") private String pvcName; + @Value("${jupyter.namespace}") + private String namespace; /** * 通过ID查询单条数据 * @@ -350,4 +357,26 @@ public class ImageServiceImpl implements ImageService { String path = loginUser.getUsername()+"/"+file.getOriginalFilename(); return minioService.uploadFile(bucketName, path, file); } + + @Override + @Synchronized + public String saveImage(ImageVo imageVo) { + if(imageDao.getByName(imageVo.getName()) != null){ + throw new IllegalStateException("镜像名称已存在"); + } + LoginUser loginUser = SecurityUtils.getLoginUser(); + String username = loginUser.getUsername().toLowerCase(); + String podName = username +"-editor-pod" + "-" + imageVo.getDevEnvironmentId(); + + try { + String containerId = k8sClientUtil.getPodContainerId(podName, namespace); + String hostIp = k8sClientUtil.getHostIp(podName, namespace); + + dockerClientUtil.commitImage(imageVo,containerId,hostIp,username); + dockerClientUtil.pushImageToHorbor(imageVo,hostIp); + } catch (Exception e) { + throw new RuntimeException(e); + } + return null; + } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java index be55468a..aa59b6ae 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java @@ -100,7 +100,7 @@ public class JupyterServiceImpl implements JupyterService { //TODO 设置镜像可配置,这里先用默认镜像启动pod // 调用修改后的 createPod 方法,传入额外的参数 - Integer podPort = k8sClientUtil.createConfiguredPod(podName, namespace, port, mountPath, pvc, image, minioPvcName, datasetPath, modelPath); + Integer podPort = k8sClientUtil.createConfiguredPod(podName, namespace, port, mountPath, pvc, devEnvironment.getImage(), minioPvcName, datasetPath, modelPath); String url = masterIp + ":" + podPort; redisService.setCacheObject(podName,masterIp + ":" + podPort); devEnvironment.setStatus("Pending"); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java new file mode 100644 index 00000000..15a1134c --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java @@ -0,0 +1,98 @@ +package com.ruoyi.platform.utils; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.CommitCmd; +import com.github.dockerjava.api.model.AuthConfig; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientConfig; +import com.github.dockerjava.core.DockerClientImpl; +import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; +import com.github.dockerjava.transport.DockerHttpClient; +import com.ruoyi.platform.vo.ImageVo; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.time.Duration; + +@Slf4j +@Component +public class DockerClientUtil { + + @Value("${harbor.bucketName}") + private String bucketName; + @Value("${harbor.repository}") + private String repository; + @Value("${harbor.harborUrl}") + private String harborUrl; + @Value("${harbor.harborUser}") + private String harborUser; + @Value("${harbor.harborpassword}") + private String harborpassword; + + public DockerClient getDockerClient(String dockerServerUrl) { + + //创建DefaultDockerClientConfig(指定docker服务器的配置) + DockerClientConfig config = DefaultDockerClientConfig + .createDefaultConfigBuilder() + .withDockerHost("tcp://"+ dockerServerUrl +":2375") + .withDockerTlsVerify(false) + .withApiVersion("1.40") +// .withDockerCertPath(dcokerCertPath) +// .withRegistryUsername(registryUser) +// .withRegistryPassword(registryPass) +// .withRegistryEmail(registryMail) +// .withRegistryUrl(registryUrl) + .build(); + + //创建DockerHttpClient + DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder() + .dockerHost(config.getDockerHost()) + .sslConfig(config.getSSLConfig()) + .maxConnections(1000) + .connectionTimeout(Duration.ofSeconds(300)) + .responseTimeout(Duration.ofSeconds(450)) + .build(); + + //创建DockerClient + return DockerClientImpl.getInstance(config, httpClient); + + } + + public String commitImage(ImageVo imageVo, String containerId, String hostIp, String userName) { + DockerClient dockerClient = getDockerClient(hostIp); + +// dockerClient.startContainerCmd(containerId).exec(); + // 提交容器为镜像,这里的"new_image"和"new_tag"是新镜像的名字和标签 + CommitCmd commitCmd = dockerClient.commitCmd(containerId) + .withRepository(imageVo.getName()) + .withTag(imageVo.getTagName()) + .withAuthor(userName) + .withMessage(imageVo.getDescription()); + String exec = commitCmd.exec(); + return exec; + } + + public void pushImageToHorbor(ImageVo imageVo, String hostIp) { + + DockerClient dockerClient = getDockerClient(hostIp); + //Harbor登录信息 + AuthConfig autoConfig = new AuthConfig().withRegistryAddress(harborUrl).withUsername(harborUser).withPassword(harborpassword); + + + String localImageName = imageVo.getName() + ":" + imageVo.getTagName(); + String imageName = harborUrl + "/" + bucketName + "/" + imageVo.getName(); + + //给镜像打上tag + dockerClient.tagImageCmd(localImageName, imageName, imageVo.getTagName()).exec(); + //推送镜像至镜像仓库 + try { + dockerClient.pushImageCmd(imageName).withAuthConfig(autoConfig).start().awaitCompletion(); + //push成功后,删除本地加载的镜像 + dockerClient.removeImageCmd(localImageName).exec(); + } catch (InterruptedException e) { + throw new RuntimeException("推送镜像失败:"+e); + } + + } +} \ No newline at end of file diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java index 23903e2a..e2f1f6ac 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java @@ -136,7 +136,7 @@ public class K8sClientUtil { if (v1ServiceList!=null) { for (V1Service svc : v1ServiceList.getItems()) { if (StringUtils.equals(svc.getMetadata().getName(), serviceName)) { - // PVC 已存在 + // SVC 已存在 return svc; } } @@ -380,6 +380,9 @@ public class K8sClientUtil { Map selector = new LinkedHashMap<>(); selector.put("k8s-jupyter", podName); + Map nodeSelector = new LinkedHashMap<>(); + nodeSelector.put("resource-type", "CPU-GPU"); + CoreV1Api api = new CoreV1Api(apiClient); V1PodList v1PodList = null; try { @@ -423,6 +426,7 @@ public class K8sClientUtil { .withVolumeMounts(volumeMounts) .endContainer() .withVolumes(volumes) + .withNodeSelector(nodeSelector) .endSpec() .build(); @@ -506,6 +510,28 @@ public class K8sClientUtil { return pod.getStatus().getPhase(); } + /** + * 根据Pod的名称和Namespace查询Pod的容器信息 + * @param podName Pod的名称 + * @param namespace Pod所在的Namespace + */ + public String getPodContainerId(String podName, String namespace) throws Exception { + CoreV1Api api = new CoreV1Api(apiClient); + V1Pod pod = api.readNamespacedPod(podName, namespace, null, null, null); + + if(pod.getStatus().getContainerStatuses().size() !=1){ + throw new RuntimeException("容器错误"); + } + String containerId = pod.getStatus().getContainerStatuses().get(0).getContainerID().split("//")[1]; + return containerId; + } + + public String getHostIp(String podName, String namespace) throws Exception { + CoreV1Api api = new CoreV1Api(apiClient); + V1Pod pod = api.readNamespacedPod(podName, namespace, null, null, null); + return pod.getStatus().getHostIP(); + } + public String getPodLogs(String podName,String namespace,String container,int line) { CoreV1Api api = new CoreV1Api(apiClient); try { diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java index c4b313c2..6ee88868 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java @@ -43,7 +43,7 @@ public class ImageVo implements Serializable { /** * 镜像tag名称 */ - @ApiModelProperty(name = "tag_name") + @ApiModelProperty(name = "tagName") private String tagName; /** @@ -64,6 +64,8 @@ public class ImageVo implements Serializable { private String path; + @ApiModelProperty(value = "环境id") + private String devEnvironmentId; // public Integer getId() { // return id; // } @@ -147,7 +149,17 @@ public class ImageVo implements Serializable { public String getPath() { return path; } + public void setPath(String path) { this.path = path; } + + public String getDevEnvironmentId() { + return devEnvironmentId; + } + + public void setDevEnvironmentId(String devEnvironmentId) { + this.devEnvironmentId = devEnvironmentId; + } + } diff --git a/ruoyi-modules/ruoyi-job/pom.xml b/ruoyi-modules/ruoyi-job/pom.xml index e295d995..7b22bc72 100644 --- a/ruoyi-modules/ruoyi-job/pom.xml +++ b/ruoyi-modules/ruoyi-job/pom.xml @@ -65,7 +65,7 @@ com.mysql mysql-connector-j - + com.ruoyi @@ -77,7 +77,13 @@ com.ruoyi ruoyi-common-swagger - + + + + com.ruoyi + ruoyi-common-datasource + + From 88f42314590b45c4c0c5298fd1104c2a8a265658 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Tue, 27 Aug 2024 17:40:33 +0800 Subject: [PATCH 2/6] =?UTF-8?q?1=E3=80=81=E4=BF=9D=E5=AD=98=E9=95=9C?= =?UTF-8?q?=E5=83=8F=EF=BC=8C=E5=90=8C=E6=97=B6=E4=BF=9D=E5=AD=98=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E3=80=82=202=E3=80=81=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=EF=BC=8C=E6=A8=A1=E5=9E=8B=E6=8C=82=E8=BD=BD?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E5=BD=93=E5=89=8D=E5=B7=A5=E4=BD=9C=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ImageServiceImpl.java | 43 ++++++++++++++----- .../platform/utils/DockerClientUtil.java | 23 +++++++--- .../ruoyi/platform/utils/K8sClientUtil.java | 4 +- .../java/com/ruoyi/platform/vo/ImageVo.java | 2 +- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java index 17bfdd48..2c2de8cc 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java @@ -14,14 +14,14 @@ import com.ruoyi.platform.utils.FileUtil; import com.ruoyi.platform.utils.K8sClientUtil; import com.ruoyi.platform.vo.ImageVo; import com.ruoyi.system.api.model.LoginUser; -import io.kubernetes.client.openapi.models.V1PersistentVolumeClaim; import io.kubernetes.client.openapi.models.V1Pod; -import lombok.Synchronized; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -42,14 +42,12 @@ import java.util.concurrent.CompletableFuture; public class ImageServiceImpl implements ImageService { @Resource private ImageDao imageDao; - + @Resource + private ImageVersionDao imageVersionDao; @Resource private ImageVersionService imageVersionService; - - @Resource - private ImageVersionDao imageVersionDao; @Resource private K8sClientUtil k8sClientUtil; @Resource @@ -359,7 +357,8 @@ public class ImageServiceImpl implements ImageService { } @Override - @Synchronized + @Async + @Transactional public String saveImage(ImageVo imageVo) { if(imageDao.getByName(imageVo.getName()) != null){ throw new IllegalStateException("镜像名称已存在"); @@ -373,10 +372,34 @@ public class ImageServiceImpl implements ImageService { String hostIp = k8sClientUtil.getHostIp(podName, namespace); dockerClientUtil.commitImage(imageVo,containerId,hostIp,username); - dockerClientUtil.pushImageToHorbor(imageVo,hostIp); + HashMap resultMap = dockerClientUtil.pushImageToHorbor(imageVo, hostIp); + + Image image = new Image(); + BeanUtils.copyProperties(imageVo,image); + image.setCreateBy(username); + image.setUpdateBy(username); + image.setUpdateTime(new Date()); + image.setCreateTime(new Date()); + image.setState(1); + imageDao.insert(image); + + ImageVersion imageVersion = new ImageVersion(); + imageVersion.setImageId(image.getId()); + imageVersion.setVersion(imageVo.getVersion()); + imageVersion.setUrl(resultMap.get("imageName")); + imageVersion.setTagName(imageVo.getTagName()); + imageVersion.setFileSize(resultMap.get("size")); + imageVersion.setCreateBy(username); + imageVersion.setUpdateBy(username); + imageVersion.setUpdateTime(new Date()); + imageVersion.setCreateTime(new Date()); + imageVersion.setState(1); + imageVersion.setStatus("available"); + imageVersionDao.insert(imageVersion); + + return "保存镜像成功"; } catch (Exception e) { - throw new RuntimeException(e); + throw new RuntimeException("保存镜像失败:" +e); } - return null; } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java index 15a1134c..b20fa077 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/DockerClientUtil.java @@ -2,6 +2,7 @@ package com.ruoyi.platform.utils; import com.github.dockerjava.api.DockerClient; import com.github.dockerjava.api.command.CommitCmd; +import com.github.dockerjava.api.command.InspectImageResponse; import com.github.dockerjava.api.model.AuthConfig; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientConfig; @@ -14,6 +15,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.time.Duration; +import java.util.HashMap; @Slf4j @Component @@ -35,7 +37,7 @@ public class DockerClientUtil { //创建DefaultDockerClientConfig(指定docker服务器的配置) DockerClientConfig config = DefaultDockerClientConfig .createDefaultConfigBuilder() - .withDockerHost("tcp://"+ dockerServerUrl +":2375") + .withDockerHost("tcp://" + dockerServerUrl + ":2375") .withDockerTlsVerify(false) .withApiVersion("1.40") // .withDockerCertPath(dcokerCertPath) @@ -62,18 +64,16 @@ public class DockerClientUtil { public String commitImage(ImageVo imageVo, String containerId, String hostIp, String userName) { DockerClient dockerClient = getDockerClient(hostIp); -// dockerClient.startContainerCmd(containerId).exec(); // 提交容器为镜像,这里的"new_image"和"new_tag"是新镜像的名字和标签 CommitCmd commitCmd = dockerClient.commitCmd(containerId) .withRepository(imageVo.getName()) .withTag(imageVo.getTagName()) .withAuthor(userName) .withMessage(imageVo.getDescription()); - String exec = commitCmd.exec(); - return exec; + return commitCmd.exec(); } - public void pushImageToHorbor(ImageVo imageVo, String hostIp) { + public HashMap pushImageToHorbor(ImageVo imageVo, String hostIp) { DockerClient dockerClient = getDockerClient(hostIp); //Harbor登录信息 @@ -90,8 +90,19 @@ public class DockerClientUtil { dockerClient.pushImageCmd(imageName).withAuthConfig(autoConfig).start().awaitCompletion(); //push成功后,删除本地加载的镜像 dockerClient.removeImageCmd(localImageName).exec(); + + String totalImageName = imageName + ":" + imageVo.getTagName(); + InspectImageResponse exec = dockerClient.inspectImageCmd(totalImageName).exec(); + + String size = String.format("%.1f GB", (float) exec.getSize() / 1073741824.0); + + HashMap resultMap = new HashMap<>(); + resultMap.put("imageName",totalImageName); + resultMap.put("size",size); + + return resultMap; } catch (InterruptedException e) { - throw new RuntimeException("推送镜像失败:"+e); + throw new RuntimeException("推送镜像失败:" + e); } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java index e2f1f6ac..519a5038 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java @@ -405,8 +405,8 @@ public class K8sClientUtil { // 配置卷和卷挂载 List volumeMounts = new ArrayList<>(); volumeMounts.add(new V1VolumeMount().name("workspace").mountPath(mountPath)); - volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/datasets").subPath(datasetPath).readOnly(true)); - volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/model").subPath(modelPath).readOnly(true)); + volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/data").subPath(datasetPath).readOnly(true)); + volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/model").subPath(modelPath).readOnly(true)); List volumes = new ArrayList<>(); volumes.add(new V1Volume().name("workspace").persistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvc.getMetadata().getName()))); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java index 6ee88868..1153b579 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java @@ -24,7 +24,7 @@ public class ImageVo implements Serializable { * 镜像类型 */ - @ApiModelProperty(name = "image_type") + @ApiModelProperty(name = "imageType") private Integer imageType; From a73b83c97df107b79957c5b0a89e3341767f611e Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Wed, 28 Aug 2024 08:51:58 +0800 Subject: [PATCH 3/6] =?UTF-8?q?1=E3=80=81=E4=BF=9D=E5=AD=98=E9=95=9C?= =?UTF-8?q?=E5=83=8F=EF=BC=8C=E5=90=8C=E6=97=B6=E4=BF=AE=E6=94=B9=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E7=9A=84=E9=95=9C=E5=83=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/service/impl/ImageServiceImpl.java | 13 +++++++++++-- .../main/java/com/ruoyi/platform/vo/ImageVo.java | 6 +++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java index 2c2de8cc..52a370f5 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java @@ -2,8 +2,10 @@ package com.ruoyi.platform.service.impl; import com.alibaba.fastjson2.util.DateUtils; import com.ruoyi.common.security.utils.SecurityUtils; +import com.ruoyi.platform.domain.DevEnvironment; import com.ruoyi.platform.domain.Image; import com.ruoyi.platform.domain.ImageVersion; +import com.ruoyi.platform.mapper.DevEnvironmentDao; import com.ruoyi.platform.mapper.ImageDao; import com.ruoyi.platform.mapper.ImageVersionDao; import com.ruoyi.platform.service.ImageService; @@ -44,6 +46,8 @@ public class ImageServiceImpl implements ImageService { private ImageDao imageDao; @Resource private ImageVersionDao imageVersionDao; + @Resource + private DevEnvironmentDao devEnvironmentDao; @Resource private ImageVersionService imageVersionService; @@ -357,7 +361,6 @@ public class ImageServiceImpl implements ImageService { } @Override - @Async @Transactional public String saveImage(ImageVo imageVo) { if(imageDao.getByName(imageVo.getName()) != null){ @@ -365,7 +368,7 @@ public class ImageServiceImpl implements ImageService { } LoginUser loginUser = SecurityUtils.getLoginUser(); String username = loginUser.getUsername().toLowerCase(); - String podName = username +"-editor-pod" + "-" + imageVo.getDevEnvironmentId(); + String podName = username +"-editor-pod" + "-" + imageVo.getDevEnvironmentId().toString(); try { String containerId = k8sClientUtil.getPodContainerId(podName, namespace); @@ -397,6 +400,12 @@ public class ImageServiceImpl implements ImageService { imageVersion.setStatus("available"); imageVersionDao.insert(imageVersion); + //更新dev环境的镜像信息 + DevEnvironment devEnvironment = new DevEnvironment(); + devEnvironment.setId(imageVo.getDevEnvironmentId()); + devEnvironment.setImage(resultMap.get("imageName")); + devEnvironmentDao.update(devEnvironment); + return "保存镜像成功"; } catch (Exception e) { throw new RuntimeException("保存镜像失败:" +e); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java index 1153b579..4dabab23 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java @@ -65,7 +65,7 @@ public class ImageVo implements Serializable { @ApiModelProperty(value = "环境id") - private String devEnvironmentId; + private Integer devEnvironmentId; // public Integer getId() { // return id; // } @@ -154,11 +154,11 @@ public class ImageVo implements Serializable { this.path = path; } - public String getDevEnvironmentId() { + public Integer getDevEnvironmentId() { return devEnvironmentId; } - public void setDevEnvironmentId(String devEnvironmentId) { + public void setDevEnvironmentId(Integer devEnvironmentId) { this.devEnvironmentId = devEnvironmentId; } From 88269061d9138bf60bcab62fce61c728facf0081 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Wed, 28 Aug 2024 09:37:52 +0800 Subject: [PATCH 4/6] =?UTF-8?q?1=E3=80=81=E4=BF=9D=E5=AD=98=E9=95=9C?= =?UTF-8?q?=E5=83=8F=E8=AE=BE=E4=B8=BA=E7=A7=81=E4=BA=BA=E9=95=9C=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/ruoyi/platform/constant/Constant.java | 7 +++++++ .../com/ruoyi/platform/service/impl/ImageServiceImpl.java | 2 ++ 2 files changed, 9 insertions(+) create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java new file mode 100644 index 00000000..b549431c --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java @@ -0,0 +1,7 @@ +package com.ruoyi.platform.constant; + +public class Constant { + + public final static int Image_Type_Pub = 1; // 公共镜像 + public final static int Image_Type_Pri = 0; // 私有镜像 +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java index 52a370f5..b13c414d 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ImageServiceImpl.java @@ -2,6 +2,7 @@ package com.ruoyi.platform.service.impl; import com.alibaba.fastjson2.util.DateUtils; import com.ruoyi.common.security.utils.SecurityUtils; +import com.ruoyi.platform.constant.Constant; import com.ruoyi.platform.domain.DevEnvironment; import com.ruoyi.platform.domain.Image; import com.ruoyi.platform.domain.ImageVersion; @@ -379,6 +380,7 @@ public class ImageServiceImpl implements ImageService { Image image = new Image(); BeanUtils.copyProperties(imageVo,image); + image.setImageType(Constant.Image_Type_Pri); image.setCreateBy(username); image.setUpdateBy(username); image.setUpdateTime(new Date()); From cfeca85f6d51981d237f96733ad1c2f8bc1e3b81 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Wed, 28 Aug 2024 14:27:19 +0800 Subject: [PATCH 5/6] =?UTF-8?q?1=E3=80=81=E8=AE=BE=E7=BD=AE=E5=90=AF?= =?UTF-8?q?=E5=8A=A8pod=E4=BA=B2=E5=92=8C=E6=80=A7=E5=8F=8D=E4=BA=B2?= =?UTF-8?q?=E5=92=8C=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/image/ImageController.java | 2 +- .../ruoyi/platform/utils/K8sClientUtil.java | 41 ++++++++++++++++++- .../java/com/ruoyi/platform/vo/ImageVo.java | 9 ++-- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java index d389bad8..1af9c983 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/image/ImageController.java @@ -151,7 +151,7 @@ public class ImageController extends BaseController { @PostMapping("/saveImage") @ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存,并推送到horbor") - public GenericsAjaxResult saveImage(ImageVo imageVo){ + public GenericsAjaxResult saveImage(@RequestBody ImageVo imageVo){ return genericsSuccess(this.imageService.saveImage(imageVo)); } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java index 519a5038..06e40622 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java @@ -377,12 +377,48 @@ public class K8sClientUtil { // 创建配置好的Pod public Integer createConfiguredPod(String podName, String namespace, Integer port, String mountPath, V1PersistentVolumeClaim pvc, String image, String dataPvcName, String datasetPath, String modelPath) { - Map selector = new LinkedHashMap<>(); - selector.put("k8s-jupyter", podName); + //设置选择节点,pod反亲和性 + Map selector = new LinkedHashMap<>(); + selector.put("k8s-jupyter", "CPU-GPU"); Map nodeSelector = new LinkedHashMap<>(); nodeSelector.put("resource-type", "CPU-GPU"); + V1LabelSelectorRequirement labelSelectorRequirement = new V1LabelSelectorRequirement() + .key("k8s-jupyter").operator("NotIn").values(Collections.singletonList("CPU-GPU")); + + V1LabelSelector labelSelector = new V1LabelSelector() + .matchExpressions(Collections.singletonList(labelSelectorRequirement)); + + V1PodAffinityTerm podAffinityTerm = new V1PodAffinityTerm() + .labelSelector(labelSelector) + .namespaces(Collections.singletonList(namespace)) + .topologyKey("kubernetes.io/hostname"); + + V1PodAffinity podAffinity = new V1PodAffinity() + .requiredDuringSchedulingIgnoredDuringExecution(Collections.singletonList(podAffinityTerm)); + + V1LabelSelectorRequirement antiLabelSelectorRequirement = new V1LabelSelectorRequirement() + .key("k8s-jupyter").operator("In").values(Collections.singletonList("CPU-GPU")); + + V1LabelSelector antiLabelSelector = new V1LabelSelector() + .matchExpressions(Collections.singletonList(antiLabelSelectorRequirement)); + + V1PodAffinityTerm antiPodAffinityTerm = new V1PodAffinityTerm() + .labelSelector(antiLabelSelector) + .namespaces(Collections.singletonList(namespace)) + .topologyKey("kubernetes.io/hostname"); + +// V1WeightedPodAffinityTerm weightedPodAffinityTerm = new V1WeightedPodAffinityTerm().weight(100).podAffinityTerm(podAffinityTerm); + + V1PodAntiAffinity podAntiAffinity = new V1PodAntiAffinity() + .requiredDuringSchedulingIgnoredDuringExecution(Collections.singletonList(antiPodAffinityTerm)); + + V1Affinity v1Affinity = new V1Affinity() + .podAffinity(podAffinity) + .podAntiAffinity(podAntiAffinity); + + // 创建Pod CoreV1Api api = new CoreV1Api(apiClient); V1PodList v1PodList = null; try { @@ -427,6 +463,7 @@ public class K8sClientUtil { .endContainer() .withVolumes(volumes) .withNodeSelector(nodeSelector) + .withAffinity(v1Affinity) .endSpec() .build(); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java index 4dabab23..8345f214 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/vo/ImageVo.java @@ -24,7 +24,7 @@ public class ImageVo implements Serializable { * 镜像类型 */ - @ApiModelProperty(name = "imageType") + @ApiModelProperty(name = "image_type") private Integer imageType; @@ -43,7 +43,7 @@ public class ImageVo implements Serializable { /** * 镜像tag名称 */ - @ApiModelProperty(name = "tagName") + @ApiModelProperty(name = "tag_name") private String tagName; /** @@ -57,15 +57,16 @@ public class ImageVo implements Serializable { @ApiModelProperty(name = "status") private String status; - @ApiModelProperty(value = "上传方式, 基于公网上传0,基于本地上传1") + @ApiModelProperty(name = "upload_type", value = "上传方式, 基于公网上传0,基于本地上传1") private Integer uploadType; @ApiModelProperty(value = "镜像上传路径") private String path; - @ApiModelProperty(value = "环境id") + @ApiModelProperty(name = "dev_environment_id", value = "环境id") private Integer devEnvironmentId; + // public Integer getId() { // return id; // } From b571b2af91f0fd1e419778ba0737806d2c8103da Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Thu, 29 Aug 2024 11:11:50 +0800 Subject: [PATCH 6/6] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=85=8D=E7=BD=AE=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/platform/constant/Constant.java | 3 + .../codeConfig/CodeConfigController.java | 61 ++++++++++ .../com/ruoyi/platform/domain/CodeConfig.java | 56 +++++++++ .../ruoyi/platform/mapper/CodeConfigDao.java | 20 ++++ .../platform/service/CodeConfigService.java | 19 +++ .../service/impl/CodeConfigServiceImpl.java | 74 ++++++++++++ .../CodeConfigDaoMapper.xml | 108 ++++++++++++++++++ 7 files changed, 341 insertions(+) create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/codeConfig/CodeConfigController.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/CodeConfig.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/CodeConfigDao.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/CodeConfigService.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/CodeConfigServiceImpl.java create mode 100644 ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/CodeConfigDaoMapper.xml diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java index b549431c..9d2fe7fb 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/constant/Constant.java @@ -4,4 +4,7 @@ public class Constant { public final static int Image_Type_Pub = 1; // 公共镜像 public final static int Image_Type_Pri = 0; // 私有镜像 + + public final static int State_valid = 1; // 有效 + public final static int State_invalid = 0; // 无效 } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/codeConfig/CodeConfigController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/codeConfig/CodeConfigController.java new file mode 100644 index 00000000..6a599028 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/codeConfig/CodeConfigController.java @@ -0,0 +1,61 @@ +package com.ruoyi.platform.controller.codeConfig; + +import com.ruoyi.common.core.web.controller.BaseController; +import com.ruoyi.common.core.web.domain.GenericsAjaxResult; +import com.ruoyi.platform.domain.CodeConfig; +import com.ruoyi.platform.service.CodeConfigService; +import io.swagger.annotations.Api; +import org.springframework.web.bind.annotation.*; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.ResponseEntity; + +import javax.annotation.Resource; + +@RestController +@RequestMapping("codeConfig") +@Api("代码配置") +public class CodeConfigController extends BaseController { + + @Resource + private CodeConfigService codeConfigService; + + /** + * 分页查询 + * @param codeConfig 筛选条件 + * @param page 页数 + * @param size 每页大小 + * @return 查询结果 + */ + @GetMapping + public GenericsAjaxResult> queryByPage(CodeConfig codeConfig, int page, int size) { + PageRequest pageRequest = PageRequest.of(page,size); + return genericsSuccess(this.codeConfigService.queryByPage(codeConfig, pageRequest)); + } + + /** + * 通过主键查询单条数据 + * @param id + * @return 单条数据 + */ + @GetMapping("{id}") + public ResponseEntity queryById(@PathVariable("id") Long id) { + return ResponseEntity.ok(this.codeConfigService.queryById(id)); + } + + + @PostMapping + public GenericsAjaxResult add(@RequestBody CodeConfig codeConfig){ + return genericsSuccess(this.codeConfigService.insert(codeConfig)); + } + + @PutMapping + public GenericsAjaxResult update(@RequestBody CodeConfig codeConfig){ + return genericsSuccess(this.codeConfigService.update(codeConfig)); + } + + @DeleteMapping("{id}") + public GenericsAjaxResult delete(@PathVariable("id") Long id){ + return genericsSuccess(this.codeConfigService.removeById(id)); + } +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/CodeConfig.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/CodeConfig.java new file mode 100644 index 00000000..957dabef --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/CodeConfig.java @@ -0,0 +1,56 @@ +package com.ruoyi.platform.domain; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +@Data +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +public class CodeConfig implements Serializable { + + + private Long id; + + @ApiModelProperty(value = "代码仓库名称") + private String codeRepoName; + + @ApiModelProperty(value = "代码仓库可见性(1-公开,0-私有)") + private Integer codeRepoVis; + + @ApiModelProperty(value = "Git地址") + private String gitUrl; + + @ApiModelProperty(value = "代码分支/Tag") + private String gitBranch; + + @ApiModelProperty(value = "验证方式(0-用户名密码,1-SSH-Key)") + private Integer verifyMode; + + @ApiModelProperty(value = "Git用户名") + private String gitUserName; + + @ApiModelProperty(value = "Git密码") + private String gitPassword; + + private String createBy; + /** + * 创建时间 + */ + private Date createTime; + /** + * 更新者 + */ + private String updateBy; + /** + * 更新时间 + */ + private Date updateTime; + /** + * 状态,0失效1生效 + */ + private Integer state; +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/CodeConfigDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/CodeConfigDao.java new file mode 100644 index 00000000..db896b59 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/CodeConfigDao.java @@ -0,0 +1,20 @@ +package com.ruoyi.platform.mapper; + +import com.ruoyi.platform.domain.CodeConfig; +import org.apache.ibatis.annotations.Param; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +public interface CodeConfigDao { + + long count(@Param("codeConfig") CodeConfig codeConfig); + + List queryAllByLimit(@Param("codeConfig") CodeConfig codeConfig, @Param("pageable") Pageable pageable); + + CodeConfig queryById(Long id); + + int insert(@Param("codeConfig") CodeConfig codeConfig); + + int update(@Param("codeConfig") CodeConfig codeConfig); +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/CodeConfigService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/CodeConfigService.java new file mode 100644 index 00000000..260c01d4 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/CodeConfigService.java @@ -0,0 +1,19 @@ +package com.ruoyi.platform.service; + +import com.ruoyi.platform.domain.CodeConfig; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +public interface CodeConfigService { + + Page queryByPage(CodeConfig codeConfig, PageRequest pageRequest); + + CodeConfig queryById(Long id); + + CodeConfig insert(CodeConfig codeConfig); + + CodeConfig update(CodeConfig codeConfig); + + String removeById(Long id); + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/CodeConfigServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/CodeConfigServiceImpl.java new file mode 100644 index 00000000..664777da --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/CodeConfigServiceImpl.java @@ -0,0 +1,74 @@ +package com.ruoyi.platform.service.impl; + +import com.ruoyi.common.security.utils.SecurityUtils; +import com.ruoyi.platform.constant.Constant; +import com.ruoyi.platform.domain.CodeConfig; +import com.ruoyi.platform.mapper.CodeConfigDao; +import com.ruoyi.platform.service.CodeConfigService; +import com.ruoyi.system.api.model.LoginUser; +import org.apache.commons.lang3.StringUtils; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; + +@Service("codeConfigService") +public class CodeConfigServiceImpl implements CodeConfigService { + + @Resource + private CodeConfigDao codeConfigDao; + + + @Override + public Page queryByPage(CodeConfig codeConfig, PageRequest pageRequest) { + long total = this.codeConfigDao.count(codeConfig); + List codeConfigList = this.codeConfigDao.queryAllByLimit(codeConfig, pageRequest); + + return new PageImpl<>(codeConfigList, pageRequest, total); + } + + @Override + public CodeConfig queryById(Long id) { + return this.codeConfigDao.queryById(id); + } + + @Override + public CodeConfig insert(CodeConfig codeConfig) { + LoginUser loginUser = SecurityUtils.getLoginUser(); + codeConfig.setCreateBy(loginUser.getUsername()); + codeConfig.setUpdateBy(loginUser.getUsername()); + codeConfig.setCreateTime(new Date()); + codeConfig.setUpdateTime(new Date()); + this.codeConfigDao.insert(codeConfig); + return codeConfig; + } + + @Override + public CodeConfig update(CodeConfig codeConfig) { + LoginUser loginUser = SecurityUtils.getLoginUser(); + codeConfig.setUpdateBy(loginUser.getUsername()); + codeConfig.setUpdateTime(new Date()); + this.codeConfigDao.update(codeConfig); + return this.codeConfigDao.queryById(codeConfig.getId()); + } + + @Override + public String removeById(Long id) { + CodeConfig codeConfig = this.codeConfigDao.queryById(id); + if (codeConfig == null) { + return "代码配置不存在"; + } + LoginUser loginUser = SecurityUtils.getLoginUser(); + String username = loginUser.getUsername(); + String createBy = codeConfig.getCreateBy(); + if (!(StringUtils.equals(username, "admin") || StringUtils.equals(username, createBy))) { + return "无权限删除该代码配置"; + } + codeConfig.setState(Constant.State_invalid); + return this.codeConfigDao.update(codeConfig) > 0 ? "删除成功" : "删除失败"; + } +} diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/CodeConfigDaoMapper.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/CodeConfigDaoMapper.xml new file mode 100644 index 00000000..64c72f88 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/CodeConfigDaoMapper.xml @@ -0,0 +1,108 @@ + + + + + + insert into code_config(code_repo_name, code_repo_vis, git_url, git_branch, verify_mode, git_user_name, git_password, create_by, create_time, update_by, update_time) + values(#{codeConfig.codeRepoName}, #{codeConfig.codeRepoVis}, #{codeConfig.gitUrl}, #{codeConfig.gitBranch}, #{codeConfig.verifyMode}, #{codeConfig.gitUserName}, #{codeConfig.gitPassword}, #{codeConfig.createBy}, #{codeConfig.createTime}, #{codeConfig.updateBy}, #{codeConfig.updateTime}) + + + + update code_config + + + code_repo_name = #{codeConfig.codeRepoName}, + + + code_repo_vis = #{codeConfig.codeRepoVis}, + + + git_url = #{codeConfig.gitUrl}, + + + git_branch = #{codeConfig.gitBranch}, + + + verify_mode = #{codeConfig.verifyMode}, + + + git_user_name = #{codeConfig.gitUserName}, + + + create_by = #{codeConfig.createBy}, + + + create_time = #{codeConfig.createTime}, + + + update_by = #{codeConfig.updateBy}, + + + update_time = #{codeConfig.updateTime}, + + + state = #{codeConfig.state}, + + + where id = #{codeConfig.id} + + + + + + + + + + + + state = 1 + + and id = #{codeConfig.id} + + + and code_repo_name = #{codeConfig.codeRepoName} + + + and code_repo_vis = #{codeConfig.codeRepoVis} + + + and git_url = #{codeConfig.gitUrl} + + + and git_branch = #{codeConfig.gitBranch} + + + and verify_mode = #{codeConfig.verifyMode} + + + and git_user_name = #{codeConfig.gitUserName} + + + and create_by = #{codeConfig.createBy} + + + and create_time = #{codeConfig.createTime} + + + and update_by = #{codeConfig.updateBy} + + + and update_time = #{codeConfig.updateTime} + + + + \ No newline at end of file