|
- package com.educoder.bridge.controller;
-
- import com.alibaba.fastjson.JSONObject;
- import com.educoder.bridge.service.DockerService;
- import com.educoder.bridge.utils.ClientUtil;
- import com.spotify.docker.client.DockerClient;
- import com.spotify.docker.client.exceptions.DockerException;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
- @Api(value = "镜像管理", hidden = true)
- @RestController
- @RequestMapping("/docker")
- public class DockerController {
- @Autowired
- private DockerService dockerService;
-
- private Logger logger = LoggerFactory.getLogger(DockerController.class);
-
- @RequestMapping(path = "/images")
- @ApiOperation(value = "获取全部实训镜像", httpMethod = "GET")
- public JSONObject getImages() throws DockerException, InterruptedException {
- logger.info("收到获取镜像请求");
-
- JSONObject result = new JSONObject();
- // 获取主节点全部镜像
- DockerClient docker = ClientUtil.getDocker();
- List<String> imageList = dockerService.getImageList(docker);
- result.put("images", imageList);
-
- // 获取未同步的镜像
- List<DockerClient> dockerDeputies = ClientUtil.getDockerDeputies();
- List<String> imageDiff = dockerService.imageDiff(imageList, dockerDeputies);
- result.put("imagesNotSync", imageDiff);
-
- result.put("code", 0);
- return result;
- }
-
- @RequestMapping(path = "/syncImage")
- @ApiOperation(value = "同步镜像", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
- public JSONObject syncImage(
- @ApiParam(name = "imageName", required = true, value = "镜像名") @RequestParam String imageName)
- throws DockerException, InterruptedException {
- logger.info("imageName: {}", imageName);
-
- JSONObject result = new JSONObject();
-
- DockerClient dockerMaster = ClientUtil.getDocker();
- List<DockerClient> dockerDeputies = ClientUtil.getDockerDeputies();
-
- dockerService.syncImage(dockerMaster, dockerDeputies, imageName);
-
- result.put("code", 0);
- return result;
-
- }
-
- @RequestMapping(path = "/updateImage")
- @ApiOperation(value = "镜像有更新", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
- public JSONObject updateImage(
- @ApiParam(name = "imageName", required = true, value = "镜像名") @RequestParam String imageName,
- @ApiParam(name = "imageID", required = true, value = "旧的镜像ID") @RequestParam String imageID,
- @ApiParam(name = "flag", required = true, value = "更新行为选择,0更新 1回退") @RequestParam Integer flag)
- throws DockerException, InterruptedException {
- logger.info("imageName: {}, imageID: {}, flag: {}", imageName, imageID, flag);
-
- JSONObject result = new JSONObject();
-
- DockerClient dockerMaster = ClientUtil.getDocker();
- List<DockerClient> dockerDeputies = ClientUtil.getDockerDeputies();
-
- if (flag == 0) {
- // 选择更新
- dockerService.syncImage(dockerMaster, dockerDeputies, imageName);
- } else {
- // 选择回退
- dockerService.resetImage(dockerMaster, imageID, imageName);
- }
-
- result.put("code", 0);
- return result;
- }
-
- }
|