You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

DockerController.java 3.8 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.educoder.bridge.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.educoder.bridge.service.DockerService;
  4. import com.educoder.bridge.utils.ClientUtil;
  5. import com.spotify.docker.client.DockerClient;
  6. import com.spotify.docker.client.exceptions.DockerException;
  7. import io.swagger.annotations.Api;
  8. import io.swagger.annotations.ApiOperation;
  9. import io.swagger.annotations.ApiParam;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. @Api(value = "镜像管理", hidden = true)
  20. @RestController
  21. @RequestMapping("/docker")
  22. public class DockerController {
  23. @Autowired
  24. private DockerService dockerService;
  25. private Logger logger = LoggerFactory.getLogger(DockerController.class);
  26. @RequestMapping(path = "/images")
  27. @ApiOperation(value = "获取全部实训镜像", httpMethod = "GET")
  28. public JSONObject getImages() throws DockerException, InterruptedException {
  29. logger.info("收到获取镜像请求");
  30. JSONObject result = new JSONObject();
  31. // 获取主节点全部镜像
  32. DockerClient docker = ClientUtil.getDocker();
  33. List<String> imageList = dockerService.getImageList(docker);
  34. result.put("images", imageList);
  35. // 获取未同步的镜像
  36. List<DockerClient> dockerDeputies = ClientUtil.getDockerDeputies();
  37. List<String> imageDiff = dockerService.imageDiff(imageList, dockerDeputies);
  38. result.put("imagesNotSync", imageDiff);
  39. result.put("code", 0);
  40. return result;
  41. }
  42. @RequestMapping(path = "/syncImage")
  43. @ApiOperation(value = "同步镜像", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  44. public JSONObject syncImage(
  45. @ApiParam(name = "imageName", required = true, value = "镜像名") @RequestParam String imageName)
  46. throws DockerException, InterruptedException {
  47. logger.info("imageName: {}", imageName);
  48. JSONObject result = new JSONObject();
  49. DockerClient dockerMaster = ClientUtil.getDocker();
  50. List<DockerClient> dockerDeputies = ClientUtil.getDockerDeputies();
  51. dockerService.syncImage(dockerMaster, dockerDeputies, imageName);
  52. result.put("code", 0);
  53. return result;
  54. }
  55. @RequestMapping(path = "/updateImage")
  56. @ApiOperation(value = "镜像有更新", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  57. public JSONObject updateImage(
  58. @ApiParam(name = "imageName", required = true, value = "镜像名") @RequestParam String imageName,
  59. @ApiParam(name = "imageID", required = true, value = "旧的镜像ID") @RequestParam String imageID,
  60. @ApiParam(name = "flag", required = true, value = "更新行为选择,0更新 1回退") @RequestParam Integer flag)
  61. throws DockerException, InterruptedException {
  62. logger.info("imageName: {}, imageID: {}, flag: {}", imageName, imageID, flag);
  63. JSONObject result = new JSONObject();
  64. DockerClient dockerMaster = ClientUtil.getDocker();
  65. List<DockerClient> dockerDeputies = ClientUtil.getDockerDeputies();
  66. if (flag == 0) {
  67. // 选择更新
  68. dockerService.syncImage(dockerMaster, dockerDeputies, imageName);
  69. } else {
  70. // 选择回退
  71. dockerService.resetImage(dockerMaster, imageID, imageName);
  72. }
  73. result.put("code", 0);
  74. return result;
  75. }
  76. }

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...