From 6f28db74bf5024836c7bab1405178238306f1894 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Sat, 3 Dec 2022 17:20:09 +0800 Subject: [PATCH] update --- .../docker/DockerSwarmServiceController.java | 201 +++++++++--------- 1 file changed, 101 insertions(+), 100 deletions(-) diff --git a/modules/server/src/main/java/io/jpom/controller/docker/DockerSwarmServiceController.java b/modules/server/src/main/java/io/jpom/controller/docker/DockerSwarmServiceController.java index f26fe35a0..6ad10e20c 100644 --- a/modules/server/src/main/java/io/jpom/controller/docker/DockerSwarmServiceController.java +++ b/modules/server/src/main/java/io/jpom/controller/docker/DockerSwarmServiceController.java @@ -61,112 +61,113 @@ import java.util.function.Consumer; @Slf4j public class DockerSwarmServiceController extends BaseServerController { - private final DockerInfoService dockerInfoService; + private final DockerInfoService dockerInfoService; - public DockerSwarmServiceController(DockerInfoService dockerInfoService) { - this.dockerInfoService = dockerInfoService; - } + public DockerSwarmServiceController(DockerInfoService dockerInfoService) { + this.dockerInfoService = dockerInfoService; + } - @PostMapping(value = "list", produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.LIST) - public JsonMessage> list( - @ValidatorItem String id, - String serviceId, String serviceName) throws Exception { - // - IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); - Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); - map.put("id", serviceId); - map.put("name", serviceName); - List listSwarmNodes = (List) plugin.execute("listServices", map); - return new JsonMessage<>(200, "", listSwarmNodes); - } + @PostMapping(value = "list", produces = MediaType.APPLICATION_JSON_VALUE) + @Feature(method = MethodFeature.LIST) + public JsonMessage> list( + @ValidatorItem String id, + String serviceId, String serviceName) throws Exception { + // + IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); + Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); + map.put("id", serviceId); + map.put("name", serviceName); + List listSwarmNodes = (List) plugin.execute("listServices", map); + return new JsonMessage<>(200, "", listSwarmNodes); + } - @PostMapping(value = "task-list", produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.LIST) - public JsonMessage> taskList( - @ValidatorItem String id, - String serviceId, String taskId, String taskName, String taskNode, String taskState) throws Exception { - // - IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); - Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); - map.put("id", taskId); - map.put("serviceId", serviceId); - map.put("name", taskName); - map.put("node", taskNode); - map.put("state", taskState); - List listSwarmNodes = (List) plugin.execute("listTasks", map); - return new JsonMessage<>(200, "", listSwarmNodes); - } + @PostMapping(value = "task-list", produces = MediaType.APPLICATION_JSON_VALUE) + @Feature(method = MethodFeature.LIST) + public JsonMessage> taskList( + @ValidatorItem String id, + String serviceId, String taskId, String taskName, String taskNode, String taskState) throws Exception { + // + IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); + Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); + map.put("id", taskId); + map.put("serviceId", serviceId); + map.put("name", taskName); + map.put("node", taskNode); + map.put("state", taskState); + List listSwarmNodes = (List) plugin.execute("listTasks", map); + return new JsonMessage<>(200, "", listSwarmNodes); + } - @GetMapping(value = "del", produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.DEL) - public JsonMessage> del(@ValidatorItem String id, @ValidatorItem String serviceId) throws Exception { - // - IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); - Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); - map.put("serviceId", serviceId); - plugin.execute("removeService", map); - return new JsonMessage<>(200, "删除服务成功"); - } + @GetMapping(value = "del", produces = MediaType.APPLICATION_JSON_VALUE) + @Feature(method = MethodFeature.DEL) + public JsonMessage> del(@ValidatorItem String id, @ValidatorItem String serviceId) throws Exception { + // + IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); + Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); + map.put("serviceId", serviceId); + plugin.execute("removeService", map); + return new JsonMessage<>(200, "删除服务成功"); + } - @PostMapping(value = "edit", produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.EDIT) - public JsonMessage> edit(@RequestBody JSONObject jsonObject) throws Exception { - // - String id = jsonObject.getString("id"); - IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); - Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); - map.putAll(jsonObject); - plugin.execute("updateService", map); - return new JsonMessage<>(200, "修改服务成功"); - } + @PostMapping(value = "edit", produces = MediaType.APPLICATION_JSON_VALUE) + @Feature(method = MethodFeature.EDIT) + public JsonMessage> edit(@RequestBody JSONObject jsonObject) throws Exception { + // + String id = jsonObject.getString("id"); + IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); + Map map = dockerInfoService.getBySwarmPluginMap(id, getRequest()); + map.putAll(jsonObject); + plugin.execute("updateService", map); + return new JsonMessage<>(200, "修改服务成功"); + } - /** - * @return json - */ - @GetMapping(value = "start-log", produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.EXECUTE) - public String pullImage(@ValidatorItem String id, @ValidatorItem String type, @ValidatorItem String dataId) throws Exception { - IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); - Map parameter = dockerInfoService.getBySwarmPluginMap(id, getRequest()); - parameter.put(StrUtil.equalsIgnoreCase(type, "service") ? "serviceId" : "taskId", dataId); - // - String uuid = IdUtil.fastSimpleUUID(); - File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), "docker-swarm-log", uuid + ".log"); - LogRecorder logRecorder = LogRecorder.builder().file(file).build(); - logRecorder.info("start pull {}", dataId); - Consumer logConsumer = logRecorder::append; - parameter.put("charset", CharsetUtil.CHARSET_UTF_8); - parameter.put("consumer", logConsumer); - parameter.put("tail", 50); - ThreadUtil.execute(() -> { - try { - plugin.execute(StrUtil.equalsIgnoreCase(type, "service") ? "logService" : "logTask", parameter); - } catch (Exception e) { - logRecorder.error("拉取日志异常", e); - } - logRecorder.info("pull end"); - }); - return JsonMessage.getString(200, "开始拉取", uuid); - } + /** + * @return json + */ + @GetMapping(value = "start-log", produces = MediaType.APPLICATION_JSON_VALUE) + @Feature(method = MethodFeature.EXECUTE) + public String pullImage(@ValidatorItem String id, @ValidatorItem String type, @ValidatorItem String dataId) throws Exception { + IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME); + Map parameter = dockerInfoService.getBySwarmPluginMap(id, getRequest()); + parameter.put(StrUtil.equalsIgnoreCase(type, "service") ? "serviceId" : "taskId", dataId); + // + String uuid = IdUtil.fastSimpleUUID(); + File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), "docker-swarm-log", uuid + ".log"); + try (LogRecorder logRecorder = LogRecorder.builder().file(file).build()) { + logRecorder.info("start pull {}", dataId); + Consumer logConsumer = logRecorder::append; + parameter.put("charset", CharsetUtil.CHARSET_UTF_8); + parameter.put("consumer", logConsumer); + parameter.put("tail", 50); + ThreadUtil.execute(() -> { + try { + plugin.execute(StrUtil.equalsIgnoreCase(type, "service") ? "logService" : "logTask", parameter); + } catch (Exception e) { + logRecorder.error("拉取日志异常", e); + } + logRecorder.info("pull end"); + }); + } + return JsonMessage.getString(200, "开始拉取", uuid); + } - /** - * 获取拉取的日志 - * - * @param id id - * @param line 需要获取的行号 - * @return json - */ - @GetMapping(value = "pull-log", produces = MediaType.APPLICATION_JSON_VALUE) - @Feature(method = MethodFeature.LIST) - public String getNowLog(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "没有数据") String id, - @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "line") int line) { - File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), "docker-swarm-log", id + ".log"); - if (!file.exists()) { - return JsonMessage.getString(201, "还没有日志文件"); - } - JSONObject data = FileUtils.readLogFile(file, line); - return JsonMessage.getString(200, "ok", data); - } + /** + * 获取拉取的日志 + * + * @param id id + * @param line 需要获取的行号 + * @return json + */ + @GetMapping(value = "pull-log", produces = MediaType.APPLICATION_JSON_VALUE) + @Feature(method = MethodFeature.LIST) + public String getNowLog(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "没有数据") String id, + @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "line") int line) { + File file = FileUtil.file(ServerConfigBean.getInstance().getUserTempPath(), "docker-swarm-log", id + ".log"); + if (!file.exists()) { + return JsonMessage.getString(201, "还没有日志文件"); + } + JSONObject data = FileUtils.readLogFile(file, line); + return JsonMessage.getString(200, "ok", data); + } }