This commit is contained in:
bwcx_jzy 2022-12-03 17:20:09 +08:00
parent 3e9ca11f90
commit 6f28db74bf
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5

View File

@ -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<JSONObject>> list(
@ValidatorItem String id,
String serviceId, String serviceName) throws Exception {
//
IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME);
Map<String, Object> map = dockerInfoService.getBySwarmPluginMap(id, getRequest());
map.put("id", serviceId);
map.put("name", serviceName);
List<JSONObject> listSwarmNodes = (List<JSONObject>) plugin.execute("listServices", map);
return new JsonMessage<>(200, "", listSwarmNodes);
}
@PostMapping(value = "list", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public JsonMessage<List<JSONObject>> list(
@ValidatorItem String id,
String serviceId, String serviceName) throws Exception {
//
IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME);
Map<String, Object> map = dockerInfoService.getBySwarmPluginMap(id, getRequest());
map.put("id", serviceId);
map.put("name", serviceName);
List<JSONObject> listSwarmNodes = (List<JSONObject>) plugin.execute("listServices", map);
return new JsonMessage<>(200, "", listSwarmNodes);
}
@PostMapping(value = "task-list", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public JsonMessage<List<JSONObject>> 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<String, Object> 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<JSONObject> listSwarmNodes = (List<JSONObject>) plugin.execute("listTasks", map);
return new JsonMessage<>(200, "", listSwarmNodes);
}
@PostMapping(value = "task-list", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.LIST)
public JsonMessage<List<JSONObject>> 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<String, Object> 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<JSONObject> listSwarmNodes = (List<JSONObject>) plugin.execute("listTasks", map);
return new JsonMessage<>(200, "", listSwarmNodes);
}
@GetMapping(value = "del", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.DEL)
public JsonMessage<List<JSONObject>> del(@ValidatorItem String id, @ValidatorItem String serviceId) throws Exception {
//
IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME);
Map<String, Object> 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<List<JSONObject>> del(@ValidatorItem String id, @ValidatorItem String serviceId) throws Exception {
//
IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME);
Map<String, Object> 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<List<JSONObject>> edit(@RequestBody JSONObject jsonObject) throws Exception {
//
String id = jsonObject.getString("id");
IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME);
Map<String, Object> 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<List<JSONObject>> edit(@RequestBody JSONObject jsonObject) throws Exception {
//
String id = jsonObject.getString("id");
IPlugin plugin = PluginFactory.getPlugin(DockerSwarmInfoService.DOCKER_PLUGIN_NAME);
Map<String, Object> 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<String, Object> 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<String> 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<String, Object> 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<String> 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);
}
}